cmake_minimum_required(VERSION 3.18)
project(pycapacity_cpp LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

option(PYCAPACITY_CPP_FETCH_DEPS
  "Download Eigen and Qhull if they are not found on the system" ON)
option(PYCAPACITY_CPP_USE_HIGHS
  "Use HiGHS (if found) as fallback LP solver of the ICHM" ON)

include(FetchContent)

# inside a conda environment, use its libraries (eigen, qhull, highs, ...)
if(DEFINED ENV{CONDA_PREFIX})
  list(APPEND CMAKE_PREFIX_PATH $ENV{CONDA_PREFIX})
endif()

# Eigen (header only; conda-forge: eigen)
find_package(Eigen3 3.3 QUIET NO_MODULE)
if(NOT TARGET Eigen3::Eigen)
  if(NOT PYCAPACITY_CPP_FETCH_DEPS)
    message(FATAL_ERROR "Eigen3 not found (conda install -c conda-forge eigen)")
  endif()
  message(STATUS "Eigen3 not found: downloading Eigen 3.4.0")
  FetchContent_Declare(eigen
    URL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz)
  FetchContent_GetProperties(eigen)
  if(NOT eigen_POPULATED)
    FetchContent_Populate(eigen)
  endif()
  add_library(Eigen3::Eigen INTERFACE IMPORTED)
  set_target_properties(Eigen3::Eigen PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES ${eigen_SOURCE_DIR})
endif()

# Qhull, reentrant C library (conda-forge: qhull)
find_package(Qhull QUIET)
if(TARGET Qhull::qhull_r)
  set(QHULL_TARGET Qhull::qhull_r)
else()
  if(NOT PYCAPACITY_CPP_FETCH_DEPS)
    message(FATAL_ERROR "Qhull not found (conda install -c conda-forge qhull)")
  endif()
  message(STATUS "Qhull not found: downloading Qhull 2020.2")
  FetchContent_Declare(qhull
    URL https://github.com/qhull/qhull/archive/refs/tags/2020.2.tar.gz)
  FetchContent_GetProperties(qhull)
  if(NOT qhull_POPULATED)
    FetchContent_Populate(qhull)
  endif()
  file(GLOB QHULL_R_SOURCES ${qhull_SOURCE_DIR}/src/libqhull_r/*.c)
  add_library(qhull_r_static STATIC ${QHULL_R_SOURCES})
  target_include_directories(qhull_r_static PUBLIC ${qhull_SOURCE_DIR}/src)
  set_target_properties(qhull_r_static PROPERTIES POSITION_INDEPENDENT_CODE ON)
  set(QHULL_TARGET qhull_r_static)
endif()

# HiGHS, optional fallback LP solver (conda-forge: highs); built from source
# as a static library when not found (pip / wheel builds)
if(PYCAPACITY_CPP_USE_HIGHS)
  find_package(highs QUIET)
  if(NOT TARGET highs::highs AND PYCAPACITY_CPP_FETCH_DEPS)
    message(STATUS "HiGHS not found: building HiGHS 1.7.2 from source")
    set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
    set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
    set(FAST_BUILD ON CACHE BOOL "" FORCE)
    set(ZLIB OFF CACHE BOOL "" FORCE)
    set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
    FetchContent_Declare(highs
      URL https://github.com/ERGO-Code/HiGHS/archive/refs/tags/v1.7.2.tar.gz)
    FetchContent_MakeAvailable(highs)
    if(NOT TARGET highs::highs AND TARGET highs)
      add_library(highs::highs ALIAS highs)
    endif()
  endif()
endif()

# C++ library (usable on its own from C++)
add_library(polycap STATIC
  src/combinations.cpp
  src/hpsm.cpp
  src/vepoli2.cpp
  src/hull.cpp
  src/hrep.cpp
  src/box_lp.cpp
  src/ichm.cpp)
target_include_directories(polycap PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_link_libraries(polycap PUBLIC Eigen3::Eigen ${QHULL_TARGET})
if(TARGET highs::highs)
  message(STATUS "HiGHS found: used as fallback LP solver")
  target_link_libraries(polycap PUBLIC highs::highs)
  target_compile_definitions(polycap PUBLIC PYCAPACITY_CPP_HAVE_HIGHS)
else()
  message(STATUS "HiGHS not used")
endif()
set_target_properties(polycap PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(MSVC)
  target_compile_options(polycap PRIVATE /W4)
else()
  target_compile_options(polycap PRIVATE -Wall -Wextra)
endif()

# Python extension module pycapacity_cpp._core
find_package(Python 3.8 REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
pybind11_add_module(_core MODULE src/bindings.cpp)
target_link_libraries(_core PRIVATE polycap)
# only this component goes into the wheel (not the install rules of HiGHS)
install(TARGETS _core LIBRARY DESTINATION pycapacity_cpp COMPONENT python)
