Skip to content

Commit 4b82c02

Browse files
ThomasDevoogdtedsiper
authored andcommitted
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 if -DFLB_PREFER_SYSTEM_LIB_JEMALLOC=Yes. Signed-off-by: Thomas Devoogdt <[email protected]>
1 parent c68d4ae commit 4b82c02

File tree

10 files changed

+46
-35
lines changed

10 files changed

+46
-35
lines changed

CMakeLists.txt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ option(FLB_RUN_LDCONFIG "Enable execution of ldconfig after installation" No)
204204
# Prefer system libraries if available
205205
option(FLB_PREFER_SYSTEM_LIBS "Prefer system libraries" No)
206206
option(FLB_PREFER_SYSTEM_LIB_BACKTRACE "Prefer the libbacktrace system library" ${FLB_PREFER_SYSTEM_LIBS})
207+
option(FLB_PREFER_SYSTEM_LIB_JEMALLOC "Prefer the libjemalloc system library" ${FLB_PREFER_SYSTEM_LIBS})
207208
option(FLB_PREFER_SYSTEM_LIB_LUAJIT "Prefer the libluajit system library" ${FLB_PREFER_SYSTEM_LIBS})
208209
option(FLB_PREFER_SYSTEM_LIB_NGHTTP2 "Prefer the libnghttp2 system library" ${FLB_PREFER_SYSTEM_LIBS})
209210

