cmake_minimum_required(VERSION 3.18)
project(boostree LANGUAGES CXX)

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

# scikit-build-core sets Release/-O3 by default.  Add LTO; do NOT add
# -march=native or any ISA flags here — wheels must run on any CPU
# matching the platform tag, not just the build runner's
include(CheckIPOSupported)
check_ipo_supported(RESULT _has_ipo)

# Boost.Geometry headers are vendored under third_party/boost (run
# scripts/vendor_boost.py to refresh from a Boost source tree).  The
# rare developer who wants to use a system Boost can set
# BOOSTREE_USE_SYSTEM_BOOST=ON
option(BOOSTREE_USE_SYSTEM_BOOST "Build against system Boost.Geometry" OFF)
if(BOOSTREE_USE_SYSTEM_BOOST)
  find_package(Boost 1.75 REQUIRED)
  set(_boost_inc ${Boost_INCLUDE_DIRS})
else()
  set(_boost_inc ${PROJECT_SOURCE_DIR}/third_party/boost)
endif()

find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
find_package(pybind11 CONFIG REQUIRED)
find_package(OpenMP COMPONENTS CXX)

pybind11_add_module(_core src/_core.cc)
target_include_directories(_core PRIVATE ${_boost_inc})
if(_has_ipo)
  set_property(TARGET _core PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
if(OpenMP_CXX_FOUND)
  target_link_libraries(_core PRIVATE OpenMP::OpenMP_CXX)
  target_compile_definitions(_core PRIVATE BOOSTREE_HAS_OPENMP=1)
  message(STATUS "OpenMP enabled (parallel batched queries)")
else()
  message(STATUS "OpenMP not found — batched queries will run serial")
endif()
install(TARGETS _core LIBRARY DESTINATION boostree)
