cmake_minimum_required(VERSION 3.21)
project(bioimage_cpp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Python 3.10 COMPONENTS Interpreter Development.Module REQUIRED)
find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(_core
    NB_STATIC
    src/bindings/affinities.cxx
    src/bindings/blocking.cxx
    src/bindings/distance.cxx
    src/bindings/module.cxx
    src/bindings/filters.cxx
    src/bindings/flow.cxx
    src/bindings/graph.cxx
    src/bindings/graph_contraction.cxx
    src/bindings/ground_truth.cxx
    src/bindings/label_multiset.cxx
    src/bindings/mesh.cxx
    src/bindings/segmentation.cxx
    src/bindings/skeleton.cxx
    src/bindings/skeleton_distributed.cxx
    src/bindings/transformation.cxx
    src/bindings/util.cxx
    src/bindings/utils.cxx
    src/cpp/segmentation/mutex_watershed.cxx
    src/cpp/segmentation/semantic_mutex_watershed.cxx
    src/cpp/take_dict.cxx
)

target_include_directories(_core PRIVATE include)
target_compile_features(_core PRIVATE cxx_std_20)
target_compile_options(_core PRIVATE
    $<$<NOT:$<CONFIG:Debug>>:-O3>
)

# Target architecture for the ISA-specific kernels. CMAKE_SYSTEM_PROCESSOR is
# the host processor, so it is wrong for a cross build:
# - macOS selects the target through CMAKE_OSX_ARCHITECTURES (cibuildwheel
#   builds the x86_64 wheel on an arm64 runner),
# - MSVC selects it through the generator platform (-A ARM64).
# A multi-architecture build (universal2) gets no ISA-specific kernel.
if(APPLE AND CMAKE_OSX_ARCHITECTURES)
    list(LENGTH CMAKE_OSX_ARCHITECTURES _bioimage_n_archs)
    if(_bioimage_n_archs EQUAL 1)
        set(_bioimage_target_arch "${CMAKE_OSX_ARCHITECTURES}")
    else()
        set(_bioimage_target_arch "multiple")
    endif()
elseif(MSVC AND CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
    set(_bioimage_target_arch "${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
else()
    set(_bioimage_target_arch "${CMAKE_SYSTEM_PROCESSOR}")
endif()
string(TOLOWER "${_bioimage_target_arch}" _bioimage_target_arch)
set(_bioimage_target_x86 OFF)
set(_bioimage_target_arm64 OFF)
if(_bioimage_target_arch MATCHES "^(x86_64|amd64|x64|i[3-6]86|x86)$")
    set(_bioimage_target_x86 ON)
elseif(_bioimage_target_arch MATCHES "^(arm64|aarch64)$")
    set(_bioimage_target_arm64 ON)
endif()
message(STATUS "bioimage_cpp: ISA-specific kernels target '${_bioimage_target_arch}'")

option(
    BIOIMAGE_FLOW_FMA_DISPATCH
    "Build an x86 FMA-specialized flow tracer with runtime CPU dispatch"
    ON
)
if(BIOIMAGE_FLOW_FMA_DISPATCH)
    if(_bioimage_target_x86)
        if(MSVC)
            target_sources(_core PRIVATE src/cpp/flow/flow_density_fma.cxx)
            set_property(
                SOURCE src/cpp/flow/flow_density_fma.cxx
                APPEND PROPERTY COMPILE_OPTIONS /arch:AVX2
            )
            target_compile_definitions(
                _core PRIVATE
                BIOIMAGE_FLOW_FMA_DISPATCH=1
                BIOIMAGE_FLOW_FMA_REQUIRES_AVX2=1
            )
        elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
            target_sources(_core PRIVATE src/cpp/flow/flow_density_fma.cxx)
            set_property(
                SOURCE src/cpp/flow/flow_density_fma.cxx
                APPEND PROPERTY COMPILE_OPTIONS -mavx -mfma
            )
            target_compile_definitions(_core PRIVATE BIOIMAGE_FLOW_FMA_DISPATCH=1)
        endif()
    endif()
endif()

option(
    BIOIMAGE_FLOW_NEON
    "Build the arm64 NEON packed flow tracer (NEON+FMA are baseline on arm64)"
    ON
)
if(BIOIMAGE_FLOW_NEON AND _bioimage_target_arm64 AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_sources(_core PRIVATE src/cpp/flow/flow_density_neon.cxx)
    target_compile_definitions(_core PRIVATE BIOIMAGE_FLOW_NEON=1)
endif()

option(
    BIOIMAGE_FILTERS_AVX2_DISPATCH
    "Build x86 AVX2/FMA filter kernels with runtime CPU dispatch"
    ON
)
if(BIOIMAGE_FILTERS_AVX2_DISPATCH)
    if(_bioimage_target_x86)
        if(MSVC)
            target_sources(
                _core PRIVATE
                src/cpp/filters/dispatch.cxx
                src/cpp/filters/convolve_avx2.cxx
                src/cpp/filters/eigenvalues_avx2.cxx
            )
            set_property(
                SOURCE
                    src/cpp/filters/convolve_avx2.cxx
                    src/cpp/filters/eigenvalues_avx2.cxx
                APPEND PROPERTY COMPILE_OPTIONS /arch:AVX2
            )
            target_compile_definitions(_core PRIVATE BIOIMAGE_FILTERS_AVX2_DISPATCH=1)
        elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
            target_sources(
                _core PRIVATE
                src/cpp/filters/dispatch.cxx
                src/cpp/filters/convolve_avx2.cxx
                src/cpp/filters/eigenvalues_avx2.cxx
            )
            set_property(
                SOURCE
                    src/cpp/filters/convolve_avx2.cxx
                    src/cpp/filters/eigenvalues_avx2.cxx
                APPEND PROPERTY COMPILE_OPTIONS -mavx2 -mfma
            )
            target_compile_definitions(_core PRIVATE BIOIMAGE_FILTERS_AVX2_DISPATCH=1)
        endif()
    endif()
endif()

option(BIOIMAGE_PROFILE "Enable per-phase profiling instrumentation (development only)" OFF)
if(BIOIMAGE_PROFILE)
    target_compile_definitions(_core PRIVATE BIOIMAGE_PROFILE)
endif()

install(TARGETS _core LIBRARY DESTINATION bioimage_cpp)
