From b7c8d8f007a02e6c73af68d300a99396aa2d9a0c Mon Sep 17 00:00:00 2001 From: myd7349 Date: Sun, 23 Jun 2019 17:26:40 +0800 Subject: [PATCH 1/2] Fix CMake configuration on Windows - Do not define MI_MALLOC_OVERRIDE when built as a static library - Use different output names for shared lib and static lib --- CMakeLists.txt | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f6968ed7..2886012a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,6 @@ endif() # Options if(OVERRIDE MATCHES "ON") message(STATUS "Override standard malloc (OVERRIDE=ON)") - list(APPEND mi_defines MI_MALLOC_OVERRIDE) if(APPLE) if(INTERPOSE MATCHES "ON") # use interpose on MacOSX @@ -111,14 +110,30 @@ endif() add_library(mimalloc SHARED ${mi_sources}) set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} NO_SONAME "YES" OUTPUT_NAME ${mi_basename} ) target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT) +if(OVERRIDE MATCHES "ON") + target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE) +endif() target_compile_options(mimalloc PRIVATE ${mi_cflags}) target_include_directories(mimalloc PRIVATE include PUBLIC $) target_link_libraries(mimalloc PUBLIC ${mi_libraries}) # static library add_library(mimalloc-static STATIC ${mi_sources}) -set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_basename}) +if(WIN32) + # 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() target_compile_definitions(mimalloc-static PRIVATE ${mi_defines} MI_STATIC_LIB) +if(NOT WIN32 AND OVERRIDE MATCHES "ON") + # 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) +endif() target_compile_options(mimalloc-static PRIVATE ${mi_cflags}) target_include_directories(mimalloc-static PRIVATE include PUBLIC $) target_link_libraries(mimalloc-static PUBLIC ${mi_libraries}) @@ -134,7 +149,12 @@ install(FILES "$" DESTINATION lib) # duplicate the .so in # single object file for more predictable static overriding add_library(mimalloc-obj OBJECT src/static.c) -target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines} MI_MALLOC_OVERRIDE) +target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines}) +if(NOT WIN32 AND OVERRIDE MATCHES "ON") + # It is only possible to override malloc on Windows when building as a DLL. + # (src/alloc-override.c) + target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE) +endif() target_compile_options(mimalloc-obj PRIVATE ${mi_cflags}) target_include_directories(mimalloc-obj PRIVATE include PUBLIC $) From 91cb4cee8afec59796390cf1c7b809061f1782c5 Mon Sep 17 00:00:00 2001 From: daan Date: Mon, 24 Jun 2019 18:54:03 -0700 Subject: [PATCH 2/2] use MI_ prefix for all options to better support subdirectory cmake, issue #3 --- CMakeLists.txt | 59 ++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e0f02b7..a35e885e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,12 @@ project(libmimalloc C) include("cmake/mimalloc-config-version.cmake") set(CMAKE_C_STANDARD 11) -option(OVERRIDE "OVERRIDE" ON) -option(INTERPOSE "INTERPOSE" ON) -option(SEE_ASM "SEE_ASM" OFF) -option(CHECK_FULL "CHECK_FULL" OFF) -option(USE_CXX "USE_CXX" OFF) -option(SECURE "SECURE" OFF) +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) set(mi_install_dir "lib/mimalloc-${mi_version}") @@ -38,42 +38,42 @@ else() endif() if("${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$") - set(SECURE "ON") + set(MI_SECURE "ON") endif() # Options -if(OVERRIDE MATCHES "ON") - message(STATUS "Override standard malloc (OVERRIDE=ON)") +if(MI_OVERRIDE MATCHES "ON") + message(STATUS "Override standard malloc (MI_OVERRIDE=ON)") if(APPLE) - if(INTERPOSE MATCHES "ON") + if(MI_INTERPOSE MATCHES "ON") # use interpose on macOS - message(STATUS " Use interpose to override malloc (INTERPOSE=ON)") + message(STATUS " Use interpose to override malloc (MI_INTERPOSE=ON)") list(APPEND mi_defines MI_INTERPOSE) else() # use zone's on macOS - message(STATUS " Use zone's to override malloc (INTERPOSE=OFF)") + message(STATUS " Use zone's to override malloc (MI_INTERPOSE=OFF)") list(APPEND mi_sources src/alloc-override-osx.c) endif() endif() endif() -if(SECURE MATCHES "ON") - message(STATUS "Set secure build (SECURE=ON)") +if(MI_SECURE MATCHES "ON") + message(STATUS "Set secure build (MI_SECURE=ON)") list(APPEND mi_defines MI_SECURE=2) endif() -if(SEE_ASM MATCHES "ON") - message(STATUS "Generate assembly listings (SEE_ASM=ON)") +if(MI_SEE_ASM MATCHES "ON") + message(STATUS "Generate assembly listings (MI_SEE_ASM=ON)") list(APPEND mi_cflags -save-temps) endif() -if(CHECK_FULL MATCHES "ON") - message(STATUS "Set debug level to full invariant checking (CHECK_FULL=ON)") +if(MI_CHECK_FULL MATCHES "ON") + message(STATUS "Set debug level to full invariant checking (MI_CHECK_FULL=ON)") list(APPEND mi_defines MI_DEBUG=3) # full invariant checking endif() -if(USE_CXX MATCHES "ON") - message(STATUS "Use the C++ compiler to compile (USE_CXX=ON)") +if(MI_USE_CXX MATCHES "ON") + message(STATUS "Use the C++ compiler to compile (MI_USE_CXX=ON)") set_source_files_properties(${mi_sources} PROPERTIES LANGUAGE CXX ) endif() @@ -89,7 +89,7 @@ if(NOT(CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")) string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type) set(mi_basename "mimalloc-${build_type}") else() - if(SECURE MATCHES "ON") + if(MI_SECURE MATCHES "ON") set(mi_basename "mimalloc-secure") else() set(mi_basename "mimalloc") @@ -110,7 +110,7 @@ endif() add_library(mimalloc SHARED ${mi_sources}) set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} NO_SONAME "YES" OUTPUT_NAME ${mi_basename} ) target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT) -if(OVERRIDE MATCHES "ON") +if(MI_OVERRIDE MATCHES "ON") target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE) endif() target_compile_options(mimalloc PRIVATE ${mi_cflags}) @@ -120,18 +120,16 @@ target_link_libraries(mimalloc PUBLIC ${mi_libraries}) # static library add_library(mimalloc-static STATIC ${mi_sources}) if(WIN32) - # 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. + # 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() target_compile_definitions(mimalloc-static PRIVATE ${mi_defines} MI_STATIC_LIB) -if(NOT WIN32 AND OVERRIDE MATCHES "ON") - # It is only possible to override malloc on Windows when building as a DLL. - # (src/alloc-override.c) +if(NOT WIN32 AND MI_OVERRIDE MATCHES "ON") + # 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) endif() target_compile_options(mimalloc-static PRIVATE ${mi_cflags}) @@ -150,9 +148,8 @@ install(FILES "$" DESTINATION lib) # duplicate the .so in # single object file for more predictable static overriding add_library(mimalloc-obj OBJECT src/static.c) target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines}) -if(NOT WIN32 AND OVERRIDE MATCHES "ON") - # It is only possible to override malloc on Windows when building as a DLL. - # (src/alloc-override.c) +if(NOT WIN32 AND MI_OVERRIDE MATCHES "ON") + # It is only possible to override malloc on Windows when building as a DLL. (src/alloc-override.c) target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE) endif() target_compile_options(mimalloc-obj PRIVATE ${mi_cflags})