
include(${CCF_DIR}/cmake/cpack_settings.cmake)
message(STATUS "CMAKE_INSTALL_PREFIX is '${CMAKE_INSTALL_PREFIX}'")
install(FILES ${CCF_DIR}/cmake/preproject.cmake DESTINATION cmake)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake)

include(GNUInstallDirs)

set(CMAKE_GENERATED_COMMENT
    "This file was auto-generated by CMake from a corresponding *.in file. DO NOT EDIT"
)
configure_file(
  ${CCF_DIR}/src/common/version.h.in ${CCF_DIR}/include/ccf/version.h @ONLY
)
configure_file(
  ${CCF_DIR}/python/version.py.in ${CCF_DIR}/python/version.py @ONLY
)
file(READ ${CCF_DIR}/doc/host_config_schema/cchost_config.json
     HOST_CONFIG_SCHEMA
)
configure_file(
  ${CCF_DIR}/src/host/config_schema.h.in ${CCF_DIR}/src/host/config_schema.h
  @ONLY
)
if(ENABLE_BFT AND (WORKER_THREADS EQUAL 0))
  set(CONSENSUSES cft bft)
  set(CONSENSUS_FILTER all)
else()
  set(CONSENSUSES cft)
  set(CONSENSUS_FILTER cft)
endif()

# Build common library for CCF enclaves
add_custom_target(ccf ALL)
set(CCF_IMPL_SOURCE
    ${CCF_DIR}/src/enclave/main.cpp 
    ${CCF_DIR}/src/enclave/enclave_time.cpp
    ${CCF_DIR}/src/enclave/thread_local.cpp 
    ${CCF_DIR}/src/js/wrap.cpp
    ${CCF_DIR}/src/node/quote.cpp
)

if("sgx" IN_LIST COMPILE_TARGETS)
  # enclave version
  add_enclave_library(
    ccf.enclave ${CCF_IMPL_SOURCE} ${CCF_GENERATED_DIR}/ccf_t.cpp
  )

  add_warning_checks(ccf.enclave)

  target_include_directories(
    ccf.enclave SYSTEM
    PUBLIC
      $<BUILD_INTERFACE:${CCF_GENERATED_DIR}>
      $<INSTALL_INTERFACE:include/ccf/> #< This contains the private headers
                                        #< which are currently under src, and
                                        #< should be removed or renamed
      $<INSTALL_INTERFACE:include/>
      $<INSTALL_INTERFACE:include/3rdparty/>
  )

  target_link_libraries(
    ccf.enclave PUBLIC quickjs.enclave ccfcrypto.enclave http_parser.enclave
                       sss.enclave ccf_endpoints.enclave ccf_kv.enclave
  )

  if(ENABLE_HTTP2)
    target_link_libraries(ccf.enclave PUBLIC nghttp2.enclave)
  endif()

  enable_quote_code(ccf.enclave)

  add_lvi_mitigations(ccf.enclave)

  install(
    TARGETS ccf.enclave
    EXPORT ccf
    DESTINATION lib
  )

  add_dependencies(ccf ccf.enclave)
endif()

if("virtual" IN_LIST COMPILE_TARGETS)
  # virtual version
  add_library(ccf.virtual STATIC ${CCF_IMPL_SOURCE})

  target_compile_definitions(
    ccf.virtual PUBLIC INSIDE_ENCLAVE VIRTUAL_ENCLAVE
                       _LIBCPP_HAS_THREAD_API_PTHREAD
  )

  target_compile_options(ccf.virtual PUBLIC ${COMPILE_LIBCXX})
  add_warning_checks(ccf.virtual)

  target_include_directories(
    ccf.virtual SYSTEM
    PUBLIC
      $<BUILD_INTERFACE:${CCF_GENERATED_DIR}>
      $<INSTALL_INTERFACE:include/ccf/> #< This contains the private headers
                                        #< which are currently under src, and
                                        #< should be removed or renamed
      $<INSTALL_INTERFACE:include/>
      $<INSTALL_INTERFACE:include/3rdparty/>
  )

  target_link_libraries(
    ccf.virtual
    PUBLIC ${LINK_LIBCXX}
           -lgcc
           ccfcrypto.host
           http_parser.host
           quickjs.host
           sss.host
           ccf_endpoints.host
           openenclave::oehost
           ccf_kv.host
           ${CMAKE_THREAD_LIBS_INIT}
  )

  if(ENABLE_HTTP2)
    target_link_libraries(ccf.virtual PUBLIC nghttp2.host)
  endif()

  set_property(TARGET ccf.virtual PROPERTY POSITION_INDEPENDENT_CODE ON)

  add_san(ccf.virtual)

  install(
    TARGETS ccf.virtual
    EXPORT ccf
    DESTINATION lib
  )

  add_dependencies(ccf ccf.virtual)
endif()
