From 83f8f5770360204b0b6072b6b0faf0cde096da20 Mon Sep 17 00:00:00 2001 From: lmangani Date: Sun, 24 Nov 2024 14:09:51 +0000 Subject: [PATCH] fix static binding --- CMakeLists.txt | 17 ++++++++++++----- src/wvlet_extension.cpp | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 342a172..cfddd76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,27 +44,34 @@ build_loadable_extension(${TARGET_NAME} " " ${EXTENSION_SOURCES}) # Create list of libraries in correct order for static linking set(STATIC_LIBS + -Wl,--whole-archive ${WVLET_STATIC_PATH} + -Wl,--no-whole-archive + # Runtime dependencies + ${CMAKE_DL_LIBS} + -pthread OpenSSL::Crypto OpenSSL::SSL ) # For static extension, use static library with correct link order target_link_libraries(${EXTENSION_NAME} - ${WVLET_DYNAMIC_PATH} +# ${WVLET_DYNAMIC_PATH} ${STATIC_LIBS} ) # For loadable extension, use dynamic library -target_link_libraries(${LOADABLE_EXTENSION_NAME} - OpenSSL::SSL - OpenSSL::Crypto +target_link_libraries(${LOADABLE_EXTENSION_NAME} + OpenSSL::SSL + OpenSSL::Crypto ${WVLET_DYNAMIC_PATH} ) # Set link flags to ensure proper symbol resolution set_target_properties(${EXTENSION_NAME} PROPERTIES - LINK_FLAGS "-Wl,--no-as-needed" + LINK_FLAGS "-Wl,--no-as-needed -Wl,-z,now -Wl,--export-dynamic" + ENABLE_EXPORTS ON + POSITION_INDEPENDENT_CODE ON ) install( diff --git a/src/wvlet_extension.cpp b/src/wvlet_extension.cpp index 801d2eb..cbf8b11 100644 --- a/src/wvlet_extension.cpp +++ b/src/wvlet_extension.cpp @@ -10,13 +10,42 @@ #include #include +#ifdef __cplusplus extern "C" { - int wvlet_compile_main(const char*); - const char* wvlet_compile_compile(const char*); +#endif + extern int ScalaNativeInit(void); + + extern int wvlet_compile_main(const char*); + extern const char* wvlet_compile_compile(const char*); + +#ifdef __cplusplus } +#endif namespace duckdb { +// EXPERIMENT INIT +bool InitializeWvletRuntime() { + try { + // Set heap sizes via environment variables + setenv("GC_INITIAL_HEAP_SIZE", "2097152", 1); // 64MB + setenv("GC_MAXIMUM_HEAP_SIZE", "8388608", 1); // 256MB + + // fprintf(stderr, "Initializing Scala Native Runtime...\n"); + int init_result = ScalaNativeInit(); + if (init_result != 0) { + fprintf(stderr, "Failed to initialize Scala Native Runtime: %d\n", init_result); + return false; + } + + // fprintf(stderr, "Scala Native Runtime initialized successfully!\n"); + return true; + } catch (...) { + fprintf(stderr, "Scala Runtime Initialization failed with exception!\n"); + return false; + } +} + void WvletScriptFunction::ParseWvletScript(DataChunk &args, ExpressionState &state, Vector &result) { auto &input_vector = args.data[0]; auto input = FlatVector::GetData(input_vector); @@ -130,6 +159,10 @@ static void LoadInternal(DatabaseInstance &instance) { void WvletExtension::Load(DuckDB &db) { LoadInternal(*db.instance); + // EXPERIMENT + if (!InitializeWvletRuntime()) { + throw std::runtime_error("Failed to initialize Wvlet runtime"); + } } std::string WvletExtension::Name() {