2024-05-19 13:25:31 -07:00
cmake_minimum_required ( VERSION 3.18 )
2019-07-07 18:11:21 -07:00
project ( libmimalloc C CXX )
2019-11-01 22:01:52 -07:00
2019-06-19 16:52:36 -07:00
set ( CMAKE_C_STANDARD 11 )
2019-07-07 18:11:21 -07:00
set ( CMAKE_CXX_STANDARD 17 )
2019-06-19 16:52:36 -07:00
2019-11-21 15:21:23 -08:00
option ( MI_SECURE "Use full security mitigations (like guard pages, allocation randomization, double-free mitigation, and free-list corruption detection)" OFF )
2020-05-04 11:01:11 -07:00
option ( MI_DEBUG_FULL "Use full internal heap invariant checking in DEBUG mode (expensive)" OFF )
2023-03-05 11:01:51 -08:00
option ( MI_PADDING "Enable padding to detect heap block overflow (always on in DEBUG or SECURE mode, or with Valgrind/ASAN)" OFF )
2024-12-18 14:35:10 -08:00
option ( MI_OVERRIDE "Override the standard malloc interface (i.e. define entry points for 'malloc', 'free', etc)" ON )
2020-05-04 11:01:11 -07:00
option ( MI_XMALLOC "Enable abort() call on memory allocation failure by default" OFF )
option ( MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF )
2023-03-06 09:02:38 -08:00
option ( MI_TRACK_VALGRIND "Compile with Valgrind support (adds a small overhead)" OFF )
option ( MI_TRACK_ASAN "Compile with address sanitizer support (adds a small overhead)" OFF )
2023-03-16 20:08:43 -07:00
option ( MI_TRACK_ETW "Compile with Windows event tracing (ETW) support (adds a small overhead)" OFF )
2020-05-04 11:01:11 -07:00
option ( MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF )
2025-03-05 15:37:37 -08:00
option ( MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for arm64: '-march=armv8.1-a' (2016))" OFF )
2019-11-14 11:01:05 -08:00
option ( MI_SEE_ASM "Generate assembly files" OFF )
2021-11-02 21:54:44 -07:00
option ( MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON )
2022-12-03 00:23:43 +01:00
option ( MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON )
2022-10-31 15:26:21 -07:00
option ( MI_WIN_REDIRECT "Use redirection module ('mimalloc-redirect') on Windows if compiling mimalloc as a DLL" ON )
2024-12-26 09:52:46 -08:00
option ( MI_WIN_USE_FIXED_TLS "Use a fixed TLS slot on Windows to avoid extra tests in the malloc fast path" OFF )
2024-12-18 14:41:20 -08:00
option ( MI_LOCAL_DYNAMIC_TLS "Use local-dynamic-tls, a slightly slower but dlopen-compatible thread local storage mechanism (Unix)" OFF )
2024-04-20 16:19:59 -07:00
option ( MI_LIBC_MUSL "Set this when linking with musl libc" OFF )
2020-04-28 18:12:43 +03:00
option ( MI_BUILD_SHARED "Build shared library" ON )
option ( MI_BUILD_STATIC "Build static library" ON )
2020-05-04 11:01:11 -07:00
option ( MI_BUILD_OBJECT "Build object library" ON )
option ( MI_BUILD_TESTS "Build test executables" ON )
2020-07-21 09:10:45 -07:00
option ( MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF )
2020-07-25 19:55:36 -07:00
option ( MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF )
2024-11-16 22:42:32 -08:00
option ( MI_GUARDED "Build with guard pages behind certain object allocations (implies MI_NO_PADDING=ON)" OFF )
2023-03-29 16:17:10 -07:00
option ( MI_SKIP_COLLECT_ON_EXIT "Skip collecting memory on program exit" OFF )
2023-03-06 09:02:38 -08:00
option ( MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OFF )
2023-04-25 10:21:34 -07:00
option ( MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF )
2024-03-02 16:55:13 -08:00
option ( MI_NO_THP "Disable transparent huge pages support on Linux/Android for the mimalloc process only" OFF )
2024-10-21 05:02:24 -07:00
option ( MI_EXTRA_CPPDEFS "Extra pre-processor definitions (use as `-DMI_EXTRA_CPPDEFS=\" opt1=val1;opt2=val2\ "`)" "" )
2021-12-15 08:53:55 -08:00
2025-01-09 23:10:12 -08:00
# negated options for vcpkg features
option ( MI_NO_USE_CXX "Use plain C compilation (has priority over MI_USE_CXX)" OFF )
option ( MI_NO_OPT_ARCH "Do not use architecture specific optimizations (like '-march=armv8.1-a' for example) (has priority over MI_OPT_ARCH)" OFF )
2021-12-15 08:53:55 -08:00
# deprecated options
2024-12-26 09:52:46 -08:00
option ( MI_WIN_USE_FLS "Use Fiber local storage on Windows to detect thread termination (deprecated)" OFF )
2020-05-04 11:01:11 -07:00
option ( MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF )
2021-12-15 08:53:55 -08:00
option ( MI_USE_LIBATOMIC "Explicitly link with -latomic (on older systems) (deprecated and detected automatically)" OFF )
2019-06-19 16:52:36 -07:00
2024-05-19 13:25:31 -07:00
include ( CheckLinkerFlag ) # requires cmake 3.18
2022-10-30 12:49:29 -07:00
include ( CheckIncludeFiles )
2021-09-28 20:34:56 +03:00
include ( GNUInstallDirs )
2019-11-03 13:34:54 -08:00
include ( "cmake/mimalloc-config-version.cmake" )
2019-06-19 16:52:36 -07:00
set ( mi_sources
src/alloc.c
src/alloc-aligned.c
2019-07-07 13:44:33 -07:00
src/alloc-posix.c
2023-03-20 10:49:56 -07:00
src/arena.c
src/bitmap.c
2019-06-19 16:52:36 -07:00
src/heap.c
2023-03-14 16:54:46 -07:00
src/init.c
2023-05-19 10:24:39 -07:00
src/libc.c
2023-03-20 10:49:56 -07:00
src/options.c
src/os.c
src/page.c
src/random.c
src/segment.c
2023-04-04 16:00:17 -07:00
src/segment-map.c
2023-03-20 10:49:56 -07:00
src/stats.c
2023-03-14 16:54:46 -07:00
src/prim/prim.c )
2019-06-19 16:52:36 -07:00
2023-03-04 09:58:17 -08:00
set ( mi_cflags "" )
2024-05-11 08:08:03 -07:00
set ( mi_cflags_static "" ) # extra flags for a static library build
set ( mi_cflags_dynamic "" ) # extra flags for a shared-object library build
2023-03-04 09:58:17 -08:00
set ( mi_libraries "" )
2024-10-21 05:10:09 -07:00
if ( MI_EXTRA_CPPDEFS )
set ( mi_defines ${ MI_EXTRA_CPPDEFS } )
else ()
set ( mi_defines "" )
endif ()
2021-11-04 18:55:12 -07:00
2025-03-01 14:29:23 -08:00
# pass git revision as a define
if ( EXISTS "${CMAKE_SOURCE_DIR}/.git/index" )
find_package ( Git )
if ( GIT_FOUND )
execute_process ( COMMAND ${ GIT_EXECUTABLE } "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE )
if ( mi_git_res EQUAL "0" )
list ( APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}" )
2025-03-01 14:51:27 -08:00
# add to dependencies so we rebuild if the git head commit changes
set_property ( GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index" )
2025-03-01 14:29:23 -08:00
endif ()
endif ()
endif ()
2019-11-14 11:01:05 -08:00
# -----------------------------------------------------------------------------
2024-12-10 20:45:59 -08:00
# Convenience: set default build type and compiler depending on the build directory
2019-11-14 11:01:05 -08:00
# -----------------------------------------------------------------------------
2022-12-03 00:23:43 +01:00
message ( STATUS "" )
2019-06-19 16:52:36 -07:00
if ( NOT CMAKE_BUILD_TYPE )
2024-12-10 20:45:59 -08:00
if ( "${CMAKE_BINARY_DIR}" MATCHES ".*((D|d)ebug|asan|tsan|ubsan|valgrind)$" OR MI_DEBUG_FULL )
message ( STATUS "No build type selected, default to 'Debug'" )
2019-06-19 16:52:36 -07:00
set ( CMAKE_BUILD_TYPE "Debug" )
else ()
2024-12-10 20:45:59 -08:00
message ( STATUS "No build type selected, default to 'Release'" )
2019-06-19 16:52:36 -07:00
set ( CMAKE_BUILD_TYPE "Release" )
endif ()
endif ()
2024-12-19 14:00:54 -08:00
if ( CMAKE_GENERATOR MATCHES "^Visual Studio.*$" )
message ( STATUS "Note: when building with Visual Studio the build type is specified when building." )
2024-12-26 09:52:46 -08:00
message ( STATUS "For example: 'cmake --build . --config=Release" )
2024-12-19 14:00:54 -08:00
endif ()
2019-06-19 16:52:36 -07:00
if ( "${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$" )
2019-11-14 11:01:05 -08:00
message ( STATUS "Default to secure build" )
2019-06-24 18:54:03 -07:00
set ( MI_SECURE "ON" )
2019-06-19 16:52:36 -07:00
endif ()
2025-03-05 15:37:37 -08:00
# Determine architecture
set ( MI_OPT_ARCH_FLAGS "" )
set ( MI_ARCH "unknown" )
if ( CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86|i[3456]86)$" OR CMAKE_GENERATOR_PLATFORM MATCHES "^(x86|Win32)$" )
set ( MI_ARCH "x86" )
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
set ( MI_ARCH "x64" )
elseif ( CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv[89].?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES )
set ( MI_ARCH "arm64" )
2025-06-09 19:05:30 -07:00
elseif ( CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567].?|ARM)$" )
2025-03-05 15:37:37 -08:00
set ( MI_ARCH "arm32" )
elseif ( CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$" )
if ( CMAKE_SIZEOF_VOID_P==4 )
set ( MI_ARCH "riscv32" )
else ()
set ( MI_ARCH "riscv64" )
endif ()
else ()
set ( MI_ARCH ${ CMAKE_SYSTEM_PROCESSOR } )
endif ()
message ( STATUS "Architecture: ${MI_ARCH}" ) # (${CMAKE_SYSTEM_PROCESSOR}, ${CMAKE_GENERATOR_PLATFORM}, ${CMAKE_GENERATOR})")
# negative overrides (mainly to support vcpkg features)
if ( MI_NO_USE_CXX )
set ( MI_USE_CXX "OFF" )
endif ()
if ( MI_NO_OPT_ARCH )
set ( MI_OPT_ARCH "OFF" )
elseif ( MI_ARCH STREQUAL "arm64" )
set ( MI_OPT_ARCH "ON" ) # enable armv8.1-a by default on arm64 unless MI_NO_OPT_ARCH is set
endif ()
2019-11-14 11:01:05 -08:00
# -----------------------------------------------------------------------------
# Process options
# -----------------------------------------------------------------------------
2025-03-05 15:37:37 -08:00
2025-01-06 10:27:46 -08:00
if ( CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC" )
set ( MI_CLANG_CL "ON" )
endif ()
2019-11-14 11:01:05 -08:00
2024-03-24 10:39:22 -07:00
# put -Wall early so other warnings can be disabled selectively
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" )
2025-01-06 10:27:46 -08:00
if ( MI_CLANG_CL )
list ( APPEND mi_cflags -W )
else ()
list ( APPEND mi_cflags -Wall -Wextra -Wpedantic )
endif ()
2024-03-24 10:39:22 -07:00
endif ()
if ( CMAKE_C_COMPILER_ID MATCHES "GNU" )
list ( APPEND mi_cflags -Wall -Wextra )
endif ()
if ( CMAKE_C_COMPILER_ID MATCHES "Intel" )
list ( APPEND mi_cflags -Wall )
endif ()
2025-04-02 10:50:36 -07:00
# force C++ compilation with msvc or clang-cl to use modern C++ atomics
if ( CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR MI_CLANG_CL )
2019-07-23 09:59:20 -07:00
set ( MI_USE_CXX "ON" )
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_OVERRIDE )
2019-06-24 18:54:03 -07:00
message ( STATUS "Override standard malloc (MI_OVERRIDE=ON)" )
2019-06-19 16:52:36 -07:00
if ( APPLE )
2021-01-22 19:49:15 -03:00
if ( MI_OSX_ZONE )
2020-02-08 20:08:52 -08:00
# use zone's on macOS
message ( STATUS " Use malloc zone to override malloc (MI_OSX_ZONE=ON)" )
2023-03-20 10:49:56 -07:00
list ( APPEND mi_sources src/prim/osx/alloc-override-zone.c )
2022-12-03 00:23:43 +01:00
list ( APPEND mi_defines MI_OSX_ZONE=1 )
2021-11-02 21:54:44 -07:00
if ( NOT MI_OSX_INTERPOSE )
message ( STATUS " WARNING: zone overriding usually also needs interpose (use -DMI_OSX_INTERPOSE=ON)" )
endif ()
2020-02-08 20:08:52 -08:00
endif ()
2021-10-20 09:35:58 -07:00
if ( MI_OSX_INTERPOSE )
2019-06-23 15:29:41 +08:00
# use interpose on macOS
2021-10-20 09:35:58 -07:00
message ( STATUS " Use interpose to override malloc (MI_OSX_INTERPOSE=ON)" )
2021-11-04 18:55:12 -07:00
list ( APPEND mi_defines MI_OSX_INTERPOSE=1 )
2021-11-02 21:54:44 -07:00
if ( NOT MI_OSX_ZONE )
message ( STATUS " WARNING: interpose usually also needs zone overriding (use -DMI_OSX_INTERPOSE=ON)" )
endif ()
2019-06-19 16:52:36 -07:00
endif ()
2022-04-07 16:12:16 -07:00
if ( MI_USE_CXX AND MI_OSX_INTERPOSE )
message ( STATUS " WARNING: if dynamically overriding malloc/free, it is more reliable to build mimalloc as C code (use -DMI_USE_CXX=OFF)" )
2021-11-04 18:55:12 -07:00
endif ()
2019-06-19 16:52:36 -07:00
endif ()
endif ()
2024-12-18 14:20:49 -08:00
if ( WIN32 )
2022-10-31 15:26:21 -07:00
if ( NOT MI_WIN_REDIRECT )
# use a negative define for backward compatibility
list ( APPEND mi_defines MI_WIN_NOREDIRECT=1 )
endif ()
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_SECURE )
2019-11-21 15:21:23 -08:00
message ( STATUS "Set full secure build (MI_SECURE=ON)" )
2024-03-02 11:50:57 -08:00
list ( APPEND mi_defines MI_SECURE=4 )
2019-06-19 16:52:36 -07:00
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_VALGRIND )
2022-10-30 12:49:29 -07:00
CHECK_INCLUDE_FILES ( "valgrind/valgrind.h;valgrind/memcheck.h" MI_HAS_VALGRINDH )
if ( NOT MI_HAS_VALGRINDH )
2023-03-06 09:02:38 -08:00
set ( MI_TRACK_VALGRIND OFF )
2024-12-18 14:41:20 -08:00
message ( WARNING "Cannot find the 'valgrind/valgrind.h' and 'valgrind/memcheck.h' -- install valgrind first?" )
message ( STATUS "Disabling Valgrind support (MI_TRACK_VALGRIND=OFF)" )
2022-10-30 12:49:29 -07:00
else ()
2023-03-06 09:02:38 -08:00
message ( STATUS "Compile with Valgrind support (MI_TRACK_VALGRIND=ON)" )
list ( APPEND mi_defines MI_TRACK_VALGRIND=1 )
2022-10-30 12:49:29 -07:00
endif ()
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_ASAN )
2023-03-04 09:58:17 -08:00
if ( APPLE AND MI_OVERRIDE )
2023-03-06 09:02:38 -08:00
set ( MI_TRACK_ASAN OFF )
message ( WARNING "Cannot enable address sanitizer support on macOS if MI_OVERRIDE is ON (MI_TRACK_ASAN=OFF)" )
2023-03-04 09:58:17 -08:00
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_VALGRIND )
set ( MI_TRACK_ASAN OFF )
message ( WARNING "Cannot enable address sanitizer support with also Valgrind support enabled (MI_TRACK_ASAN=OFF)" )
2023-03-04 09:58:17 -08:00
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_ASAN )
2022-11-28 10:55:19 -08:00
CHECK_INCLUDE_FILES ( "sanitizer/asan_interface.h" MI_HAS_ASANH )
if ( NOT MI_HAS_ASANH )
2023-03-06 09:02:38 -08:00
set ( MI_TRACK_ASAN OFF )
2022-11-28 10:55:19 -08:00
message ( WARNING "Cannot find the 'sanitizer/asan_interface.h' -- install address sanitizer support first" )
2023-03-06 09:02:38 -08:00
message ( STATUS "Compile **without** address sanitizer support (MI_TRACK_ASAN=OFF)" )
2022-11-28 10:55:19 -08:00
else ()
2023-03-06 09:02:38 -08:00
message ( STATUS "Compile with address sanitizer support (MI_TRACK_ASAN=ON)" )
list ( APPEND mi_defines MI_TRACK_ASAN=1 )
2022-11-28 10:55:19 -08:00
list ( APPEND mi_cflags -fsanitize=address )
2023-03-04 09:58:17 -08:00
list ( APPEND mi_libraries -fsanitize=address )
2022-11-28 10:55:19 -08:00
endif ()
endif ()
endif ()
2023-03-16 20:08:43 -07:00
if ( MI_TRACK_ETW )
2023-03-16 20:13:21 -07:00
if ( NOT WIN32 )
2023-03-16 20:08:43 -07:00
set ( MI_TRACK_ETW OFF )
message ( WARNING "Can only enable ETW support on Windows (MI_TRACK_ETW=OFF)" )
endif ()
if ( MI_TRACK_VALGRIND OR MI_TRACK_ASAN )
set ( MI_TRACK_ETW OFF )
message ( WARNING "Cannot enable ETW support with also Valgrind or ASAN support enabled (MI_TRACK_ETW=OFF)" )
endif ()
if ( MI_TRACK_ETW )
message ( STATUS "Compile with Windows event tracing support (MI_TRACK_ETW=ON)" )
list ( APPEND mi_defines MI_TRACK_ETW=1 )
endif ()
endif ()
2024-11-16 22:42:32 -08:00
if ( MI_GUARDED )
message ( STATUS "Compile guard pages behind certain object allocations (MI_GUARDED=ON)" )
list ( APPEND mi_defines MI_GUARDED=1 )
2024-07-16 09:15:59 -07:00
if ( NOT MI_NO_PADDING )
message ( STATUS " Disabling padding due to guard pages (MI_NO_PADDING=ON)" )
set ( MI_NO_PADDING ON )
endif ()
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_SEE_ASM )
2019-06-24 18:54:03 -07:00
message ( STATUS "Generate assembly listings (MI_SEE_ASM=ON)" )
2019-06-19 16:52:36 -07:00
list ( APPEND mi_cflags -save-temps )
2024-03-24 10:39:22 -07:00
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" )
message ( STATUS "No GNU Line marker" )
list ( APPEND mi_cflags -Wno-gnu-line-marker )
endif ()
2019-06-19 16:52:36 -07:00
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_CHECK_FULL )
2019-11-14 11:01:05 -08:00
message ( STATUS "The MI_CHECK_FULL option is deprecated, use MI_DEBUG_FULL instead" )
set ( MI_DEBUG_FULL "ON" )
endif ()
2022-01-18 21:11:12 -08:00
if ( MI_SKIP_COLLECT_ON_EXIT )
2022-02-03 15:56:53 -08:00
message ( STATUS "Skip collecting memory on program exit (MI_SKIP_COLLECT_ON_EXIT=ON)" )
2022-01-18 21:11:12 -08:00
list ( APPEND mi_defines MI_SKIP_COLLECT_ON_EXIT=1 )
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_DEBUG_FULL )
2019-11-21 15:21:23 -08:00
message ( STATUS "Set debug level to full internal invariant checking (MI_DEBUG_FULL=ON)" )
2019-06-19 16:52:36 -07:00
list ( APPEND mi_defines MI_DEBUG=3 ) # full invariant checking
endif ()
2023-03-05 11:01:51 -08:00
if ( MI_NO_PADDING )
message ( STATUS "Suppress any padding of heap blocks (MI_NO_PADDING=ON)" )
list ( APPEND mi_defines MI_PADDING=0 )
else ()
if ( MI_PADDING )
message ( STATUS "Enable explicit padding of heap blocks (MI_PADDING=ON)" )
list ( APPEND mi_defines MI_PADDING=1 )
endif ()
2020-04-07 10:01:18 -07:00
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_XMALLOC )
2020-04-21 11:39:57 +03:00
message ( STATUS "Enable abort() calls on memory allocation failure (MI_XMALLOC=ON)" )
list ( APPEND mi_defines MI_XMALLOC=1 )
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_SHOW_ERRORS )
2020-04-21 10:27:42 +03:00
message ( STATUS "Enable printing of error and warning messages by default (MI_SHOW_ERRORS=ON)" )
list ( APPEND mi_defines MI_SHOW_ERRORS=1 )
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_DEBUG_TSAN )
2020-07-25 19:55:36 -07:00
if ( CMAKE_C_COMPILER_ID MATCHES "Clang" )
message ( STATUS "Build with thread sanitizer (MI_DEBUG_TSAN=ON)" )
2020-07-25 23:50:22 -07:00
list ( APPEND mi_defines MI_TSAN=1 )
2020-07-25 19:55:36 -07:00
list ( APPEND mi_cflags -fsanitize=thread -g -O1 )
2023-03-04 09:58:17 -08:00
list ( APPEND mi_libraries -fsanitize=thread )
2020-07-25 19:55:36 -07:00
else ()
2022-12-03 00:23:43 +01:00
message ( WARNING "Can only use thread sanitizer with clang (MI_DEBUG_TSAN=ON but ignored)" )
endif ()
2020-07-25 19:55:36 -07:00
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_DEBUG_UBSAN )
2022-12-03 00:23:43 +01:00
if ( CMAKE_BUILD_TYPE MATCHES "Debug" )
2020-07-25 19:55:36 -07:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
message ( STATUS "Build with undefined-behavior sanitizer (MI_DEBUG_UBSAN=ON)" )
2024-06-03 21:26:38 -07:00
list ( APPEND mi_defines MI_UBSAN=1 )
2022-02-10 11:46:28 -08:00
list ( APPEND mi_cflags -fsanitize=undefined -g -fno-sanitize-recover=undefined )
2023-03-04 09:58:17 -08:00
list ( APPEND mi_libraries -fsanitize=undefined )
2021-01-22 19:49:15 -03:00
if ( NOT MI_USE_CXX )
2020-07-25 19:55:36 -07:00
message ( STATUS "(switch to use C++ due to MI_DEBUG_UBSAN)" )
set ( MI_USE_CXX "ON" )
endif ()
else ()
2022-12-03 00:23:43 +01:00
message ( WARNING "Can only use undefined-behavior sanitizer with clang++ (MI_DEBUG_UBSAN=ON but ignored)" )
endif ()
2020-07-25 19:55:36 -07:00
else ()
2024-03-02 17:07:09 -08:00
message ( WARNING "Can only use undefined-behavior sanitizer with a debug build (CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})" )
2020-07-25 19:55:36 -07:00
endif ()
endif ()
2021-01-22 19:49:15 -03:00
if ( MI_USE_CXX )
2019-06-24 18:54:03 -07:00
message ( STATUS "Use the C++ compiler to compile (MI_USE_CXX=ON)" )
2019-06-19 16:52:36 -07:00
set_source_files_properties ( ${ mi_sources } PROPERTIES LANGUAGE CXX )
2022-02-10 11:58:25 -08:00
set_source_files_properties ( src/static.c test/test-api.c test/test-api-fill test/test-stress PROPERTIES LANGUAGE CXX )
2020-01-17 15:39:41 -08:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "AppleClang|Clang" )
list ( APPEND mi_cflags -Wno-deprecated )
endif ()
2022-06-06 16:18:33 +00:00
if ( CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM" )
2020-01-19 18:35:45 -08:00
list ( APPEND mi_cflags -Kc++ )
endif ()
2019-06-19 16:52:36 -07:00
endif ()
2023-07-14 21:55:30 +01:00
if ( CMAKE_SYSTEM_NAME MATCHES "Linux|Android" )
if ( MI_NO_THP )
message ( STATUS "Disable transparent huge pages support (MI_NO_THP=ON)" )
list ( APPEND mi_defines MI_NO_THP=1 )
endif ()
endif ()
2024-04-20 16:19:59 -07:00
if ( MI_LIBC_MUSL )
2024-05-11 08:08:03 -07:00
message ( STATUS "Assume using musl libc (MI_LIBC_MUSL=ON)" )
2024-04-20 16:19:59 -07:00
list ( APPEND mi_defines MI_LIBC_MUSL=1 )
endif ()
2024-10-21 22:59:41 -07:00
if ( MI_WIN_USE_FLS )
2024-12-26 09:52:46 -08:00
message ( STATUS "Use the Fiber API to detect thread termination (deprecated) (MI_WIN_USE_FLS=ON)" )
2024-10-21 22:59:41 -07:00
list ( APPEND mi_defines MI_WIN_USE_FLS=1 )
endif ()
2024-12-26 09:52:46 -08:00
if ( MI_WIN_USE_FIXED_TLS )
message ( STATUS "Use fixed TLS slot on Windows to avoid extra tests in the malloc fast path (MI_WIN_USE_FIXED_TLS=ON)" )
list ( APPEND mi_defines MI_WIN_USE_FIXED_TLS=1 )
endif ()
2024-12-07 14:03:16 -08:00
# Check /proc/cpuinfo for an SV39 MMU and limit the virtual address bits.
# (this will skip the aligned hinting in that case. Issue #939, #949)
if ( EXISTS /proc/cpuinfo )
file ( STRINGS /proc/cpuinfo mi_sv39_mmu REGEX "^mmu[ \t]+:[ \t]+sv39$" )
if ( mi_sv39_mmu )
MESSAGE ( STATUS "Set virtual address bits to 39 (SV39 MMU detected)" )
list ( APPEND mi_defines MI_DEFAULT_VIRTUAL_ADDRESS_BITS=39 )
endif ()
endif ()
2024-10-27 21:58:20 -07:00
2024-03-02 16:44:06 -08:00
# On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788
# if(CMAKE_SYSTEM_NAME MATCHES "Haiku")
# SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib)
# SET(CMAKE_INSTALL_INCLUDEDIR ~/config/non-packaged/headers)
# endif()
2023-03-29 16:02:46 -07:00
2019-06-19 16:52:36 -07:00
# Compiler flags
2025-01-06 10:27:46 -08:00
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU" AND NOT MI_CLANG_CL )
2024-03-24 10:39:22 -07:00
list ( APPEND mi_cflags -Wno-unknown-pragmas -fvisibility=hidden )
2021-10-20 09:55:03 -07:00
if ( NOT MI_USE_CXX )
list ( APPEND mi_cflags -Wstrict-prototypes )
2022-12-03 00:23:43 +01:00
endif ()
2021-06-07 17:51:27 -07:00
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" )
2024-03-24 10:39:22 -07:00
list ( APPEND mi_cflags -Wno-static-in-inline )
2025-01-09 18:35:03 -08:00
endif ()
2020-01-19 18:35:45 -08:00
endif ()
if ( CMAKE_C_COMPILER_ID MATCHES "Intel" )
2024-03-24 10:39:22 -07:00
list ( APPEND mi_cflags -fvisibility=hidden )
2020-01-19 18:35:45 -08:00
endif ()
2025-01-06 10:27:46 -08:00
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku" AND NOT MI_CLANG_CL )
2021-01-22 19:49:15 -03:00
if ( MI_LOCAL_DYNAMIC_TLS )
2019-09-04 12:02:16 +02:00
list ( APPEND mi_cflags -ftls-model=local-dynamic )
else ()
2024-05-11 08:08:03 -07:00
if ( MI_LIBC_MUSL )
# with musl we use local-dynamic for the static build, see issue #644
list ( APPEND mi_cflags_static -ftls-model=local-dynamic )
list ( APPEND mi_cflags_dynamic -ftls-model=initial-exec )
message ( STATUS "Use local dynamic TLS for the static build (since MI_LIBC_MUSL=ON)" )
else ()
2024-06-03 17:58:34 -07:00
list ( APPEND mi_cflags -ftls-model=initial-exec )
2024-05-11 08:08:03 -07:00
endif ()
2019-09-04 12:02:16 +02:00
endif ()
2021-11-02 21:54:44 -07:00
if ( MI_OVERRIDE )
2021-11-13 13:30:03 -08:00
list ( APPEND mi_cflags -fno-builtin-malloc )
2021-11-02 21:54:44 -07:00
endif ()
2025-01-06 10:27:46 -08:00
endif ()
if ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku" )
2024-12-08 09:02:16 -08:00
if ( MI_OPT_ARCH )
2025-03-20 11:24:59 -07:00
if ( APPLE AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" AND CMAKE_OSX_ARCHITECTURES ) # to support multi-arch binaries (#999)
2025-02-08 12:18:27 -08:00
if ( "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES )
list ( APPEND MI_OPT_ARCH_FLAGS "-Xarch_arm64;-march=armv8.1-a" )
endif ()
2025-02-01 17:04:46 -08:00
elseif ( MI_ARCH STREQUAL "arm64" )
2025-02-08 12:18:27 -08:00
set ( MI_OPT_ARCH_FLAGS "-march=armv8.1-a" ) # fast atomics
2024-12-07 14:03:16 -08:00
endif ()
endif ()
2019-06-19 16:52:36 -07:00
endif ()
2021-01-28 23:58:41 -03:00
if ( MSVC AND MSVC_VERSION GREATER_EQUAL 1914 )
list ( APPEND mi_cflags /Zc:__cplusplus )
2025-01-06 10:27:46 -08:00
if ( MI_OPT_ARCH AND NOT MI_CLANG_CL )
2024-12-08 09:02:16 -08:00
if ( MI_ARCH STREQUAL "arm64" )
2025-01-09 18:35:03 -08:00
set ( MI_OPT_ARCH_FLAGS "/arch:armv8.1" ) # fast atomics
2024-12-07 14:16:39 -08:00
endif ()
endif ()
2021-01-28 23:58:41 -03:00
endif ()
2024-05-21 12:06:52 -07:00
if ( MINGW )
2025-03-06 19:09:51 -08:00
add_definitions ( -D_WIN32_WINNT=0x600 ) # issue #976
2024-05-21 12:06:52 -07:00
endif ()
2024-12-08 09:13:48 -08:00
if ( MI_OPT_ARCH_FLAGS )
list ( APPEND mi_cflags ${ MI_OPT_ARCH_FLAGS } )
message ( STATUS "Architecture specific optimization is enabled (with ${MI_OPT_ARCH_FLAGS}) (MI_OPT_ARCH=ON)" )
2024-12-07 14:16:39 -08:00
endif ()
2019-06-19 16:52:36 -07:00
# extra needed libraries
2024-05-19 15:38:26 -07:00
2024-06-03 17:58:34 -07:00
# we prefer -l<lib> test over `find_library` as sometimes core libraries
2024-05-19 15:38:26 -07:00
# like `libatomic` are not on the system path (see issue #898)
2024-06-03 17:58:34 -07:00
function ( find_link_library libname outlibname )
check_linker_flag ( C "-l${libname}" mi_has_lib ${ libname } )
2024-05-19 15:38:26 -07:00
if ( mi_has_lib ${ libname } )
message ( VERBOSE "link library: -l${libname}" )
2024-06-03 17:58:34 -07:00
set ( ${ outlibname } ${ libname } PARENT_SCOPE )
2024-05-19 15:38:26 -07:00
else ()
find_library ( MI_LIBPATH libname )
if ( MI_LIBPATH )
message ( VERBOSE "link library ${libname} at ${MI_LIBPATH}" )
2024-06-03 17:58:34 -07:00
set ( ${ outlibname } ${ MI_LIBPATH } PARENT_SCOPE )
2024-05-19 15:38:26 -07:00
else ()
message ( VERBOSE "link library not found: ${libname}" )
set ( ${ outlibname } "" PARENT_SCOPE )
endif ()
endif ()
endfunction ()
2019-06-19 16:52:36 -07:00
if ( WIN32 )
2024-06-03 17:58:34 -07:00
list ( APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt )
2019-06-19 16:52:36 -07:00
else ()
2024-05-19 15:43:15 -07:00
find_link_library ( "pthread" MI_LIB_PTHREAD )
2024-06-03 17:58:34 -07:00
if ( MI_LIB_PTHREAD )
2024-05-19 15:38:26 -07:00
list ( APPEND mi_libraries "${MI_LIB_PTHREAD}" )
2019-08-24 21:24:56 +08:00
endif ()
2024-05-19 15:43:15 -07:00
find_link_library ( "rt" MI_LIB_RT )
2024-06-03 17:58:34 -07:00
if ( MI_LIB_RT )
2024-05-19 15:38:26 -07:00
list ( APPEND mi_libraries "${MI_LIB_RT}" )
2022-06-19 13:20:53 -04:00
endif ()
2024-05-19 15:43:15 -07:00
find_link_library ( "atomic" MI_LIB_ATOMIC )
2024-06-03 17:58:34 -07:00
if ( MI_LIB_ATOMIC )
list ( APPEND mi_libraries "${MI_LIB_ATOMIC}" )
2021-12-15 08:42:58 -08:00
endif ()
2021-10-20 09:35:58 -07:00
endif ()
2025-03-01 14:29:23 -08:00
2019-06-27 13:36:19 -07:00
# -----------------------------------------------------------------------------
2019-11-14 11:01:05 -08:00
# Install and output names
2019-06-27 13:36:19 -07:00
# -----------------------------------------------------------------------------
2021-12-15 08:41:52 -08:00
# dynamic/shared library and symlinks always go to /usr/local/lib equivalent
2025-01-09 18:35:03 -08:00
# we use ${CMAKE_INSTALL_BINDIR} and ${CMAKE_INSTALL_LIBDIR}.
2021-12-14 18:29:14 -08:00
2021-12-15 08:41:52 -08:00
# static libraries and object files, includes, and cmake config files
# are either installed at top level, or use versioned directories for side-by-side installation (default)
2021-01-29 15:49:57 -08:00
if ( MI_INSTALL_TOPLEVEL )
2021-12-14 18:29:14 -08:00
set ( mi_install_objdir "${CMAKE_INSTALL_LIBDIR}" )
2022-12-03 00:23:43 +01:00
set ( mi_install_incdir "${CMAKE_INSTALL_INCLUDEDIR}" )
2021-12-14 18:29:14 -08:00
set ( mi_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/mimalloc" )
2020-05-30 07:19:57 +02:00
else ()
2021-12-14 18:29:14 -08:00
set ( mi_install_objdir "${CMAKE_INSTALL_LIBDIR}/mimalloc-${mi_version}" ) # for static library and object files
set ( mi_install_incdir "${CMAKE_INSTALL_INCLUDEDIR}/mimalloc-${mi_version}" ) # for includes
set ( mi_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/mimalloc-${mi_version}" ) # for cmake package info
2020-05-30 07:19:57 +02:00
endif ()
2021-01-29 15:49:57 -08:00
2025-01-09 20:57:08 -08:00
set ( mi_libname "mimalloc" )
2021-01-29 15:52:18 -08:00
if ( MI_SECURE )
2025-01-09 20:57:08 -08:00
set ( mi_libname "${mi_libname}-secure" )
2022-10-30 14:20:35 -07:00
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_VALGRIND )
2025-01-09 20:57:08 -08:00
set ( mi_libname "${mi_libname}-valgrind" )
2022-12-03 00:23:43 +01:00
endif ()
2023-03-06 09:02:38 -08:00
if ( MI_TRACK_ASAN )
2025-01-09 20:57:08 -08:00
set ( mi_libname "${mi_libname}-asan" )
2019-11-14 11:01:05 -08:00
endif ()
string ( TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC )
2025-03-01 14:29:23 -08:00
list ( APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_LC}" ) #todo: multi-config project needs $<CONFIG> ?
2025-03-19 20:29:29 -07:00
if ( CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$" )
list ( APPEND mi_defines MI_BUILD_RELEASE )
else ()
2025-01-09 20:57:08 -08:00
set ( mi_libname "${mi_libname}-${CMAKE_BUILD_TYPE_LC}" ) #append build type (e.g. -debug) if not a release version
2019-11-14 11:01:05 -08:00
endif ()
2022-10-30 14:20:35 -07:00
2020-05-04 11:01:11 -07:00
if ( MI_BUILD_SHARED )
list ( APPEND mi_build_targets "shared" )
endif ()
if ( MI_BUILD_STATIC )
list ( APPEND mi_build_targets "static" )
endif ()
if ( MI_BUILD_OBJECT )
list ( APPEND mi_build_targets "object" )
endif ()
if ( MI_BUILD_TESTS )
list ( APPEND mi_build_targets "tests" )
endif ()
2021-01-29 15:49:57 -08:00
2019-11-14 11:01:05 -08:00
message ( STATUS "" )
2025-01-09 20:57:08 -08:00
message ( STATUS "Library name : ${mi_libname}" )
2025-01-03 20:20:17 -08:00
message ( STATUS "Version : ${mi_version}.${mi_version_patch}" )
2019-11-14 11:01:05 -08:00
message ( STATUS "Build type : ${CMAKE_BUILD_TYPE_LC}" )
2021-01-22 19:49:15 -03:00
if ( MI_USE_CXX )
2021-11-04 18:55:12 -07:00
message ( STATUS "C++ Compiler : ${CMAKE_CXX_COMPILER}" )
2020-07-26 00:21:10 -07:00
else ()
2021-11-04 18:55:12 -07:00
message ( STATUS "C Compiler : ${CMAKE_C_COMPILER}" )
2020-07-26 00:21:10 -07:00
endif ()
2021-11-13 13:30:03 -08:00
message ( STATUS "Compiler flags : ${mi_cflags}" )
2021-12-13 13:10:33 -08:00
message ( STATUS "Compiler defines : ${mi_defines}" )
message ( STATUS "Link libraries : ${mi_libraries}" )
2020-05-04 11:01:11 -07:00
message ( STATUS "Build targets : ${mi_build_targets}" )
2019-11-14 11:01:05 -08:00
message ( STATUS "" )
# -----------------------------------------------------------------------------
# Main targets
# -----------------------------------------------------------------------------
2019-06-19 16:52:36 -07:00
# shared library
2020-04-28 18:12:43 +03:00
if ( MI_BUILD_SHARED )
add_library ( mimalloc SHARED ${ mi_sources } )
2025-01-09 20:57:08 -08:00
set_target_properties ( mimalloc PROPERTIES VERSION ${ mi_version } SOVERSION ${ mi_version_major } OUTPUT_NAME ${ mi_libname } )
2020-04-28 18:12:43 +03:00
target_compile_definitions ( mimalloc PRIVATE ${ mi_defines } MI_SHARED_LIB MI_SHARED_LIB_EXPORT )
2024-05-11 08:08:03 -07:00
target_compile_options ( mimalloc PRIVATE ${ mi_cflags } ${ mi_cflags_dynamic } )
2023-03-04 16:03:14 -08:00
target_link_libraries ( mimalloc PRIVATE ${ mi_libraries } )
2021-06-07 16:47:57 -07:00
target_include_directories ( mimalloc PUBLIC
$< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include >
$< INSTALL_INTERFACE:${mi_install_incdir} >
)
2025-01-09 20:57:08 -08:00
install ( TARGETS mimalloc EXPORT mimalloc ARCHIVE DESTINATION ${ CMAKE_INSTALL_LIBDIR } RUNTIME DESTINATION ${ CMAKE_INSTALL_BINDIR } LIBRARY DESTINATION ${ CMAKE_INSTALL_LIBDIR } )
install ( EXPORT mimalloc DESTINATION ${ mi_install_cmakedir } )
2025-03-24 08:35:15 +03:00
if ( WIN32 AND NOT MINGW )
2025-01-11 12:32:23 -08:00
# On windows, the import library name for the dll would clash with the static mimalloc.lib library
# so we postfix the dll import library with `.dll.lib` (and also the .pdb debug file)
2025-01-09 20:57:08 -08:00
set_property ( TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname}.dll" )
install ( FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.lib" DESTINATION ${ CMAKE_INSTALL_LIBDIR } )
2025-01-11 12:32:23 -08:00
set_property ( TARGET mimalloc PROPERTY PDB_NAME "${mi_libname}.dll" )
# don't try to install the pdb since it may not be generated depending on the configuration
# install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.pdb" DESTINATION ${CMAKE_INSTALL_LIBDIR})
2025-01-09 20:57:08 -08:00
endif ()
2022-10-31 15:26:21 -07:00
if ( WIN32 AND MI_WIN_REDIRECT )
2025-03-24 09:39:42 +08:00
if ( MINGW )
set_property ( TARGET mimalloc PROPERTY PREFIX "" )
endif ()
2022-10-31 15:26:21 -07:00
# On windows, link and copy the mimalloc redirection dll too.
2024-12-19 14:00:54 -08:00
if ( CMAKE_GENERATOR_PLATFORM STREQUAL "arm64ec" )
set ( MIMALLOC_REDIRECT_SUFFIX "-arm64ec" )
elseif ( MI_ARCH STREQUAL "x64" )
2024-12-18 14:20:49 -08:00
set ( MIMALLOC_REDIRECT_SUFFIX "" )
2024-12-19 14:00:54 -08:00
if ( CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" )
2025-01-09 20:57:08 -08:00
message ( STATUS "Note: x64 code emulated on Windows for arm64 should use an arm64ec build of 'mimalloc.dll'" )
message ( STATUS " together with 'mimalloc-redirect-arm64ec.dll'. See the 'bin\\readme.md' for more information." )
2024-12-19 14:00:54 -08:00
endif ()
2024-12-18 14:20:49 -08:00
elseif ( MI_ARCH STREQUAL "x86" )
2021-05-04 11:26:07 +02:00
set ( MIMALLOC_REDIRECT_SUFFIX "32" )
else ()
2024-12-18 14:20:49 -08:00
set ( MIMALLOC_REDIRECT_SUFFIX "-${MI_ARCH}" ) # -arm64 etc.
2021-05-04 11:26:07 +02:00
endif ()
2025-01-09 20:57:08 -08:00
target_link_libraries ( mimalloc PRIVATE ${ CMAKE_CURRENT_SOURCE_DIR } /bin/mimalloc-redirect ${ MIMALLOC_REDIRECT_SUFFIX } .lib ) # the DLL import library
2020-04-28 18:12:43 +03:00
add_custom_command ( TARGET mimalloc POST_BUILD
2021-05-04 11:26:07 +02:00
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.dll" $< TARGET_FILE_DIR:mimalloc >
COMMENT "Copy mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.dll to output directory" )
2025-01-09 18:35:03 -08:00
install ( FILES "$<TARGET_FILE_DIR:mimalloc>/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.dll" DESTINATION ${ CMAKE_INSTALL_BINDIR } )
2020-04-28 18:12:43 +03:00
endif ()
2019-07-23 09:36:58 -07:00
endif ()
2019-06-19 16:52:36 -07:00
2025-01-09 20:57:08 -08:00
2019-06-19 16:52:36 -07:00
# static library
2020-04-28 18:12:43 +03:00
if ( MI_BUILD_STATIC )
add_library ( mimalloc-static STATIC ${ mi_sources } )
2025-01-09 20:57:08 -08:00
set_property ( TARGET mimalloc-static PROPERTY OUTPUT_NAME ${ mi_libname } )
2020-05-04 09:51:09 -07:00
set_property ( TARGET mimalloc-static PROPERTY POSITION_INDEPENDENT_CODE ON )
2020-04-28 18:12:43 +03:00
target_compile_definitions ( mimalloc-static PRIVATE ${ mi_defines } MI_STATIC_LIB )
2024-05-11 08:08:03 -07:00
target_compile_options ( mimalloc-static PRIVATE ${ mi_cflags } ${ mi_cflags_static } )
2022-11-22 10:54:40 -08:00
target_link_libraries ( mimalloc-static PRIVATE ${ mi_libraries } )
2021-06-07 16:47:57 -07:00
target_include_directories ( mimalloc-static PUBLIC
$< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include >
$< INSTALL_INTERFACE:${mi_install_incdir} >
)
2021-12-14 18:29:14 -08:00
install ( TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${ mi_install_objdir } LIBRARY )
2021-12-26 18:17:53 +09:00
install ( EXPORT mimalloc DESTINATION ${ mi_install_cmakedir } )
2019-06-23 17:26:40 +08:00
endif ()
2019-06-19 16:52:36 -07:00
2020-04-28 18:12:43 +03:00
# install include files
2021-05-21 15:15:50 -07:00
install ( FILES include/mimalloc.h DESTINATION ${ mi_install_incdir } )
install ( FILES include/mimalloc-override.h DESTINATION ${ mi_install_incdir } )
install ( FILES include/mimalloc-new-delete.h DESTINATION ${ mi_install_incdir } )
2025-03-02 15:34:37 -08:00
install ( FILES include/mimalloc-stats.h DESTINATION ${ mi_install_incdir } )
2021-05-21 15:15:50 -07:00
install ( FILES cmake/mimalloc-config.cmake DESTINATION ${ mi_install_cmakedir } )
install ( FILES cmake/mimalloc-config-version.cmake DESTINATION ${ mi_install_cmakedir } )
2019-09-15 20:26:30 -07:00
2019-06-19 16:52:36 -07:00
# single object file for more predictable static overriding
2020-04-28 18:12:43 +03:00
if ( MI_BUILD_OBJECT )
add_library ( mimalloc-obj OBJECT src/static.c )
2020-05-04 09:51:09 -07:00
set_property ( TARGET mimalloc-obj PROPERTY POSITION_INDEPENDENT_CODE ON )
2020-04-28 18:12:43 +03:00
target_compile_definitions ( mimalloc-obj PRIVATE ${ mi_defines } )
2024-05-11 08:08:03 -07:00
target_compile_options ( mimalloc-obj PRIVATE ${ mi_cflags } ${ mi_cflags_static } )
2021-06-07 16:47:57 -07:00
target_include_directories ( mimalloc-obj PUBLIC
$< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include >
$< INSTALL_INTERFACE:${mi_install_incdir} >
)
2019-07-08 13:37:41 -07:00
2023-03-29 11:48:01 -07:00
# Copy the generated object file (`static.o`) to the output directory (as `mimalloc.o`)
2025-01-10 08:44:31 -08:00
if ( CMAKE_GENERATOR MATCHES "^Visual Studio.*$" )
2025-01-09 20:57:08 -08:00
set ( mimalloc-obj-static "${CMAKE_CURRENT_BINARY_DIR}/mimalloc-obj.dir/$<CONFIG>/static${CMAKE_C_OUTPUT_EXTENSION}" )
else ()
2023-03-29 11:55:00 -07:00
set ( mimalloc-obj-static "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/mimalloc-obj.dir/src/static.c${CMAKE_C_OUTPUT_EXTENSION}" )
endif ()
2025-01-09 20:57:08 -08:00
set ( mimalloc-obj-out "${CMAKE_CURRENT_BINARY_DIR}/${mi_libname}${CMAKE_C_OUTPUT_EXTENSION}" )
add_custom_command ( OUTPUT ${ mimalloc-obj-out } DEPENDS mimalloc-obj COMMAND "${CMAKE_COMMAND}" -E copy "${mimalloc-obj-static}" "${mimalloc-obj-out}" )
add_custom_target ( mimalloc-obj-target ALL DEPENDS ${ mimalloc-obj-out } )
2023-03-29 11:48:01 -07:00
2020-04-28 18:12:43 +03:00
# the following seems to lead to cmake warnings/errors on some systems, disable for now :-(
2022-01-10 16:53:19 -08:00
# install(TARGETS mimalloc-obj EXPORT mimalloc DESTINATION ${mi_install_objdir})
2020-04-28 18:12:43 +03:00
# the FILES expression can also be: $<TARGET_OBJECTS:mimalloc-obj>
# but that fails cmake versions less than 3.10 so we leave it as is for now
2023-03-29 11:48:01 -07:00
install ( FILES ${ mimalloc-obj-static }
2021-12-14 18:29:14 -08:00
DESTINATION ${ mi_install_objdir }
2025-01-09 20:57:08 -08:00
RENAME ${ mi_libname }${ CMAKE_C_OUTPUT_EXTENSION } )
2020-04-28 18:12:43 +03:00
endif ()
2019-06-27 13:36:19 -07:00
2025-01-09 20:57:08 -08:00
2022-06-19 13:20:53 -04:00
# pkg-config file support
2025-01-09 19:31:05 -08:00
set ( mi_pc_libraries "" )
2024-05-19 15:38:26 -07:00
foreach ( item IN LISTS mi_libraries )
if ( item MATCHES " *[-].*" )
2025-01-09 19:31:05 -08:00
set ( mi_pc_libraries "${mi_pc_libraries} ${item}" )
2024-05-19 15:38:26 -07:00
else ()
2025-01-09 19:31:05 -08:00
set ( mi_pc_libraries "${mi_pc_libraries} -l${item}" )
2024-05-19 15:38:26 -07:00
endif ()
endforeach ()
2022-06-19 13:20:53 -04:00
include ( "cmake/JoinPaths.cmake" )
2025-01-09 19:31:05 -08:00
join_paths ( mi_pc_includedir "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}" )
join_paths ( mi_pc_libdir "\${prefix}" "${CMAKE_INSTALL_LIBDIR}" )
2022-06-19 13:20:53 -04:00
configure_file ( mimalloc.pc.in mimalloc.pc @ONLY )
install ( FILES "${CMAKE_CURRENT_BINARY_DIR}/mimalloc.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/" )
2024-05-19 15:38:26 -07:00
2019-06-27 13:36:19 -07:00
# -----------------------------------------------------------------------------
# API surface testing
# -----------------------------------------------------------------------------
2019-07-10 21:52:28 -07:00
2021-01-22 19:49:15 -03:00
if ( MI_BUILD_TESTS )
2019-08-28 17:26:56 +02:00
enable_testing ()
2021-12-21 12:54:15 +01:00
2025-02-09 18:32:16 -08:00
# static link tests
2025-02-09 18:55:14 -08:00
foreach ( TEST_NAME api api-fill stress )
add_executable ( mimalloc-test- ${ TEST_NAME } test/test- ${ TEST_NAME } .c )
target_compile_definitions ( mimalloc-test- ${ TEST_NAME } PRIVATE ${ mi_defines } )
target_compile_options ( mimalloc-test- ${ TEST_NAME } PRIVATE ${ mi_cflags } )
target_include_directories ( mimalloc-test- ${ TEST_NAME } PRIVATE include )
2025-06-08 16:50:29 -07:00
if ( MI_BUILD_STATIC AND NOT MI_DEBUG_TSAN )
2025-02-09 18:32:16 -08:00
target_link_libraries ( mimalloc-test- ${ TEST_NAME } PRIVATE mimalloc-static ${ mi_libraries } )
2025-06-08 16:50:29 -07:00
elseif ( MI_BUILD_SHARED )
2025-06-08 16:02:44 -07:00
target_link_libraries ( mimalloc-test- ${ TEST_NAME } PRIVATE mimalloc ${ mi_libraries } )
2025-06-08 16:50:29 -07:00
else ()
message ( STATUS "cannot build TSAN tests without MI_BUILD_SHARED being enabled" )
2025-02-09 18:55:14 -08:00
endif ()
add_test ( NAME test- ${ TEST_NAME } COMMAND mimalloc-test- ${ TEST_NAME } )
endforeach ()
2025-02-09 18:32:16 -08:00
# dynamic override test
2025-02-09 19:22:26 -08:00
if ( MI_BUILD_SHARED AND NOT ( MI_TRACK_ASAN OR MI_DEBUG_TSAN OR MI_DEBUG_UBSAN ))
2025-02-09 18:32:16 -08:00
add_executable ( mimalloc-test-stress-dynamic test/test-stress.c )
target_compile_definitions ( mimalloc-test-stress-dynamic PRIVATE ${ mi_defines } "USE_STD_MALLOC=1" )
target_compile_options ( mimalloc-test-stress-dynamic PRIVATE ${ mi_cflags } )
target_include_directories ( mimalloc-test-stress-dynamic PRIVATE include )
2025-02-09 19:22:26 -08:00
if ( WIN32 )
2025-03-31 11:02:10 -07:00
target_compile_definitions ( mimalloc-test-stress-dynamic PRIVATE "MI_LINK_VERSION=1" ) # link mi_version
target_link_libraries ( mimalloc-test-stress-dynamic PRIVATE mimalloc ${ mi_libraries } ) # link mi_version
2025-03-31 11:00:05 -07:00
add_test ( NAME test-stress-dynamic COMMAND ${ CMAKE_COMMAND } -E env MIMALLOC_VERBOSE=1 $< TARGET_FILE:mimalloc-test-stress-dynamic > )
2025-02-09 18:32:16 -08:00
else ()
2025-02-09 19:22:26 -08:00
if ( APPLE )
set ( LD_PRELOAD "DYLD_INSERT_LIBRARIES" )
else ()
set ( LD_PRELOAD "LD_PRELOAD" )
endif ()
2025-03-31 11:00:05 -07:00
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 > )
2025-02-09 18:32:16 -08:00
endif ()
endif ()
2019-08-28 17:26:56 +02:00
endif ()
2019-06-27 13:36:19 -07:00
# -----------------------------------------------------------------------------
# Set override properties
# -----------------------------------------------------------------------------
2021-01-22 19:49:15 -03:00
if ( MI_OVERRIDE )
2020-04-28 18:12:43 +03:00
if ( MI_BUILD_SHARED )
2024-12-26 09:52:46 -08:00
target_compile_definitions ( mimalloc PRIVATE MI_MALLOC_OVERRIDE )
2020-04-28 18:12:43 +03:00
endif ()
2019-06-27 13:36:19 -07:00
if ( NOT WIN32 )
2019-11-14 11:01:05 -08:00
# It is only possible to override malloc on Windows when building as a DLL.
2020-04-28 18:12:43 +03:00
if ( MI_BUILD_STATIC )
target_compile_definitions ( mimalloc-static PRIVATE MI_MALLOC_OVERRIDE )
endif ()
if ( MI_BUILD_OBJECT )
target_compile_definitions ( mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE )
endif ()
2019-06-27 13:36:19 -07:00
endif ()
endif ()