# Python package raytatouille (ADR 0002, ADR 0018): extension module raytatouille._core built
# with nanobind, the pure Python modules from python/raytatouille and generated type stubs.
#
# nanobind is a Python build dependency (pyproject.toml, ADR 0018); its CMake package is found
# through the interpreter that builds the package.

find_package(Python 3.10 REQUIRED COMPONENTS Interpreter Development.Module)
execute_process(
  COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE
  OUTPUT_VARIABLE nanobind_ROOT
  RESULT_VARIABLE rtt_nanobind_result)
if(NOT rtt_nanobind_result EQUAL 0)
  message(FATAL_ERROR "nanobind not found for ${Python_EXECUTABLE}; "
                      "pip install -r libs/rtt-py/tests/requirements.txt")
endif()
find_package(nanobind CONFIG REQUIRED)

# Package directory in the build tree: _core, the Python modules and the stubs side by side, so
# that pytest can import raytatouille from here (PYTHONPATH = RTT_PY_BUILD_DIR).
set(RTT_PY_BUILD_DIR "${CMAKE_BINARY_DIR}/python")
set(RTT_PY_PACKAGE_DIR "${RTT_PY_BUILD_DIR}/raytatouille")

# NB_SUPPRESS_WARNINGS: nanobind and Python headers as system includes, so that the project
# warnings (-Wold-style-cast, ...) apply to our code only.
nanobind_add_module(_core NB_STATIC NB_SUPPRESS_WARNINGS
  src/module.cpp
  src/errors.cpp
  src/bind_model.cpp
  src/bind_model_tree.cpp
  src/bind_edit.cpp
  src/bind_compile.cpp
  src/bind_material.cpp
  src/bind_paraxial.cpp
  src/bind_trace.cpp
  src/bind_analysis.cpp src/bind_pupil.cpp src/bind_psf.cpp
  src/bind_jones.cpp
  src/bind_paths.cpp
  src/bind_irradiance.cpp
  src/bind_reports.cpp
  src/bind_optim.cpp
  src/bind_tolerance.cpp
  src/bind_polar.cpp
  src/bind_results.cpp
  src/polar_batch.cpp
  src/bind_layout.cpp)
target_link_libraries(_core PRIVATE
  rtt::io rtt::diagnostics rtt::model rtt::material rtt::coating rtt::compile rtt::paraxial
  rtt::trace rtt::analysis rtt::optim rtt::tolerance
  rtt::polar TBB::tbb)
# LIBRARY: clang-tidy checks the bindings like the libraries (#336); the preset ci-linux-clang
# builds the Python package for this.
rtt_target_defaults(_core LIBRARY)
# The $<1:...> keeps multi-config generators from appending a configuration directory.
set_target_properties(_core PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY "$<1:${RTT_PY_PACKAGE_DIR}>"
  RUNTIME_OUTPUT_DIRECTORY "$<1:${RTT_PY_PACKAGE_DIR}>")

set(RTT_PY_SOURCES
  python/raytatouille/__init__.py
  python/raytatouille/_util.py
  python/raytatouille/analysis.py
  python/raytatouille/diagnostics.py
  python/raytatouille/edit.py
  python/raytatouille/errors.py
  python/raytatouille/layout.py
  python/raytatouille/materials.py
  python/raytatouille/model.py
  python/raytatouille/optim.py
  python/raytatouille/paraxial.py
  python/raytatouille/plot.py
  python/raytatouille/polar.py
  python/raytatouille/reports.py
  python/raytatouille/results.py
  python/raytatouille/schemas.py
  python/raytatouille/tolerance.py
  python/raytatouille/trace.py
  python/raytatouille/py.typed)
