Skip to content

Commit

Permalink
build: use the system provided jemalloc if found
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ThomasDevoogdt committed Jun 9, 2024
1 parent 787b843 commit cdc5b49
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 35 deletions.
54 changes: 31 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -961,31 +961,39 @@ endif()
# Memory Allocator
# ================
if(FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
FLB_DEFINITION(FLB_HAVE_JEMALLOC)
FLB_DEFINITION(JEMALLOC_MANGLE)
find_package(PkgConfig)
pkg_check_modules(JEMALLOC jemalloc>=5.3.0)
if(JEMALLOC_FOUND)
include_directories(${JEMALLOC_INCLUDE_DIRS})
link_directories(${JEMALLOC_LIBRARY_DIRS})
else()
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")
# 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=<INSTALL_DIR>
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")
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=<INSTALL_DIR>
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/")
FLB_DEFINITION(FLB_HAVE_JEMALLOC)
else()
FLB_OPTION(FLB_JEMALLOC OFF)
endif()
Expand Down
2 changes: 1 addition & 1 deletion plugins/processor_sql/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ add_library(processor-sql-parser STATIC

add_flex_bison_dependency(lexer parser)

if (FLB_JEMALLOC)
if (FLB_JEMALLOC AND (NOT JEMALLOC_FOUND))
add_dependencies(processor-sql-parser libjemalloc)
include_directories("${CMAKE_BINARY_DIR}/include/")
endif ()
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ set(extra_libs
if(FLB_JEMALLOC)
set(extra_libs
${extra_libs}
"libjemalloc")
${JEMALLOC_LIBRARIES})
endif()

if(FLB_REGEX)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/aws/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion src/proxy/go/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/record_accessor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion src/stream_processor/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 2 additions & 2 deletions src/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ set(src

add_library(flb-wasm-static STATIC ${src})

if (FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
if (FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND (NOT JEMALLOC_FOUND))
set(${JEMALLOC_LIBS} libjemalloc)
add_dependencies(flb-wasm-static libjemalloc)
include_directories("${CMAKE_BINARY_DIR}/include/")
Expand All @@ -140,5 +140,5 @@ endif ()
if (WAMR_BUILD_LIBC_UVWASI)
target_link_libraries(flb-wasm-static vmlib-static ${UV_A_LIBS})
else ()
target_link_libraries(flb-wasm-static vmlib-static ${JEMALLOC_LIBS})
target_link_libraries(flb-wasm-static vmlib-static ${JEMALLOC_LIBRARIES})
endif()
2 changes: 1 addition & 1 deletion tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/internal/fuzzers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cdc5b49

Please sign in to comment.