cmake_minimum_required(VERSION 3.20)
project(potato_peeling VERSION 0.1.0 LANGUAGES CXX)

foreach (policy CMP0135 CMP0167)
    if (POLICY ${policy})
        cmake_policy(SET ${policy} NEW)
    endif ()
endforeach ()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)

# Building a language binding (Python through scikit-build-core, Node.js through
# cmake-js): the core objects go into the binding module only, with no shared library,
# tests or install of the C++ package.
if (SKBUILD OR CMAKE_JS_VERSION)
    set(POTATO_PEELING_BINDING ON)
else ()
    set(POTATO_PEELING_BINDING OFF)
endif ()

if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif ()

# Shared by default, so LGPL users can relink against a modified library.
option(BUILD_SHARED_LIBS "Build potato_peeling as a shared library" ON)

if (MSVC)
    # Large objects (CGAL, Chang & Yap), standard conformance, UTF-8 sources.
    add_compile_options(/bigobj /permissive- /utf-8)
    # The binding modules link MSVC's runtime statically, so wheels and npm packages
    # need no msvcp140.dll (a vendored one can be older than the toolset that built
    # the module). Safe: GMP and MPFR allocate and free their own memory. Set before
    # any target, since everything linked into a module must match.
    if (POTATO_PEELING_BINDING)
        set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
    endif ()
endif ()
# Windows finds DLLs beside the executable: keep the library and the test programs
# in one directory.
if (WIN32 AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif ()

include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)

# CGAL's exact rational, and so FT, is GMP's mpq_class when GMP's C++ interface
# is linked and Boost.Multiprecision's GMP rational otherwise. FT is part of the
# API, and refine's double-steered search can differ between the two in the last
# bits, so every build uses the second (and nothing links libgmpxx).
set(CGAL_WITH_GMPXX OFF CACHE BOOL "Use GMP's C++ classes in CGAL; must be OFF for potato_peeling" FORCE)
find_package(CGAL 5.4 REQUIRED)
find_package(Boost 1.74 CONFIG REQUIRED)

# CDT (MPL-2.0), header-only and private to the build: an installed package if
# there is one, else this pinned release. The release is only unpacked (its
# CMake project, with its install rules, is not added).
find_package(CDT CONFIG QUIET)
if (NOT TARGET CDT::CDT)
    include(FetchContent)
    FetchContent_Declare(CDT
            URL https://github.com/artem-ogre/CDT/archive/refs/tags/2.0.1.tar.gz
            URL_HASH SHA256=5e3c45715e55b5f2d4390082c8c1ac026f635ddee233516fdcf94b197954367d
            SOURCE_SUBDIR no-cmake-project)
    FetchContent_MakeAvailable(CDT)
    add_library(potato_peeling_cdt INTERFACE)
    target_include_directories(potato_peeling_cdt SYSTEM INTERFACE ${cdt_SOURCE_DIR}/CDT/include)
    add_library(CDT::CDT ALIAS potato_peeling_cdt)
endif ()

