@@ -12,8 +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_GUARDED "Build with guard pages behind certain object allocations (implies MI_NO_PADDING=ON)" OFF )
1414option (MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF )
15-
16- option (MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for x64: '-march=haswell;-mavx2' (2013), for arm64: '-march=armv8.1-a' (2016))" ON )
15+ option (MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for x64: '-march=haswell;-mavx2' (2013), for arm64: '-march=armv8.1-a' (2016))" OFF )
1716option (MI_OPT_SIMD "Use SIMD instructions (requires MI_OPT_ARCH to be enabled)" OFF )
1817option (MI_SEE_ASM "Generate assembly files" OFF )
1918option (MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON )
@@ -83,6 +82,19 @@ else()
8382 set (mi_defines "" )
8483endif ()
8584
85+ # pass git revision as a define
86+ if (EXISTS "${CMAKE_SOURCE_DIR} /.git/index" )
87+ find_package (Git)
88+ if (GIT_FOUND)
89+ execute_process (COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
90+ if (mi_git_res EQUAL "0" )
91+ list (APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe} " )
92+ # add to dependencies so we rebuild if the git head commit changes
93+ set_property (GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR} /.git/index" )
94+ endif ()
95+ endif ()
96+ endif ()
97+
8698# -----------------------------------------------------------------------------
8799# Convenience: set default build type and compiler depending on the build directory
88100# -----------------------------------------------------------------------------
@@ -108,9 +120,44 @@ if("${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$")
108120 set (MI_SECURE "ON" )
109121endif ()
110122
123+
124+ # Determine architecture
125+ set (MI_OPT_ARCH_FLAGS "" )
126+ set (MI_ARCH "unknown" )
127+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86|i[3456]86)$" OR CMAKE_GENERATOR_PLATFORM MATCHES "^(x86|Win32)$" )
128+ set (MI_ARCH "x86" )
129+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|x64|amd64|AMD64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR "x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES) # must be before arm64
130+ set (MI_ARCH "x64" )
131+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv[89].?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
132+ set (MI_ARCH "arm64" )
133+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567].?|ARM)$" )
134+ set (MI_ARCH "arm32" )
135+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$" )
136+ if (CMAKE_SIZEOF_VOID_P ==4)
137+ set (MI_ARCH "riscv32" )
138+ else ()
139+ set (MI_ARCH "riscv64" )
140+ endif ()
141+ else ()
142+ set (MI_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
143+ endif ()
144+ message (STATUS "Architecture: ${MI_ARCH} " ) # (${CMAKE_SYSTEM_PROCESSOR}, ${CMAKE_GENERATOR_PLATFORM}, ${CMAKE_GENERATOR})")
145+
146+ # negative overrides (mainly to support vcpkg features)
147+ if (MI_NO_USE_CXX)
148+ set (MI_USE_CXX "OFF" )
149+ endif ()
150+ if (MI_NO_OPT_ARCH)
151+ set (MI_OPT_ARCH "OFF" )
152+ elseif (MI_ARCH STREQUAL "arm64" )
153+ set (MI_OPT_ARCH "ON" ) # enable armv8.1-a by default on arm64 unless MI_NO_OPT_ARCH is set
154+ endif ()
155+
156+
111157# -----------------------------------------------------------------------------
112158# Process options
113159# -----------------------------------------------------------------------------
160+
114161if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC" )
115162 set (MI_CLANG_CL "ON" )
116163endif ()
@@ -130,27 +177,11 @@ if(CMAKE_C_COMPILER_ID MATCHES "Intel")
130177 list (APPEND mi_cflags -Wall)
131178endif ()
132179
133- # negative overrides (mainly to support vcpkg features)
134- if (MI_NO_USE_CXX)
135- set (MI_USE_CXX "OFF" )
136- endif ()
137- if (MI_NO_OPT_ARCH)
138- set (MI_OPT_ARCH "OFF" )
139- endif ()
140-
141-
142- if (CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" )
180+ # force C++ compilation with msvc or clang-cl to use modern C++ atomics
181+ if (CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR MI_CLANG_CL)
143182 set (MI_USE_CXX "ON" )
144183endif ()
145184
146- if (CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo" )
147- if (NOT MI_OPT_ARCH)
148- message (STATUS "Architecture specific optimizations are disabled (MI_OPT_ARCH=OFF)" )
149- endif ()
150- #else()
151- # set(MI_OPT_ARCH OFF)
152- endif ()
153-
154185if (MI_OVERRIDE)
155186 message (STATUS "Override standard malloc (MI_OVERRIDE=ON)" )
156187 if (APPLE )
@@ -357,28 +388,6 @@ if(MI_WIN_USE_FIXED_TLS)
357388 list (APPEND mi_defines MI_WIN_USE_FIXED_TLS=1)
358389endif ()
359390
360- # Determine architecture
361- set (MI_OPT_ARCH_FLAGS "" )
362- set (MI_ARCH "unknown" )
363- if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86|i[3456]86)$" OR CMAKE_GENERATOR_PLATFORM MATCHES "^(x86|Win32)$" )
364- set (MI_ARCH "x86" )
365- elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|x64|amd64|AMD64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x64" ) # must be before arm64
366- set (MI_ARCH "x64" )
367- elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv8.?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" )
368- set (MI_ARCH "arm64" )
369- elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567]|ARM)$" )
370- set (MI_ARCH "arm32" )
371- elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$" )
372- if (CMAKE_SIZEOF_VOID_P ==4)
373- set (MI_ARCH "riscv32" )
374- else ()
375- set (MI_ARCH "riscv64" )
376- endif ()
377- else ()
378- set (MI_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
379- endif ()
380- message (STATUS "Architecture: ${MI_ARCH} " ) # (${CMAKE_SYSTEM_PROCESSOR}, ${CMAKE_GENERATOR_PLATFORM}, ${CMAKE_GENERATOR})")
381-
382391# Check /proc/cpuinfo for an SV39 MMU and limit the virtual address bits.
383392# (this will skip the aligned hinting in that case. Issue #939, #949)
384393if (EXISTS /proc/cpuinfo)
@@ -434,18 +443,17 @@ endif()
434443# Compiler and architecture specific flags
435444if (CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku" )
436445 if (MI_OPT_ARCH)
437- if (APPLE AND CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_OSX_ARCHITECTURES) # to support multi-arch binaries (#999)
438- set (MI_OPT_ARCH_FLAGS "" )
446+ if (APPLE AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" AND CMAKE_OSX_ARCHITECTURES) # to support multi-arch binaries (#999)
439447 if ("arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
440- list (APPEND MI_OPT_ARCH_FLAGS "-Xarch_arm64;-march=armv8.1-a;-mtune=native " )
448+ list (APPEND MI_OPT_ARCH_FLAGS "-Xarch_arm64;-march=armv8.1-a" )
441449 endif ()
442450 if ("x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES)
443451 list (APPEND MI_OPT_ARCH_FLAGS "-Xarch_x86_64;-march=haswell;-Xarch_x86_64;-mavx2" )
444452 endif ()
445453 elseif (MI_ARCH STREQUAL "x64" )
446- set (MI_OPT_ARCH_FLAGS "-march=haswell;-mavx2;-mtune=native " ) # fast bit scan (since 2013)
454+ set (MI_OPT_ARCH_FLAGS "-march=haswell;-mavx2" ) # fast bit scan (since 2013)
447455 elseif (MI_ARCH STREQUAL "arm64" )
448- set (MI_OPT_ARCH_FLAGS "-march=armv8.1-a;-mtune=native " ) # fast atomics (since 2016)
456+ set (MI_OPT_ARCH_FLAGS "-march=armv8.1-a" ) # fast atomics (since 2016)
449457 endif ()
450458 endif ()
451459endif ()
@@ -462,7 +470,7 @@ if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914) # vs2017+
462470endif ()
463471
464472if (MINGW)
465- add_definitions (-D_WIN32_WINNT=0x601 ) # issue #976
473+ add_definitions (-D_WIN32_WINNT=0x600 ) # issue #976
466474endif ()
467475
468476if (MI_OPT_ARCH_FLAGS)
@@ -514,6 +522,7 @@ else()
514522 endif ()
515523endif ()
516524
525+
517526# -----------------------------------------------------------------------------
518527# Install and output names
519528# -----------------------------------------------------------------------------
@@ -544,7 +553,10 @@ if(MI_TRACK_ASAN)
544553 set (mi_libname "${mi_libname} -asan" )
545554endif ()
546555string (TOLOWER "${CMAKE_BUILD_TYPE} " CMAKE_BUILD_TYPE_LC)
547- if (NOT (CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$" ))
556+ list (APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_LC} " ) #todo: multi-config project needs $<CONFIG> ?
557+ if (CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$" )
558+ list (APPEND mi_defines MI_BUILD_RELEASE)
559+ else ()
548560 set (mi_libname "${mi_libname} -${CMAKE_BUILD_TYPE_LC} " ) #append build type (e.g. -debug) if not a release version
549561endif ()
550562
@@ -594,7 +606,7 @@ if(MI_BUILD_SHARED)
594606 install (TARGETS mimalloc EXPORT mimalloc ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} )
595607 install (EXPORT mimalloc DESTINATION ${mi_install_cmakedir} )
596608
597- if (WIN32 )
609+ if (WIN32 AND NOT MINGW )
598610 # On windows, the import library name for the dll would clash with the static mimalloc.lib library
599611 # so we postfix the dll import library with `.dll.lib` (and also the .pdb debug file)
600612 set_property (TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname} .dll" )
@@ -604,6 +616,9 @@ if(MI_BUILD_SHARED)
604616 # install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.pdb" DESTINATION ${CMAKE_INSTALL_LIBDIR})
605617 endif ()
606618 if (WIN32 AND MI_WIN_REDIRECT)
619+ if (MINGW)
620+ set_property (TARGET mimalloc PROPERTY PREFIX "" )
621+ endif ()
607622 # On windows, link and copy the mimalloc redirection dll too.
608623 if (CMAKE_GENERATOR_PLATFORM STREQUAL "arm64ec" )
609624 set (MIMALLOC_REDIRECT_SUFFIX "-arm64ec" )
@@ -719,10 +734,12 @@ if (MI_BUILD_TESTS)
719734 target_compile_definitions (mimalloc-test -${TEST_NAME} PRIVATE ${mi_defines} )
720735 target_compile_options (mimalloc-test -${TEST_NAME} PRIVATE ${mi_cflags} )
721736 target_include_directories (mimalloc-test -${TEST_NAME} PRIVATE include )
722- if (MI_BUILD_SHARED AND (MI_TRACK_ASAN OR MI_DEBUG_TSAN OR MI_DEBUG_UBSAN))
737+ if (MI_BUILD_STATIC AND NOT MI_DEBUG_TSAN)
738+ target_link_libraries (mimalloc-test -${TEST_NAME} PRIVATE mimalloc-static ${mi_libraries} )
739+ elseif (MI_BUILD_SHARED)
723740 target_link_libraries (mimalloc-test -${TEST_NAME} PRIVATE mimalloc ${mi_libraries} )
724741 else ()
725- target_link_libraries (mimalloc- test - ${TEST_NAME} PRIVATE mimalloc- static ${mi_libraries} )
742+ message ( STATUS "cannot build TSAN tests without MI_BUILD_SHARED being enabled" )
726743 endif ()
727744 add_test (NAME test -${TEST_NAME} COMMAND mimalloc-test -${TEST_NAME} )
728745 endforeach ()
@@ -731,21 +748,19 @@ if (MI_BUILD_TESTS)
731748 if (MI_BUILD_SHARED AND NOT (MI_TRACK_ASAN OR MI_DEBUG_TSAN OR MI_DEBUG_UBSAN))
732749 add_executable (mimalloc-test -stress-dynamic test /test -stress.c)
733750 target_compile_definitions (mimalloc-test -stress-dynamic PRIVATE ${mi_defines} "USE_STD_MALLOC=1" )
734- if (WIN32 )
735- target_compile_definitions (mimalloc-test -stress-dynamic PRIVATE "MI_LINK_VERSION=1" )
736- endif ()
737751 target_compile_options (mimalloc-test -stress-dynamic PRIVATE ${mi_cflags} )
738752 target_include_directories (mimalloc-test -stress-dynamic PRIVATE include )
739- target_link_libraries (mimalloc-test -stress-dynamic PRIVATE mimalloc ${mi_libraries} ) # mi_version
740753 if (WIN32 )
741- add_test (NAME test -stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SHOW_STATS=1 $<TARGET_FILE:mimalloc-test -stress-dynamic>)
754+ target_compile_definitions (mimalloc-test -stress-dynamic PRIVATE "MI_LINK_VERSION=1" ) # link mi_version
755+ target_link_libraries (mimalloc-test -stress-dynamic PRIVATE mimalloc ${mi_libraries} ) # link mi_version
756+ add_test (NAME test -stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_VERBOSE=1 $<TARGET_FILE:mimalloc-test -stress-dynamic>)
742757 else ()
743758 if (APPLE )
744759 set (LD_PRELOAD "DYLD_INSERT_LIBRARIES" )
745760 else ()
746761 set (LD_PRELOAD "LD_PRELOAD" )
747762 endif ()
748- add_test (NAME test -stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SHOW_STATS =1 ${LD_PRELOAD} =$<TARGET_FILE:mimalloc> $<TARGET_FILE:mimalloc-test -stress-dynamic>)
763+ add_test (NAME test -stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_VERBOSE =1 ${LD_PRELOAD} =$<TARGET_FILE:mimalloc> $<TARGET_FILE:mimalloc-test -stress-dynamic>)
749764 endif ()
750765 endif ()
751766endif ()
0 commit comments