@@ -12,7 +12,7 @@ option(MI_XMALLOC "Enable abort() call on memory allocation failure by
1212option (MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF )
1313option (MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF )
1414option (MI_SEE_ASM "Generate assembly files" OFF )
15- option (MI_INTERPOSE "Use interpose to override standard malloc on macOS" OFF )
15+ option (MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON )
1616option (MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON )
1717option (MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF )
1818option (MI_BUILD_SHARED "Build shared library" ON )
@@ -21,10 +21,14 @@ option(MI_BUILD_OBJECT "Build object library" ON)
2121option (MI_BUILD_TESTS "Build test executables" ON )
2222option (MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF )
2323option (MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF )
24+
25+ # deprecated options
2426option (MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF )
25- option (MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF )
27+ option (MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version (deprecated)" OFF )
28+ option (MI_USE_LIBATOMIC "Explicitly link with -latomic (on older systems) (deprecated and detected automatically)" OFF )
2629option (MI_SKIP_COLLECT_ON_EXIT, "Skip collecting memory on exit" OFF )
2730
31+ include (GNUInstallDirs)
2832include ("cmake/mimalloc-config-version.cmake" )
2933
3034set (mi_sources
@@ -43,10 +47,12 @@ set(mi_sources
4347 src/options .c
4448 src/init.c)
4549
50+
4651# -----------------------------------------------------------------------------
47- # Converience : set default build type depending on the build directory
52+ # Convenience : set default build type depending on the build directory
4853# -----------------------------------------------------------------------------
4954
55+ message (STATUS "" )
5056if (NOT CMAKE_BUILD_TYPE )
5157 if ("${CMAKE_BINARY_DIR} " MATCHES ".*(D|d)ebug$" OR MI_DEBUG_FULL)
5258 message (STATUS "No build type selected, default to: Debug" )
@@ -62,6 +68,7 @@ if("${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$")
6268 set (MI_SECURE "ON" )
6369endif ()
6470
71+
6572# -----------------------------------------------------------------------------
6673# Process options
6774# -----------------------------------------------------------------------------
@@ -78,12 +85,20 @@ if(MI_OVERRIDE)
7885 message (STATUS " Use malloc zone to override malloc (MI_OSX_ZONE=ON)" )
7986 list (APPEND mi_sources src/alloc-override-osx.c)
8087 list (APPEND mi_defines MI_OSX_ZONE=1)
88+ if (NOT MI_OSX_INTERPOSE)
89+ message (STATUS " WARNING: zone overriding usually also needs interpose (use -DMI_OSX_INTERPOSE=ON)" )
90+ endif ()
8191 endif ()
82- if (MI_INTERPOSE )
92+ if (MI_OSX_INTERPOSE )
8393 # use interpose on macOS
84- message (STATUS " Use interpose to override malloc (MI_INTERPOSE=ON)" )
85- message (STATUS " WARNING: interpose does not seem to work reliably on the M1; use -DMI_OSX_ZONE=ON instead" )
86- list (APPEND mi_defines MI_INTERPOSE)
94+ message (STATUS " Use interpose to override malloc (MI_OSX_INTERPOSE=ON)" )
95+ list (APPEND mi_defines MI_OSX_INTERPOSE=1)
96+ if (NOT MI_OSX_ZONE)
97+ message (STATUS " WARNING: interpose usually also needs zone overriding (use -DMI_OSX_INTERPOSE=ON)" )
98+ endif ()
99+ endif ()
100+ if ((NOT MI_USE_CXX) AND MI_OVERRIDE)
101+ message (STATUS " WARNING: if overriding C++ new/delete, it is best to build mimalloc with a C++ compiler (use -DMI_USE_CXX=ON)" )
87102 endif ()
88103 endif ()
89104endif ()
@@ -171,9 +186,9 @@ endif()
171186# Compiler flags
172187if (CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU" )
173188 list (APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas -fvisibility=hidden)
174- if (CMAKE_C_COMPILER_ID MATCHES "GNU" )
175- list (APPEND mi_cflags -Wno-invalid-memory-model )
176- endif ()
189+ if (NOT MI_USE_CXX )
190+ list (APPEND mi_cflags -Wstrict-prototypes )
191+ endif ()
177192 if (CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" )
178193 list (APPEND mi_cflags -Wpedantic -Wno-static -in-inline)
179194 endif ()
@@ -189,6 +204,9 @@ if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM
189204 else ()
190205 list (APPEND mi_cflags -ftls-model=initial-exec)
191206 endif ()
207+ if (MI_OVERRIDE)
208+ list (APPEND mi_cflags -fno-builtin-malloc)
209+ endif ()
192210endif ()
193211
194212if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
@@ -199,27 +217,37 @@ endif()
199217if (WIN32 )
200218 list (APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
201219else ()
202- if (NOT ${CMAKE_C_COMPILER} MATCHES "android" )
203- list (APPEND mi_libraries pthread)
204- find_library (LIBRT rt)
205- if (LIBRT)
206- list (APPEND mi_libraries ${LIBRT} )
207- endif ()
220+ find_library (MI_LIBPTHREAD pthread)
221+ if (MI_LIBPTHREAD)
222+ list (APPEND mi_libraries ${MI_LIBPTHREAD} )
223+ endif ()
224+ find_library (MI_LIBRT rt)
225+ if (MI_LIBRT)
226+ list (APPEND mi_libraries ${MI_LIBRT} )
227+ endif ()
228+ find_library (MI_LIBATOMIC atomic)
229+ if (MI_LIBATOMIC OR MI_USE_LIBATOMIC)
230+ list (APPEND mi_libraries atomic)
208231 endif ()
209232endif ()
210233
211234# -----------------------------------------------------------------------------
212235# Install and output names
213236# -----------------------------------------------------------------------------
214237
238+ # dynamic/shared library and symlinks always go to /usr/local/lib equivalent
239+ set (mi_install_libdir "${CMAKE_INSTALL_LIBDIR} " )
240+
241+ # static libraries and object files, includes, and cmake config files
242+ # are either installed at top level, or use versioned directories for side-by-side installation (default)
215243if (MI_INSTALL_TOPLEVEL)
216- set (mi_install_libdir "lib " )
217- set (mi_install_incdir "include" )
218- set (mi_install_cmakedir " cmake" )
244+ set (mi_install_objdir " ${CMAKE_INSTALL_LIBDIR} " )
245+ set (mi_install_incdir " ${CMAKE_INSTALL_INCLUDEDIR} " )
246+ set (mi_install_cmakedir " ${CMAKE_INSTALL_LIBDIR} / cmake/mimalloc " )
219247else ()
220- set (mi_install_libdir "lib /mimalloc-${mi_version} " )
221- set (mi_install_incdir "include /mimalloc-${mi_version} " )
222- set (mi_install_cmakedir "share/ mimalloc-${mi_version} /cmake " )
248+ set (mi_install_objdir " ${CMAKE_INSTALL_LIBDIR} /mimalloc-${mi_version} " ) # for static library and object files
249+ set (mi_install_incdir " ${CMAKE_INSTALL_INCLUDEDIR} /mimalloc-${mi_version} " ) # for includes
250+ set (mi_install_cmakedir " ${CMAKE_INSTALL_LIBDIR} /cmake/ mimalloc-${mi_version} " ) # for cmake package info
223251endif ()
224252
225253if (MI_SECURE)
@@ -229,7 +257,7 @@ else()
229257endif ()
230258
231259string (TOLOWER "${CMAKE_BUILD_TYPE} " CMAKE_BUILD_TYPE_LC)
232- if (NOT (CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel)$" ))
260+ if (NOT (CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none )$" ))
233261 set (mi_basename "${mi_basename} -${CMAKE_BUILD_TYPE_LC} " ) #append build type (e.g. -debug) if not a release version
234262endif ()
235263if (MI_BUILD_SHARED)
@@ -247,13 +275,16 @@ endif()
247275
248276message (STATUS "" )
249277message (STATUS "Library base name: ${mi_basename} " )
278+ message (STATUS "Version : ${mi_version} " )
250279message (STATUS "Build type : ${CMAKE_BUILD_TYPE_LC} " )
251280if (MI_USE_CXX)
252- message (STATUS "Compiler : ${CMAKE_CXX_COMPILER} " )
281+ message (STATUS "C++ Compiler : ${CMAKE_CXX_COMPILER} " )
253282else ()
254- message (STATUS "Compiler : ${CMAKE_C_COMPILER} " )
283+ message (STATUS "C Compiler : ${CMAKE_C_COMPILER} " )
255284endif ()
256- message (STATUS "Version : ${mi_version} " )
285+ message (STATUS "Compiler flags : ${mi_cflags} " )
286+ message (STATUS "Compiler defines : ${mi_defines} " )
287+ message (STATUS "Link libraries : ${mi_libraries} " )
257288message (STATUS "Build targets : ${mi_build_targets} " )
258289message (STATUS "" )
259290
@@ -264,7 +295,7 @@ message(STATUS "")
264295# shared library
265296if (MI_BUILD_SHARED)
266297 add_library (mimalloc SHARED ${mi_sources} )
267- set_target_properties (mimalloc PROPERTIES VERSION ${mi_version} OUTPUT_NAME ${mi_basename} )
298+ set_target_properties (mimalloc PROPERTIES VERSION ${mi_version} SOVERSION ${mi_version_major} OUTPUT_NAME ${mi_basename} )
268299 target_compile_definitions (mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT)
269300 target_compile_options (mimalloc PRIVATE ${mi_cflags} )
270301 target_link_libraries (mimalloc PUBLIC ${mi_libraries} )
@@ -310,7 +341,8 @@ if (MI_BUILD_STATIC)
310341 set_target_properties (mimalloc-static PROPERTIES OUTPUT_NAME ${mi_basename} )
311342 endif ()
312343
313- install (TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${mi_install_libdir} LIBRARY)
344+ install (TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${mi_install_objdir} LIBRARY)
345+ install (EXPORT mimalloc DESTINATION ${mi_install_cmakedir} )
314346endif ()
315347
316348# install include files
@@ -320,16 +352,6 @@ install(FILES include/mimalloc-new-delete.h DESTINATION ${mi_install_incdir})
320352install (FILES cmake/mimalloc-config.cmake DESTINATION ${mi_install_cmakedir} )
321353install (FILES cmake/mimalloc-config-version .cmake DESTINATION ${mi_install_cmakedir} )
322354
323- if (NOT WIN32 AND MI_BUILD_SHARED AND NOT MI_INSTALL_TOPLEVEL)
324- # install a symlink in the /usr/local/lib to the versioned library
325- # note: use delayed prefix expansion as \${CMAKE_INSTALL_PREFIX}
326- set (mi_symlink "${CMAKE_SHARED_MODULE_PREFIX}${mi_basename}${CMAKE_SHARED_LIBRARY_SUFFIX} " )
327- set (mi_soname "mimalloc-${mi_version} /${mi_symlink} .${mi_version} " )
328- install (CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${mi_soname} ${mi_symlink} WORKING_DIRECTORY \$ {CMAKE_INSTALL_PREFIX}/lib)" )
329- install (CODE "MESSAGE(\" -- Symbolic link: \$ {CMAKE_INSTALL_PREFIX}/lib/${mi_symlink} -> ${mi_soname} \" )" )
330- install (CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${mi_soname} ${mi_symlink} .${mi_version} WORKING_DIRECTORY \$ {CMAKE_INSTALL_PREFIX}/lib)" )
331- install (CODE "MESSAGE(\" -- Symbolic link: \$ {CMAKE_INSTALL_PREFIX}/lib/${mi_symlink} .${mi_version} -> ${mi_soname} \" )" )
332- endif ()
333355
334356# single object file for more predictable static overriding
335357if (MI_BUILD_OBJECT)
@@ -343,12 +365,12 @@ if (MI_BUILD_OBJECT)
343365 )
344366
345367 # the following seems to lead to cmake warnings/errors on some systems, disable for now :-(
346- # install(TARGETS mimalloc-obj EXPORT mimalloc DESTINATION ${mi_install_libdir })
368+ # install(TARGETS mimalloc-obj EXPORT mimalloc DESTINATION ${mi_install_objdir })
347369
348370 # the FILES expression can also be: $<TARGET_OBJECTS:mimalloc-obj>
349371 # but that fails cmake versions less than 3.10 so we leave it as is for now
350372 install (FILES ${CMAKE_CURRENT_BINARY_DIR} /CMakeFiles/mimalloc-obj.dir/src/static .c${CMAKE_C_OUTPUT_EXTENSION}
351- DESTINATION ${mi_install_libdir }
373+ DESTINATION ${mi_install_objdir }
352374 RENAME ${mi_basename}${CMAKE_C_OUTPUT_EXTENSION} )
353375endif ()
354376
@@ -357,21 +379,17 @@ endif()
357379# -----------------------------------------------------------------------------
358380
359381if (MI_BUILD_TESTS)
360- add_executable (mimalloc-test -api test /test -api.c)
361- target_compile_definitions (mimalloc-test -api PRIVATE ${mi_defines} )
362- target_compile_options (mimalloc-test -api PRIVATE ${mi_cflags} )
363- target_include_directories (mimalloc-test -api PRIVATE include )
364- target_link_libraries (mimalloc-test -api PRIVATE mimalloc-static ${mi_libraries} )
365-
366- add_executable (mimalloc-test -stress test /test -stress.c)
367- target_compile_definitions (mimalloc-test -stress PRIVATE ${mi_defines} )
368- target_compile_options (mimalloc-test -stress PRIVATE ${mi_cflags} )
369- target_include_directories (mimalloc-test -stress PRIVATE include )
370- target_link_libraries (mimalloc-test -stress PRIVATE mimalloc ${mi_libraries} )
371-
372382 enable_testing ()
373- add_test (test_api, mimalloc-test -api)
374- add_test (test_stress, mimalloc-test -stress)
383+
384+ foreach (TEST_NAME api api-fill stress)
385+ add_executable (mimalloc-test -${TEST_NAME} test /test -${TEST_NAME} .c)
386+ target_compile_definitions (mimalloc-test -${TEST_NAME} PRIVATE ${mi_defines} )
387+ target_compile_options (mimalloc-test -${TEST_NAME} PRIVATE ${mi_cflags} )
388+ target_include_directories (mimalloc-test -${TEST_NAME} PRIVATE include )
389+ target_link_libraries (mimalloc-test -${TEST_NAME} PRIVATE mimalloc ${mi_libraries} )
390+
391+ add_test (NAME test -${TEST_NAME} COMMAND mimalloc-test -${TEST_NAME} )
392+ endforeach ()
375393endif ()
376394
377395# -----------------------------------------------------------------------------
0 commit comments