configure_file(${PROJECT_SOURCE_DIR}/include/fg/version.hpp.in
               ${PROJECT_BINARY_DIR}/generated/fg/version.hpp @ONLY)

add_library(fg_core
  core/index_map.cpp
  core/quadrature.cpp
  core/prof.cpp
  core/parallel.cpp
  fourier/coefficients.cpp
  fourier/toeplitz.cpp
  fourier/sampling.cpp
  fourier/contour.cpp
  geometry/stack.cpp
  geometry/dispersion.cpp
  adjoint/primitives.cpp
  adjoint/shape.cpp
  geometry/aperiodic.cpp
  geometry/coordinate_transform.cpp
  geometry/contour.cpp
  geometry/line_section.cpp
  geometry/normal_field.cpp
  geometry/profile.cpp
  physics/propagation_matrix.cpp
  physics/modes.cpp
  physics/incidence.cpp
  physics/qmatrix.cpp
  physics/efficiency.cpp
  physics/anisotropic.cpp
  smatrix/smatrix.cpp
  solvers/gradient.cpp
  solvers/butcher.cpp
  solvers/solver.cpp
  solvers/layer_model.cpp
  solvers/rcwa.cpp
  solvers/resident.cpp
  solvers/rk.cpp
  solvers/simulation.cpp
  solvers/cyl_basis.cpp
  field/nearfield.cpp
  cyl/bessel.cpp
  cyl/spectrum.cpp
  cyl/galerkin.cpp
  cyl/propagation.cpp
  cyl/excitation.cpp
  linalg/cpu_backend.cpp
  io/config.cpp
)
add_library(FourierGrating::core ALIAS fg_core)

# CAD slicer (M14): Nurbforge only in src/cad
target_sources(fg_core PRIVATE cad/cad_common.cpp)
if(FG_ENABLE_NURBFORGE)
  target_sources(fg_core PRIVATE cad/slicer.cpp)
  target_link_libraries(fg_core PRIVATE Nurbforge::core)
  target_compile_definitions(fg_core PUBLIC FG_HAVE_NURBFORGE)
else()
  target_sources(fg_core PRIVATE cad/cad_stub.cpp)
endif()

target_include_directories(fg_core PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/generated>)
target_link_libraries(fg_core PUBLIC Eigen3::Eigen PRIVATE nlohmann_json::nlohmann_json fg_warnings)
target_compile_features(fg_core PUBLIC cxx_std_20)

# The two copies of Algorithm NF (Polygon2DLayer and boundary samples) must agree bit for bit
# ("identical arithmetic", tests NormalFieldSamples.PolygonsBitIdentical*). With FMA contraction
# (GCC default -ffp-contract=fast; x86-64-v3 is the baseline of AlmaLinux 10) the compiler fuses a*b+c
# differently in the two copies: differences of a few ulp. No contraction in this file.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  set_source_files_properties(geometry/normal_field.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
endif()

# parallelFor falls back to std::thread without OpenMP (M15)
find_package(Threads REQUIRED)
target_link_libraries(fg_core PUBLIC Threads::Threads)

if(FG_ENABLE_OPENMP AND OpenMP_CXX_FOUND)
  target_link_libraries(fg_core PUBLIC OpenMP::OpenMP_CXX)
  target_compile_definitions(fg_core PUBLIC FG_HAVE_OPENMP)
endif()

if(FG_ENABLE_HDF5)
  target_link_libraries(fg_core PUBLIC HDF5::HDF5)
  target_compile_definitions(fg_core PUBLIC FG_HAVE_HDF5)
endif()

if(FG_ENABLE_CUDA)
  target_sources(fg_core PRIVATE cuda/cuda_backend.cpp cuda/device_policy.cpp)
  target_link_libraries(fg_core PUBLIC CUDA::cudart CUDA::cublas CUDA::cusolver)
  target_compile_definitions(fg_core PUBLIC FG_HAVE_CUDA)
  if(MINGW)
    # See cmake/mingw_cuda_security_stub.cpp: the import libs above need MSVC CRT symbols that
    # MinGW lacks. Deliberately NOT linked into fg_core: fg_core is a static library, and CMake
    # merges an OBJECT library linked to a static library into its archive -- which makes the
    # stub an archive member again, only pulled in if ld already needs it by the time it scans
    # fg_core.a, i.e. before it reaches cudart.lib later on the link line. It must instead reach
    # each final executable's own link line as a plain object (see top-level CMakeLists.txt).
    add_library(fg_cuda_mingw_abi_shim OBJECT ${PROJECT_SOURCE_DIR}/cmake/mingw_cuda_security_stub.cpp)
  endif()
endif()