@@ -912,31 +913,41 @@ endif()
912913
# Memory Allocator
913914
# ================
914915
if(FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
915-
FLB_DEFINITION(FLB_HAVE_JEMALLOC)
916-
FLB_DEFINITION(JEMALLOC_MANGLE)
916+
if(FLB_PREFER_SYSTEM_LIB_JEMALLOC)
917+
find_package(PkgConfig)
918+
pkg_check_modules(JEMALLOC jemalloc>=5.0.0)
919+
endif()
920+
if(JEMALLOC_FOUND)
921+
include_directories(${JEMALLOC_INCLUDE_DIRS})
922+
link_directories(${JEMALLOC_LIBRARY_DIRS})
923+
else()
924+
FLB_DEFINITION(JEMALLOC_MANGLE)
917925

918-
# Add support for options like page size, if empty we default it
919-
if(NOT DEFINED FLB_JEMALLOC_OPTIONS OR "${FLB_JEMALLOC_OPTIONS}" STREQUAL "")
920-
set(FLB_JEMALLOC_OPTIONS "--with-lg-quantum=3")
926+
# Add support for options like page size, if empty we default it
927+
if(NOT DEFINED FLB_JEMALLOC_OPTIONS OR "${FLB_JEMALLOC_OPTIONS}" STREQUAL "")
928+
set(FLB_JEMALLOC_OPTIONS "--with-lg-quantum=3")
929+
endif()
930+
# Split into a list so CMake handles it correctly when passing to configure command
931+
separate_arguments(FLB_JEMALLOC_OPTIONS_LIST UNIX_COMMAND ${FLB_JEMALLOC_OPTIONS})
932+
message(STATUS "jemalloc configure: ${FLB_JEMALLOC_OPTIONS_LIST}")
933+
934+
# Link to Jemalloc as an external dependency
935+
set(FLB_LIBJEMALLOC_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libjemalloc_pic.a")
936+
ExternalProject_Add(jemalloc
937+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0
938+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0/configure ${AUTOCONF_HOST_OPT} "${FLB_JEMALLOC_OPTIONS_LIST}" --prefix=<INSTALL_DIR>
939+
CFLAGS=-std=gnu99\ -Wall\ -pipe\ -g3\ -O3\ -funroll-loops
940+
BUILD_COMMAND ${EXTERNAL_BUILD_TOOL}
941+
BUILD_BYPRODUCTS ${FLB_LIBJEMALLOC_PATH}
942+
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/
943+
INSTALL_COMMAND ${EXTERNAL_BUILD_TOOL} install_lib_static install_include)
944+
add_library(libjemalloc STATIC IMPORTED GLOBAL)
945+
set_target_properties(libjemalloc PROPERTIES IMPORTED_LOCATION ${FLB_LIBJEMALLOC_PATH})
946+
add_dependencies(libjemalloc jemalloc)
947+
include_directories("${CMAKE_BINARY_DIR}/include/")
948+
set(JEMALLOC_LIBRARIES "libjemalloc")
921949
endif()
922-
# Split into a list so CMake handles it correctly when passing to configure command
923-
separate_arguments(FLB_JEMALLOC_OPTIONS_LIST UNIX_COMMAND ${FLB_JEMALLOC_OPTIONS})
924-
message(STATUS "jemalloc configure: ${FLB_JEMALLOC_OPTIONS_LIST}")
925-
926-
# Link to Jemalloc as an external dependency
927-
set(FLB_LIBJEMALLOC_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libjemalloc_pic.a")
928-
ExternalProject_Add(jemalloc
929-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0
930-
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/jemalloc-5.3.0/configure ${AUTOCONF_HOST_OPT} "${FLB_JEMALLOC_OPTIONS_LIST}" --prefix=<INSTALL_DIR>
931-
CFLAGS=-std=gnu99\ -Wall\ -pipe\ -g3\ -O3\ -funroll-loops
932-
BUILD_COMMAND ${EXTERNAL_BUILD_TOOL}
933-
BUILD_BYPRODUCTS ${FLB_LIBJEMALLOC_PATH}
934-
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/
935-
INSTALL_COMMAND ${EXTERNAL_BUILD_TOOL} install_lib_static install_include)
936-
add_library(libjemalloc STATIC IMPORTED GLOBAL)
937-
set_target_properties(libjemalloc PROPERTIES IMPORTED_LOCATION ${FLB_LIBJEMALLOC_PATH})
938-
add_dependencies(libjemalloc jemalloc)
939-
include_directories("${CMAKE_BINARY_DIR}/include/")
950+
FLB_DEFINITION(FLB_HAVE_JEMALLOC)
940951
else()
941952
FLB_OPTION(FLB_JEMALLOC OFF)
942953
endif()

plugins/processor_sql/parser/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_library(processor-sql-parser STATIC
2626

2727
add_flex_bison_dependency(lexer parser)
2828

29-
if (FLB_JEMALLOC)
29+
if (FLB_JEMALLOC AND (NOT JEMALLOC_FOUND))
3030
add_dependencies(processor-sql-parser libjemalloc)
3131
include_directories("${CMAKE_BINARY_DIR}/include/")
3232
endif ()

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ set(extra_libs
213213
if(FLB_JEMALLOC)
214214
set(extra_libs
215215
${extra_libs}
216-
"libjemalloc")
216+
${JEMALLOC_LIBRARIES})
217217
endif()
218218

219219
if(FLB_REGEX)
@@ -485,7 +485,7 @@ else()
485485
endif(MSVC)
486486

487487
if(FLB_JEMALLOC)
488-
target_link_libraries(fluent-bit-static libjemalloc)
488+
target_link_libraries(fluent-bit-static ${JEMALLOC_LIBRARIES})
489489
endif()
490490

491491
# Binary / Executable
@@ -508,7 +508,7 @@ if(FLB_BINARY)
508508
endif()
509509

510510
if(FLB_JEMALLOC)
511-
target_link_libraries(fluent-bit-bin libjemalloc)
511+
target_link_libraries(fluent-bit-bin ${JEMALLOC_LIBRARIES})
512512
endif()
513513

514514
if(FLB_BACKTRACE)

src/aws/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ add_library(flb-aws STATIC ${src})
2828
target_link_libraries(flb-aws flb-aws-compress)
2929

3030
if(FLB_JEMALLOC)
31-
target_link_libraries(flb-aws libjemalloc)
31+
target_link_libraries(flb-aws ${JEMALLOC_LIBRARIES})
3232
endif()

src/proxy/go/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set(src
33

44
add_library(flb-plugin-proxy-go STATIC ${src})
55
if(FLB_JEMALLOC)
6-
target_link_libraries(flb-plugin-proxy-go libjemalloc)
6+
target_link_libraries(flb-plugin-proxy-go ${JEMALLOC_LIBRARIES})
77
endif()
88
if(FLB_REGEX)
99
target_link_libraries(flb-plugin-proxy-go onigmo-static)

src/record_accessor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ add_flex_bison_dependency(lexer parser)
2727
add_dependencies(flb-ra-parser onigmo-static)
2828

2929
if(FLB_JEMALLOC)
30-
target_link_libraries(flb-ra-parser libjemalloc)
30+
target_link_libraries(flb-ra-parser ${JEMALLOC_LIBRARIES})
3131
endif()

src/stream_processor/parser/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ add_flex_bison_dependency(lexer parser)
2727
add_dependencies(flb-sp-parser onigmo-static)
2828

2929
if(FLB_JEMALLOC)
30-
target_link_libraries(flb-sp-parser libjemalloc)
30+
target_link_libraries(flb-sp-parser ${JEMALLOC_LIBRARIES})
3131
endif()

src/wasm/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ set(src
131131

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

134-
if (FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
134+
if (FLB_JEMALLOC AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND (NOT JEMALLOC_FOUND))
135135
set(${JEMALLOC_LIBS} libjemalloc)
136136
add_dependencies(flb-wasm-static libjemalloc)
137137
include_directories("${CMAKE_BINARY_DIR}/include/")
@@ -140,5 +140,5 @@ endif ()
140140
if (WAMR_BUILD_LIBC_UVWASI)
141141
target_link_libraries(flb-wasm-static vmlib-static ${UV_A_LIBS})
142142
else ()
143-
target_link_libraries(flb-wasm-static vmlib-static ${JEMALLOC_LIBS})
143+
target_link_libraries(flb-wasm-static vmlib-static ${JEMALLOC_LIBRARIES})
144144
endif()

tests/internal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function(prepare_unit_tests TEST_PREFIX SOURCEFILES)
185185
add_sanitizers(${source_file_we})
186186

187187
if(FLB_JEMALLOC)
188-
target_link_libraries(${source_file_we} libjemalloc ${CMAKE_THREAD_LIBS_INIT})
188+
target_link_libraries(${source_file_we} ${JEMALLOC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
189189
else()
190190
target_link_libraries(${source_file_we} ${CMAKE_THREAD_LIBS_INIT})
191191
endif()

tests/internal/fuzzers/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ foreach(source_file ${UNIT_TESTS_FILES})
4040
)
4141

4242
if(FLB_JEMALLOC)
43-
target_link_libraries(${source_file_we} libjemalloc ${CMAKE_THREAD_LIBS_INIT})
43+
target_link_libraries(${source_file_we} ${JEMALLOC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
4444
else()
4545
target_link_libraries(${source_file_we} ${CMAKE_THREAD_LIBS_INIT})
4646
endif()

0 commit comments

Comments
 (0)