cmake: restructure allocator related code

before this change, the ALLOCATOR cmake option are handled at two
difference places: one to handle ALLOCATOR option, another to add
compilation options based on ALLOCATOR. after this change:

* the ALLOCATOR option is handled in a single place
* dedup the branches handling different ALLOCATORS into a single
  condition: (NOT ALLOCATOR STREQUAL "libc")
* add_compile_options() calls are consolidated into a single one

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
This commit is contained in:
Kefu Chai 2022-05-02 10:46:05 +08:00
parent 249a57d28a
commit 677e134606
2 changed files with 11 additions and 21 deletions

View File

@ -341,9 +341,11 @@ and then jemalloc. If neither of then is found. use the one in libc.")
if(ALLOCATOR)
if(${ALLOCATOR} MATCHES "tcmalloc(_minimal)?")
find_package(gperftools 2.6.2 REQUIRED)
set(ALLOC_LIBS gperftools::${ALLOCATOR})
set(HAVE_LIBTCMALLOC ON)
elseif(${ALLOCATOR} STREQUAL "jemalloc")
find_package(JeMalloc REQUIRED)
set(ALLOC_LIBS JeMalloc::JeMalloc)
set(HAVE_JEMALLOC 1)
elseif(NOT ALLOCATOR STREQUAL "libc")
message(FATAL_ERROR "Unsupported allocator selected: ${ALLOCATOR}")
@ -356,8 +358,10 @@ else(ALLOCATOR)
endif()
if(gperftools_FOUND)
set(ALLOCATOR tcmalloc)
set(ALLOC_LIBS gperftools::tcmalloc)
elseif(JeMalloc_FOUND)
set(ALLOCATOR jemalloc)
set(ALLOC_LIBS JeMalloc::JeMalloc)
else()
if(NOT FREEBSD)
# FreeBSD already has jemalloc as its default allocator
@ -366,6 +370,13 @@ else(ALLOCATOR)
set(ALLOCATOR "libc")
endif(gperftools_FOUND)
endif(ALLOCATOR)
if(NOT ALLOCATOR STREQUAL "libc")
add_compile_options(
$<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-malloc>
$<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-calloc>
$<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-realloc>
$<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-free>)
endif()
# Mingw generates incorrect entry points when using "-pie".
if(WIN32 OR (HAVE_LIBTCMALLOC AND WITH_STATIC_LIBSTDCXX))

View File

@ -276,27 +276,6 @@ if(WITH_CEPHFS_JAVA)
add_subdirectory(java)
endif()
# sort out which allocator to use
if(ALLOCATOR STREQUAL "tcmalloc")
set(ALLOC_LIBS gperftools::tcmalloc)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-malloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-calloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-realloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-free>)
elseif(ALLOCATOR STREQUAL "tcmalloc_minimal")
set(ALLOC_LIBS gperftools::tcmalloc_minimal)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-malloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-calloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-realloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-free>)
elseif(ALLOCATOR STREQUAL "jemalloc")
set(ALLOC_LIBS JeMalloc::JeMalloc)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-malloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-calloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-realloc>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-builtin-free>)
endif()
if (WITH_BLKIN)
add_subdirectory(blkin/blkin-lib)
endif(WITH_BLKIN)