@@ -155,79 +155,92 @@ 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 (CheckCXXSourceCompiles)
171+         set (CMAKE_REQUIRED_INCLUDES ${JEMALLOC_INCLUDE_DIRS} )
172+         set (CMAKE_REQUIRED_LIBRARIES ${JEMALLOC_Library} )
173+         check_cxx_source_compiles("${JEMALLOC_TEST_PROGRAM} " 
174+                                   SYSTEM_JEMALLOC_FOUND)
175+     elseif (NOT  WINDOWS) # Cannot build from source on Windows 
176+         if (NOT  DEFINED  UMF_JEMALLOC_REPO)
177+             set (UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git" )
178+         endif ()
167179
168-     if (NOT  DEFINED  UMF_JEMALLOC_TAG)
169-         set (UMF_JEMALLOC_TAG 5.3.0 )
170-     endif ()
180+          if (NOT  DEFINED  UMF_JEMALLOC_TAG)
181+              set (UMF_JEMALLOC_TAG ${JEMALLOC_VER} )
182+          endif ()
171183
172-     message (
173-         STATUS 
174-             "Will fetch jemalloc from ${UMF_JEMALLOC_REPO}  (tag: ${UMF_JEMALLOC_TAG} )" 
175-     )
184+          message (
185+              STATUS 
186+                  "Will fetch jemalloc from ${UMF_JEMALLOC_REPO}  (tag: ${UMF_JEMALLOC_TAG} )" 
187+          )
176188
177-     FetchContent_Declare(
178-         jemalloc_targ
179-         GIT_REPOSITORY ${UMF_JEMALLOC_REPO} 
180-         GIT_TAG ${UMF_JEMALLOC_TAG} )
181-     FetchContent_MakeAvailable(jemalloc_targ)
189+          FetchContent_Declare(
190+              jemalloc_targ
191+              GIT_REPOSITORY ${UMF_JEMALLOC_REPO} 
192+              GIT_TAG ${UMF_JEMALLOC_TAG} )
193+          FetchContent_MakeAvailable(jemalloc_targ)
182194
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 ()
195+          add_custom_command (
196+              COMMAND  ./autogen.sh
197+              WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
198+              OUTPUT  ${jemalloc_targ_SOURCE_DIR} /configure)
199+          add_custom_command (
200+              # Custom jemalloc build. Non-default options used: 
201+              # --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs 
202+              # --disable-cxx - Disable C++ integration. This will cause new and 
203+              # delete operators implementations to be omitted. 
204+              # --disable-initial-exec-tls - Disable the initial-exec TLS model 
205+              # for  jemalloc's internal thread-local storage (on those platforms
206+              # that  support explicit settings). This can allow jemalloc to be
207+              # dynamically  loaded after program startup (e.g. using dlopen).
208+              # --disable-doc -  Disable building and installing the documentation.
209+              COMMAND 
210+                  ./configure --prefix =${jemalloc_targ_BINARY_DIR} 
211+                  --with-jemalloc-prefix =je_ --disable-cxx
212+                 --disable-initial-exec-tls  --disable-doc  CFLAGS=-fPIC
213+              WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
214+              OUTPUT  ${jemalloc_targ_SOURCE_DIR} /Makefile
215+              DEPENDS  ${jemalloc_targ_SOURCE_DIR} /configure)
216+ 
217+          if (NOT  UMF_QEMU_BUILD)
218+              set (MAKE_ARGUMENTS "-j$(nproc)" )
219+          endif ()
208220
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)
221+         add_custom_command (
222+             COMMAND  make ${MAKE_ARGUMENTS} 
223+             WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
224+             OUTPUT  ${jemalloc_targ_SOURCE_DIR} /lib/libjemalloc.a
225+             DEPENDS  ${jemalloc_targ_SOURCE_DIR} /Makefile)
226+         add_custom_command (
227+             COMMAND  make install 
228+             WORKING_DIRECTORY  ${jemalloc_targ_SOURCE_DIR} 
229+             OUTPUT  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a
230+             DEPENDS  ${jemalloc_targ_SOURCE_DIR} /lib/libjemalloc.a)
231+ 
232+         add_custom_target (jemalloc_prod
233+                           DEPENDS  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
234+         add_library (jemalloc INTERFACE )
235+         target_link_libraries (
236+             jemalloc INTERFACE  ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
237+         add_dependencies (jemalloc jemalloc_prod)
238+ 
239+         set (JEMALLOC_FOUND TRUE )
240+         set (JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR} /lib)
241+         set (JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR} /include )
242+         set (JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR} /lib/libjemalloc.a)
243+     endif ()
231244endif ()
232245
233246if (JEMALLOC_FOUND)
0 commit comments