2019-06-19 16:52:36 -07:00
|
|
|
cmake_minimum_required(VERSION 3.0)
|
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)
|
|
|
|
option(MI_PADDING "Enable padding to detect heap block overflow (used only in DEBUG mode)" ON)
|
|
|
|
option(MI_OVERRIDE "Override the standard malloc interface (e.g. define entry points for malloc() etc)" ON)
|
|
|
|
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)
|
|
|
|
option(MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" 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)
|
2021-06-17 19:15:09 -07:00
|
|
|
option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON)
|
2019-09-04 12:02:16 +02:00
|
|
|
option(MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" 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)
|
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)
|
2020-05-30 07:19:57 +02:00
|
|
|
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
|
2021-10-20 09:35:58 -07:00
|
|
|
option(MI_USE_LIBATOMIC "Explicitly link with -latomic (on older systems)" OFF)
|
2019-06-19 16:52:36 -07:00
|
|
|
|
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/stats.c
|
2019-12-22 17:07:01 -08:00
|
|
|
src/random.c
|
2019-06-19 16:52:36 -07:00
|
|
|
src/os.c
|
2020-09-08 10:14:13 -07:00
|
|
|
src/bitmap.c
|
2019-11-01 20:19:00 -07:00
|
|
|
src/arena.c
|
2020-01-25 13:28:49 -08:00
|
|
|
src/region.c
|
2019-06-19 16:52:36 -07:00
|
|
|
src/segment.c
|
|
|
|
src/page.c
|
|
|
|
src/alloc.c
|
|
|
|
src/alloc-aligned.c
|
2019-07-07 13:44:33 -07:00
|
|
|
src/alloc-posix.c
|
2019-06-19 16:52:36 -07:00
|
|
|
src/heap.c
|
|
|
|
src/options.c
|
|
|
|
src/init.c)
|
|
|
|
|
2021-11-04 18:55:12 -07:00
|
|
|
|
2019-11-14 11:01:05 -08:00
|
|
|
# -----------------------------------------------------------------------------
|
2021-09-16 03:35:56 +09:00
|
|
|
# Convenience: set default build type depending on the build directory
|
2019-11-14 11:01:05 -08:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2021-11-13 13:30:03 -08:00
|
|
|
message(STATUS "")
|
2019-06-19 16:52:36 -07:00
|
|
|
if (NOT CMAKE_BUILD_TYPE)
|
2021-01-22 19:49:15 -03:00
|
|
|
if ("${CMAKE_BINARY_DIR}" MATCHES ".*(D|d)ebug$" OR MI_DEBUG_FULL)
|
2019-11-14 11:01:05 -08:00
|
|
|
message(STATUS "No build type selected, default to: Debug")
|
2019-06-19 16:52:36 -07:00
|
|
|
set(CMAKE_BUILD_TYPE "Debug")
|
|
|
|
else()
|
2019-11-14 11:01:05 -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()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-11-04 18:55:12 -07:00
|
|
|
|
2019-11-14 11:01:05 -08:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Process options
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2020-01-19 18:35:45 -08:00
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel")
|
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)")
|
|
|
|
list(APPEND mi_sources src/alloc-override-osx.c)
|
2021-06-17 19:15:09 -07: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()
|
2021-11-04 18:55:12 -07:00
|
|
|
if((NOT MI_USE_CXX) AND MI_OVERRIDE)
|
|
|
|
message(STATUS " WARNING: if overriding C++ new/delete, it is best to build mimalloc with a C++ compiler (use -DMI_USE_CXX=ON)")
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
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)")
|
2019-11-07 10:26:52 -08:00
|
|
|
list(APPEND mi_defines MI_SECURE=4)
|
2019-06-19 16:52:36 -07:00
|
|
|
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)
|
|
|
|
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()
|
|
|
|
|
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()
|
|
|
|
|
2021-01-22 19:49:15 -03:00
|
|
|
if(NOT MI_PADDING)
|
2020-04-07 10:01:18 -07:00
|
|
|
message(STATUS "Disable padding of heap blocks in debug mode (MI_PADDING=OFF)")
|
|
|
|
list(APPEND mi_defines MI_PADDING=0)
|
|
|
|
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)
|
|
|
|
list(APPEND CMAKE_EXE_LINKER_FLAGS -fsanitize=thread)
|
|
|
|
else()
|
|
|
|
message(WARNING "Can only use thread sanitizer with clang (MI_DEBUG_TSAN=ON but ignored)")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2021-01-22 19:49:15 -03:00
|
|
|
if(MI_DEBUG_UBSAN)
|
2020-07-25 19:55:36 -07:00
|
|
|
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
|
|
|
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
|
|
|
message(STATUS "Build with undefined-behavior sanitizer (MI_DEBUG_UBSAN=ON)")
|
|
|
|
list(APPEND mi_cflags -fsanitize=undefined -g)
|
|
|
|
list(APPEND CMAKE_EXE_LINKER_FLAGS -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()
|
|
|
|
message(WARNING "Can only use undefined-behavior sanitizer with clang++ (MI_DEBUG_UBSAN=ON but ignored)")
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
message(WARNING "Can only use thread sanitizer with a debug build (CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})")
|
|
|
|
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 )
|
2020-01-19 18:35:45 -08:00
|
|
|
set_source_files_properties(src/static.c test/test-api.c 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()
|
2020-01-19 18:35:45 -08:00
|
|
|
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
|
|
|
list(APPEND mi_cflags -Kc++)
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Compiler flags
|
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU")
|
2021-10-20 09:55:03 -07:00
|
|
|
list(APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas -fvisibility=hidden)
|
|
|
|
if(NOT MI_USE_CXX)
|
|
|
|
list(APPEND mi_cflags -Wstrict-prototypes)
|
2021-11-15 10:52:09 -08:00
|
|
|
endif()
|
2021-06-07 17:51:27 -07:00
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
|
|
|
|
list(APPEND mi_cflags -Wpedantic -Wno-static-in-inline)
|
|
|
|
endif()
|
2020-01-19 18:35:45 -08:00
|
|
|
endif()
|
|
|
|
|
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "Intel")
|
|
|
|
list(APPEND mi_cflags -Wall -fvisibility=hidden)
|
|
|
|
endif()
|
|
|
|
|
2020-06-28 13:53:45 +00:00
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku")
|
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()
|
|
|
|
list(APPEND mi_cflags -ftls-model=initial-exec)
|
|
|
|
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()
|
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)
|
|
|
|
endif()
|
|
|
|
|
2019-06-19 16:52:36 -07:00
|
|
|
# extra needed libraries
|
|
|
|
if(WIN32)
|
2020-09-06 08:33:27 -07:00
|
|
|
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
|
2019-06-19 16:52:36 -07:00
|
|
|
else()
|
2021-12-15 08:41:52 -08:00
|
|
|
find_library(MI_LIBPTHREAD pthread)
|
|
|
|
if (MI_LIBPTHREAD)
|
|
|
|
list(APPEND mi_libraries ${MI_LIBPTHREAD})
|
2019-08-24 21:24:56 +08:00
|
|
|
endif()
|
2021-12-15 08:41:52 -08:00
|
|
|
find_library(MI_LIBRT rt)
|
|
|
|
if(MI_LIBRT)
|
|
|
|
list(APPEND mi_libraries ${MI_LIBRT})
|
2021-12-13 13:10:33 -08:00
|
|
|
endif()
|
2021-12-15 08:42:58 -08:00
|
|
|
find_library(MI_LIBATOMIC atomic)
|
|
|
|
if (MI_LIBATOMIC OR MI_USE_LIBATOMIC)
|
|
|
|
list(APPEND mi_libraries atomic)
|
|
|
|
endif()
|
2021-10-20 09:35:58 -07:00
|
|
|
endif()
|
|
|
|
|
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
|
|
|
|
set(mi_install_libdir "${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}")
|
|
|
|
set(mi_install_incdir "${CMAKE_INSTALL_INCLUDEDIR}")
|
|
|
|
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
|
|
|
|
2021-01-29 15:52:18 -08:00
|
|
|
if(MI_SECURE)
|
2019-11-14 11:01:05 -08:00
|
|
|
set(mi_basename "mimalloc-secure")
|
|
|
|
else()
|
|
|
|
set(mi_basename "mimalloc")
|
|
|
|
endif()
|
2021-01-29 15:49:57 -08:00
|
|
|
|
2019-11-14 11:01:05 -08:00
|
|
|
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
|
2021-10-19 10:56:13 -07:00
|
|
|
if(NOT(CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$"))
|
2019-11-14 11:01:05 -08:00
|
|
|
set(mi_basename "${mi_basename}-${CMAKE_BUILD_TYPE_LC}") #append build type (e.g. -debug) if not a release version
|
|
|
|
endif()
|
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 "")
|
|
|
|
message(STATUS "Library base name: ${mi_basename}")
|
2021-11-13 13:30:03 -08:00
|
|
|
message(STATUS "Version : ${mi_version}")
|
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})
|
2021-11-28 19:14:31 +02:00
|
|
|
set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} SOVERSION ${mi_version_major} OUTPUT_NAME ${mi_basename} )
|
2020-04-28 18:12:43 +03:00
|
|
|
target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT)
|
|
|
|
target_compile_options(mimalloc PRIVATE ${mi_cflags})
|
|
|
|
target_link_libraries(mimalloc PUBLIC ${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}>
|
|
|
|
)
|
2020-04-28 18:12:43 +03:00
|
|
|
if(WIN32)
|
|
|
|
# On windows copy the mimalloc redirection dll too.
|
2021-05-04 11:26:07 +02:00
|
|
|
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
|
|
|
|
set(MIMALLOC_REDIRECT_SUFFIX "32")
|
|
|
|
else()
|
|
|
|
set(MIMALLOC_REDIRECT_SUFFIX "")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
target_link_libraries(mimalloc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect${MIMALLOC_REDIRECT_SUFFIX}.lib)
|
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")
|
2020-04-28 18:12:43 +03:00
|
|
|
endif()
|
|
|
|
|
2021-05-21 13:01:11 -07:00
|
|
|
install(TARGETS mimalloc EXPORT mimalloc DESTINATION ${mi_install_libdir} LIBRARY)
|
2021-05-21 15:15:50 -07:00
|
|
|
install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir})
|
2019-07-23 09:36:58 -07:00
|
|
|
endif()
|
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})
|
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)
|
|
|
|
target_compile_options(mimalloc-static PRIVATE ${mi_cflags})
|
|
|
|
target_link_libraries(mimalloc-static PUBLIC ${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}>
|
|
|
|
)
|
2020-05-04 09:51:09 -07:00
|
|
|
if(WIN32)
|
2020-04-28 18:12:43 +03:00
|
|
|
# When building both static and shared libraries on Windows, a static library should use a
|
|
|
|
# different output name to avoid the conflict with the import library of a shared one.
|
|
|
|
string(REPLACE "mimalloc" "mimalloc-static" mi_output_name ${mi_basename})
|
|
|
|
set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_output_name})
|
|
|
|
else()
|
|
|
|
set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_basename})
|
|
|
|
endif()
|
|
|
|
|
2021-12-14 18:29:14 -08:00
|
|
|
install(TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${mi_install_objdir} LIBRARY)
|
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})
|
|
|
|
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})
|
|
|
|
target_compile_options(mimalloc-obj PRIVATE ${mi_cflags})
|
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}>
|
|
|
|
)
|
2020-04-28 18:12:43 +03:00
|
|
|
|
|
|
|
# the following seems to lead to cmake warnings/errors on some systems, disable for now :-(
|
2021-05-21 15:15:50 -07:00
|
|
|
# install(TARGETS mimalloc-obj EXPORT mimalloc DESTINATION ${mi_install_libdir})
|
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
|
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/mimalloc-obj.dir/src/static.c${CMAKE_C_OUTPUT_EXTENSION}
|
2021-12-14 18:29:14 -08:00
|
|
|
DESTINATION ${mi_install_objdir}
|
2020-04-28 18:12:43 +03:00
|
|
|
RENAME ${mi_basename}${CMAKE_C_OUTPUT_EXTENSION} )
|
|
|
|
endif()
|
2019-06-27 13:36:19 -07:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# API surface testing
|
|
|
|
# -----------------------------------------------------------------------------
|
2019-08-28 17:26:56 +02:00
|
|
|
|
2021-01-22 19:49:15 -03:00
|
|
|
if (MI_BUILD_TESTS)
|
2019-08-28 17:26:56 +02:00
|
|
|
add_executable(mimalloc-test-api test/test-api.c)
|
|
|
|
target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines})
|
|
|
|
target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags})
|
|
|
|
target_include_directories(mimalloc-test-api PRIVATE include)
|
2021-11-02 21:54:44 -07:00
|
|
|
target_link_libraries(mimalloc-test-api PRIVATE mimalloc ${mi_libraries})
|
2019-08-28 17:26:56 +02:00
|
|
|
|
|
|
|
add_executable(mimalloc-test-stress test/test-stress.c)
|
|
|
|
target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines})
|
|
|
|
target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags})
|
|
|
|
target_include_directories(mimalloc-test-stress PRIVATE include)
|
2020-01-29 22:46:44 -08:00
|
|
|
target_link_libraries(mimalloc-test-stress PRIVATE mimalloc ${mi_libraries})
|
2019-08-28 17:26:56 +02:00
|
|
|
|
|
|
|
enable_testing()
|
|
|
|
add_test(test_api, mimalloc-test-api)
|
|
|
|
add_test(test_stress, mimalloc-test-stress)
|
|
|
|
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)
|
|
|
|
target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE)
|
|
|
|
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()
|