code review pt-1

This commit is contained in:
Richard Ramos 2021-09-15 14:26:46 -04:00
parent bcf3f6bf29
commit 589dba2d27
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
10 changed files with 136 additions and 28 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ vendor/.nimble
/bottles/ /bottles/
/test_nim/build /test_nim/build
tmp tmp
src/libstatuslib-stamp
settings.json

View File

@ -117,20 +117,20 @@ $(STATUSGO): | deps
+ cd vendor/status-go && \ + cd vendor/status-go && \
$(MAKE) statusgo-shared-library $(HANDLE_OUTPUT) $(MAKE) statusgo-shared-library $(HANDLE_OUTPUT)
LIBSTATUSLIB := build/$@.$(LIBSTATUS_EXT).0 LIBSTATUSLIB := build/libstatuslib.$(LIBSTATUS_EXT).0
libstatuslib: | $(STATUSGO) libstatuslib: | $(STATUSGO)
echo -e $(BUILD_MSG) "$@" && \ echo -e $(BUILD_MSG) "$@" && \
$(ENV_SCRIPT) nim c $(NIM_PARAMS) $(NIM_EXTRA_PARAMS) --passL:"-L$(STATUSGO_LIBDIR)" --passL:"-lstatus" -o:build/$@.$(LIBSTATUS_EXT).0 -d:ssl --app:lib --noMain --header --nimcache:nimcache/libstatuslib statuslib.nim && \ $(ENV_SCRIPT) nim c $(NIM_PARAMS) $(NIM_EXTRA_PARAMS) --passL:"-L$(STATUSGO_LIBDIR)" --passL:"-lstatus" -o:build/$@.$(LIBSTATUS_EXT).0 -d:ssl --app:lib --noMain --header --nimcache:nimcache/libstatuslib statuslib.nim && \
rm -f build/$@.$(LIBSTATUS_EXT) && \ rm -f build/$@.$(LIBSTATUS_EXT) && \
ln -s $@.$(LIBSTATUS_EXT).0 build/$@.$(LIBSTATUS_EXT) && \ ln -s $@.$(LIBSTATUS_EXT).0 build/$@.$(LIBSTATUS_EXT) && \
cp nimcache/libstatuslib/*.h build/. && \ cp nimcache/libstatuslib/statuslib.h build/statuslib.h.for-reference-only && \
[[ $$? = 0 ]] [[ $$? = 0 ]]
# libraries for dynamic linking of non-Nim objects # libraries for dynamic linking of non-Nim objects
EXTRA_LIBS_DYNAMIC := -L"$(CURDIR)/build" -lstatuslib -lm -L"$(STATUSGO_LIBDIR)" -lstatus EXTRA_LIBS_DYNAMIC := -L"$(CURDIR)/build" -lstatuslib -lm -L"$(STATUSGO_LIBDIR)" -lstatus
build_ctest: | $(LIBSTATUSLIB) build deps build_ctest: | $(LIBSTATUSLIB) build deps
echo -e $(BUILD_MSG) "build/ctest" && \ echo -e $(BUILD_MSG) "build/ctest" && \
$(CC) test/main.c -Wl,-rpath,'$$ORIGIN' -I./vendor/nimbus-build-system/vendor/Nim/lib $(EXTRA_LIBS_DYNAMIC) -g -o build/ctest $(CC) test/main.c -Wl,-rpath,'$$ORIGIN' -Iinclude/ $(EXTRA_LIBS_DYNAMIC) -g -o build/ctest
LD_LIBRARY_PATH_NIMBLE := $${LD_LIBRARY_PATH} LD_LIBRARY_PATH_NIMBLE := $${LD_LIBRARY_PATH}
ifneq ($(detected_OS),Windows) ifneq ($(detected_OS),Windows)

18
include/statuslib.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __statuslib__
#define __statuslib__
void NimMain(void);
void helloWorld(void);
typedef struct Status Status;
Status* newStatusInstance(char* fleetConfig);
void freeStatusInstance(Status* instance);
void ensureDirectories(char* dataDir, char* tmpDir, char* logDir);
void initNode(Status* self, char* statusGoDir, char* keystoreDir);
#endif /* __statuslib__ */

View File

