From 32f839b0072d3805718904dff2a857ed5e48e6b6 Mon Sep 17 00:00:00 2001 From: Thomas Devoogdt Date: Sat, 8 Jun 2024 15:51:22 +0200 Subject: [PATCH] build: use the system provided jemalloc if found e.g. buildroot has logic to build jemalloc, so if pkg_check_modules can find a suitable version, then use that one. Signed-off-by: Thomas Devoogdt --- CMakeLists.txt | 49 ++++++++++++--------- plugins/processor_sql/parser/CMakeLists.txt | 2 +- src/CMakeLists.txt | 6 +-- src/aws/CMakeLists.txt | 2 +- src/proxy/go/CMakeLists.txt | 2 +- src/record_accessor/CMakeLists.txt | 2 +- src/stream_processor/parser/CMakeLists.txt | 2 +- src/wasm/CMakeLists.txt | 4 +- tests/internal/CMakeLists.txt | 2 +- tests/internal/fuzzers/CMakeLists.txt | 2 +- 10 files changed, 40 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91c860b9953..c21597f6179 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -961,28 +961,35 @@ if(FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") FLB_DEFINITION(FLB_HAVE_JEMALLOC) FLB_DEFINITION(JEMALLOC_MANGLE) - # Add support for options like page size, if empty we default it - if(NOT DEFINED FLB_JEMALLOC_OPTIONS OR "${FLB_JEMALLOC_OPTIONS}" STREQUAL "") - set(FLB_JEMALLOC_OPTIONS "--with-lg-quantum=3") + find_package(PkgConfig) + pkg_check_modules(JEMALLOC jemalloc>=5.3.0) + if(JEMALLOC_FOUND) + # Add support for options like page size, if empty we default it + if(NOT DEFINED FLB_JEMALLOC_OPTIONS OR "${FLB_JEMALLOC_OPTIONS}" STREQUAL "") + set(FLB_JEMALLOC_OPTIONS "--with-lg-quantum=3") + endif() + # Split into a list so CMake handles it correctly when passing to configure command + separate_arguments(FLB_JEMALLOC_OPTIONS_LIST UNIX_COMMAND ${FLB_JEMALLOC_OPTIONS}) + message(STATUS "jemalloc configure: ${FLB_JEMALLOC_OPTIONS_LIST}") + + # Link to Jemalloc as an external dependency + set(FLB_LIBJEMALLOC_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libjemalloc_pic.a") + ExternalProject_Add(jemalloc + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0 + CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0/configure ${AUTOCONF_HOST_OPT} "${FLB_JEMALLOC_OPTIONS_LIST}" --prefix= + CFLAGS=-std=gnu99\ -Wall\ -pipe\ -g3\ -O3\ -funroll-loops + BUILD_COMMAND ${EXTERNAL_BUILD_TOOL} + BUILD_BYPRODUCTS ${FLB_LIBJEMALLOC_PATH} + INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/ + INSTALL_COMMAND ${EXTERNAL_BUILD_TOOL} install_lib_static install_include) + add_library(libjemalloc STATIC IMPORTED GLOBAL) + set_target_properties(libjemalloc PROPERTIES IMPORTED_LOCATION ${FLB_LIBJEMALLOC_PATH}) + add_dependencies(libjemalloc jemalloc) + include_directories("${CMAKE_BINARY_DIR}/include/") + set(JEMALLOC_LIBRARIES "libjemalloc") + else() + include_directories(${JEMALLOC_INCLUDE_DIRS}) endif() - # Split into a list so CMake handles it correctly when passing to configure command - separate_arguments(FLB_JEMALLOC_OPTIONS_LIST UNIX_COMMAND ${FLB_JEMALLOC_OPTIONS}) - message(STATUS "jemalloc configure: ${FLB_JEMALLOC_OPTIONS_LIST}") - - # Link to Jemalloc as an external dependency - set(FLB_LIBJEMALLOC_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libjemalloc_pic.a") - ExternalProject_Add(jemalloc - SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0 - CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0/configure ${AUTOCONF_HOST_OPT} "${FLB_JEMALLOC_OPTIONS_LIST}" --prefix= - CFLAGS=-std=gnu99\ -Wall\ -pipe\ -g3\ -O3\ -funroll-loops - BUILD_COMMAND ${EXTERNAL_BUILD_TOOL} - BUILD_BYPRODUCTS ${FLB_LIBJEMALLOC_PATH} - INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/ - INSTALL_COMMAND ${EXTERNAL_BUILD_TOOL} install_lib_static install_include) - add_library(libjemalloc STATIC IMPORTED GLOBAL) - set_target_properties(libjemalloc PROPERTIES IMPORTED_LOCATION ${FLB_LIBJEMALLOC_PATH}) - add_dependencies(libjemalloc jemalloc) - include_directories("${CMAKE_BINARY_DIR}/include/") else() FLB_OPTION(FLB_JEMALLOC OFF) endif() diff --git a/plugins/processor_sql/parser/CMakeLists.txt b/plugins/processor_sql/parser/CMakeLists.txt index 7e5feca944e..deb6ce51094 100644 --- a/plugins/processor_sql/parser/CMakeLists.txt +++ b/plugins/processor_sql/parser/CMakeLists.txt @@ -27,6 +27,6 @@ add_library(processor-sql-parser STATIC add_flex_bison_dependency(lexer parser) if (FLB_JEMALLOC) - add_dependencies(processor-sql-parser libjemalloc) + add_dependencies(processor-sql-parser ${JEMALLOC_LIBRARIES}) include_directories("${CMAKE_BINARY_DIR}/include/") endif () \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bee936acc58..17d4db17aca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -207,7 +207,7 @@ set(extra_libs if(FLB_JEMALLOC) set(extra_libs ${extra_libs} - "libjemalloc") + ${JEMALLOC_LIBRARIES}) endif() if(FLB_REGEX) @@ -471,7 +471,7 @@ else() endif(MSVC) if(FLB_JEMALLOC) - target_link_libraries(fluent-bit-static libjemalloc) + target_link_libraries(fluent-bit-static ${JEMALLOC_LIBRARIES}) endif() # Binary / Executable @@ -494,7 +494,7 @@ if(FLB_BINARY) endif() if(FLB_JEMALLOC) - target_link_libraries(fluent-bit-bin libjemalloc) + target_link_libraries(fluent-bit-bin ${JEMALLOC_LIBRARIES}) endif() if(FLB_BACKTRACE) diff --git a/src/aws/CMakeLists.txt b/src/aws/CMakeLists.txt index a6580e7a511..0b02fdb5127 100644 --- a/src/aws/CMakeLists.txt +++ b/src/aws/CMakeLists.txt @@ -28,5 +28,5 @@ add_library(flb-aws STATIC ${src}) target_link_libraries(flb-aws flb-aws-compress) if(FLB_JEMALLOC) - target_link_libraries(flb-aws libjemalloc) + target_link_libraries(flb-aws ${JEMALLOC_LIBRARIES}) endif() diff --git a/src/proxy/go/CMakeLists.txt b/src/proxy/go/CMakeLists.txt index 93e5003537a..69ef4204dc5 100644 --- a/src/proxy/go/CMakeLists.txt +++ b/src/proxy/go/CMakeLists.txt @@ -3,7 +3,7 @@ set(src add_library(flb-plugin-proxy-go STATIC ${src}) if(FLB_JEMALLOC) - target_link_libraries(flb-plugin-proxy-go libjemalloc) + target_link_libraries(flb-plugin-proxy-go ${JEMALLOC_LIBRARIES}) endif() if(FLB_REGEX) target_link_libraries(flb-plugin-proxy-go onigmo-static) diff --git a/src/record_accessor/CMakeLists.txt b/src/record_accessor/CMakeLists.txt index 9eb3825d9cb..406f9013845 100644 --- a/src/record_accessor/CMakeLists.txt +++ b/src/record_accessor/CMakeLists.txt @@ -27,5 +27,5 @@ add_flex_bison_dependency(lexer parser) add_dependencies(flb-ra-parser onigmo-static) if(FLB_JEMALLOC) - target_link_libraries(flb-ra-parser libjemalloc) + target_link_libraries(flb-ra-parser ${JEMALLOC_LIBRARIES}) endif() diff --git a/src/stream_processor/parser/CMakeLists.txt b/src/stream_processor/parser/CMakeLists.txt index d14b02bbd3d..05649ec4ff1 100644 --- a/src/stream_processor/parser/CMakeLists.txt +++ b/src/stream_processor/parser/CMakeLists.txt @@ -27,5 +27,5 @@ add_flex_bison_dependency(lexer parser) add_dependencies(flb-sp-parser onigmo-static) if(FLB_JEMALLOC) - target_link_libraries(flb-sp-parser libjemalloc) + target_link_libraries(flb-sp-parser ${JEMALLOC_LIBRARIES}) endif() diff --git a/src/wasm/CMakeLists.txt b/src/wasm/CMakeLists.txt index 4ea7eba65a7..d5e10dd1d62 100644 --- a/src/wasm/CMakeLists.txt +++ b/src/wasm/CMakeLists.txt @@ -132,8 +132,8 @@ set(src add_library(flb-wasm-static STATIC ${src}) if (FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") - set(${JEMALLOC_LIBS} libjemalloc) - add_dependencies(flb-wasm-static libjemalloc) + set(${JEMALLOC_LIBS} ${JEMALLOC_LIBRARIES}) + add_dependencies(flb-wasm-static ${JEMALLOC_LIBRARIES}) include_directories("${CMAKE_BINARY_DIR}/include/") endif () diff --git a/tests/internal/CMakeLists.txt b/tests/internal/CMakeLists.txt index 7fe08ea0d8f..6ef5df4bd4a 100644 --- a/tests/internal/CMakeLists.txt +++ b/tests/internal/CMakeLists.txt @@ -184,7 +184,7 @@ function(prepare_unit_tests TEST_PREFIX SOURCEFILES) add_sanitizers(${source_file_we}) if(FLB_JEMALLOC) - target_link_libraries(${source_file_we} libjemalloc ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(${source_file_we} ${JEMALLOC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) else() target_link_libraries(${source_file_we} ${CMAKE_THREAD_LIBS_INIT}) endif() diff --git a/tests/internal/fuzzers/CMakeLists.txt b/tests/internal/fuzzers/CMakeLists.txt index e76be548a20..aae2b19ba85 100644 --- a/tests/internal/fuzzers/CMakeLists.txt +++ b/tests/internal/fuzzers/CMakeLists.txt @@ -40,7 +40,7 @@ foreach(source_file ${UNIT_TESTS_FILES}) ) if(FLB_JEMALLOC) - target_link_libraries(${source_file_we} libjemalloc ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(${source_file_we} ${JEMALLOC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) else() target_link_libraries(${source_file_we} ${CMAKE_THREAD_LIBS_INIT}) endif()