Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 91 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ option(ENABLE_AVX2_FMA "Enable AVX2 and FMA support" ON) # Default support for x
# Option to enable mimalloc for Release builds
option(USE_MIMALLOC "Use mimalloc allocator for Release builds" ON)

# Disable x86-specific SIMD flags on non-x86 architectures (e.g., Apple Silicon arm64)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64|ARM64")
if(ENABLE_AVX512)
message(STATUS "Disabling AVX-512: not supported on ${CMAKE_SYSTEM_PROCESSOR}")
set(ENABLE_AVX512 OFF CACHE BOOL "Enable AVX-512 support" FORCE)
endif()
if(ENABLE_AVX2_FMA)
message(STATUS "Disabling AVX2/FMA: not supported on ${CMAKE_SYSTEM_PROCESSOR}, using SIMDe emulation")
set(ENABLE_AVX2_FMA OFF CACHE BOOL "Enable AVX2 and FMA support" FORCE)
endif()
set(CVUTIL_USE_SIMDE ON)
else()
set(CVUTIL_USE_SIMDE OFF)
endif()

# Detect the OS
if(WIN32)
message(STATUS "Configuring for Windows")
Expand Down Expand Up @@ -112,6 +127,20 @@ if(USE_MIMALLOC)

# Use ExternalProject_Add for complete control - this excludes mimalloc from install
# Build for both Debug (with ASAN tracking) and Release (without ASAN)
if(WIN32)
set(MIMALLOC_LIB_PATH ${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll)
set(MIMALLOC_BYPRODUCTS
${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll
${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll
${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib)
elseif(APPLE)
set(MIMALLOC_LIB_PATH ${MIMALLOC_INSTALL_DIR}/lib/libmimalloc.dylib)
set(MIMALLOC_BYPRODUCTS ${MIMALLOC_INSTALL_DIR}/lib/libmimalloc.dylib)
else()
set(MIMALLOC_LIB_PATH ${MIMALLOC_INSTALL_DIR}/lib/libmimalloc.so)
set(MIMALLOC_BYPRODUCTS ${MIMALLOC_INSTALL_DIR}/lib/libmimalloc.so)
endif()

ExternalProject_Add(
mimalloc_external
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
Expand All @@ -127,36 +156,23 @@ if(USE_MIMALLOC)
# $<$<CONFIG:Debug>:-DMI_TRACK_ASAN=ON>
# $<$<CONFIG:Debug>:-DMI_SHOW_ERRORS=ON>
-DCMAKE_INSTALL_PREFIX=${MIMALLOC_INSTALL_DIR}
BUILD_BYPRODUCTS
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll>
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll>
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib>
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.so>
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dylib>
BUILD_BYPRODUCTS ${MIMALLOC_BYPRODUCTS}
)

# Create imported target for linking
add_library(mimalloc SHARED IMPORTED GLOBAL)
add_dependencies(mimalloc mimalloc_external)
set_target_properties(mimalloc PROPERTIES
IMPORTED_LOCATION
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll>
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.so>
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dylib>
IMPORTED_IMPLIB
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib>
)

# Create imported target for mimalloc-redirect
add_library(mimalloc-redirect SHARED IMPORTED GLOBAL)
add_dependencies(mimalloc-redirect mimalloc_external)
set_target_properties(mimalloc-redirect PROPERTIES
IMPORTED_LOCATION
$<$<PLATFORM_ID:Windows>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll>
$<$<PLATFORM_ID:Linux>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc-redirect.so>
$<$<PLATFORM_ID:Darwin>:${MIMALLOC_INSTALL_DIR}/lib/mimalloc-redirect.dylib>
)

set_target_properties(mimalloc PROPERTIES IMPORTED_LOCATION ${MIMALLOC_LIB_PATH})
if(WIN32)
set_target_properties(mimalloc PROPERTIES
IMPORTED_IMPLIB ${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib)
# mimalloc-redirect is Windows-only (OSX uses zone interpose instead)
add_library(mimalloc-redirect SHARED IMPORTED GLOBAL)
add_dependencies(mimalloc-redirect mimalloc_external)
set_target_properties(mimalloc-redirect PROPERTIES
IMPORTED_LOCATION ${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll)
endif()

message(STATUS "mimalloc external project configured")
endif()

Expand All @@ -167,6 +183,15 @@ find_package(OpenCV REQUIRED)
find_package(Qt6 REQUIRED COMPONENTS Core Charts Gui Widgets OpenGL)
qt_standard_project_setup()

# Add SIMDe include path for portable SIMD on non-x86 platforms
if(CVUTIL_USE_SIMDE)
find_path(SIMDE_INCLUDE_DIR simde/x86/avx2.h REQUIRED
HINTS ${CMAKE_PREFIX_PATH} $ENV{CONDA_PREFIX}
PATH_SUFFIXES include)
message(STATUS "Found SIMDe headers: ${SIMDE_INCLUDE_DIR}")
target_include_directories(cvutil_compiler_flags INTERFACE ${SIMDE_INCLUDE_DIR})
endif()

message(STATUS "Found CMAKE_INSTALL_BINDIR: ${CMAKE_INSTALL_BINDIR}")
message(STATUS "Found CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}")

Expand Down Expand Up @@ -278,6 +303,46 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# foreach(qt_lib Qt6::Core Qt6::Widgets Qt6::Gui Qt6::Charts Qt6::OpenGL Qt6::OpenGLWidgets)
# install(FILES $<TARGET_FILE:${qt_lib}> CONFIGURATIONS Debug Release DESTINATION lib/cvutil COMPONENT runtime)
# endforeach()

elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CMAKE_INSTALL_RPATH "@loader_path/../lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set_target_properties(cvutil PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
MACOSX_RPATH ON
)

set_target_properties(PluginManager PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
MACOSX_RPATH ON
)

set_target_properties(RoiManager PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
MACOSX_RPATH ON
)

if(USE_MIMALLOC)
install(FILES
${MIMALLOC_INSTALL_DIR}/lib/libmimalloc.dylib
CONFIGURATIONS Release
DESTINATION lib
COMPONENT runtime
)
endif()

# Qt6 platform plugin for macOS (Cocoa)
# if(TARGET Qt6::QCocoaIntegrationPlugin)
# install(FILES $<TARGET_FILE:Qt6::QCocoaIntegrationPlugin>
# CONFIGURATIONS Debug Release
# DESTINATION lib/platforms
# COMPONENT runtime)
# endif()

endif()

# Install PDB files for OpenCV libraries
Expand Down
19 changes: 18 additions & 1 deletion PluginManager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ target_link_libraries(PluginManager
if(USE_MIMALLOC)
if(TARGET mimalloc)
target_link_libraries(PluginManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
target_link_options(PluginManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
target_link_options(PluginManager PRIVATE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/INCLUDE:mi_version>)
message(STATUS "PluginManager will use mimalloc shared library for Release configuration")
else()
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
Expand Down Expand Up @@ -152,6 +152,23 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
install(TARGETS PluginManager
EXPORT cvutilTargets
CONFIGURATIONS Debug Release
RUNTIME
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE
COMPONENT development
DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
endif()

# Install public headers
Expand Down
19 changes: 18 additions & 1 deletion RoiManager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ target_link_libraries(RoiManager
if(USE_MIMALLOC)
if(TARGET mimalloc)
target_link_libraries(RoiManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
target_link_options(RoiManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
target_link_options(RoiManager PRIVATE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/INCLUDE:mi_version>)
message(STATUS "RoiManager will use mimalloc shared library for Release configuration")
else()
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
Expand Down Expand Up @@ -142,6 +142,23 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
install(TARGETS RoiManager
EXPORT cvutilTargets
CONFIGURATIONS Debug Release
RUNTIME
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE
COMPONENT development
DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
endif()

# Install public headers
Expand Down
17 changes: 17 additions & 0 deletions cvutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
install(TARGETS cvutil
EXPORT cvutilTargets
CONFIGURATIONS Debug Release
RUNTIME
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY
COMPONENT runtime
DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE
COMPONENT development
DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER
COMPONENT development
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cvutil
)
endif()

# Install public headers
Expand Down
8 changes: 7 additions & 1 deletion cvutil/stdproto.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ along with cvutil; see the file COPYING. If not, see
#include <thread>
#include <sys/stat.h>

// Defaulting to Intel's AVX2 and FMA
// Use native Intel intrinsics on x86/x64; SIMDe emulation on other architectures (e.g., Apple Silicon)
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#include <immintrin.h>
#else
#define SIMDE_ENABLE_NATIVE_ALIASES
#include <simde/x86/avx2.h>
#include <simde/x86/fma.h>
#endif

// #if defined(_MSC_VER) && !defined(__clang__)
// #define M2I32(x, idx) (x).m256i_i32[(idx)]
Expand Down