Skip to content

Commit 443d04a

Browse files
asn5dSeethepalliCopilotCopilot
authored
Added optional mimalloc allocator support for Release builds (#6)
* Add optional mimalloc allocator support for Release builds. * Update RoiManager/CMakeLists.txt to check mimalloc target exists before linking. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update PluginManager/CMakeLists.txt to check mimalloc target exists before linking. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update CMakeLists.txt to show message that mimalloc will be linked for Release only. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Added link option /INCLUDE:mi_version in cvutil/CMakeLists.txt only for MSVC Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Updated CMakeLists.txt to include platform specific import locations. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix generator expression in ExternalProject_Add for mimalloc (#8) * Initial plan * Replace $<CONFIG> with ${CMAKE_BUILD_TYPE} in ExternalProject_Add Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> * Use hardcoded Release build type for mimalloc ExternalProject Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> * Remove undefined ASAN_RUNTIME_DEP from runtime deps (#9) * Initial plan * Remove undefined ASAN_RUNTIME_DEP from EXTRA_RUNTIME_DEPS Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> * Remove undefined ASAN_RUNTIME_DEP from runtime deps (#10) * Initial plan * Remove undefined ASAN_RUNTIME_DEP from EXTRA_RUNTIME_DEPS expression Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> * Use $<IF> syntax for EXTRA_RUNTIME_DEPS generator expression Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: asn5d <16312669+asn5d@users.noreply.github.com> --------- Co-authored-by: Seethepalli <7uz@ornl.gov> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 3976872 commit 443d04a

5 files changed

Lines changed: 139 additions & 1 deletion

File tree

CMake/find_runtime_deps.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ if(unresolved)
8787
endif()
8888
endforeach()
8989
endif()
90+
91+
# Add any additional runtime dependencies that weren't auto-detected
92+
# This is passed as EXTRA_RUNTIME_DEPS variable
93+
if(DEFINED EXTRA_RUNTIME_DEPS)
94+
foreach(extra_dep IN LISTS EXTRA_RUNTIME_DEPS)
95+
if(EXISTS "${extra_dep}")
96+
get_filename_component(dep_name "${extra_dep}" NAME)
97+
set(dest "${INSTALL_BIN}/${dep_name}")
98+
if(NOT EXISTS "${dest}")
99+
file(INSTALL "${extra_dep}" DESTINATION "${INSTALL_BIN}")
100+
message(STATUS "Copied extra dependency: ${dep_name}")
101+
else()
102+
message(STATUS "Extra dependency already exists: ${dep_name}")
103+
endif()
104+
# Add to the runtime dependencies list
105+
file(APPEND "${BINARY_DIR}/cvutil_runtime_dependencies-${BUILD_CONFIG}.cmake"
106+
"set(CVUTIL_RUNTIME_DEPENDENCIES \"\${CVUTIL_RUNTIME_DEPENDENCIES};\${CMAKE_CURRENT_LIST_DIR}/../../../bin/${dep_name}\")\n")
107+
endif()
108+
endforeach()
109+
endif()

CMakeLists.txt

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2929
option(ENABLE_AVX512 "Enable AVX-512 support" OFF) # To be enabled on x86_64 server processors
3030
option(ENABLE_AVX2_FMA "Enable AVX2 and FMA support" ON) # Default support for x86_64 desktop processors
3131

32+
# Option to enable mimalloc for Release builds
33+
option(USE_MIMALLOC "Use mimalloc allocator for Release builds" ON)
34+
3235
# Detect the OS
3336
if(WIN32)
3437
message(STATUS "Configuring for Windows")
@@ -43,7 +46,7 @@ message(STATUS "Using ${CMAKE_CXX_COMPILER_ID} as the C++ compiler")
4346
# Create an interface library for compiler flags (modern CMake approach)
4447
add_library(cvutil_compiler_flags INTERFACE)
4548

46-
# MSVC-specific flags
49+
# MSVC-specific flags
4750
target_compile_options(cvutil_compiler_flags INTERFACE
4851
$<$<CXX_COMPILER_ID:MSVC>:
4952
$<$<CONFIG:Debug>:/MDd /Zi /Ob0 /Od /RTC1 /fsanitize=address>
@@ -97,6 +100,66 @@ target_link_options(cvutil_compiler_flags INTERFACE
97100
# endif()
98101
# endif()
99102

103+
# Build mimalloc for both Debug and Release builds using ExternalProject
104+
if(USE_MIMALLOC)
105+
include(ExternalProject)
106+
107+
message(STATUS "Building mimalloc as external project (will be linked for Release configuration)...")
108+
109+
# Determine build directory for mimalloc
110+
set(MIMALLOC_PREFIX ${CMAKE_BINARY_DIR}/_external/mimalloc)
111+
set(MIMALLOC_INSTALL_DIR ${MIMALLOC_PREFIX}/install)
112+
113+
# Use ExternalProject_Add for complete control - this excludes mimalloc from install
114+
# Build for both Debug (with ASAN tracking) and Release (without ASAN)
115+
ExternalProject_Add(
116+
mimalloc_external
117+
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
118+
GIT_TAG v2.2.4
119+
PREFIX ${MIMALLOC_PREFIX}
120+
CMAKE_ARGS
121+
-DCMAKE_BUILD_TYPE=Release
122+
-DMI_BUILD_SHARED=ON
123+
-DMI_BUILD_STATIC=OFF
124+
-DMI_BUILD_TESTS=OFF
125+
-DMI_BUILD_OBJECT=OFF
126+
-DMI_OVERRIDE=ON
127+
# $<$<CONFIG:Debug>:-DMI_TRACK_ASAN=ON>
128+
# $<$<CONFIG:Debug>:-DMI_SHOW_ERRORS=ON>
129+
-DCMAKE_INSTALL_PREFIX=${MIMALLOC_INSTALL_DIR}
130+
BUILD_BYPRODUCTS
131+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll>
132+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll>
133+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib>
134+
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.so>
135+
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dylib>
136+
)
137+
138+
# Create imported target for linking
139+
add_library(mimalloc SHARED IMPORTED GLOBAL)
140+
add_dependencies(mimalloc mimalloc_external)
141+
set_target_properties(mimalloc PROPERTIES
142+
IMPORTED_LOCATION
143+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll>
144+
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.so>
145+
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dylib>
146+
IMPORTED_IMPLIB
147+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib>
148+
)
149+
150+
# Create imported target for mimalloc-redirect
151+
add_library(mimalloc-redirect SHARED IMPORTED GLOBAL)
152+
add_dependencies(mimalloc-redirect mimalloc_external)
153+
set_target_properties(mimalloc-redirect PROPERTIES
154+
IMPORTED_LOCATION
155+
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll>
156+
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc-redirect.so>
157+
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc-redirect.dylib>
158+
)
159+
160+
message(STATUS "mimalloc external project configured")
161+
endif()
162+
100163
# Find OpenCV
101164
find_package(OpenCV REQUIRED)
102165

@@ -175,6 +238,17 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
175238
endif()
176239
endif()
177240

241+
# Install mimalloc DLLs if used (only runtime DLLs, no headers/cmake files)
242+
if(USE_MIMALLOC)
243+
install(FILES
244+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll
245+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll
246+
CONFIGURATIONS Release
247+
DESTINATION bin
248+
COMPONENT runtime
249+
)
250+
endif()
251+
178252
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
179253
# Set RPATH to include our bundled libraries
180254
# set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib/cvutil;\$ORIGIN/../lib;/usr/lib/x86_64-linux-gnu;/lib/x86_64-linux-gnu")
@@ -329,6 +403,10 @@ install(EXPORT cvutilTargets
329403
COMPONENT development
330404
)
331405

406+
# Note: mimalloc is NOT exported in cvutilTargets
407+
# It's an internal dependency that gets loaded automatically via DLL dependencies
408+
# Applications should NOT link to cvutil::mimalloc - it doesn't exist
409+
332410
# Add to your root CMakeLists.txt
333411
configure_file(
334412
${CMAKE_CURRENT_SOURCE_DIR}/CMake/copy_runtime_dependencies.cmake

PluginManager/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ target_link_libraries(PluginManager
5959
Qt6::OpenGL
6060
)
6161

62+
# Link mimalloc for Release builds only
63+
if(USE_MIMALLOC)
64+
if(TARGET mimalloc)
65+
target_link_libraries(PluginManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
66+
target_link_options(PluginManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
67+
message(STATUS "PluginManager will use mimalloc shared library for Release configuration")
68+
else()
69+
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
70+
endif()
71+
endif()
72+
6273
target_compile_definitions(PluginManager PRIVATE PLUGINMANAGER_SOURCE)
6374

6475
set(PUBLIC_HEADERS

RoiManager/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ target_link_libraries(RoiManager
5252
Qt6::OpenGL
5353
)
5454

55+
# Link mimalloc for Release builds only
56+
if(USE_MIMALLOC)
57+
if(TARGET mimalloc)
58+
target_link_libraries(RoiManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
59+
target_link_options(RoiManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
60+
message(STATUS "RoiManager will use mimalloc shared library for Release configuration")
61+
else()
62+
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
63+
endif()
64+
endif()
65+
5566
target_compile_definitions(RoiManager PRIVATE ROIMANAGER_SOURCE)
5667

5768
set(PUBLIC_HEADERS

cvutil/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ target_link_libraries(cvutil
120120
RoiManager
121121
)
122122

123+
# Link mimalloc for Release builds only
124+
if(USE_MIMALLOC)
125+
if(TARGET mimalloc)
126+
target_link_libraries(cvutil PRIVATE $<$<CONFIG:Release>:mimalloc>)
127+
# Force the linker to include mimalloc.dll as a dependency
128+
target_link_options(cvutil PRIVATE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/INCLUDE:mi_version>)
129+
message(STATUS "cvutil will use mimalloc shared library for Release configuration")
130+
else()
131+
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
132+
endif()
133+
endif()
134+
123135
target_compile_definitions(cvutil PRIVATE CVUTIL_SOURCE)
124136

125137
set(PUBLIC_HEADERS
@@ -233,11 +245,17 @@ endif()
233245
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
234246
# Build search directories list for runtime dependency detection
235247
set(RUNTIME_SEARCH_DIRS ${DEP_PATH})
248+
if(USE_MIMALLOC AND TARGET mimalloc)
249+
# Add mimalloc install directory to search paths
250+
list(APPEND RUNTIME_SEARCH_DIRS ${MIMALLOC_INSTALL_DIR}/bin)
251+
endif()
252+
236253
add_custom_command(TARGET cvutil POST_BUILD
237254
COMMAND ${CMAKE_COMMAND}
238255
-D TARGET_FILE=$<TARGET_FILE:cvutil>
239256
-D INSTALL_BIN=${CMAKE_INSTALL_PREFIX}/$<IF:$<BOOL:${WIN32}>,bin,lib/cvutil>
240257
-D SEARCH_DIRS="${RUNTIME_SEARCH_DIRS}"
258+
-D EXTRA_RUNTIME_DEPS="$<IF:$<AND:$<BOOL:${USE_MIMALLOC}>,$<CONFIG:Release>>,${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll;${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll,>"
241259
-D BUILD_CONFIG=$<CONFIG>
242260
-D BINARY_DIR=${CMAKE_BINARY_DIR}
243261
-P "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/find_runtime_deps.cmake"

0 commit comments

Comments
 (0)