Skip to content

Commit 81e0fca

Browse files
committed
[CMake] Expand support for preinstalled jemalloc
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent c765222 commit 81e0fca

File tree

2 files changed

+70
-68
lines changed

2 files changed

+70
-68
lines changed

CMakeLists.txt

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -155,79 +155,81 @@ if(NOT UMF_BUILD_LIBUMF_POOL_JEMALLOC)
155155
set(UMF_POOL_JEMALLOC_ENABLED FALSE)
156156
set(JEMALLOC_FOUND FALSE)
157157
set(JEMALLOC_LIBRARIES FALSE)
158-
elseif(WINDOWS)
159-
pkg_check_modules(JEMALLOC jemalloc)
160-
if(NOT JEMALLOC_FOUND)
161-
find_package(JEMALLOC REQUIRED jemalloc)
162-
endif()
163158
else()
164-
if(NOT DEFINED UMF_JEMALLOC_REPO)
165-
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
159+
set(JEMALLOC_VER 5.3.0)
160+
pkg_check_modules(JEMALLOC QUIET jemalloc)
161+
if(NOT JEMALLOC_FOUND)
162+
find_package(JEMALLOC ${JEMALLOC_VER} QUIET)
166163
endif()
164+
if(NOT JEMALLOC_FOUND AND NOT WINDOWS) # Cannot build from source on Windows
165+
if(NOT DEFINED UMF_JEMALLOC_REPO)
166+
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
167+
endif()
167168

168-
if(NOT DEFINED UMF_JEMALLOC_TAG)
169-
set(UMF_JEMALLOC_TAG 5.3.0)
170-
endif()
169+
if(NOT DEFINED UMF_JEMALLOC_TAG)
170+
set(UMF_JEMALLOC_TAG ${JEMALLOC_VER})
171+
endif()
171172

172-
message(
173-
STATUS
174-
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
175-
)
173+
message(
174+
STATUS
175+
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
176+
)
176177

177-
FetchContent_Declare(
178-
jemalloc_targ
179-
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
180-
GIT_TAG ${UMF_JEMALLOC_TAG})
181-
FetchContent_MakeAvailable(jemalloc_targ)
178+
FetchContent_Declare(
179+
jemalloc_targ
180+
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
181+
GIT_TAG ${UMF_JEMALLOC_TAG})
182+
FetchContent_MakeAvailable(jemalloc_targ)
182183

183-
add_custom_command(
184-
COMMAND ./autogen.sh
185-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
186-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
187-
add_custom_command(
188-
# Custom jemalloc build. Non-default options used:
189-
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
190-
# --disable-cxx - Disable C++ integration. This will cause new and
191-
# delete operators implementations to be omitted.
192-
# --disable-initial-exec-tls - Disable the initial-exec TLS model for
193-
# jemalloc's internal thread-local storage (on those platforms that
194-
# support explicit settings). This can allow jemalloc to be dynamically
195-
# loaded after program startup (e.g. using dlopen). --disable-doc -
196-
# Disable building and installing the documentation.
197-
COMMAND
198-
./configure --prefix=${jemalloc_targ_BINARY_DIR}
199-
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
200-
--disable-doc CFLAGS=-fPIC
201-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
202-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
203-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
204-
205-
if(NOT UMF_QEMU_BUILD)
206-
set(MAKE_ARGUMENTS "-j$(nproc)")
207-
endif()
184+
add_custom_command(
185+
COMMAND ./autogen.sh
186+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
187+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
188+
add_custom_command(
189+
# Custom jemalloc build. Non-default options used:
190+
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
191+
# --disable-cxx - Disable C++ integration. This will cause new and
192+
# delete operators implementations to be omitted.
193+
# --disable-initial-exec-tls - Disable the initial-exec TLS model
194+
# for jemalloc's internal thread-local storage (on those platforms
195+
# that support explicit settings). This can allow jemalloc to be
196+
# dynamically loaded after program startup (e.g. using dlopen).
197+
# --disable-doc - Disable building and installing the documentation.
198+
COMMAND
199+
./configure --prefix=${jemalloc_targ_BINARY_DIR}
200+
--with-jemalloc-prefix=je_ --disable-cxx
201+
--disable-initial-exec-tls --disable-doc CFLAGS=-fPIC
202+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
203+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
204+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
205+
206+
if(NOT UMF_QEMU_BUILD)
207+
set(MAKE_ARGUMENTS "-j$(nproc)")
208+
endif()
208209

209-
add_custom_command(
210-
COMMAND make ${MAKE_ARGUMENTS}
211-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
212-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a
213-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
214-
add_custom_command(
215-
COMMAND make install
216-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
217-
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
218-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a)
219-
220-
add_custom_target(jemalloc_prod
221-
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
222-
add_library(jemalloc INTERFACE)
223-
target_link_libraries(
224-
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
225-
add_dependencies(jemalloc jemalloc_prod)
226-
227-
set(JEMALLOC_FOUND TRUE)
228-
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
229-
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
230-
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
210+
add_custom_command(
211+
COMMAND make ${MAKE_ARGUMENTS}
212+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
213+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a
214+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
215+
add_custom_command(
216+
COMMAND make install
217+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
218+
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
219+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a)
220+
221+
add_custom_target(jemalloc_prod
222+
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
223+
add_library(jemalloc INTERFACE)
224+
target_link_libraries(
225+
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
226+
add_dependencies(jemalloc jemalloc_prod)
227+
228+
set(JEMALLOC_FOUND TRUE)
229+
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
230+
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
231+
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
232+
endif()
231233
endif()
232234

233235
if(JEMALLOC_FOUND)

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ if(UMF_POOL_JEMALLOC_ENABLED)
7575
set(UMF_PRIVATE_INCLUDE_DIRS ${UMF_PRIVATE_INCLUDE_DIRS}
7676
${JEMALLOC_INCLUDE_DIRS})
7777
set(UMF_COMMON_COMPILE_DEFINITIONS ${UMF_COMMON_COMPILE_DEFINITIONS}
78-
"UMF_POOL_JEMALLOC_ENABLED=1")
78+
"UMF_POOL_JEMALLOC_ENABLED=1" "JEMALLOC_NO_DEMANGLE=1")
7979
endif()
8080

8181
set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}
@@ -166,7 +166,7 @@ if(UMF_LINK_HWLOC_STATICALLY)
166166
endif()
167167
endif()
168168

169-
if(NOT WINDOWS AND UMF_POOL_JEMALLOC_ENABLED)
169+
if(UMF_POOL_JEMALLOC_ENABLED AND TARGET jemalloc)
170170
add_dependencies(umf jemalloc)
171171
endif()
172172

0 commit comments

Comments
 (0)