## Copyright 2022-2026 The Khronos Group
## SPDX-License-Identifier: Apache-2.0

# Provides anari_embree, which consumers link for Embree and for the
# tasking system behind anari_embree::parallel_for()
# (include/anari_embree/parallel_for.h). It uses an installed Embree 4.3 or
# newer with TBB when both are found, and otherwise builds Embree from source
# as a static library with its internal tasking system. Set
# CMAKE_DISABLE_FIND_PACKAGE_embree=ON to always build from source; point
# CMAKE_PREFIX_PATH (or embree_DIR and TBB_DIR) at the installs to use them.
#
# Added by anari_embree_target() (../anari_embree.cmake), once per build: the
# first device to call it sets anari_embree up, and the others link the same
# target. This directory is installed with the SDK's other CMake files, so
# that a device built alone against an installed SDK gets it too.

project(anari_embree LANGUAGES CXX)

## Installed Embree ##

add_subdirectory(installed)
if (TARGET anari_embree)
  return()
endif()

## Embree built from source ##

message(STATUS "Building Embree from source")

if (APPLE AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  set(COMPILE_FOR_ARM ON)
elseif (UNIX AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  set(COMPILE_FOR_ARM ON)
else()
  set(COMPILE_FOR_ARM OFF)
endif()

option(EMBREE_GEOMETRY_CURVE          "" ON)
option(EMBREE_GEOMETRY_GRID           "" OFF)
option(EMBREE_GEOMETRY_INSTANCE       "" ON)
option(EMBREE_GEOMETRY_INSTANCE_ARRAY "" ON)
option(EMBREE_GEOMETRY_POINT          "" ON)
option(EMBREE_GEOMETRY_QUAD           "" ON)
option(EMBREE_GEOMETRY_SUBDIVISION    "" OFF)
option(EMBREE_GEOMETRY_TRIANGLE       "" ON)
option(EMBREE_GEOMETRY_USER           "" OFF)
# Embree picks the best enabled ISA at runtime; ARM builds a single one
if (COMPILE_FOR_ARM)
  option(EMBREE_ISA_NEON           "" OFF)
  option(EMBREE_ISA_NEON2X         "" ON)
else()
  option(EMBREE_ISA_SSE2           "" ON)
  option(EMBREE_ISA_SSE42          "" ON)
  option(EMBREE_ISA_AVX            "" ON)
  option(EMBREE_ISA_AVX2           "" ON)
  # Embree itself leaves AVX512 off by default on Windows and macOS
  if (WIN32 OR APPLE)
    option(EMBREE_ISA_AVX512       "" OFF)
  else()
    option(EMBREE_ISA_AVX512       "" ON)
  endif()
endif()
option(EMBREE_ISPC_SUPPORT             "" OFF)
option(EMBREE_STATIC_LIB               "" ON)
option(EMBREE_STAT_COUNTERS            "" OFF)
option(EMBREE_SYCL_SUPPORT             "" OFF)
option(EMBREE_TUTORIALS                "" OFF)
option(EMBREE_TUTORIALS_GLFW           "" OFF)
# No EMBREE_API_NAMESPACE: halcyon's ISPC shading code calls the C API, which a
# namespace would turn into C++ symbols. The static library's symbols have
# hidden visibility instead, so they don't clash with another Embree in the
# same process (see --exclude-libs below).
set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE STRING "" FORCE)
set(EMBREE_MAX_INSTANCE_LEVEL_COUNT "1" CACHE STRING "" FORCE)

anari_sdk_fetch_project(
  NAME anari_embree
  URL "https://github.com/RenderKit/embree/archive/refs/tags/v4.4.1.zip"
  MD5 6e2eecafb312d8cf1f1ff555702637cf
  ADD_SUBDIR
)

project_add_library(INTERFACE)
project_link_libraries(INTERFACE embree tasking)
project_compile_definitions(INTERFACE TASKING_INTERNAL)
project_include_directories(INTERFACE
  ${anari_embree_LOCATION}/common
  ${CMAKE_CURRENT_LIST_DIR}/include
)
if (WIN32)
  # C++ only: a consumer's ISPC sources get its options too, and ISPC rejects
  # MSVC's. One generator expression per option, as project_compile_options()
  # would split a list inside one.
  project_compile_options(INTERFACE
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSE__>
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSE2__>
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSE3__>
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSSE3__>
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSE4_1__>
    $<$<COMPILE_LANGUAGE:CXX>:/D__SSE4_2__>
  )
elseif(COMPILE_FOR_ARM)
  project_compile_options(INTERFACE
    $<$<COMPILE_LANGUAGE:CXX>:-flax-vector-conversions>
    $<$<COMPILE_LANGUAGE:CXX>:-fsigned-char>
  )
endif()

# Embree's tasking library exports its TaskScheduler; keep it out of the
# device's dynamic symbols, so another Embree loaded in the same process can't
# interpose it
if (UNIX AND NOT APPLE)
  target_link_options(${PROJECT_NAME}
    INTERFACE "LINKER:--exclude-libs,$<TARGET_LINKER_FILE_NAME:tasking>")
endif()

# Keep Embree's and FetchContent's options out of the way in CMake GUIs
get_cmake_property(cacheVariables CACHE_VARIABLES)
foreach (var ${cacheVariables})
  if (var MATCHES "^(EMBREE|FETCHCONTENT)_")
    mark_as_advanced(${var})
  endif()
endforeach()