set(RTT_PY_COPIES "")
foreach(file IN LISTS RTT_PY_SOURCES)
  get_filename_component(name "${file}" NAME)
  add_custom_command(
    OUTPUT "${RTT_PY_PACKAGE_DIR}/${name}"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${RTT_PY_PACKAGE_DIR}/${name}"
    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  list(APPEND RTT_PY_COPIES "${RTT_PY_PACKAGE_DIR}/${name}")
endforeach()
# The JSON schemas of the file formats (schema/, the only source) in the package (#386):
# raytatouille/schema/*.schema.json in the build tree and in the wheel.
set(RTT_PY_SCHEMAS
  "${PROJECT_SOURCE_DIR}/schema/raytatouille.schema.json"
  "${PROJECT_SOURCE_DIR}/schema/raytatouille-result.schema.json"
  "${PROJECT_SOURCE_DIR}/schema/raytatouille-coatings.schema.json")
foreach(file IN LISTS RTT_PY_SCHEMAS)
  get_filename_component(name "${file}" NAME)
  add_custom_command(
    OUTPUT "${RTT_PY_PACKAGE_DIR}/schema/${name}"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${RTT_PY_PACKAGE_DIR}/schema"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${file}" "${RTT_PY_PACKAGE_DIR}/schema/${name}"
    DEPENDS "${file}")
  list(APPEND RTT_PY_COPIES "${RTT_PY_PACKAGE_DIR}/schema/${name}")
endforeach()
add_custom_target(rtt_py_package ALL DEPENDS ${RTT_PY_COPIES})
# Whoever builds _core (CI builds only named targets) gets the complete package in the build
# tree; the stubs no longer depend on it (stubgen uses the staging package below).
add_dependencies(_core rtt_py_package)

# TBB is a shared library; on Windows its DLL has to sit next to _core. Only TBB: the runtime
# DLLs of _core also include python3*.dll, which must not be copied into the package.
if(WIN32)
  add_custom_command(TARGET _core POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "$<TARGET_FILE:TBB::tbb>" "${RTT_PY_PACKAGE_DIR}")
endif()

# The stubs depend on _core only: stubgen imports raytatouille._core from a staging package
# that holds nothing but _core (and the TBB DLL) and an empty __init__.py, so the package's
# own __init__ with its runtime imports (numpy, ...) is not loaded from the build tree. The
# build environment of pip install . has no numpy (a runtime dependency only).
set(RTT_PY_STUBGEN_DIR "${CMAKE_BINARY_DIR}/stubgen")
add_custom_command(TARGET _core POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E make_directory "${RTT_PY_STUBGEN_DIR}/raytatouille"
  COMMAND ${CMAKE_COMMAND} -E touch "${RTT_PY_STUBGEN_DIR}/raytatouille/__init__.py"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
          "$<TARGET_FILE:_core>" "${RTT_PY_STUBGEN_DIR}/raytatouille")
if(WIN32)
  add_custom_command(TARGET _core POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "$<TARGET_FILE:TBB::tbb>" "${RTT_PY_STUBGEN_DIR}/raytatouille")
endif()

# stubgen runs with -S -E instead of through nanobind_add_stub (#271): without site, no .pth
# file of the target environment runs, so a meta path finder of an earlier editable install
# (scikit-build-core puts it at sys.meta_path[0], also under pip's build isolation) cannot
# redirect raytatouille to the source tree or to an old _core; -E ignores PYTHONPATH. Everything
# the run imports therefore has to sit in the staging directory, which is also where stubgen.py
# runs from: Python puts the directory of the script at sys.path[0], and stubgen.py imports
# typing_extensions at module level on Python < 3.11, before main() handles -i. A copy of
# stubgen.py in the staging directory makes that directory visible from the start (one code
# path for all versions). stubgen.py imports only the standard library otherwise (no imports
# from the nanobind package; nanobind 2.13.0), as does _core. typing_extensions is copied there
# on Python < 3.11 only (below). The staging directory is not installed (install() below takes
# _core.pyi from RTT_PY_PACKAGE_DIR), so neither copy reaches the wheel.
#
# The arguments are those of nanobind_add_stub (-q, -i, -m, -o) and -P for INCLUDE_PRIVATE:
# stubgen takes a name with one trailing underscore for private and leaves it out, but yield_
# (MonteCarloResult, CriterionStatistics) only avoids the keyword (PEP 8); the stub then also
# lists MaterialLibrary._glass_records. The paths of stubgen.py and of the interpreter are found
# and normalized as there. Not taken over: the sanitizer preload (LD_PRELOAD) of
# nanobind_add_stub, harmless since no preset combines the sanitizers with RTT_BUILD_PYTHON.
if(EXISTS "${NB_DIR}/src/stubgen.py")
  set(rtt_nanobind_stubgen "${NB_DIR}/src/stubgen.py")
elseif(EXISTS "${NB_DIR}/stubgen.py")
  set(rtt_nanobind_stubgen "${NB_DIR}/stubgen.py")
else()
  message(FATAL_ERROR "stubgen.py of nanobind not found in ${NB_DIR}")
endif()
set(RTT_PY_STUBGEN "${RTT_PY_STUBGEN_DIR}/stubgen.py")
add_custom_command(
  OUTPUT "${RTT_PY_STUBGEN}"
  COMMAND ${CMAKE_COMMAND} -E make_directory "${RTT_PY_STUBGEN_DIR}"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "${rtt_nanobind_stubgen}" "${RTT_PY_STUBGEN}"
  DEPENDS "${rtt_nanobind_stubgen}"
  VERBATIM)
set(RTT_PY_STUBGEN_DEPENDS _core "${RTT_PY_STUBGEN}")
if(Python_VERSION VERSION_LESS 3.11)
  # typing_extensions is there in every build environment: scikit-build-core, pinned in
  # pyproject.toml and requirements.txt, declares "Requires-Dist: typing-extensions>=4;
  # python_version < '3.11'" (METADATA of 1.1.1). Only this one module is copied, not the whole
  # site-packages: nothing else becomes importable, and the stub does not depend on what else is
  # installed.
  execute_process(
    COMMAND "${Python_EXECUTABLE}" -c "import typing_extensions; print(typing_extensions.__file__)"
    OUTPUT_STRIP_TRAILING_WHITESPACE
    OUTPUT_VARIABLE rtt_typing_extensions
    RESULT_VARIABLE rtt_typing_extensions_result)
  if(NOT rtt_typing_extensions_result EQUAL 0)
    message(FATAL_ERROR "typing_extensions not found for ${Python_EXECUTABLE} "
                        "(needed by nanobind's stubgen on Python < 3.11); "
                        "pip install -r libs/rtt-py/tests/requirements.txt")
  endif()
  file(TO_CMAKE_PATH "${rtt_typing_extensions}" rtt_typing_extensions)
  add_custom_command(
    OUTPUT "${RTT_PY_STUBGEN_DIR}/typing_extensions.py"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${RTT_PY_STUBGEN_DIR}"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${rtt_typing_extensions}" "${RTT_PY_STUBGEN_DIR}/typing_extensions.py"
    DEPENDS "${rtt_typing_extensions}"
    VERBATIM)
  list(APPEND RTT_PY_STUBGEN_DEPENDS "${RTT_PY_STUBGEN_DIR}/typing_extensions.py")
endif()
file(TO_CMAKE_PATH "${Python_EXECUTABLE}" RTT_PY_STUBGEN_PYTHON)
add_custom_command(
  OUTPUT "${RTT_PY_PACKAGE_DIR}/_core.pyi"
  COMMAND "${RTT_PY_STUBGEN_PYTHON}" -S -E "${RTT_PY_STUBGEN}" -q -P
          -i "${RTT_PY_STUBGEN_DIR}" -m raytatouille._core -o "${RTT_PY_PACKAGE_DIR}/_core.pyi"
  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  DEPENDS ${RTT_PY_STUBGEN_DEPENDS}
  VERBATIM)
add_custom_target(rtt_py_stub ALL DEPENDS "${RTT_PY_PACKAGE_DIR}/_core.pyi")

# Installation into the wheel (scikit-build-core): the pure Python modules come from
# wheel.packages in pyproject.toml.
install(TARGETS _core LIBRARY DESTINATION raytatouille RUNTIME DESTINATION raytatouille)
install(FILES "${RTT_PY_PACKAGE_DIR}/_core.pyi" DESTINATION raytatouille)
install(FILES ${RTT_PY_SCHEMAS} DESTINATION raytatouille/schema)
if(WIN32)
  install(FILES "$<TARGET_FILE:TBB::tbb>" DESTINATION raytatouille)
endif()

if(RTT_BUILD_TESTS)
  # Writes the C++ results of the cases in tests/test_bitwise*.py as .npy files.
  # src/polar_batch.cpp: the same polarization quantities as raytatouille.polar (#62).
  add_executable(rtt_py_reference tests/rtt_py_reference.cpp tests/analysis_cases.cpp
    tests/paths_cases.cpp tests/diffraction_cases.cpp tests/crystal_cases.cpp
    tests/configuration_cases.cpp tests/reports_cases.cpp tests/optim_cases.cpp
    tests/ideal_lens_cases.cpp tests/jones_cases.cpp tests/m6_cases.cpp tests/pupil_cases.cpp
    tests/psf_cases.cpp tests/q_cases.cpp tests/tolerance_cases.cpp tests/m7_cases.cpp
    tests/anamorphic_cases.cpp tests/rotational_cases.cpp
    tests/forbes_cases.cpp
    src/polar_batch.cpp)
  target_include_directories(rtt_py_reference PRIVATE src)
  target_link_libraries(rtt_py_reference PRIVATE
    rtt::io rtt::material rtt::coating rtt::compile rtt::paraxial rtt::trace rtt::analysis
    rtt::optim rtt::polar rtt::tolerance TBB::tbb)
  rtt_target_defaults(rtt_py_reference)

  add_test(NAME rtt_py_pytest
    COMMAND "${Python_EXECUTABLE}" -m pytest -q "${CMAKE_CURRENT_SOURCE_DIR}/tests"
            -p no:cacheprovider)
  # MPLBACKEND=Agg: the examples plot without a display, whatever the test order (#35, #55).
  set_tests_properties(rtt_py_pytest PROPERTIES
    ENVIRONMENT
      "MPLBACKEND=Agg;PYTHONPATH=${RTT_PY_BUILD_DIR};RTT_PY_REFERENCE_EXE=$<TARGET_FILE:rtt_py_reference>;RTT_REFERENCE_DIR=${RTT_REFERENCE_DIR};RTT_CATALOG_DIR=${PROJECT_SOURCE_DIR}/tests/catalogs")
  add_test(NAME rtt_py_mypy
    COMMAND "${Python_EXECUTABLE}" -m mypy --config-file "${PROJECT_SOURCE_DIR}/pyproject.toml"
            "${CMAKE_CURRENT_SOURCE_DIR}/tests" "${RTT_PY_PACKAGE_DIR}")
  set_tests_properties(rtt_py_mypy PROPERTIES
    ENVIRONMENT "MYPYPATH=${RTT_PY_BUILD_DIR}")
endif()