@ -100,8 +100,13 @@ proc getBloomFilterBitsSet*(self: Status): int {.exportc, dynlib.} =
# exported correctly # exported correctly
proc newStatusInstance*(fleetConfig: cstring): Status {.exportc, dynlib.} = proc newStatusInstance*(fleetConfig: cstring): Status {.exportc, cdecl, dynlib.} =
newStatusInstance($fleetConfig) result = newStatusInstance($fleetConfig)
GC_ref(result)
proc freeStatusInstance*(instance: var Status) {.exportc, cdecl, dynlib.} =
GC_unref(instance)
instance = nil
proc initNode*(self: Status, statusGoDir, keystoreDir: cstring) {.exportc, dynlib.} = proc initNode*(self: Status, statusGoDir, keystoreDir: cstring) {.exportc, dynlib.} =
self.initNode($statusGoDir, $keystoreDir) self.initNode($statusGoDir, $keystoreDir)
@ -109,5 +114,5 @@ proc initNode*(self: Status, statusGoDir, keystoreDir: cstring) {.exportc, dynli
proc saveStringSetting*(self: Status, setting: Setting, value: cstring) {.exportc, dynlib.} = proc saveStringSetting*(self: Status, setting: Setting, value: cstring) {.exportc, dynlib.} =
self.saveSetting(setting, $value) self.saveSetting(setting, $value)
proc saveBoolSetting*(self: Status, setting: Setting, value: bool) {.exportc, dynlib.} = proc saveBoolSetting*(self: Status, setting: Setting, value: cint) {.exportc, dynlib.} =
self.saveSetting(setting, value) self.saveSetting(setting, value.bool)

View File

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include "../build/statuslib.h" #include "statuslib.h"
void NimMain(); void NimMain();
@ -18,4 +18,6 @@ int main(int argc, char *argv[])
initNode(statusObj, "./build/A", "./build/B"); initNode(statusObj, "./build/A", "./build/B");
freeStatusInstance(statusObj);
} }

53
test/qtapp/.gitignore vendored Normal file
View File

@ -0,0 +1,53 @@
# https://github.com/github/gitignore/blob/master/CMake.gitignore
# ==============================================================================
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
# https://github.com/github/gitignore/blob/master/C%2B%2B.gitignore
# ==============================================================================
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app

View File

