# The parts of radiance compiled by icpx: the SYCL device backend (librad_sycl.so) and, later, the
# Intel kernel library. The main project builds this directory through ExternalProject with icpx
# as the C++ compiler (top-level CMakeLists.txt, RAD_SYCL), so the engine and every host plugin
# stay on the main project's compiler (docs/SYCL_PORTING.md §4.5, docs/SYCL_PROGRESS.md D6).
cmake_minimum_required(VERSION 3.21)
project(radiance_sycl LANGUAGES CXX)

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
  message(FATAL_ERROR "sycl/ is built by icpx (IntelLLVM); this configure has "
                      "${CMAKE_CXX_COMPILER_ID} at ${CMAKE_CXX_COMPILER}.")
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

set(RAD_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." CACHE PATH "the radiance checkout")
set(RAD_SYCL_TARGETS "intel_gpu_bmg_g31" CACHE STRING "AOT targets for SYCL device code")

# The same warnings as the main build. icpx defaults to -fp-model=fast; host code here is precise
# as the main build's is, and device code keeps the main build's -ffp-contract=off discipline in
# the kernels themselves.
add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
                    -ffp-model=precise)
set(RAD_SYCL_FLAGS -fsycl "-fsycl-targets=${RAD_SYCL_TARGETS}" -foffload-fp32-prec-div
                    -foffload-fp32-prec-sqrt)

# The oneAPI runtime libraries sit beside the compiler; record their directory so the engine runs
# without setvars.sh in its environment.
get_filename_component(_icpx_bin "${CMAKE_CXX_COMPILER}" DIRECTORY)
get_filename_component(RAD_ONEAPI_LIBDIR "${_icpx_bin}/../lib" ABSOLUTE)

# ---------------------------------------------------------------- the device backend
# The backend, and the core's own kernels under it (the step's advance, the weight-table patches),
# each the SYCL twin of a .hip the AMD build compiles. The engine calls the kernels' launchers by
# the same names on either backend.
add_library(rad_sycl SHARED
  ${RAD_SOURCE_DIR}/core/device/sycl.cpp
  ${RAD_SOURCE_DIR}/core/device/sycl_host.cpp
  ${RAD_SOURCE_DIR}/core/sched/advance_sycl.cpp
  ${RAD_SOURCE_DIR}/core/runtime/wtab_sycl.cpp)
target_include_directories(rad_sycl PRIVATE
  ${RAD_SOURCE_DIR}/core/device ${RAD_SOURCE_DIR}/core ${RAD_SOURCE_DIR}/abi
  ${RAD_SOURCE_DIR}/vendor)
target_compile_definitions(rad_sycl PRIVATE RAD_HAVE_SYCL=1)
target_compile_options(rad_sycl PRIVATE ${RAD_SYCL_FLAGS} -fvisibility=hidden)
target_link_options(rad_sycl PRIVATE ${RAD_SYCL_FLAGS} "LINKER:-rpath,${RAD_ONEAPI_LIBDIR}"
                    "LINKER:--no-undefined")
set_target_properties(rad_sycl PROPERTIES OUTPUT_NAME rad_sycl)

# ---------------------------------------------------------------- the kernel library
# libxe2 is a plugin like any other: built by rad_add_plugin against the ABI headers alone, into
# the main build's $RADIANCE_HOME (RAD_PLUGIN_OUT, passed down by the main project). The variables
# cmake/RadAddPlugin.cmake expects describe a build with no HIP; libxe2 adds its SYCL flags itself.
set(RAD_PLUGIN_OUT "${CMAKE_BINARY_DIR}/radiance_home" CACHE PATH "the build tree's $RADIANCE_HOME")
set(RAD_HIP_FOUND OFF)
set(RAD_GPU_TARGETS "")
set(RAD_HIP_RUNTIME "")
# The sub-build installs nothing; the main project installs what it needs from here.
set(RAD_PLUGIN_INSTALL OFF)
add_library(rad_abi INTERFACE)
add_library(radiance::abi ALIAS rad_abi)
target_include_directories(rad_abi INTERFACE ${RAD_SOURCE_DIR}/abi)
include(GNUInstallDirs)
include(${RAD_SOURCE_DIR}/cmake/RadAddPlugin.cmake)
add_subdirectory(${RAD_SOURCE_DIR}/libxe2 ${CMAKE_BINARY_DIR}/libxe2)
