option(CLANG_SCAN_DEPS "Support clang-scan-deps" OFF)

include(cmake/deps.cmake)

# Platform-specific settings

if(NOT ${PLATFORM_COMPILER} STREQUAL "unknown")
  add_definitions(-DRECC_PLATFORM_COMPILER="${PLATFORM_COMPILER}")
endif()

# On MacOS (Mojave onwards) /usr/include is deprecated.
# Use CMAKE_SYSROOT to get the base directory of /usr/include
# On other platforms CMAKE_OSX_SYSROOT is ""
set(SYSROOT "${CMAKE_OSX_SYSROOT}")
if(APPLE AND NOT SYSROOT)
  # For a native build CMake leaves CMAKE_OSX_SYSROOT empty and lets the
  # compiler use its default SDK, so ask for that SDK's path directly.
  execute_process(
    COMMAND xcrun --show-sdk-path
    OUTPUT_VARIABLE SYSROOT
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET)
  if(NOT IS_DIRECTORY "${SYSROOT}")
    set(SYSROOT "")
  endif()
endif()
add_compile_definitions(SYSROOT="${SYSROOT}")

#string(REPLACE "/" ";" SYSROOT_DIR_SEQ ${SYSROOT})
#add_compile_definitions(SYSROOT_DIR_SEQ="${SYSROOT_DIR_SEQ}")

# define DEFAULT_RECC_INSTANCE
add_definitions(-DDEFAULT_RECC_INSTANCE="${DEFAULT_RECC_INSTANCE}")

# define CMAKE_INSTALL_DIR
add_definitions(-DCMAKE_INSTALL_DIR="${CMAKE_INSTALL_PREFIX}")

# define RECC_CONFIG_PREFIX_DIR
add_definitions(-DRECC_CONFIG_PREFIX_DIR="${RECC_CONFIG_PREFIX_DIR}")

if(CLANG_SCAN_DEPS)
  add_definitions(-DRECC_CLANG_SCAN_DEPS)
endif()

FILE(GLOB SRCS *.cpp)

add_library(remoteexecution STATIC ${SRCS})
target_precompile_headers(remoteexecution REUSE_FROM common)
target_include_directories(remoteexecution
    PUBLIC
    nlohmann_json
    "."
)
target_link_libraries(remoteexecution
    common
    commonmetrics
    ${STATIC_GRPC_LINKER_RULE}
    ${ZLIB_LIBRARIES}
)

# recc
add_executable(recc bin/recc.m.cpp)

target_precompile_headers(recc REUSE_FROM common)
target_include_directories(recc PRIVATE ".")
target_link_libraries(recc remoteexecution)

install(TARGETS recc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# deps
add_executable(deps deps.cpp bin/deps.m.cpp)
target_precompile_headers(deps REUSE_FROM common)
target_include_directories(deps PRIVATE ".")
target_link_libraries(deps remoteexecution)

include(CTest)
if(BUILD_TESTING)
  add_subdirectory(test)
endif()