# ── C++ core, compiled once ───────────────────────────────────────────────────
# The library, the Python module and the tests all link these objects.
add_library(potato_peeling_core OBJECT
        src/internal/geometry.cpp
        src/internal/longest_segment.cpp
        src/potato_peeling.cpp
        src/utilities.cpp
        src/simple.cpp
        src/internal/chang_and_yap/chang_and_yap.cpp
        src/internal/aronov/mesh.cpp
        src/internal/aronov/predicates.cpp
        src/internal/aronov/sweep.cpp
        src/internal/aronov/aronov.cpp
        src/internal/cabello/kernel.cpp
        src/internal/cabello/certify.cpp
        src/internal/cabello/sampling.cpp
        src/internal/cabello/visibility.cpp
        src/internal/cabello/convex_clique.cpp
        src/internal/cabello/a_bound.cpp
        src/internal/cabello/cabello.cpp
        src/internal/cabello/vertex_skull.cpp
        src/internal/refinement/halfplanes.cpp
        src/internal/refinement/evaluator.cpp
        src/internal/refinement/push.cpp
        src/internal/refinement/refinement.cpp
        src/internal/bounds/upper_bound.cpp
)
target_include_directories(potato_peeling_core
        PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_compile_features(potato_peeling_core PUBLIC cxx_std_17)
target_link_libraries(potato_peeling_core PUBLIC CGAL::CGAL PRIVATE Boost::headers CDT::CDT)
# The upper bound's double-precision search must round the same everywhere.
# (MSVC's default /fp:precise does not contract since Visual Studio 2022 17.0.)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    set_source_files_properties(src/internal/bounds/upper_bound.cpp
            PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
endif ()
target_compile_definitions(potato_peeling_core PRIVATE
        # Fail the build if any GPL-licensed CGAL package is included.
        CGAL_LICENSE_ERROR
        CDT_ENSURE_PRECISE_MATH_IN_CONSTRUCTIONS)

# The objects are compiled for the library, so they export under its name.
set_target_properties(potato_peeling_core PROPERTIES DEFINE_SYMBOL potato_peeling_EXPORTS)
generate_export_header(potato_peeling_core
        BASE_NAME potato_peeling
        EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/potato_peeling/export.hpp
        STATIC_DEFINE POTATO_PEELING_STATIC_DEFINE)

set(POTATO_PEELING_SHARED OFF)
if (BUILD_SHARED_LIBS AND NOT POTATO_PEELING_BINDING)
    set(POTATO_PEELING_SHARED ON)
endif ()
if (POTATO_PEELING_SHARED)
    target_compile_definitions(potato_peeling_core PRIVATE potato_peeling_EXPORTS)
else ()
    target_compile_definitions(potato_peeling_core PUBLIC POTATO_PEELING_STATIC_DEFINE)
endif ()

# ── The C++ library: potato_peeling::potato_peeling ───────────────────────────
if (NOT POTATO_PEELING_BINDING)
    add_library(potato_peeling $<TARGET_OBJECTS:potato_peeling_core>)
    add_library(potato_peeling::potato_peeling ALIAS potato_peeling)
    target_include_directories(potato_peeling PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
            $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
            $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
    target_compile_features(potato_peeling PUBLIC cxx_std_17)
    target_link_libraries(potato_peeling PUBLIC CGAL::CGAL)
    if (NOT POTATO_PEELING_SHARED)
        target_compile_definitions(potato_peeling PUBLIC POTATO_PEELING_STATIC_DEFINE)
    endif ()
    # The ABI may change with every minor release while the version is 0.x.
    set_target_properties(potato_peeling PROPERTIES
            VERSION ${PROJECT_VERSION}
            SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})

    install(TARGETS potato_peeling EXPORT potato_peelingTargets
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT runtime
            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT runtime
            NAMELINK_COMPONENT development
            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT development)
    install(DIRECTORY include/potato_peeling ${CMAKE_CURRENT_BINARY_DIR}/include/potato_peeling
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT development)

    set(config_dir ${CMAKE_INSTALL_LIBDIR}/cmake/potato_peeling)
    install(EXPORT potato_peelingTargets NAMESPACE potato_peeling::
            DESTINATION ${config_dir} COMPONENT development)
    configure_package_config_file(cmake/potato_peelingConfig.cmake.in
            ${CMAKE_CURRENT_BINARY_DIR}/potato_peelingConfig.cmake
            INSTALL_DESTINATION ${config_dir})
    write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/potato_peelingConfigVersion.cmake
            COMPATIBILITY SameMinorVersion)
    install(FILES
            ${CMAKE_CURRENT_BINARY_DIR}/potato_peelingConfig.cmake
            ${CMAKE_CURRENT_BINARY_DIR}/potato_peelingConfigVersion.cmake
            DESTINATION ${config_dir} COMPONENT development)
endif ()

# ── C++ tests (ctest) ─────────────────────────────────────────────────────────
if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT POTATO_PEELING_BINDING)
    set(POTATO_PEELING_TESTS_DEFAULT ON)
else ()
    set(POTATO_PEELING_TESTS_DEFAULT OFF)
endif ()
option(POTATO_PEELING_BUILD_TESTS "Build the C++ tests" ${POTATO_PEELING_TESTS_DEFAULT})
if (POTATO_PEELING_BUILD_TESTS AND NOT POTATO_PEELING_BINDING)
    enable_testing()
    add_subdirectory(tests/cpp)
endif ()

# ── Python bindings: potato_peeling._core ─────────────────────────────────────
if (SKBUILD)
    find_package(Python COMPONENTS Interpreter Development.Module Development.SABIModule REQUIRED)
    execute_process(
            COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
            OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT)
    find_package(nanobind 3.0 CONFIG REQUIRED)

    nanobind_add_module(_core
            STABLE_ABI
            NB_STATIC
            bindings/python/module.cpp
    )
    target_link_libraries(_core PRIVATE potato_peeling_core)
    target_compile_definitions(_core PRIVATE CGAL_LICENSE_ERROR)
    install(TARGETS _core DESTINATION potato_peeling COMPONENT python)
endif ()

# ── Node.js addon (cmake-js): potato_peeling.node ─────────────────────────────
if (CMAKE_JS_VERSION)
    # node-addon-api's headers, from the js/ package's dependencies.
    execute_process(
            COMMAND node -p "require('node-addon-api').include"
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/js
            OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE node_addon_api_dir
            COMMAND_ERROR_IS_FATAL ANY)
    string(REPLACE "\"" "" node_addon_api_dir "${node_addon_api_dir}")

    add_library(potato_peeling_node SHARED bindings/node/addon.cpp ${CMAKE_JS_SRC})
    set_target_properties(potato_peeling_node PROPERTIES
            OUTPUT_NAME potato_peeling PREFIX "" SUFFIX ".node")
    target_include_directories(potato_peeling_node PRIVATE ${CMAKE_JS_INC} ${node_addon_api_dir})
    target_link_libraries(potato_peeling_node PRIVATE potato_peeling_core ${CMAKE_JS_LIB})
    # Node-API 8 (Node 12.22+); C++ exceptions map to JavaScript ones, and a value of
    # the wrong JavaScript type throws instead of being misread.
    target_compile_definitions(potato_peeling_node PRIVATE
            NAPI_VERSION=8 NAPI_CPP_EXCEPTIONS NODE_ADDON_API_DISABLE_DEPRECATED
            NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS CGAL_LICENSE_ERROR)
    if (MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
        execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF}
                /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
    endif ()
endif ()
