@@ -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,57 @@ 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 Debug and Release configurations)..." )
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=$<CONFIG >
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+ ${MIMALLOC_INSTALL_DIR} /bin/mimalloc.dll
132+ ${MIMALLOC_INSTALL_DIR} /bin/mimalloc-redirect.dll
133+ ${MIMALLOC_INSTALL_DIR} /lib/mimalloc.dll.lib
134+ )
135+
136+ # Create imported target for linking
137+ add_library (mimalloc SHARED IMPORTED GLOBAL )
138+ add_dependencies (mimalloc mimalloc_external )
139+ set_target_properties (mimalloc PROPERTIES
140+ IMPORTED_LOCATION ${MIMALLOC_INSTALL_DIR} /bin/mimalloc.dll
141+ IMPORTED_IMPLIB ${MIMALLOC_INSTALL_DIR} /lib/mimalloc.dll.lib
142+ )
143+
144+ # Create imported target for mimalloc-redirect
145+ add_library (mimalloc-redirect SHARED IMPORTED GLOBAL )
146+ add_dependencies (mimalloc-redirect mimalloc_external )
147+ set_target_properties (mimalloc-redirect PROPERTIES
148+ IMPORTED_LOCATION ${MIMALLOC_INSTALL_DIR} /bin/mimalloc-redirect.dll
149+ )
150+
151+ message (STATUS "mimalloc external project configured" )
152+ endif ()
153+
100154# Find OpenCV
101155find_package (OpenCV REQUIRED )
102156
@@ -175,6 +229,17 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
175229 endif ()
176230 endif ()
177231
232+ # Install mimalloc DLLs if used (only runtime DLLs, no headers/cmake files)
233+ if (USE_MIMALLOC)
234+ install (FILES
235+ ${MIMALLOC_INSTALL_DIR} /bin/mimalloc.dll
236+ ${MIMALLOC_INSTALL_DIR} /bin/mimalloc-redirect.dll
237+ CONFIGURATIONS Release
238+ DESTINATION bin
239+ COMPONENT runtime
240+ )
241+ endif ()
242+
178243elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
179244 # Set RPATH to include our bundled libraries
180245 # set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib/cvutil;\$ORIGIN/../lib;/usr/lib/x86_64-linux-gnu;/lib/x86_64-linux-gnu")
@@ -329,6 +394,10 @@ install(EXPORT cvutilTargets
329394 COMPONENT development
330395)
331396
397+ # Note: mimalloc is NOT exported in cvutilTargets
398+ # It's an internal dependency that gets loaded automatically via DLL dependencies
399+ # Applications should NOT link to cvutil::mimalloc - it doesn't exist
400+
332401# Add to your root CMakeLists.txt
333402configure_file (
334403 ${CMAKE_CURRENT_SOURCE_DIR } /CMake/copy_runtime_dependencies.cmake
0 commit comments