@@ -155,79 +155,95 @@ 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 ()
163158else ()
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 (JEMALLOC_FOUND)
165+         # We may find a cached result from the in-tree built which uses 
166+         # different symbol names, so compile a test profile to check if the 
167+         # found version is a system install or not. 
168+         set (JEMALLOC_TEST_PROGRAM "#include <jemalloc/jemalloc.h> 
169+         int main(int, char*[]) { (void) je_mallocx(0, 0); return 0; }" )
170+         include (CheckCSourceCompiles)
171+         set (CMAKE_REQUIRED_INCLUDES ${JEMALLOC_INCLUDE_DIRS} )
172+         set (CMAKE_REQUIRED_LIBRARIES ${JEMALLOC_LIBRARIES} )
173+         check_c_source_compiles("${JEMALLOC_TEST_PROGRAM} " 
174+                                 JEMALLOC_SYMBOLS_PREFIXED)
175+         if (NOT  JEMALLOC_SYMBOLS_PREFIXED)
176+             set (SYSTEM_JEMALLOC_FOUND TRUE )
177+         endif ()
178+     elseif (NOT  WINDOWS) # Cannot build from source on Windows 
179+         if (NOT  DEFINED  UMF_JEMALLOC_REPO)
180+             set (UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git" )
181+         endif ()
167182
168-     if (NOT  DEFINED  UMF_JEMALLOC_TAG)
169-         set (UMF_JEMALLOC_TAG 5.3.0 )
170-     endif ()
183+          if (NOT  DEFINED  UMF_JEMALLOC_TAG)
184+              set (UMF_JEMALLOC_TAG ${JEMALLOC_VER} )
185+          endif ()
171186
172-     message (
173-         STATUS 
174-             "Will fetch jemalloc from ${UMF_JEMALLOC_REPO}  (tag: ${UMF_JEMALLOC_TAG} )" 
175-     )
187+          message (
188+              STATUS 
189+                  "Will fetch jemalloc from ${UMF_JEMALLOC_REPO}  (tag: ${UMF_JEMALLOC_TAG} )" 
190+          )
176191
177-     FetchContent_Declare(
178-         jemalloc_targ
179-         GIT_REPOSITORY ${UMF_JEMALLOC_REPO} 
180-         GIT_TAG ${UMF_JEMALLOC_TAG} )
181-     FetchContent_MakeAvailable(jemalloc_targ)
192+          FetchContent_Declare(
193+              jemalloc_targ
194+              GIT_REPOSITORY ${UMF_JEMALLOC_REPO} 
195+              GIT_TAG ${UMF_JEMALLOC_TAG} )
196+          FetchContent_MakeAvailable(jemalloc_targ)
182197
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 ()
198+          add_custom_command (
199+              COMMAND  ./autogen.sh
200+              WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
201+              OUTPUT  ${jemalloc_targ_SOURCE_DIR} /configure)
202+          add_custom_command (
203+              # Custom jemalloc build. Non-default options used: 
204+              # --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs 
205+              # --disable-cxx - Disable C++ integration. This will cause new and 
206+              # delete operators implementations to be omitted. 
207+              # --disable-initial-exec-tls - Disable the initial-exec TLS model 
208+              # for  jemalloc's internal thread-local storage (on those platforms
209+              # that  support explicit settings). This can allow jemalloc to be
210+              # dynamically  loaded after program startup (e.g. using dlopen).
211+              # --disable-doc -  Disable building and installing the documentation.
212+              COMMAND 
213+                  ./configure --prefix =${jemalloc_targ_BINARY_DIR} 
214+                  --with-jemalloc-prefix =je_ --disable-cxx
215+                 --disable-initial-exec-tls  --disable-doc  CFLAGS=-fPIC
216+              WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
217+              OUTPUT  ${jemalloc_targ_SOURCE_DIR} /Makefile
218+              DEPENDS  ${jemalloc_targ_SOURCE_DIR} /configure)
219+ 
220+          if (NOT  UMF_QEMU_BUILD)
221+              set (MAKE_ARGUMENTS "-j$(nproc)" )
222+          endif ()
208223
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)
224+         add_custom_command (
225+             COMMAND  make ${MAKE_ARGUMENTS} 
226+             WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
227+             OUTPUT  ${jemalloc_targ_SOURCE_DIR} /lib/libjemalloc.a
228+             DEPENDS  ${jemalloc_targ_SOURCE_DIR} /Makefile)
229+         add_custom_command (
230+             COMMAND  make install 
231+             WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
232+             OUTPUT  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a
233+             DEPENDS  ${jemalloc_targ_SOURCE_DIR} /lib/libjemalloc.a)
234+ 
235+         add_custom_target (jemalloc_prod
236+                           DEPENDS  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
237+         add_library (jemalloc INTERFACE )
238+         target_link_libraries (
239+             jemalloc INTERFACE  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
240+         add_dependencies (jemalloc jemalloc_prod)
241+ 
242+         set (JEMALLOC_FOUND TRUE )
243+         set (JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR} /lib)
244+         set (JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR} /include )
245+         set (JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
246+     endif ()
231247endif ()
232248
233249if (JEMALLOC_FOUND)
0 commit comments