## Copyright 2009-2021 Intel Corporation
## SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.5)

project(ze_raytracing)

INCLUDE(CTest)

SET(RTHWIF_VERSION_MAJOR 4)
SET(RTHWIF_VERSION_MINOR 1)
SET(RTHWIF_VERSION_PATCH 0)
SET(RTHWIF_VERSION ${RTHWIF_VERSION_MAJOR}.${RTHWIF_VERSION_MINOR}.${RTHWIF_VERSION_PATCH})

SET(CMAKE_CXX_STANDARD 17)

IF (NOT DEFINED EMBREE_VERSION_MAJOR)
  
  SET(RTHWIF_STANDALONE ON)
  SET(RTHWIF_NAME ze_raytracing)
  ADD_DEFINITIONS("-DRTHWIF_STANDALONE")

  SET(EMBREE_CMAKEEXPORT_DIR "cmake")

  OPTION(EMBREE_SYCL_IMPLICIT_DISPATCH_GLOBALS "Using L0 allocated Dispatch Globals" ON)
  
  SET(EMBREE_RTHWIF_STATIC_LIB OFF)
  SET(EMBREE_BUILDER_TBB_STATIC ON)

  SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ${CMAKE_MODULE_PATH})

  CONFIGURE_FILE(
    "${PROJECT_SOURCE_DIR}/../../kernels/config.h.in"
    "${PROJECT_SOURCE_DIR}/../../kernels/config.h"
    )

  SET(EMBREE_MAX_INSTANCE_LEVEL_COUNT 1)
  CONFIGURE_FILE(
    "${PROJECT_SOURCE_DIR}/../../kernels/rtcore_config.h.in"
    "${PROJECT_SOURCE_DIR}/../../include/embree4/rtcore_config.h"
    )

  IF (NOT WIN32)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")                       # generate position independent code suitable for shared libraries
  ENDIF()

  ADD_SUBDIRECTORY(../../common/sys sys)
  ADD_SUBDIRECTORY(../../common/simd simd)

  GET_FILENAME_COMPONENT(SYCL_COMPILER_DIR ${CMAKE_CXX_COMPILER} PATH)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem \"${SYCL_COMPILER_DIR}/../include/sycl\" -isystem \"${SYCL_COMPILER_DIR}/../include/\"")       # disable warning from SYCL header (FIXME: why required?)

  find_package(TBB 2020)
      
ELSE()
  SET(RTHWIF_NAME embree_rthwif)
  OPTION(EMBREE_RTHWIF_STATIC_LIB "Build RTHWIF as a static library." ON)
  option(EMBREE_BUILDER_TBB_STATIC "Use a staticaly compiled TBB version for the Embree builder for GPU." OFF)
ENDIF()

IF (EMBREE_SYCL_RT_VALIDATION_API AND NOT EMBREE_SYCL_IMPLICIT_DISPATCH_GLOBALS)
    ADD_DEFINITIONS("-DEMBREE_SYCL_ALLOC_DISPATCH_GLOBALS")
ENDIF()

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-bitwise-instead-of-logical") # disables "use of bitwise '&' with boolean operands" warning
SET(CMAKE_CXX_FLAGS_SYCL "${CMAKE_CXX_FLAGS_SYCL} -Wno-bitwise-instead-of-logical") # disables "use of bitwise '&' with boolean operands" warning

if (EMBREE_RTHWIF_STATIC_LIB)
  set(RTHWIF_LIB_TYPE STATIC)
else()
  set(RTHWIF_LIB_TYPE SHARED)
endif()

# by default link against the tasking target that has all TBB related
# information we need when TASKING_TBB is used
set(TBB_TARGET tasking)

