Skip to content

Commit 30b8624

Browse files
authored
Merge pull request #22 from myd7349/win32-cmake-patch
Fix CMake configuration on Windows
2 parents 8bb8852 + b7c8d8f commit 30b8624

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

CMakeLists.txt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ endif()
4444
# Options
4545
if(OVERRIDE MATCHES "ON")
4646
message(STATUS "Override standard malloc (OVERRIDE=ON)")
47-
list(APPEND mi_defines MI_MALLOC_OVERRIDE)
4847
if(APPLE)
4948
if(INTERPOSE MATCHES "ON")
5049
# use interpose on macOS
@@ -111,14 +110,30 @@ endif()
111110
add_library(mimalloc SHARED ${mi_sources})
112111
set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} NO_SONAME "YES" OUTPUT_NAME ${mi_basename} )
113112
target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT)
113+
if(OVERRIDE MATCHES "ON")
114+
target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE)
115+
endif()
114116
target_compile_options(mimalloc PRIVATE ${mi_cflags})
115117
target_include_directories(mimalloc PRIVATE include PUBLIC $<INSTALL_INTERFACE:${mi_install_dir}/include>)
116118
target_link_libraries(mimalloc PUBLIC ${mi_libraries})
117119

118120
# static library
119121
add_library(mimalloc-static STATIC ${mi_sources})
120-
set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_basename})
122+
if(WIN32)
123+
# When building both static and shared libraries on Windows,
124+
# a static library should use a different output name to
125+
# avoid the conflict with the import library of a shared one.
126+
string(REPLACE "mimalloc" "mimalloc-static" mi_output_name ${mi_basename})
127+
set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_output_name})
128+
else()
129+
set_target_properties(mimalloc-static PROPERTIES OUTPUT_NAME ${mi_basename})
130+
endif()
121131
target_compile_definitions(mimalloc-static PRIVATE ${mi_defines} MI_STATIC_LIB)
132+
if(NOT WIN32 AND OVERRIDE MATCHES "ON")
133+
# It is only possible to override malloc on Windows when building as a DLL.
134+
# (src/alloc-override.c)
135+
target_compile_definitions(mimalloc-static PRIVATE MI_MALLOC_OVERRIDE)
136+
endif()
122137
target_compile_options(mimalloc-static PRIVATE ${mi_cflags})
123138
target_include_directories(mimalloc-static PRIVATE include PUBLIC $<INSTALL_INTERFACE:${mi_install_dir}/include>)
124139
target_link_libraries(mimalloc-static PUBLIC ${mi_libraries})
@@ -134,7 +149,12 @@ install(FILES "$<TARGET_FILE:mimalloc>" DESTINATION lib) # duplicate the .so in
134149

135150
# single object file for more predictable static overriding
136151
add_library(mimalloc-obj OBJECT src/static.c)
137-
target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines} MI_MALLOC_OVERRIDE)
152+
target_compile_definitions(mimalloc-obj PRIVATE ${mi_defines})
153+
if(NOT WIN32 AND OVERRIDE MATCHES "ON")
154+
# It is only possible to override malloc on Windows when building as a DLL.
155+
# (src/alloc-override.c)
156+
target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE)
157+
endif()
138158
target_compile_options(mimalloc-obj PRIVATE ${mi_cflags})
139159
target_include_directories(mimalloc-obj PRIVATE include PUBLIC $<INSTALL_INTERFACE:include>)
140160

0 commit comments

Comments
 (0)