cmake_minimum_required(VERSION 3.15...4.2)

project(bl LANGUAGES CXX)
option(BL_BUILD_PYTHON_BINDINGS "Build Python bindings" OFF)
option(BL_FORMATTER_SPECIALIZATIONS "Enable std::formatter specializations" OFF)

set(BL_SKBUILD "${SKBUILD_PROJECT_NAME}" STREQUAL "python-bl")
set(BL_SOURCES bl.cpp cfg.cpp empl.cpp traits.cpp)

if(NOT BL_SKBUILD)
  add_library(bl STATIC)
  add_library(bl::bl ALIAS bl)
  target_sources(
    bl
    PRIVATE ${BL_SOURCES}
    PUBLIC FILE_SET HEADERS FILES bl.hpp)

  target_compile_features(bl PRIVATE cxx_std_20)

  if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(bl PRIVATE -Wall -Wextra -Wpedantic)
  endif()

  if(BL_FORMATTER_SPECIALIZATIONS)
    target_compile_definitions(bl PRIVATE BL_FORMATTER_SPECIALIZATIONS)
  endif()
endif()

if(BL_BUILD_PYTHON_BINDINGS OR BL_SKBUILD)
  include(FetchContent)

  FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://github.com/pybind/pybind11
    GIT_TAG 97bf890)

  set(PYBIND11_FINDPYTHON ON)
  if(SKBUILD_SABI_COMPONENT)
    set(PYBIND11_STABLE_ABI ON)
    set(PYBIND11_STABLE_ABI_VERSION
        "${SKBUILD_SABI_VERSION}"
        CACHE STRING "")
  endif()

  FetchContent_MakeAvailable(pybind11)

  pybind11_add_module(pybl ${BL_SOURCES} py.cpp)
  target_compile_features(pybl PRIVATE cxx_std_20)
endif()

if(BL_SKBUILD)
  install(TARGETS pybl DESTINATION .)
endif()