if (EMBREE_BUILDER_TBB_STATIC OR NOT TASKING_TBB)

  ####################################################################
  # fetch TBB and build static version of it
  set(TBB_TARGET tbb)
  ADD_DEFINITIONS("-D_CRT_SECURE_NO_WARNINGS")

  option(TBB_STRICT "Treat compiler warnings as errors" OFF)
  option(TBB_TEST "Enable testing" OFF)
  option(TBBMALLOC_BUILD "Enable tbbmalloc build" OFF)
  SET(TBB_DIR OFF)
  SET(BUILD_SHARED_LIBS OFF)

  INCLUDE(FetchContent)

  SET(FETCHCONTENT_QUIET OFF)

  IF (NOT EMBREE_RTHWIF_TBB_GIT_REPOSITORY) # allow setting this externally
    SET(EMBREE_RTHWIF_TBB_GIT_REPOSITORY "https://github.com/oneapi-src/oneTBB.git")
  ENDIF()

  FetchContent_Declare(
    tbb_static
    GIT_REPOSITORY ${EMBREE_RTHWIF_TBB_GIT_REPOSITORY}
    GIT_TAG v2021.6.0
  )

  FetchContent_GetProperties(tbb_static)
  if(NOT tbb_static_POPULATED)
    FetchContent_Populate(tbb_static)
    # We want to build tbb_static to link it into embree_rthwif, but don't want to
    # install it as part of the Embree install targets.
    add_subdirectory(${tbb_static_SOURCE_DIR} ${tbb_static_BINARY_DIR} EXCLUDE_FROM_ALL)
  endif()

  MARK_AS_ADVANCED(FETCHCONTENT_BASE_DIR)
  MARK_AS_ADVANCED(FETCHCONTENT_FULLY_DISCONNECTED)
  MARK_AS_ADVANCED(FETCHCONTENT_QUIET)
  MARK_AS_ADVANCED(FETCHCONTENT_SOURCE_DIR_TBB_STATIC)
  MARK_AS_ADVANCED(FETCHCONTENT_UPDATES_DISCONNECTED)
  MARK_AS_ADVANCED(FETCHCONTENT_UPDATES_DISCONNECTED_TBB_STATIC)

  MARK_AS_ADVANCED(TBB4PY_BUILD)
  MARK_AS_ADVANCED(TBBMALLOC_BUILD)
  MARK_AS_ADVANCED(TBB_BUILD)
  MARK_AS_ADVANCED(TBB_CPF)
  MARK_AS_ADVANCED(TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH)
  MARK_AS_ADVANCED(TBB_ENABLE_IPO)
  MARK_AS_ADVANCED(TBB_EXAMPLES)
  MARK_AS_ADVANCED(TBB_FIND_PACKAGE)
  MARK_AS_ADVANCED(TBB_INSTALL_VARS)
  MARK_AS_ADVANCED(TBB_NO_APPCONTAINER)
  MARK_AS_ADVANCED(TBB_SANITIZE)
  MARK_AS_ADVANCED(TBB_STRICT)
  MARK_AS_ADVANCED(TBB_TEST)
  MARK_AS_ADVANCED(TBB_TEST_SPEC)
  MARK_AS_ADVANCED(TBB_VALGRIND_MEMCHECK)
  MARK_AS_ADVANCED(TBB_WINDOWS_DRIVER)

  ADD_DEFINITIONS(-DTASKING_TBB)
  ####################################################################
ENDIF()

IF (RTHWIF_STANDALONE)
  include(package_ze_raytracing)
  INCLUDE(CPack)
ENDIF()

IF (EMBREE_SYCL_RT_VALIDATION_API)
  ADD_LIBRARY(embree_rthwif_sycl STATIC rttrace/rttrace_validation.cpp)
  SET_PROPERTY(TARGET embree_rthwif_sycl APPEND PROPERTY COMPILE_FLAGS "-DEMBREE_SYCL_SUPPORT")
  SET_TARGET_PROPERTIES(embree_rthwif_sycl PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS_SYCL})

  INSTALL(TARGETS embree_rthwif_sycl EXPORT embree_rthwif_sycl-targets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib)
  INSTALL(EXPORT embree_rthwif_sycl-targets DESTINATION "${EMBREE_CMAKEEXPORT_DIR}" COMPONENT devel)

  SET(EMBREE_RTHWIF_SYCL embree_rthwif_sycl)
ENDIF()

IF (NOT EMBREE_SYCL_L0_RTAS_BUILDER)
ADD_LIBRARY(embree_rthwif ${RTHWIF_LIB_TYPE} rtbuild/rtbuild.cpp rtbuild/qbvh6.cpp rtbuild/statistics.cpp)
TARGET_LINK_LIBRARIES(embree_rthwif PUBLIC ${EMBREE_RTHWIF_SYCL} PRIVATE ${TBB_TARGET} simd sys)
SET_TARGET_PROPERTIES(embree_rthwif PROPERTIES OUTPUT_NAME ${RTHWIF_NAME})
IF (EMBREE_RTHWIF_STATIC_LIB)
  TARGET_COMPILE_DEFINITIONS(embree_rthwif PUBLIC EMBREE_RTHWIF_STATIC_LIB)
ENDIF()

TARGET_COMPILE_DEFINITIONS(embree_rthwif PUBLIC EMBREE_SYCL_SUPPORT)

IF (EMBREE_STATIC_LIB OR NOT EMBREE_RTHWIF_STATIC_LIB)
  INSTALL(TARGETS embree_rthwif EXPORT ${RTHWIF_NAME}-targets
    LIBRARY NAMELINK_SKIP DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT examples
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel)
  INSTALL(EXPORT ${RTHWIF_NAME}-targets DESTINATION "${EMBREE_CMAKEEXPORT_DIR}" COMPONENT devel)
ENDIF()
ENDIF()

ADD_SUBDIRECTORY(testing)