@ -8,7 +8,7 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(ExternalProject) include(ExternalProject)
@ -30,6 +30,7 @@ find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)
set(PROJECT_SOURCES set(PROJECT_SOURCES
nimstatus.cpp
main.cpp main.cpp
qml.qrc qml.qrc
) )
@ -74,11 +75,6 @@ include_directories(${STATUSGO_LIB_DIR})
# Include nim headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../vendor/nimbus-build-system/vendor/Nim/lib)
# Begin: status-lib # Begin: status-lib
set(STATUSLIB_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) set(STATUSLIB_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
set(STATUSLIB_LIB_DIR ${STATUSLIB_ROOT}/build) set(STATUSLIB_LIB_DIR ${STATUSLIB_ROOT}/build)
@ -97,8 +93,8 @@ ExternalProject_Get_Property(libstatuslib SOURCE_DIR)
add_library(statuslib SHARED IMPORTED) add_library(statuslib SHARED IMPORTED)
set_property(TARGET statuslib PROPERTY IMPORTED_LOCATION ${STATUSLIB_LIB_DIR}/libstatuslib.so) set_property(TARGET statuslib PROPERTY IMPORTED_LOCATION ${STATUSLIB_LIB_DIR}/libstatuslib.so)
add_dependencies(statuslib libstatuslib) add_dependencies(statuslib libstatuslib)
include_directories(${STATUSLIB_LIB_DIR}) include_directories(${STATUSLIB_ROOT}/include)
message(STATUS "foo include dir: ${STATUSLIB_LIB_DIR}")
target_compile_definitions(test-qtapp target_compile_definitions(test-qtapp

View File

@ -1,9 +1,10 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include "nimstatus.hpp"
extern "C" extern "C"
{ {
#include "statuslib.h" // TODO: is it possible to export directly to C++? #include "statuslib.h"
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -12,20 +13,19 @@ int main(int argc, char *argv[])
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif #endif
NimMain(); NimMain(); // Should be called always to initialize nim runtime
helloWorld();
std::string someDirName{"./test"};
char *cstr = &someDirName[0];
ensureDirectories(cstr, cstr, cstr); helloWorld(); // Example invocation of a nim function
std::string fleetStr{"{\"fleets\":{\"eth.prod\":{\"boot\":{\"boot-01.ac-cn-hongkong-c.eth.prod\":\"enode://6e6554fb3034b211398fcd0f0082cbb6bd13619e1a7e76ba66e1809aaa0c5f1ac53c9ae79cf2fd4a7bacb10d12010899b370c75fed19b991d9c0cdd02891abad@47.75.99.169:443\"},\"mail\":{\"mail-01.ac-cn-hongkong-c.eth.prod\":\"enode://606ae04a71e5db868a722c77a21c8244ae38f1bd6e81687cc6cfe88a3063fa1c245692232f64f45bd5408fed5133eab8ed78049332b04f9c110eac7f71c1b429@47.75.247.214:443\"},\"rendezvous\":{\"boot-01.ac-cn-hongkong-c.eth.prod\":\"/ip4/47.75.99.169/tcp/30703/ethv4/16Uiu2HAmV8Hq9e3zm9TMVP4zrVHo3BjqW5D6bDVV6VQntQd687e4\"},\"whisper\":{\"node-01.ac-cn-hongkong-c.eth.prod\":\"enode://b957e51f41e4abab8382e1ea7229e88c6e18f34672694c6eae389eac22dab8655622bbd4a08192c321416b9becffaab11c8e2b7a5d0813b922aa128b82990dab@47.75.222.178:443\"}}},\"meta\":{\"hostname\":\"node-01.do-ams3.proxy.misc\",\"timestamp\":\"2021-09-09T00:00:14.760436\"}}"}; auto someDirName = QString{"./test"}.toLocal8Bit().data();
char *cfleetStr = &fleetStr[0]; ensureDirectories(someDirName, someDirName, someDirName);
// TODO: figure out how to unmangle this type QString fleetStr{"{\"fleets\":{\"eth.prod\":{\"boot\":{\"boot-01.ac-cn-hongkong-c.eth.prod\":\"enode://6e6554fb3034b211398fcd0f0082cbb6bd13619e1a7e76ba66e1809aaa0c5f1ac53c9ae79cf2fd4a7bacb10d12010899b370c75fed19b991d9c0cdd02891abad@47.75.99.169:443\"},\"mail\":{\"mail-01.ac-cn-hongkong-c.eth.prod\":\"enode://606ae04a71e5db868a722c77a21c8244ae38f1bd6e81687cc6cfe88a3063fa1c245692232f64f45bd5408fed5133eab8ed78049332b04f9c110eac7f71c1b429@47.75.247.214:443\"},\"rendezvous\":{\"boot-01.ac-cn-hongkong-c.eth.prod\":\"/ip4/47.75.99.169/tcp/30703/ethv4/16Uiu2HAmV8Hq9e3zm9TMVP4zrVHo3BjqW5D6bDVV6VQntQd687e4\"},\"whisper\":{\"node-01.ac-cn-hongkong-c.eth.prod\":\"enode://b957e51f41e4abab8382e1ea7229e88c6e18f34672694c6eae389eac22dab8655622bbd4a08192c321416b9becffaab11c8e2b7a5d0813b922aa128b82990dab@47.75.222.178:443\"}}},\"meta\":{\"hostname\":\"node-01.do-ams3.proxy.misc\",\"timestamp\":\"2021-09-09T00:00:14.760436\"}}"};
tyObject_StatuscolonObjectType___PzMt9bO9aFlTtVlqs6Od8kZA *statusObj = newStatusInstance(cfleetStr);
initNode(statusObj, cstr, cstr); NimStatus statusObj(fleetStr);
statusObj.initializeNode("./test", "./test");
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);

14
test/qtapp/nimstatus.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "nimstatus.hpp"
NimStatus::NimStatus(const QString& fleet){
_ptr = newStatusInstance(fleet.toLocal8Bit().data());
}
NimStatus::~NimStatus(){
freeStatusInstance(_ptr);
_ptr = nullptr;
}
void NimStatus::initializeNode(const QString& statusGoDir, const QString& keystoreDir){
initNode(_ptr, statusGoDir.toLocal8Bit().data(), keystoreDir.toLocal8Bit().data());
}

18
test/qtapp/nimstatus.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <QString>
extern "C"
{
#include "statuslib.h"
}
class NimStatus {
public:
NimStatus(const QString& fleet);
virtual ~NimStatus();
void initializeNode(const QString& statusGoDir, const QString& keystoreDir);
private:
Status* _ptr;
};