cmake_minimum_required(VERSION 3.21)

project(esasim-worker
    VERSION 0.13.2
    DESCRIPTION "EsaSim worker: runs simulation jobs on your own machine for the EsaSim platform"
    LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(ESASIM_BUILD_TESTS "Build the unit tests" ON)
option(ESASIM_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF)
option(ESASIM_PREBUILT "A ready-made program (the installer's): it updates by downloading the new program" OFF)
option(ESASIM_SANITIZE "Build with address and undefined-behaviour sanitizers (GCC/Clang)" OFF)

# ---------------------------------------------------------------- dependencies
# JsonCpp and libcurl. With vcpkg (Windows, or --vcpkg on Linux/macOS) they come from vcpkg.json;
# otherwise from the system packages installed by scripts/setup.sh.
find_package(Threads REQUIRED)
find_package(CURL REQUIRED)
include(cmake/FindJsonCppTarget.cmake)

configure_file(src/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/version.h @ONLY)

# ---------------------------------------------------------------- library (everything except main)
add_library(esasim_core STATIC
    src/core/log.cpp
    src/core/config.cpp
    src/core/http.cpp
    src/core/api.cpp
    src/core/agent.cpp
    src/core/jobs.cpp
    src/core/disk.cpp
    src/core/deliver.cpp
    src/core/update.cpp
    src/core/settings.cpp
    src/core/util.cpp
    src/platform/process.cpp
    src/platform/system.cpp
    src/platform/child.cpp
    src/platform/awake.cpp
    src/platform/service.cpp
    src/runner/native.cpp
    src/tasks/benchmark.cpp
    src/tasks/sweep.cpp
    src/probe/parsers.cpp
    src/probe/nvidia.cpp
    src/probe/vendors.cpp
    src/probe/pci.cpp
    src/probe/vulkan.cpp
    src/probe/docker.cpp
    src/probe/inventory.cpp
    src/probe/usage.cpp
    src/data/sha256.cpp
    src/data/scan.cpp
    src/data/service.cpp
)
target_include_directories(esasim_core PUBLIC src ${CMAKE_CURRENT_BINARY_DIR}/generated)
target_link_libraries(esasim_core PUBLIC esasim::jsoncpp CURL::libcurl Threads::Threads ${CMAKE_DL_LIBS})
if(WIN32)
    target_compile_definitions(esasim_core PUBLIC WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS)
    target_link_libraries(esasim_core PUBLIC advapi32 shell32)
endif()

if(MSVC)
    target_compile_options(esasim_core PUBLIC /W4 /permissive- /utf-8)
    if(ESASIM_WARNINGS_AS_ERRORS)
        target_compile_options(esasim_core PUBLIC /WX)
    endif()
else()
    target_compile_options(esasim_core PUBLIC -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wno-sign-conversion)
    if(ESASIM_WARNINGS_AS_ERRORS)
        target_compile_options(esasim_core PUBLIC -Werror)
    endif()
    if(ESASIM_SANITIZE)
        target_compile_options(esasim_core PUBLIC -fsanitize=address,undefined -fno-omit-frame-pointer)
        target_link_options(esasim_core PUBLIC -fsanitize=address,undefined)
    endif()
endif()

# ---------------------------------------------------------------- program
add_executable(esasim-worker src/main.cpp)
target_link_libraries(esasim-worker PRIVATE esasim_core)
install(TARGETS esasim-worker RUNTIME DESTINATION bin)

# ---------------------------------------------------------------- tests
if(ESASIM_PREBUILT)
    target_compile_definitions(esasim_core PUBLIC ESASIM_PREBUILT=1)
endif()

if(ESASIM_BUILD_TESTS)
    enable_testing()
    add_executable(esasim-tests tests/test_main.cpp tests/test_parsers.cpp tests/test_util.cpp tests/test_runner.cpp tests/test_settings.cpp tests/test_data.cpp tests/test_accel.cpp)
    target_link_libraries(esasim-tests PRIVATE esasim_core)
    target_compile_definitions(esasim-tests PRIVATE ESASIM_TEST_DATA="${CMAKE_CURRENT_SOURCE_DIR}/tests/data")
    add_test(NAME unit COMMAND esasim-tests)
endif()
