cmake_minimum_required(VERSION 3.16)
project(synkit_canon VERSION 1.7.0 LANGUAGES CXX)
add_library(synkit_canon SHARED src/canonical.cpp src/stereo_match.cpp src/configured.cpp src/arrangements.cpp)
target_compile_definitions(synkit_canon PRIVATE SYNKIT_CANON_BUILD)
target_compile_features(synkit_canon PUBLIC cxx_std_17)
target_include_directories(synkit_canon PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)
set_target_properties(synkit_canon PROPERTIES CXX_EXTENSIONS OFF)
if(MSVC)
  target_compile_options(synkit_canon PRIVATE /W4)
else()
  target_compile_options(synkit_canon PRIVATE -Wall -Wextra -Wpedantic)
endif()
option(SYNKIT_STEREO_BUILD_EXPERIMENTS "Build native experiment runner (JSON SDK required)" OFF)
if(SYNKIT_STEREO_WITH_RDKIT AND NOT SYNKIT_STEREO_BUILD_EXPERIMENTS)
  message(FATAL_ERROR "The SDK-backed science targets require SYNKIT_STEREO_BUILD_EXPERIMENTS=ON")
endif()
if(SYNKIT_STEREO_BUILD_EXPERIMENTS)
  find_path(SYNKIT_JSON_INCLUDE_DIR nlohmann/json.hpp REQUIRED)
  add_executable(stereo_experiments examples/stereo_experiments.cpp)
  set(native_manifest "{\"compiler\":\"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}\",\"build_type\":\"${CMAKE_BUILD_TYPE}\"}")
  file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/native_build_info.hpp"
    "#pragma once\nconstexpr const char *synkit_native_build_info=R\"manifest(${native_manifest})manifest\";\n")
  target_include_directories(stereo_experiments PRIVATE ${SYNKIT_JSON_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
  target_link_libraries(stereo_experiments PRIVATE synkit_canon)
  option(SYNKIT_STEREO_WITH_RDKIT "Enable native molecular input/resonance preparation" OFF)
  if(SYNKIT_STEREO_WITH_RDKIT)
    if(POLICY CMP0167)
      cmake_policy(SET CMP0167 NEW)
    endif()
    find_package(rdkit CONFIG REQUIRED)
    find_package(Eigen3 CONFIG REQUIRED)
    add_library(synkit_science STATIC examples/rdkit_preparation.cpp src/science/cip.cpp
      src/science/coordinates.cpp src/science/perception.cpp src/science/coordinate_assistance.cpp
      src/science/global.cpp src/science/coordination.cpp src/science/local_orders.cpp)
    target_compile_features(synkit_science PUBLIC cxx_std_20)
    target_include_directories(synkit_science PUBLIC ${RDKit_INCLUDE_DIRS} ${SYNKIT_JSON_INCLUDE_DIR}
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/examples>)
    target_link_libraries(synkit_science PUBLIC synkit_canon RDKit::SmilesParse RDKit::GraphMol RDKit::SubstructMatch Eigen3::Eigen)
    add_executable(science_experiments examples/science_experiments.cpp)
    target_include_directories(science_experiments PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
    target_link_libraries(science_experiments PRIVATE synkit_science RDKit::FileParsers)
    add_executable(stereo_conformance examples/stereo_conformance.cpp)
    target_include_directories(stereo_conformance PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
    target_link_libraries(stereo_conformance PRIVATE synkit_science)
    target_compile_features(stereo_experiments PRIVATE cxx_std_20)
    target_include_directories(stereo_experiments PRIVATE ${RDKit_INCLUDE_DIRS})
    target_compile_definitions(stereo_experiments PRIVATE SYNKIT_STEREO_WITH_RDKIT=1)
    target_link_libraries(stereo_experiments PRIVATE synkit_science)
  endif()
endif()
option(SYNKIT_CANON_BUILD_EXAMPLES "Build standalone graph and stereo examples" OFF)
if(SYNKIT_CANON_BUILD_EXAMPLES)
  add_executable(canonical_graph examples/canonical_graph.cpp)
  target_link_libraries(canonical_graph PRIVATE synkit_canon)
  add_executable(stereo_compare examples/stereo_compare.cpp)
  target_link_libraries(stereo_compare PRIVATE synkit_canon)
endif()
option(SYNKIT_CANON_BUILD_TESTS "Build independent native symmetry and stereo checks" OFF)
if(SYNKIT_CANON_BUILD_TESTS)
  enable_testing()
  add_executable(canonical_symmetry_tests tests/symmetry.cpp)
  target_compile_features(canonical_symmetry_tests PRIVATE cxx_std_17)
  target_include_directories(canonical_symmetry_tests PRIVATE src)
  add_test(NAME canonical_symmetry COMMAND canonical_symmetry_tests)
  add_executable(stereo_match_tests tests/stereo_match.cpp)
  find_package(Threads REQUIRED)
  target_link_libraries(stereo_match_tests PRIVATE synkit_canon Threads::Threads)
  add_test(NAME stereo_match COMMAND stereo_match_tests)
  add_executable(configured_stereo_tests tests/configured.cpp)
  target_link_libraries(configured_stereo_tests PRIVATE synkit_canon)
  add_test(NAME configured_stereo COMMAND configured_stereo_tests)
  if(SYNKIT_STEREO_WITH_RDKIT)
    add_executable(native_chemistry_tests tests/rdkit_preparation.cpp)
    target_compile_features(native_chemistry_tests PRIVATE cxx_std_20)
    target_include_directories(native_chemistry_tests PRIVATE examples ${RDKit_INCLUDE_DIRS} ${SYNKIT_JSON_INCLUDE_DIR})
    target_link_libraries(native_chemistry_tests PRIVATE synkit_science)
    add_test(NAME native_chemistry COMMAND native_chemistry_tests)
    add_executable(native_science_tests tests/science.cpp)
    target_link_libraries(native_science_tests PRIVATE synkit_science)
    add_test(NAME native_science COMMAND native_science_tests)
  endif()
endif()
include(GNUInstallDirs)
option(SYNKIT_CANON_WHEEL "Install the runtime library inside the Python wheel" OFF)
if(SYNKIT_CANON_WHEEL)
  if(MSVC)
    set_property(TARGET synkit_canon PROPERTY MSVC_RUNTIME_LIBRARY
      "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  endif()
  install(TARGETS synkit_canon
    LIBRARY DESTINATION synkit/Graph/Canon/_native/lib
    RUNTIME DESTINATION synkit/Graph/Canon/_native/lib)
else()
  install(TARGETS synkit_canon LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  if(TARGET synkit_science)
    install(TARGETS synkit_science ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  endif()
endif()
