cmake_minimum_required(VERSION 3.28...4.0)

project(monoprop LANGUAGES CXX)

# warn if the user invokes CMake directly
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
   $ 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()

if(APPLE)
  set(THREADS_FOUND TRUE CACHE BOOL "")
  set(CMAKE_THREAD_LIBS_INIT "-lpthread" CACHE STRING "")
  set(CMAKE_HAVE_THREADS_LIBRARY TRUE CACHE BOOL "")
  set(CMAKE_USE_PTHREADS_INIT TRUE CACHE BOOL "")
  set(THREADS_PREFER_PTHREAD_FLAG ON CACHE BOOL "")
endif()

# if CMAKE_BUILD_TYPE undefined, we set it to Release
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
endif()

set(
  monoprop_MAX_NUM_MODES
  "1024"
  CACHE STRING
  "Maximum number of simulable Fermionic modes with Python bindings"
)
option(monoprop_ENABLE_MPI "Enable MPI parallelization" OFF)

# cibuildwheel exports CIBUILDWHEEL=1. Wheels must not carry build-machine library paths, which could
# shadow the MPI library mpi4py selects; source installs keep them to find Spack or conda libraries.
# Read on every configure, not cached: a build tree may be reused outside cibuildwheel. Used as a
# boolean, so unset or empty is false.
set(monoprop_WHEEL_BUILD "$ENV{CIBUILDWHEEL}")

option(monoprop_ENABLE_CXX_UNIT_TESTS "Enable C++ unit test suite" ON)

set(Python_FIND_VIRTUALENV FIRST)
find_package(
  Python
  3.11
  REQUIRED
  COMPONENTS
    Interpreter
    Development.Module
    ${SKBUILD_SABI_COMPONENT}
)

find_package(Threads REQUIRED)

include(${PROJECT_SOURCE_DIR}/cmake/custom/monopropMPI.cmake)

include(${PROJECT_SOURCE_DIR}/cmake/compiler_flags/Sanitizers.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/compiler_flags/CXXFlags.cmake)

# report on compiler flags in use
message(STATUS "Configuring a ${CMAKE_BUILD_TYPE} build")
string(TOUPPER ${CMAKE_BUILD_TYPE} _cmake_build_type_upper)

message(STATUS "Compiler flags for ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "   From environment       : ${CMAKE_CXX_FLAGS}")
set(
  _cmake_build_type_specific_flags
  "${CMAKE_CXX_FLAGS_${_cmake_build_type_upper}}"
)
message(
  STATUS
  "   Build-type-specific    : ${_cmake_build_type_specific_flags}"
)
message(STATUS "   Vectorization flag     : ${ARCH_FLAG}")
message(
  STATUS
  "   Project defaults       : ${CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION} ${monoprop_CXX_FLAGS}"
)
message(STATUS "   User-appended          : ${EXTRA_CXXFLAGS}")
message(STATUS "   Sanitizer profile      : ${monoprop_SANITIZER}")

message(STATUS "   MPI parallelization    : ${monoprop_ENABLE_MPI}")
message(STATUS "   MPI ABI families       : ${monoprop_MPI_ABI_NAMES}")
message(STATUS "   cibuildwheel build     : ${monoprop_WHEEL_BUILD}")
message(STATUS "   Max simulable modes    : ${monoprop_MAX_NUM_MODES}")
message(STATUS "   C++ unit tests         : ${monoprop_ENABLE_CXX_UNIT_TESTS}")

include(GNUInstallDirs)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})

# Its objects are attached in cpp/monoprop once every subdirectory has registered its sources.
add_library(monoprop SHARED)

# must run before add_subdirectory(cpp): CTest's enabled-ness does not propagate
# back up to a parent directory that has already been added as a subdirectory.
if(monoprop_ENABLE_CXX_UNIT_TESTS)
  enable_testing()
  include(CTest)
endif()

add_subdirectory(cpp)
add_subdirectory(src/monoprop/bindings)
