cmake_minimum_required(VERSION 3.26)
project(chopper-calculations)

set(CMAKE_CXX_STANDARD 20)
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_LIST_DIR}/cmake")

# Warn if the user invokes CMake directly
if (NOT SKBUILD)
    message(WARNING "\
  This CMake file is meant to be executed using 'scikit-build-core'.
  Running it directly will almost certainly not produce the desired
  result. If you are a user trying to install this package, use the
  command below, which will install all necessary build dependencies,
  compile the package in an isolated environment, and then install it.
  =====================================================================
   $ pip install .
  =====================================================================
  If you are a software developer, and this is your own package, then
  it is usually much more efficient to install the build dependencies
  in your environment once and use the following command that avoids
  a costly creation of a new virtual environment at every compilation:
  =====================================================================
   $ pip install nanobind scikit-build-core[pyproject]
   $ pip install --no-build-isolation -ve .
  =====================================================================
  You may optionally add -Ceditable.rebuild=true to auto-rebuild when
  the package is imported. Otherwise, you need to rerun the above
  after editing C++ files.")
endif()

find_package(Python 3.8 COMPONENTS
        REQUIRED COMPONENTS Interpreter Development.Module
        OPTIONAL_COMPONENTS Development.SABIModule
        )

if (NOT SKBUILD)
    execute_process(
            COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
            OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE NB_DIR)
    list(APPEND CMAKE_PREFIX_PATH "${NB_DIR}")
endif()

include(fetcher)
git_fetch(chopper_lib 4.3.0 https://github.com/mcdotstar/mcstas-chopper-lib.git False)

# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(
        # Name of the extension
        _chopcal_impl

        # Target the stable ABI for Python 3.12+, which reduces
        # the number of binary wheels that must be built. This
        # does nothing on older Python versions
        STABLE_ABI

        # Source code goes here
        src/chopcal.cpp
        src/choppers.cpp
        ${chopper_lib_SOURCE_DIR}/chopper-lib.c
)
target_include_directories(_chopcal_impl PRIVATE ${chopper_lib_SOURCE_DIR})

nanobind_add_module(_chopper_lib_impl
        STABLE_ABI
        src/mcstas_chopper_lib.cpp
        ${chopper_lib_SOURCE_DIR}/chopper-lib.c
)
target_include_directories(_chopper_lib_impl PRIVATE ${chopper_lib_SOURCE_DIR})

# chopper-lib.c refuses to compile without V2K, K2V and PI since 4.0.0; it used to define
# its own behind #ifndef. These are the McStas runtime's values, cached by the library for
# consumers like this one, which compile the source into a target of their own rather than
# linking chopper_lib. They are deliberately not chopcal::constants -- those are derived
# from SI/CODATA, and agreeing with McStas is the point of these.
#
# On the source file rather than the targets, because chopcal::constants declares
# variables named PI, V2K and K2V: as target definitions these macros reach the C++ too
# and mangle every one of those declarations. Only the C file needs them, and this is the
# same reason chopper-lib's own CMakeLists keeps them PRIVATE.
set_source_files_properties(${chopper_lib_SOURCE_DIR}/chopper-lib.c PROPERTIES
        COMPILE_DEFINITIONS "${CHOPPER_LIB_DEFINITIONS}")

# The bindings convert between wavelength and inverse velocity, and have to do it with
# the same numbers the library does or the two disagree in the eighth digit. Same source
# of truth, re-emitted under prefixed names so the C++ can read them without PI, V2K and
# K2V colliding with the chopcal::constants variables of those names.
set(CHOPCAL_RUNTIME_DEFINITIONS)
foreach (definition IN LISTS CHOPPER_LIB_DEFINITIONS)
    if (definition MATCHES "^([A-Za-z0-9_]+)=(.*)$")
        list(APPEND CHOPCAL_RUNTIME_DEFINITIONS
                "CHOPPER_LIB_${CMAKE_MATCH_1}=${CMAKE_MATCH_2}")
    endif ()
endforeach ()
target_compile_definitions(_chopper_lib_impl PRIVATE ${CHOPCAL_RUNTIME_DEFINITIONS})

# Install directive for scikit-build-core
install(TARGETS _chopcal_impl LIBRARY DESTINATION chopcal)
install(TARGETS _chopper_lib_impl LIBRARY DESTINATION chopcal)

#if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
#    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
#    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
#endif()
#
## Detect the installed nanobind package and import it into CMake
#execute_process(
#        COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
#        OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE NB_DIR)
#list(APPEND CMAKE_PREFIX_PATH "${NB_DIR}")
#find_package(nanobind CONFIG REQUIRED)
#
#add_subdirectory(src)
