From 589dba2d27841a070dac038f4451c28995867def Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 15 Sep 2021 14:26:46 -0400 Subject: [PATCH] code review pt-1 --- .gitignore | 2 ++ Makefile | 8 +++--- include/statuslib.h | 18 +++++++++++++ status/status.nim | 13 +++++++--- test/main.c | 4 ++- test/qtapp/.gitignore | 53 +++++++++++++++++++++++++++++++++++++++ test/qtapp/CMakeLists.txt | 12 +++------ test/qtapp/main.cpp | 22 ++++++++-------- test/qtapp/nimstatus.cpp | 14 +++++++++++ test/qtapp/nimstatus.hpp | 18 +++++++++++++ 10 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 include/statuslib.h create mode 100644 test/qtapp/.gitignore create mode 100644 test/qtapp/nimstatus.cpp create mode 100644 test/qtapp/nimstatus.hpp diff --git a/.gitignore b/.gitignore index 311e874..d7df4e5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ vendor/.nimble /bottles/ /test_nim/build tmp +src/libstatuslib-stamp +settings.json diff --git a/Makefile b/Makefile index eff2cd5..7b066ce 100644 --- a/Makefile +++ b/Makefile @@ -117,20 +117,20 @@ $(STATUSGO): | deps + cd vendor/status-go && \ $(MAKE) statusgo-shared-library $(HANDLE_OUTPUT) -LIBSTATUSLIB := build/$@.$(LIBSTATUS_EXT).0 -libstatuslib: | $(STATUSGO) +LIBSTATUSLIB := build/libstatuslib.$(LIBSTATUS_EXT).0 +libstatuslib: | $(STATUSGO) 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 && \ rm -f 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 ]] # libraries for dynamic linking of non-Nim objects EXTRA_LIBS_DYNAMIC := -L"$(CURDIR)/build" -lstatuslib -lm -L"$(STATUSGO_LIBDIR)" -lstatus build_ctest: | $(LIBSTATUSLIB) build deps 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} ifneq ($(detected_OS),Windows) diff --git a/include/statuslib.h b/include/statuslib.h new file mode 100644 index 0000000..28d5871 --- /dev/null +++ b/include/statuslib.h @@ -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__ */ diff --git a/status/status.nim b/status/status.nim index cff4068..aaace99 100644 --- a/status/status.nim +++ b/status/status.nim @@ -100,8 +100,13 @@ proc getBloomFilterBitsSet*(self: Status): int {.exportc, dynlib.} = # exported correctly -proc newStatusInstance*(fleetConfig: cstring): Status {.exportc, dynlib.} = - newStatusInstance($fleetConfig) +proc newStatusInstance*(fleetConfig: cstring): Status {.exportc, cdecl, dynlib.} = + 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.} = 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.} = self.saveSetting(setting, $value) -proc saveBoolSetting*(self: Status, setting: Setting, value: bool) {.exportc, dynlib.} = - self.saveSetting(setting, value) \ No newline at end of file +proc saveBoolSetting*(self: Status, setting: Setting, value: cint) {.exportc, dynlib.} = + self.saveSetting(setting, value.bool) diff --git a/test/main.c b/test/main.c index eebe403..1842af7 100644 --- a/test/main.c +++ b/test/main.c @@ -1,7 +1,7 @@ #include #include -#include "../build/statuslib.h" +#include "statuslib.h" void NimMain(); @@ -18,4 +18,6 @@ int main(int argc, char *argv[]) initNode(statusObj, "./build/A", "./build/B"); + freeStatusInstance(statusObj); + } \ No newline at end of file diff --git a/test/qtapp/.gitignore b/test/qtapp/.gitignore new file mode 100644 index 0000000..5f30463 --- /dev/null +++ b/test/qtapp/.gitignore @@ -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 diff --git a/test/qtapp/CMakeLists.txt b/test/qtapp/CMakeLists.txt index 7907601..7c38f00 100644 --- a/test/qtapp/CMakeLists.txt +++ b/test/qtapp/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) 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) set(PROJECT_SOURCES + nimstatus.cpp main.cpp 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 set(STATUSLIB_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) set(STATUSLIB_LIB_DIR ${STATUSLIB_ROOT}/build) @@ -97,8 +93,8 @@ ExternalProject_Get_Property(libstatuslib SOURCE_DIR) add_library(statuslib SHARED IMPORTED) set_property(TARGET statuslib PROPERTY IMPORTED_LOCATION ${STATUSLIB_LIB_DIR}/libstatuslib.so) add_dependencies(statuslib libstatuslib) -include_directories(${STATUSLIB_LIB_DIR}) -message(STATUS "foo include dir: ${STATUSLIB_LIB_DIR}") +include_directories(${STATUSLIB_ROOT}/include) + target_compile_definitions(test-qtapp diff --git a/test/qtapp/main.cpp b/test/qtapp/main.cpp index 6a39601..b880917 100644 --- a/test/qtapp/main.cpp +++ b/test/qtapp/main.cpp @@ -1,9 +1,10 @@ #include #include +#include "nimstatus.hpp" extern "C" { -#include "statuslib.h" // TODO: is it possible to export directly to C++? +#include "statuslib.h" } int main(int argc, char *argv[]) @@ -12,20 +13,19 @@ int main(int argc, char *argv[]) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif - NimMain(); - helloWorld(); + NimMain(); // Should be called always to initialize nim runtime - 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\"}}"}; - char *cfleetStr = &fleetStr[0]; + auto someDirName = QString{"./test"}.toLocal8Bit().data(); + ensureDirectories(someDirName, someDirName, someDirName); - // TODO: figure out how to unmangle this type - tyObject_StatuscolonObjectType___PzMt9bO9aFlTtVlqs6Od8kZA *statusObj = newStatusInstance(cfleetStr); - initNode(statusObj, cstr, cstr); + 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\"}}"}; + + NimStatus statusObj(fleetStr); + + statusObj.initializeNode("./test", "./test"); QGuiApplication app(argc, argv); diff --git a/test/qtapp/nimstatus.cpp b/test/qtapp/nimstatus.cpp new file mode 100644 index 0000000..876cdd7 --- /dev/null +++ b/test/qtapp/nimstatus.cpp @@ -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()); +} diff --git a/test/qtapp/nimstatus.hpp b/test/qtapp/nimstatus.hpp new file mode 100644 index 0000000..ceef54b --- /dev/null +++ b/test/qtapp/nimstatus.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +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; +};