@@ -29,6 +29,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2929option (ENABLE_AVX512 "Enable AVX-512 support" OFF ) # To be enabled on x86_64 server processors
3030option (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
3336if (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)
4447add_library (cvutil_compiler_flags INTERFACE )
4548
46- # MSVC-specific flags
49+ # MSVC-specific flags
4750target_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
101164find_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+
178252elseif (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
333411configure_file (
334412 ${CMAKE_CURRENT_SOURCE_DIR } /CMake/copy_runtime_dependencies.cmake
0 commit comments