@@ -7,31 +7,36 @@ set(CMAKE_CXX_STANDARD 17)
77option (MI_SECURE "Use full security mitigations (like guard pages, allocation randomization, double-free mitigation, and free-list corruption detection)" OFF )
88option (MI_DEBUG_FULL "Use full internal heap invariant checking in DEBUG mode (expensive)" OFF )
99option (MI_PADDING "Enable padding to detect heap block overflow (always on in DEBUG or SECURE mode, or with Valgrind/ASAN)" OFF )
10- option (MI_OVERRIDE "Override the standard malloc interface (e.g. define entry points for malloc() etc)" ON )
10+ option (MI_OVERRIDE "Override the standard malloc interface (i.e. define entry points for ' malloc', 'free', etc)" ON )
1111option (MI_XMALLOC "Enable abort() call on memory allocation failure by default" OFF )
1212option (MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF )
1313option (MI_TRACK_VALGRIND "Compile with Valgrind support (adds a small overhead)" OFF )
1414option (MI_TRACK_ASAN "Compile with address sanitizer support (adds a small overhead)" OFF )
1515option (MI_TRACK_ETW "Compile with Windows event tracing (ETW) support (adds a small overhead)" OFF )
1616option (MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF )
17+ option (MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for arm64: '-march=armv8.1-a' (2016))" ON )
1718option (MI_SEE_ASM "Generate assembly files" OFF )
1819option (MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON )
1920option (MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON )
2021option (MI_WIN_REDIRECT "Use redirection module ('mimalloc-redirect') on Windows if compiling mimalloc as a DLL" ON )
21- option (MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF )
22+ option (MI_WIN_USE_FIXED_TLS "Use a fixed TLS slot on Windows to avoid extra tests in the malloc fast path" OFF )
23+ option (MI_LOCAL_DYNAMIC_TLS "Use local-dynamic-tls, a slightly slower but dlopen-compatible thread local storage mechanism (Unix)" OFF )
2224option (MI_LIBC_MUSL "Set this when linking with musl libc" OFF )
2325option (MI_BUILD_SHARED "Build shared library" ON )
2426option (MI_BUILD_STATIC "Build static library" ON )
2527option (MI_BUILD_OBJECT "Build object library" ON )
2628option (MI_BUILD_TESTS "Build test executables" ON )
2729option (MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF )
2830option (MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF )
31+ option (MI_GUARDED "Build with guard pages behind certain object allocations (implies MI_NO_PADDING=ON)" OFF )
2932option (MI_SKIP_COLLECT_ON_EXIT "Skip collecting memory on program exit" OFF )
3033option (MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OFF )
3134option (MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF )
3235option (MI_NO_THP "Disable transparent huge pages support on Linux/Android for the mimalloc process only" OFF )
36+ option (MI_EXTRA_CPPDEFS "Extra pre-processor definitions (use as `-DMI_EXTRA_CPPDEFS=\" opt1=val1;opt2=val2\" `)" "" )
3337
3438# deprecated options
39+ option (MI_WIN_USE_FLS "Use Fiber local storage on Windows to detect thread termination (deprecated)" OFF )
3540option (MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF )
3641option (MI_USE_LIBATOMIC "Explicitly link with -latomic (on older systems) (deprecated and detected automatically)" OFF )
3742
@@ -61,30 +66,39 @@ set(mi_sources
6166set (mi_cflags "" )
6267set (mi_cflags_static "" ) # extra flags for a static library build
6368set (mi_cflags_dynamic "" ) # extra flags for a shared-object library build
64- set (mi_defines "" )
6569set (mi_libraries "" )
6670
71+ if (MI_EXTRA_CPPDEFS)
72+ set (mi_defines ${MI_EXTRA_CPPDEFS} )
73+ else ()
74+ set (mi_defines "" )
75+ endif ()
76+
6777# -----------------------------------------------------------------------------
68- # Convenience: set default build type depending on the build directory
78+ # Convenience: set default build type and compiler depending on the build directory
6979# -----------------------------------------------------------------------------
7080
7181message (STATUS "" )
7282if (NOT CMAKE_BUILD_TYPE )
73- if ("${CMAKE_BINARY_DIR} " MATCHES ".*(D|d)ebug$" OR MI_DEBUG_FULL)
74- message (STATUS "No build type selected, default to: Debug" )
83+ if ("${CMAKE_BINARY_DIR} " MATCHES ".*(( D|d)ebug|asan|tsan|ubsan|valgrind) $" OR MI_DEBUG_FULL)
84+ message (STATUS "No build type selected, default to ' Debug' " )
7585 set (CMAKE_BUILD_TYPE "Debug" )
7686 else ()
77- message (STATUS "No build type selected, default to: Release" )
87+ message (STATUS "No build type selected, default to ' Release' " )
7888 set (CMAKE_BUILD_TYPE "Release" )
7989 endif ()
8090endif ()
8191
92+ if (CMAKE_GENERATOR MATCHES "^Visual Studio.*$" )
93+ message (STATUS "Note: when building with Visual Studio the build type is specified when building." )
94+ message (STATUS "For example: 'cmake --build . --config=Release" )
95+ endif ()
96+
8297if ("${CMAKE_BINARY_DIR} " MATCHES ".*(S|s)ecure$" )
8398 message (STATUS "Default to secure build" )
8499 set (MI_SECURE "ON" )
85100endif ()
86101
87-
88102# -----------------------------------------------------------------------------
89103# Process options
90104# -----------------------------------------------------------------------------
@@ -104,6 +118,14 @@ if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel")
104118 set (MI_USE_CXX "ON" )
105119endif ()
106120
121+ if (CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo" )
122+ if (NOT MI_OPT_ARCH)
123+ message (STATUS "Architecture specific optimizations are disabled (MI_OPT_ARCH=OFF)" )
124+ endif ()
125+ else ()
126+ set (MI_OPT_ARCH OFF )
127+ endif ()
128+
107129if (MI_OVERRIDE)
108130 message (STATUS "Override standard malloc (MI_OVERRIDE=ON)" )
109131 if (APPLE )
@@ -131,12 +153,6 @@ if(MI_OVERRIDE)
131153endif ()
132154
133155if (WIN32 )
134- if (MI_WIN_REDIRECT)
135- if (MSVC_C_ARCHITECTURE_ID MATCHES "ARM" )
136- message (STATUS "Cannot use redirection on Windows ARM (MI_WIN_REDIRECT=OFF)" )
137- set (MI_WIN_REDIRECT OFF )
138- endif ()
139- endif ()
140156 if (NOT MI_WIN_REDIRECT)
141157 # use a negative define for backward compatibility
142158 list (APPEND mi_defines MI_WIN_NOREDIRECT=1)
@@ -152,8 +168,8 @@ if(MI_TRACK_VALGRIND)
152168 CHECK_INCLUDE_FILES("valgrind/valgrind.h;valgrind/memcheck.h" MI_HAS_VALGRINDH)
153169 if (NOT MI_HAS_VALGRINDH)
154170 set (MI_TRACK_VALGRIND OFF )
155- message (WARNING "Cannot find the 'valgrind/valgrind.h' and 'valgrind/memcheck.h' -- install valgrind first" )
156- message (STATUS "Compile **without** Valgrind support (MI_TRACK_VALGRIND=OFF)" )
171+ message (WARNING "Cannot find the 'valgrind/valgrind.h' and 'valgrind/memcheck.h' -- install valgrind first? " )
172+ message (STATUS "Disabling Valgrind support (MI_TRACK_VALGRIND=OFF)" )
157173 else ()
158174 message (STATUS "Compile with Valgrind support (MI_TRACK_VALGRIND=ON)" )
159175 list (APPEND mi_defines MI_TRACK_VALGRIND=1)
@@ -199,6 +215,15 @@ if(MI_TRACK_ETW)
199215 endif ()
200216endif ()
201217
218+ if (MI_GUARDED)
219+ message (STATUS "Compile guard pages behind certain object allocations (MI_GUARDED=ON)" )
220+ list (APPEND mi_defines MI_GUARDED=1)
221+ if (NOT MI_NO_PADDING)
222+ message (STATUS " Disabling padding due to guard pages (MI_NO_PADDING=ON)" )
223+ set (MI_NO_PADDING ON )
224+ endif ()
225+ endif ()
226+
202227if (MI_SEE_ASM)
203228 message (STATUS "Generate assembly listings (MI_SEE_ASM=ON)" )
204229 list (APPEND mi_cflags -save-temps)
@@ -258,6 +283,7 @@ if(MI_DEBUG_UBSAN)
258283 if (CMAKE_BUILD_TYPE MATCHES "Debug" )
259284 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
260285 message (STATUS "Build with undefined-behavior sanitizer (MI_DEBUG_UBSAN=ON)" )
286+ list (APPEND mi_defines MI_UBSAN=1)
261287 list (APPEND mi_cflags -fsanitize=undefined -g -fno-sanitize-recover=undefined)
262288 list (APPEND mi_libraries -fsanitize=undefined)
263289 if (NOT MI_USE_CXX)
@@ -296,6 +322,48 @@ if(MI_LIBC_MUSL)
296322 list (APPEND mi_defines MI_LIBC_MUSL=1)
297323endif ()
298324
325+ if (MI_WIN_USE_FLS)
326+ message (STATUS "Use the Fiber API to detect thread termination (deprecated) (MI_WIN_USE_FLS=ON)" )
327+ list (APPEND mi_defines MI_WIN_USE_FLS=1)
328+ endif ()
329+
330+ if (MI_WIN_USE_FIXED_TLS)
331+ message (STATUS "Use fixed TLS slot on Windows to avoid extra tests in the malloc fast path (MI_WIN_USE_FIXED_TLS=ON)" )
332+ list (APPEND mi_defines MI_WIN_USE_FIXED_TLS=1)
333+ endif ()
334+
335+ # Determine architecture
336+ set (MI_OPT_ARCH_FLAGS "" )
337+ set (MI_ARCH "unknown" )
338+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86|i[3456]86)$" OR CMAKE_GENERATOR_PLATFORM MATCHES "^(x86|Win32)$" )
339+ set (MI_ARCH "x86" )
340+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|x64|amd64|AMD64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x64" ) # must be before arm64
341+ set (MI_ARCH "x64" )
342+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv8.?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" )
343+ set (MI_ARCH "arm64" )
344+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567]|ARM)$" )
345+ set (MI_ARCH "arm32" )
346+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$" )
347+ if (CMAKE_SIZEOF_VOID_P ==4)
348+ set (MI_ARCH "riscv32" )
349+ else ()
350+ set (MI_ARCH "riscv64" )
351+ endif ()
352+ else ()
353+ set (MI_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
354+ endif ()
355+ message (STATUS "Architecture: ${MI_ARCH} " ) # (${CMAKE_SYSTEM_PROCESSOR}, ${CMAKE_GENERATOR_PLATFORM}, ${CMAKE_GENERATOR})")
356+
357+ # Check /proc/cpuinfo for an SV39 MMU and limit the virtual address bits.
358+ # (this will skip the aligned hinting in that case. Issue #939, #949)
359+ if (EXISTS /proc/cpuinfo)
360+ file (STRINGS /proc/cpuinfo mi_sv39_mmu REGEX "^mmu[ \t ]+:[ \t ]+sv39$" )
361+ if (mi_sv39_mmu)
362+ MESSAGE ( STATUS "Set virtual address bits to 39 (SV39 MMU detected)" )
363+ list (APPEND mi_defines MI_DEFAULT_VIRTUAL_ADDRESS_BITS=39)
364+ endif ()
365+ endif ()
366+
299367# On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788
300368# if(CMAKE_SYSTEM_NAME MATCHES "Haiku")
301369# SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib)
@@ -327,36 +395,51 @@ if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM
327395 list (APPEND mi_cflags_dynamic -ftls-model=initial-exec)
328396 message (STATUS "Use local dynamic TLS for the static build (since MI_LIBC_MUSL=ON)" )
329397 else ()
330- list (APPEND mi_cflags -ftls-model=initial-exec)
398+ list (APPEND mi_cflags -ftls-model=initial-exec)
331399 endif ()
332400 endif ()
333401 if (MI_OVERRIDE)
334402 list (APPEND mi_cflags -fno-builtin-malloc)
335403 endif ()
404+ if (MI_OPT_ARCH)
405+ if (MI_ARCH STREQUAL "arm64" )
406+ set (MI_OPT_ARCH_FLAGS "-march=armv8.1-a" ) # fast atomics
407+ endif ()
408+ endif ()
336409endif ()
337410
338411if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
339412 list (APPEND mi_cflags /Zc:__cplusplus)
413+ if (MI_OPT_ARCH)
414+ if (MI_ARCH STREQUAL "arm64" )
415+ set (MI_OPT_ARCH_FLAGS "/arch:armv8.1" ) # fast atomics
416+ endif ()
417+ endif ()
340418endif ()
341419
342420if (MINGW)
343- add_definitions (-D_WIN32_WINNT=0x600)
421+ add_definitions (-D_WIN32_WINNT=0x601) # issue #976
422+ endif ()
423+
424+ if (MI_OPT_ARCH_FLAGS)
425+ list (APPEND mi_cflags ${MI_OPT_ARCH_FLAGS} )
426+ message (STATUS "Architecture specific optimization is enabled (with ${MI_OPT_ARCH_FLAGS} ) (MI_OPT_ARCH=ON)" )
344427endif ()
345428
346429# extra needed libraries
347430
348- # we prefer -l<lib> test over `find_library` as sometimes core libraries
431+ # we prefer -l<lib> test over `find_library` as sometimes core libraries
349432# like `libatomic` are not on the system path (see issue #898)
350- function (find_link_library libname outlibname)
351- check_linker_flag(C "-l${libname} " mi_has_lib${libname} )
433+ function (find_link_library libname outlibname)
434+ check_linker_flag(C "-l${libname} " mi_has_lib${libname} )
352435 if (mi_has_lib${libname} )
353436 message (VERBOSE "link library: -l${libname} " )
354- set (${outlibname} ${libname} PARENT_SCOPE)
437+ set (${outlibname} ${libname} PARENT_SCOPE)
355438 else ()
356439 find_library (MI_LIBPATH libname)
357440 if (MI_LIBPATH)
358441 message (VERBOSE "link library ${libname} at ${MI_LIBPATH} " )
359- set (${outlibname} ${MI_LIBPATH} PARENT_SCOPE)
442+ set (${outlibname} ${MI_LIBPATH} PARENT_SCOPE)
360443 else ()
361444 message (VERBOSE "link library not found: ${libname} " )
362445 set (${outlibname} "" PARENT_SCOPE)
@@ -365,19 +448,19 @@ function(find_link_library libname outlibname)
365448endfunction ()
366449
367450if (WIN32 )
368- list (APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
451+ list (APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
369452else ()
370453 find_link_library("pthread" MI_LIB_PTHREAD)
371- if (MI_LIB_PTHREAD)
454+ if (MI_LIB_PTHREAD)
372455 list (APPEND mi_libraries "${MI_LIB_PTHREAD} " )
373456 endif ()
374457 find_link_library("rt" MI_LIB_RT)
375- if (MI_LIB_RT)
458+ if (MI_LIB_RT)
376459 list (APPEND mi_libraries "${MI_LIB_RT} " )
377460 endif ()
378461 find_link_library("atomic" MI_LIB_ATOMIC)
379- if (MI_LIB_ATOMIC)
380- list (APPEND mi_libraries "${MI_LIB_ATOMIC} " )
462+ if (MI_LIB_ATOMIC)
463+ list (APPEND mi_libraries "${MI_LIB_ATOMIC} " )
381464 endif ()
382465endif ()
383466
@@ -431,7 +514,7 @@ endif()
431514
432515message (STATUS "" )
433516message (STATUS "Library base name: ${mi_basename} " )
434- message (STATUS "Version : ${mi_version} " )
517+ message (STATUS "Version : ${mi_version} . ${mi_version_patch} " )
435518message (STATUS "Build type : ${CMAKE_BUILD_TYPE_LC} " )
436519if (MI_USE_CXX)
437520 message (STATUS "C++ Compiler : ${CMAKE_CXX_COMPILER} " )
@@ -461,10 +544,18 @@ if(MI_BUILD_SHARED)
461544 )
462545 if (WIN32 AND MI_WIN_REDIRECT)
463546 # On windows, link and copy the mimalloc redirection dll too.
464- if (CMAKE_SIZEOF_VOID_P EQUAL 4)
547+ if (CMAKE_GENERATOR_PLATFORM STREQUAL "arm64ec" )
548+ set (MIMALLOC_REDIRECT_SUFFIX "-arm64ec" )
549+ elseif (MI_ARCH STREQUAL "x64" )
550+ set (MIMALLOC_REDIRECT_SUFFIX "" )
551+ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" )
552+ message (STATUS "Note: x64 code emulated on Windows for arm64 should use an arm64ec build of 'mimalloc-override.dll'" )
553+ message (STATUS " with 'mimalloc-redirect-arm64ec.dll'. See the 'bin\\ readme.md' for more information." )
554+ endif ()
555+ elseif (MI_ARCH STREQUAL "x86" )
465556 set (MIMALLOC_REDIRECT_SUFFIX "32" )
466557 else ()
467- set (MIMALLOC_REDIRECT_SUFFIX "" )
558+ set (MIMALLOC_REDIRECT_SUFFIX "- ${MI_ARCH} " ) # -arm64 etc.
468559 endif ()
469560
470561 target_link_libraries (mimalloc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} /bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX} .lib)
0 commit comments