cmake_minimum_required(VERSION 3.25)

# The po-sco 2 engine. It is versioned separately from the historical po-sco 1.x programs (removed from
# the tree, docs/legacy_removed.md). VERSION is the single version source (cmake/PoscoVersion.cmake).
include(cmake/PoscoVersion.cmake)

project(posco
  VERSION ${POSCO_VERSION_BASE}
  DESCRIPTION "po-sco 2: atom-resolved interaction analysis of pre-protonated structures"
  LANGUAGES CXX)

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

option(POSCO_BUILD_TESTS "Build the C++ test suite (doctest + CTest)" ON)
# The Python extension posco._native (python/, docs/python_api.md). pyproject.toml turns it on;
# the engine and the po-sco program never need Python.
option(POSCO_BUILD_PYTHON "Build the nanobind Python extension" OFF)

include(cmake/PoscoWarnings.cmake)
include(cmake/PoscoSanitizers.cmake)

set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${POSCO_VERSION_FILE}")
# libstdc++ precondition checks (bounds, null) in debug builds.
add_compile_definitions($<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>)

message(STATUS "po-sco ${POSCO_VERSION} (PEP 440 ${POSCO_VERSION_PEP440}), sanitizers: ${POSCO_SANITIZE}")

configure_file(include/posco/version.hpp.in
  "${PROJECT_BINARY_DIR}/generated/include/posco/version.hpp" @ONLY)

# Vendored, pinned single-header dependencies (third_party/README.md).
add_library(posco_doctest INTERFACE)
target_include_directories(posco_doctest SYSTEM INTERFACE "${PROJECT_SOURCE_DIR}/third_party")
add_library(posco_nlohmann_json INTERFACE)
target_include_directories(posco_nlohmann_json SYSTEM INTERFACE "${PROJECT_SOURCE_DIR}/third_party")

add_library(posco_core STATIC
  src/analysis/aggregate.cpp
  src/analysis/analyze.cpp
  src/analysis/exclusions.cpp
  src/analysis/fingerprint.cpp
  src/analysis/interaction.cpp
  src/analysis/prepare.cpp
  src/analysis/run.cpp
  src/analysis/stage_probe.cpp
  src/chemistry/aromaticity.cpp
  src/chemistry/charge_groups.cpp
  src/chemistry/consistency.cpp
  src/chemistry/covalent.cpp
  src/chemistry/element.cpp
  src/chemistry/kekule.cpp
  src/chemistry/matching.cpp
  src/chemistry/molecular_graph.cpp
  src/chemistry/perception.cpp
  src/chemistry/residue_templates.cpp
  src/chemistry/rings.cpp
  src/chemistry/topology_builder.cpp
  src/chemistry/topology_checks.cpp
  src/chemistry/typing.cpp
  src/chemistry/vdw.cpp
  src/cli/config.cpp
  src/core/component.cpp
  src/core/diagnostics.cpp
  src/core/sha256.cpp
  src/core/types.cpp
  src/geometry/contact_geometry.cpp
  src/geometry/geometry.cpp
  src/geometry/neighbours.cpp
  src/geometry/site_geometry.cpp
  src/io/pdb_component.cpp
  src/io/pdb_reader.cpp
  src/io/pdb_text.cpp
  src/io/pdb_writer.cpp
  src/io/sdf_reader.cpp
  src/output/columns.cpp
  src/output/gate_columns.cpp
  src/output/json.cpp
  src/output/output.cpp
  src/output/summary.cpp
  src/output/tsv.cpp
  src/repulsion/allowance.cpp
  src/repulsion/assess.cpp
  src/repulsion/centre.cpp
  src/repulsion/charge.cpp
  src/repulsion/evidence.cpp
  src/repulsion/gate.cpp
  src/repulsion/index.cpp
  src/repulsion/links.cpp
  src/repulsion/radii.cpp
  src/scoring/aromatic.cpp
  src/scoring/contacts.cpp
  src/scoring/halogen.cpp
  src/scoring/hydrogen_bond.cpp
  src/scoring/lipophilic.cpp
  src/scoring/metal.cpp
  src/scoring/salt_bridge.cpp
  src/scoring/score_models.cpp
  src/solvent/polar_sites.cpp
  src/solvent/polar_summary.cpp
  src/solvent/radii.cpp
  src/solvent/sasa.cpp
  src/solvent/satisfaction.cpp
  src/solvent/solvent_options.cpp
  src/solvent/surface.cpp)
add_library(posco::core ALIAS posco_core)
# Position independent so that the Python extension (a shared module) can link it.
set_target_properties(posco_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(posco_core PUBLIC
  "${PROJECT_SOURCE_DIR}/include"
  "${PROJECT_BINARY_DIR}/generated/include")
target_compile_features(posco_core PUBLIC cxx_std_20)
# The JSON writer only; nlohmann/json is not part of the public headers.
target_link_libraries(posco_core PRIVATE posco_nlohmann_json)
posco_target_warnings(posco_core)

# The command-line program (docs/cli.md).
add_executable(posco_cli src/cli/main.cpp)
set_target_properties(posco_cli PROPERTIES OUTPUT_NAME po-sco)
target_link_libraries(posco_cli PRIVATE posco::core)
posco_target_warnings(posco_cli)

if(POSCO_BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests/cpp)
endif()

if(POSCO_BUILD_PYTHON)
  add_subdirectory(python)
endif()
