diff --git a/CMakeLists.txt b/CMakeLists.txt index 793b1c872..4f5fec79e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ option(USE_AVX2 "enable avx2 code" OFF) option(USE_NETNS "enable networking namespace support. Linux only" OFF) option(NATIVE_BUILD "optimise for host system and FPU" ON) option(EMBEDDED_CFG "optimise for older hardware or embedded systems" OFF) -option(BUILD_SHARED_LIBS "build lokinet libraries as shared libraries instead of static" OFF) +option(BUILD_LIBLOKINET "build liblokinet.so" ON) option(SHADOW "use shadow testing framework. linux only" OFF) option(XSAN "use sanitiser, if your system has it (requires -DCMAKE_BUILD_TYPE=Debug)" OFF) option(WITH_JEMALLOC "use jemalloc as allocator" OFF) diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index bbadc09f0..083d6bfb6 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(lokinet-cryptography + STATIC libntrup/src/ntru.cpp libntrup/src/ref/randomsmall.c libntrup/src/ref/swap.c @@ -58,10 +59,6 @@ endif() enable_lto(lokinet-cryptography) add_log_tag(lokinet-cryptography) -if(BUILD_SHARED_LIBS) - install(TARGETS lokinet-cryptography LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() - if (WARNINGS_AS_ERRORS) target_compile_options(lokinet-cryptography PUBLIC -Wall -Wextra -Werror) endif() diff --git a/include/lokinet.h b/include/lokinet.h index 878070afc..92a754ff0 100644 --- a/include/lokinet.h +++ b/include/lokinet.h @@ -2,6 +2,7 @@ #define LOKINET_H #include +#include #ifdef __cplusplus extern "C" diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 54566ee07..864d0218d 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -1,6 +1,7 @@ include(Version) add_library(lokinet-util + STATIC ${CMAKE_CURRENT_BINARY_DIR}/constants/version.cpp util/bencode.cpp util/buffer.cpp @@ -47,7 +48,8 @@ if(ANDROID) endif() add_library(lokinet-platform -# for networking + STATIC + # for networking ev/ev.cpp ev/ev_libuv.cpp net/ip.cpp @@ -91,6 +93,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") endif() add_library(liblokinet + STATIC config/config.cpp config/definition.cpp config/ini.cpp @@ -230,13 +233,16 @@ target_link_libraries(liblokinet PUBLIC cxxopts lokinet-platform lokinet-util lo target_link_libraries(liblokinet PRIVATE libunbound) -if(BUILD_SHARED_LIBS) +if(BUILD_LIBLOKINET) include(GNUInstallDirs) - - install(TARGETS lokinet-util lokinet-platform liblokinet LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + add_library(lokinet-shared SHARED lokinet_shared.cpp) + set_target_properties(lokinet-shared PROPERTIES OUTPUT_NAME lokinet) + target_link_libraries(lokinet-shared liblokinet) + install(TARGETS lokinet-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(WIN32) - target_link_libraries(liblokinet PUBLIC ws2_32 iphlpapi) + target_link_libraries(lokinet-shared PUBLIC ws2_32 iphlpapi) endif() + add_log_tag(lokinet-shared) endif() foreach(lokinet_lib liblokinet lokinet-platform lokinet-util lokinet-cryptography) @@ -244,5 +250,5 @@ foreach(lokinet_lib liblokinet lokinet-platform lokinet-util lokinet-cryptograph endforeach() file(GLOB_RECURSE docs_SRC */*.hpp *.hpp) - + set(DOCS_SRC ${docs_SRC} PARENT_SCOPE) diff --git a/llarp/lokinet_shared.cpp b/llarp/lokinet_shared.cpp new file mode 100644 index 000000000..0bbdb5287 --- /dev/null +++ b/llarp/lokinet_shared.cpp @@ -0,0 +1,63 @@ + + +#include "lokinet.h" +#include "llarp.hpp" +#include "config/config.hpp" + +struct lokinet_context +{ + std::shared_ptr impl; + + std::unique_ptr runner; + + lokinet_context() : impl{std::make_shared()} + {} + + ~lokinet_context() + { + if (runner) + runner->join(); + } +}; + +struct lokinet_context g_context +{}; + +extern "C" +{ + struct lokinet_context* + lokinet_default() + { + return &g_context; + } + + struct lokinet_context* + lokinet_context_new() + { + return new lokinet_context{}; + } + + void + lokinet_context_free(struct lokinet_context* ctx) + { + delete ctx; + } + + void + lokinet_context_start(struct lokinet_context* ctx) + { + ctx->runner = std::make_unique([ctx]() { + auto config = std::make_shared(fs::path{""}); + ctx->impl->Configure(config); + const llarp::RuntimeOptions opts{}; + ctx->impl->Setup(opts); + }); + } + + void + lokinet_context_stop(struct lokinet_context* ctx) + { + ctx->impl->CloseAsync(); + ctx->impl->Wait(); + } +}