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-09-04 12:02:16 +02:00
|
|
|
option(MI_OVERRIDE "Override the standard malloc interface" ON)
|
|
|
|
option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON)
|
|
|
|
option(MI_SEE_ASM "Generate assembly files" OFF)
|
|
|
|
option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode" OFF)
|
|
|
|
option(MI_USE_CXX "Use the C++ compiler to compile the library" OFF)
|
|
|
|
option(MI_SECURE "Use security mitigations (like guard pages and randomization)" OFF)
|
|
|
|
option(MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF)
|
|
|
|
option(MI_BUILD_TESTS "Build test executables" ON)
|
2019-06-19 16:52:36 -07:00
|
|
|
|
2019-11-03 13:34:54 -08:00
|
|
|
include("cmake/mimalloc-config-version.cmake")
|
|
|
|
include("CheckIncludeFile")
|
|
|
|
|
2019-06-19 16:52:36 -07:00
|
|
|
set(mi_install_dir "lib/mimalloc-${mi_version}")
|
|
|
|
|
|
|
|
set(mi_sources
|
|
|
|
src/stats.c
|
|
|
|
src/os.c
|
2019-11-01 20:19:00 -07:00
|
|
|
src/arena.c
|
2019-07-02 07:23:24 -07:00
|
|
|
src/memory.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)
|
|
|
|
|
|
|
|
# Set default build type
|
|
|
|
if (NOT CMAKE_BUILD_TYPE)
|
|
|
|
if ("${CMAKE_BINARY_DIR}" MATCHES ".*(D|d)ebug$")
|
|
|
|
message(STATUS "No build type selected, default to *** Debug ***")
|
|
|
|
set(CMAKE_BUILD_TYPE "Debug")
|
|
|
|
else()
|
|
|
|
message(STATUS "No build type selected, default to *** Release ***")
|
|
|
|
set(CMAKE_BUILD_TYPE "Release")
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
message(STATUS "Build type specified as *** ${CMAKE_BUILD_TYPE} ***")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if("${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$")
|
2019-06-24 18:54:03 -07:00
|
|
|
set(MI_SECURE "ON")
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
|
2019-07-23 09:59:20 -07:00
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
|
|
|
|
set(MI_USE_CXX "ON")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
2019-06-19 16:52:36 -07:00
|
|
|
# Options
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_OVERRIDE MATCHES "ON")
|
|
|
|
message(STATUS "Override standard malloc (MI_OVERRIDE=ON)")
|
2019-06-19 16:52:36 -07:00
|
|
|
if(APPLE)
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_INTERPOSE MATCHES "ON")
|
2019-06-23 15:29:41 +08:00
|
|
|
# use interpose on macOS
|
2019-06-24 18:54:03 -07:00
|
|
|
message(STATUS " Use interpose to override malloc (MI_INTERPOSE=ON)")
|
2019-06-19 16:52:36 -07:00
|
|
|
list(APPEND mi_defines MI_INTERPOSE)
|
|
|
|
else()
|
2019-06-23 15:29:41 +08:00
|
|
|
# use zone's on macOS
|
2019-06-24 18:54:03 -07:00
|
|
|
message(STATUS " Use zone's to override malloc (MI_INTERPOSE=OFF)")
|
2019-06-19 16:52:36 -07:00
|
|
|
list(APPEND mi_sources src/alloc-override-osx.c)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_SECURE MATCHES "ON")
|
|
|
|
message(STATUS "Set secure build (MI_SECURE=ON)")
|
2019-10-18 18:11:04 -07:00
|
|
|
list(APPEND mi_defines MI_SECURE=3)
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_SEE_ASM MATCHES "ON")
|
|
|
|
message(STATUS "Generate assembly listings (MI_SEE_ASM=ON)")
|
2019-06-19 16:52:36 -07:00
|
|
|
list(APPEND mi_cflags -save-temps)
|
|
|
|
endif()
|
|
|
|
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_CHECK_FULL MATCHES "ON")
|
|
|
|
message(STATUS "Set debug level to full invariant checking (MI_CHECK_FULL=ON)")
|
2019-06-19 16:52:36 -07:00
|
|
|
list(APPEND mi_defines MI_DEBUG=3) # full invariant checking
|
|
|
|
endif()
|
|
|
|
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_USE_CXX MATCHES "ON")
|
|
|
|
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 )
|
2019-08-12 08:55:20 -07:00
|
|
|
set_source_files_properties(src/static.c test/test-api.c PROPERTIES LANGUAGE CXX )
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
|
2019-11-01 22:01:52 -07:00
|
|
|
CHECK_INCLUDE_FILE("numaif.h" MI_HAVE_NUMA_H)
|
|
|
|
if(MI_HAVE_NUMA_H)
|
|
|
|
list(APPEND mi_defines MI_HAS_NUMA)
|
|
|
|
list(APPEND mi_libraries numa)
|
|
|
|
else()
|
|
|
|
if (NOT(WIN32))
|
|
|
|
message(WARNING "Compiling without using NUMA optimized allocation (on Linux, install libnuma-dev?)")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2019-06-19 16:52:36 -07:00
|
|
|
# Compiler flags
|
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU")
|
2019-09-04 12:02:16 +02:00
|
|
|
list(APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas)
|
|
|
|
if(MI_LOCAL_DYNAMIC_TLS MATCHES "ON")
|
|
|
|
list(APPEND mi_cflags -ftls-model=local-dynamic)
|
|
|
|
else()
|
|
|
|
list(APPEND mi_cflags -ftls-model=initial-exec)
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
|
|
|
|
list(APPEND mi_cflags -Wno-invalid-memory-model)
|
2019-06-23 23:04:43 +08:00
|
|
|
list(APPEND mi_cflags -fvisibility=hidden)
|
2019-08-11 10:31:00 -07:00
|
|
|
list(APPEND mi_cflags -fbranch-target-load-optimize)
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2019-09-02 09:57:39 -07:00
|
|
|
if(NOT(CMAKE_BUILD_TYPE MATCHES "Release|release|RelWithDebInfo|relwithdebinfo"))
|
2019-06-19 16:52:36 -07:00
|
|
|
string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
|
|
|
|
set(mi_basename "mimalloc-${build_type}")
|
|
|
|
else()
|
2019-06-24 18:54:03 -07:00
|
|
|
if(MI_SECURE MATCHES "ON")
|
2019-06-19 16:52:36 -07:00
|
|
|
set(mi_basename "mimalloc-secure")
|
|
|
|
else()
|
|
|
|
set(mi_basename "mimalloc")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
message(STATUS "Output library name : ${mi_basename}")
|
|
|
|
message(STATUS "Installation directory: ${mi_install_dir}")
|
|
|
|
|
|
|
|
# extra needed libraries
|
|
|
|
if(WIN32)
|
|
|
|
list(APPEND mi_libraries psapi shell32 user32)
|
|
|
|
else()
|
|
|
|
list(APPEND mi_libraries pthread)
|
2019-08-24 21:24:56 +08:00
|
|
|
find_library(LIBRT rt)
|
|
|
|
if(LIBRT)
|
|
|
|
list(APPEND mi_libraries ${LIBRT})
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
endif()
|
|
|
|
|
2019-06-27 13:36:19 -07:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Main targets
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2019-06-19 16:52:36 -07:00
|
|
|
|
|
|
|
# shared library
|
|
|
|
add_library(mimalloc SHARED ${mi_sources})
|
2019-09-15 20:26:30 -07:00
|
|
|
set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} OUTPUT_NAME ${mi_basename} )
|
2019-06-19 16:52:36 -07:00
|
|
|
target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT)
|
|
|
|
target_compile_options(mimalloc PRIVATE ${mi_cflags})
|
2019-07-23 09:36:58 -07:00
|
|
|
target_link_libraries(mimalloc PUBLIC ${mi_libraries})
|
2019-06-27 12:43:27 +02:00
|
|
|
target_include_directories(mimalloc PUBLIC
|
|
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
|
|
$<INSTALL_INTERFACE:${mi_install_dir}/include>
|
|
|
|
)
|
2019-07-23 09:36:58 -07:00
|
|
|
if(WIN32)
|
|
|
|
# On windows copy the mimalloc redirection dll too.
|
2019-07-23 12:40:42 -07:00
|
|
|
target_link_libraries(mimalloc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect.lib)
|
2019-07-23 09:36:58 -07:00
|
|
|
add_custom_command(TARGET mimalloc POST_BUILD
|
2019-07-23 12:40:42 -07:00
|
|
|
COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/bin/mimalloc-redirect.dll" $<TARGET_FILE_DIR:mimalloc>
|
2019-07-23 09:36:58 -07:00
|
|
|
COMMENT "Copy mimalloc-redirect.dll to output directory")
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
|
|
|
|
# static library
|
|
|
|
add_library(mimalloc-static STATIC ${mi_sources})
|
2019-07-23 09:36:58 -07: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})
|
|
|
|
target_include_directories(mimalloc-static PUBLIC
|
|
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
|
|
$<INSTALL_INTERFACE:${mi_install_dir}/include>
|
|
|
|
)
|
2019-06-23 17:26:40 +08:00
|
|
|
if(WIN32)
|
2019-06-24 18:54:03 -07: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.
|
2019-06-23 17:26:40 +08:00
|
|
|
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()
|
2019-06-19 16:52:36 -07:00
|
|
|
|
|
|
|
# install static and shared library, and the include files
|
2019-09-15 20:26:30 -07:00
|
|
|
install(TARGETS mimalloc EXPORT mimalloc DESTINATION ${mi_install_dir} LIBRARY)
|
2019-06-19 16:52:36 -07:00
|
|
|
install(TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${mi_install_dir})
|
|
|
|
install(FILES include/mimalloc.h DESTINATION ${mi_install_dir}/include)
|
2019-07-18 19:52:29 -07:00
|
|
|
install(FILES include/mimalloc-override.h DESTINATION ${mi_install_dir}/include)
|
2019-09-15 19:19:00 -07:00
|
|
|
install(FILES include/mimalloc-new-delete.h DESTINATION ${mi_install_dir}/include)
|
2019-06-19 16:52:36 -07:00
|
|
|
install(FILES cmake/mimalloc-config.cmake DESTINATION ${mi_install_dir}/cmake)
|
|
|
|
install(FILES cmake/mimalloc-config-version.cmake DESTINATION ${mi_install_dir}/cmake)
|
|
|
|
install(EXPORT mimalloc DESTINATION ${mi_install_dir}/cmake)
|
2019-09-15 20:26:30 -07:00
|
|
|
|
|
|
|
if(NOT WIN32)
|
|
|
|
# install a symlink in the /usr/local/lib to the versioned library
|
|
|
|
set(mi_symlink "${CMAKE_SHARED_MODULE_PREFIX}${mi_basename}${CMAKE_SHARED_LIBRARY_SUFFIX}")
|
|
|
|
set(mi_soname "mimalloc-${mi_version}/${mi_symlink}.${mi_version}")
|
|
|
|
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${mi_soname} ${mi_symlink} WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/${mi_install_dir}/..)")
|
|
|
|
install(CODE "MESSAGE(\"-- Symbolic link: ${CMAKE_INSTALL_PREFIX}/lib/${mi_symlink} -> ${mi_soname}\")")
|
|
|
|
endif()
|
2019-06-19 16:52:36 -07:00
|
|
|
|
|
|
|
# single object file for more predictable static overriding
|
|
|
|
add_library(mimalloc-obj OBJECT src/static.c)
|
2019-06-23 17:26:40 +08:00
|
|
|
target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines})
|
2019-06-19 16:52:36 -07:00
|
|
|
target_compile_options(mimalloc-obj PRIVATE ${mi_cflags})
|
2019-06-27 12:43:27 +02:00
|
|
|
target_include_directories(mimalloc-obj PUBLIC
|
|
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
|
|
$<INSTALL_INTERFACE:${mi_install_dir}/include>
|
|
|
|
)
|
2019-06-19 16:52:36 -07:00
|
|
|
|
2019-07-14 14:47:59 -07:00
|
|
|
# the following seems to lead to cmake warnings/errors on some systems, disable for now :-(
|
2019-07-09 11:32:24 -07:00
|
|
|
# install(TARGETS mimalloc-obj EXPORT mimalloc DESTINATION ${mi_install_dir})
|
2019-07-08 13:37:41 -07:00
|
|
|
|
2019-07-14 14:47:59 -07: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}
|
2019-06-19 16:52:36 -07:00
|
|
|
DESTINATION ${mi_install_dir}
|
|
|
|
RENAME ${mi_basename}${CMAKE_C_OUTPUT_EXTENSION} )
|
2019-06-27 13:36:19 -07:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# API surface testing
|
|
|
|
# -----------------------------------------------------------------------------
|
2019-08-28 17:26:56 +02:00
|
|
|
|
|
|
|
if (MI_BUILD_TESTS MATCHES "ON")
|
|
|
|
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)
|
|
|
|
target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static ${mi_libraries})
|
|
|
|
|
|
|
|
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)
|
|
|
|
target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static ${mi_libraries})
|
|
|
|
|
|
|
|
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
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
if (MI_OVERRIDE MATCHES "ON")
|
|
|
|
target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE)
|
|
|
|
if(NOT WIN32)
|
|
|
|
# It is only possible to override malloc on Windows when building as a DLL. (src/alloc-override.c)
|
|
|
|
target_compile_definitions(mimalloc-static PRIVATE MI_MALLOC_OVERRIDE)
|
|
|
|
target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE)
|
|
|
|
endif()
|
|
|
|
endif()
|