cmake_minimum_required(VERSION 3.20)
project(pipsipmpp_ext LANGUAGES CXX)

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

if (NOT SKBUILD)
  message(WARNING "\
  This CMake file is meant to be executed using 'scikit-build-core'.
  Running it directly will almost certainly not produce the desired
  result. If you are a user trying to install this package, use the
  command below, which will install all necessary build dependencies,
  compile the package in an isolated environment, and then install it.
  =====================================================================
   $ pip install .
  =====================================================================
  If you are a software developer, and this is your own package, then
  it is usually much more efficient to install the build dependencies
  in your environment once and use the following command that avoids
  a costly creation of a new virtual environment at every compilation:
  =====================================================================
   $ pip install nanobind scikit-build-core[pyproject]
   $ pip install --no-build-isolation -ve .
  =====================================================================
  You may optionally add -Ceditable.rebuild=true to auto-rebuild when
  the package is imported. Otherwise, you need to rerun the above
  after editing C++ files.")
endif()


find_package(MPI REQUIRED COMPONENTS CXX)

if(NOT PIPS_SOURCE_DIR)
  set(PIPS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/pips-ipmpp")
endif()

add_compile_definitions(OMPI_SKIP_MPICXX MPICH_SKIP_MPICXX)

if(NOT EXISTS "${PIPS_SOURCE_DIR}/PIPS-IPM/Core/Interface/PIPSIPMppInterface.h")
  message(FATAL_ERROR
    "PIPS sources not found at ${PIPS_SOURCE_DIR}.\n"
    "Initialise the submodule:  git submodule update --init external/pips-ipmpp\n"
    "or point -DPIPS_SOURCE_DIR at a checkout of your own.")
endif()

# a binary directory is named explicitly so that PIPS_SOURCE_DIR may point outside
# this tree, which is the whole point of it being settable
add_subdirectory(${PIPS_SOURCE_DIR} "${CMAKE_CURRENT_BINARY_DIR}/pips-ipmpp" EXCLUDE_FROM_ALL)

get_target_property(PIPS_VERSION pips-ipmpp VERSION)
if(NOT PIPS_VERSION)
  message(FATAL_ERROR
    "The pips-ipmpp target at ${PIPS_SOURCE_DIR} carries no VERSION property, so the "
    "version the interface is built against cannot be determined.")
endif()
string(REPLACE "." ";" PIPS_VERSION_PARTS "${PIPS_VERSION}")
list(LENGTH PIPS_VERSION_PARTS PIPS_VERSION_PART_COUNT)
if(NOT PIPS_VERSION_PART_COUNT EQUAL 3)
  message(FATAL_ERROR "Expected a MAJOR.MINOR.PATCH PIPS-IPM++ version, got ${PIPS_VERSION}")
endif()
list(GET PIPS_VERSION_PARTS 0 PIPS_VERSION_MAJOR)
list(GET PIPS_VERSION_PARTS 1 PIPS_VERSION_MINOR)
list(GET PIPS_VERSION_PARTS 2 PIPS_VERSION_PATCH)
message(STATUS "Building the interface against PIPS-IPM++ ${PIPS_VERSION}")

find_package(Python 3.11 
  REQUIRED COMPONENTS Interpreter Development.Module
  OPTIONAL_COMPONENTS Development.SABIModule)

execute_process(
  COMMAND "${Python_EXECUTABLE}" -c "import nanobind; print(nanobind.cmake_dir())"
  OUTPUT_VARIABLE nanobind_ROOT OUTPUT_STRIP_TRAILING_WHITESPACE)
list(APPEND CMAKE_PREFIX_PATH "${nanobind_ROOT}")

find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(_core STABLE_ABI src/ext/pips_ext.cpp)
target_include_directories(_core PRIVATE "${PIPS_SOURCE_DIR}/PIPS-IPM/Core/Interface/")
target_compile_definitions(_core PRIVATE
  PIPSIPMPPPY_PIPS_VERSION_MAJOR=${PIPS_VERSION_MAJOR}
  PIPSIPMPPPY_PIPS_VERSION_MINOR=${PIPS_VERSION_MINOR}
  PIPSIPMPPPY_PIPS_VERSION_PATCH=${PIPS_VERSION_PATCH})
set_target_properties(_core PROPERTIES INSTALL_RPATH "$ORIGIN")
target_link_libraries(_core PRIVATE pips-ipmpp)
target_link_options(_core PRIVATE "LINKER:--as-needed")

set_target_properties(pips-ipmpp PROPERTIES INSTALL_RPATH "$ORIGIN")
target_link_options(pips-ipmpp PRIVATE "LINKER:--as-needed")

install(TARGETS pips-ipmpp _core
  LIBRARY DESTINATION pipsipmpp
  COMPONENT pipsipmpp)
