cmake_minimum_required(VERSION 3.18)
project(moira_native LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
find_package(pybind11 CONFIG QUIET)

if(NOT pybind11_FOUND)
    execute_process(
        COMMAND "${Python3_EXECUTABLE}" -c "import pybind11; print(pybind11.get_cmake_dir())"
        RESULT_VARIABLE MOIRA_PYBIND11_DIR_RESULT
        OUTPUT_VARIABLE MOIRA_PYBIND11_DIR
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    if(MOIRA_PYBIND11_DIR_RESULT EQUAL 0 AND MOIRA_PYBIND11_DIR)
        list(PREPEND CMAKE_PREFIX_PATH "${MOIRA_PYBIND11_DIR}")
    endif()

    find_package(pybind11 CONFIG REQUIRED)
endif()

pybind11_add_module(_moira_native
    src/native/bindings/moira_native.cpp
    src/native/src/cpu_features.cpp
    src/native/src/lola.cpp
    src/native/src/nutation.cpp
    src/native/src/spk_file_mapping.cpp
    src/native/src/marriage_enclosure.cpp
    src/native/src/eclipse_strict_snapshot.cpp
)

# The marriage enclosure kernel preserves each Python reference rounding
# boundary. Keep contraction, reassociation and link-time rewriting disabled
# only for this translation unit; other native kernels retain their settings.
if(MSVC)
    set_source_files_properties(src/native/src/marriage_enclosure.cpp PROPERTIES
        COMPILE_OPTIONS "/fp:strict;/GL-")
else()
    set_source_files_properties(src/native/src/marriage_enclosure.cpp PROPERTIES
        COMPILE_OPTIONS "-ffp-contract=off;-fno-fast-math;-fno-lto")
endif()

# Python is the governing implementation and native code must reproduce its
# rounding. GCC and Clang contract a*b+c into a fused multiply-add whenever the
# target has FMA (any -march at or above x86-64-v3, or aarch64 by default),
# which changes the last bit of sums such as the Earth rotation angle and fails
# the 1e-12 parity tests. Forbid contraction for the whole module so parity does
# not depend on the build environment's default flags.
if(NOT MSVC)
    target_compile_options(_moira_native PRIVATE -ffp-contract=off)
endif()

# Keep the extension baseline portable and compile the AVX2 kernel as one
# separately dispatched translation unit.  This avoids making AVX2 a wheel-wide
# CPU requirement while still admitting it on capable x86 hosts.
include(CheckCXXCompilerFlag)

# The eclipse snapshot is an explicitly separate scalar numerical policy. Only
# its own translation unit is compiled with the attested strict contract; this
# does not attest or change the existing serving evaluator or AVX dispatch.
if(MSVC)
    check_cxx_compiler_flag("/fp:strict" MOIRA_ECLIPSE_HAS_FP_STRICT)
    check_cxx_compiler_flag("/GL-" MOIRA_ECLIPSE_HAS_NO_LTO)
    if(MOIRA_ECLIPSE_HAS_FP_STRICT AND MOIRA_ECLIPSE_HAS_NO_LTO)
        set_source_files_properties(src/native/src/eclipse_strict_snapshot.cpp PROPERTIES
            COMPILE_OPTIONS "/fp:strict;/GL-"
            COMPILE_DEFINITIONS "MOIRA_ECLIPSE_STRICT_SNAPSHOT_BUILD=1")
    endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    check_cxx_compiler_flag("-fno-fast-math" MOIRA_ECLIPSE_HAS_NO_FAST_MATH)
    check_cxx_compiler_flag("-ffp-contract=off" MOIRA_ECLIPSE_HAS_NO_CONTRACT)
    check_cxx_compiler_flag("-frounding-math" MOIRA_ECLIPSE_HAS_ROUNDING_MATH)
    check_cxx_compiler_flag("-fno-lto" MOIRA_ECLIPSE_HAS_NO_LTO)
    set(MOIRA_ECLIPSE_STRICT_FLAGS "-fno-fast-math;-ffp-contract=off;-frounding-math;-fno-lto")
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        check_cxx_compiler_flag("-fdenormal-fp-math=ieee" MOIRA_ECLIPSE_HAS_IEEE_DENORMAL)
        set(MOIRA_ECLIPSE_DENORMAL_OK ${MOIRA_ECLIPSE_HAS_IEEE_DENORMAL})
        list(APPEND MOIRA_ECLIPSE_STRICT_FLAGS "-fdenormal-fp-math=ieee")
    else()
        set(MOIRA_ECLIPSE_DENORMAL_OK TRUE)
    endif()
    if(MOIRA_ECLIPSE_HAS_NO_FAST_MATH AND MOIRA_ECLIPSE_HAS_NO_CONTRACT AND
       MOIRA_ECLIPSE_HAS_ROUNDING_MATH AND MOIRA_ECLIPSE_HAS_NO_LTO AND MOIRA_ECLIPSE_DENORMAL_OK)
        set_source_files_properties(src/native/src/eclipse_strict_snapshot.cpp PROPERTIES
            COMPILE_OPTIONS "${MOIRA_ECLIPSE_STRICT_FLAGS}"
            COMPILE_DEFINITIONS "MOIRA_ECLIPSE_STRICT_SNAPSHOT_BUILD=1")
    endif()
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|amd64|x86_64|X86_64|i[3-6]86)$")
    if(MSVC)
        set(MOIRA_AVX2_FLAG "/arch:AVX2")
    else()
        set(MOIRA_AVX2_FLAG "-mavx2")
    endif()
    check_cxx_compiler_flag("${MOIRA_AVX2_FLAG}" MOIRA_HAS_AVX2_COMPILE_FLAG)
    if(MOIRA_HAS_AVX2_COMPILE_FLAG)
        target_sources(_moira_native PRIVATE src/native/src/interpolation_avx2.cpp)
        set_source_files_properties(
            src/native/src/interpolation_avx2.cpp
            PROPERTIES COMPILE_OPTIONS "${MOIRA_AVX2_FLAG}"
        )
        target_compile_definitions(_moira_native PRIVATE MOIRA_NATIVE_AVX2_COMPILED=1)
    endif()
endif()

target_include_directories(_moira_native PRIVATE src/native/include)

set_target_properties(_moira_native PROPERTIES
    OUTPUT_NAME "_moira_native"
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN ON
)

if(MOIRA_EXTENSION_FULL_OUTPUT_PATH)
    get_filename_component(MOIRA_EXTENSION_OUTPUT_DIR "${MOIRA_EXTENSION_FULL_OUTPUT_PATH}" DIRECTORY)

    set_target_properties(_moira_native PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${MOIRA_EXTENSION_OUTPUT_DIR}"
        RUNTIME_OUTPUT_DIRECTORY "${MOIRA_EXTENSION_OUTPUT_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY "${MOIRA_EXTENSION_OUTPUT_DIR}"
        LIBRARY_OUTPUT_DIRECTORY_RELEASE "${MOIRA_EXTENSION_OUTPUT_DIR}"
        RUNTIME_OUTPUT_DIRECTORY_RELEASE "${MOIRA_EXTENSION_OUTPUT_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${MOIRA_EXTENSION_OUTPUT_DIR}"
        LIBRARY_OUTPUT_DIRECTORY_DEBUG "${MOIRA_EXTENSION_OUTPUT_DIR}"
        RUNTIME_OUTPUT_DIRECTORY_DEBUG "${MOIRA_EXTENSION_OUTPUT_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${MOIRA_EXTENSION_OUTPUT_DIR}"
    )
endif()

find_package(OpenMP QUIET)
if(OpenMP_CXX_FOUND)
    target_link_libraries(_moira_native PRIVATE OpenMP::OpenMP_CXX)
endif()

install(TARGETS _moira_native LIBRARY DESTINATION moira RUNTIME DESTINATION moira)
