cmake_minimum_required(VERSION 3.28)
project(AeternalisEvolution VERSION 0.1.0 LANGUAGES CXX)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
  message(FATAL_ERROR "Use an out-of-source build, e.g. cmake --preset debug")
endif()
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#include <expected>
#if !defined(__cpp_lib_expected) || __cpp_lib_expected < 202202L
#error std::expected is required
#endif
int main() { std::expected<int, int> result{42}; return *result == 42 ? 0 : 1; }"
  EVO_HAS_EXPECTED)
if(NOT EVO_HAS_EXPECTED)
  message(FATAL_ERROR "This compiler/standard-library pair lacks the required C++23 std::expected")
endif()

option(EVO_WARNINGS_AS_ERRORS "Treat owned-code warnings as errors" ON)
option(EVO_ENABLE_TIDY "Run clang-tidy during compilation" OFF)
option(EVO_BUILD_FUZZER "Build the Clang libFuzzer snapshot target" OFF)
set(EVO_SANITIZER "none" CACHE STRING "none, address (ASan/UBSan), or thread")
set_property(CACHE EVO_SANITIZER PROPERTY STRINGS none address thread)
include(cmake/ProjectOptions.cmake)

set(evo_simulation_sources src/simulation/world.cpp src/simulation/snapshot.cpp)
add_library(evo_simulation STATIC ${evo_simulation_sources})
add_library(evo::simulation ALIAS evo_simulation)
target_include_directories(evo_simulation PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(evo_simulation PUBLIC cxx_std_23)
evo_owned_target(evo_simulation)

add_executable(evo-sim apps/evo-sim/main.cpp)
target_link_libraries(evo-sim PRIVATE evo::simulation)
evo_owned_target(evo-sim)

include(CTest)
if(BUILD_TESTING)
  add_executable(evo-tests tests/simulation_tests.cpp)
  target_link_libraries(evo-tests PRIVATE evo::simulation)
  evo_owned_target(evo-tests)
  add_test(NAME simulation-contracts COMMAND evo-tests)
  add_test(NAME test-harness-failure COMMAND evo-tests --self-test-failure)
  set_tests_properties(test-harness-failure PROPERTIES WILL_FAIL TRUE)
  add_test(NAME cli-replay COMMAND evo-sim --seed 123456 --ticks 1000 --entities 8 --verify-replay)
  set_tests_properties(cli-replay PROPERTIES PASS_REGULAR_EXPRESSION "d61ad5bf0c928c76")
  add_test(NAME cli-invalid-number COMMAND evo-sim --ticks -1)
  set_tests_properties(cli-invalid-number PROPERTIES WILL_FAIL TRUE)
  add_test(NAME cli-file-integration COMMAND "${CMAKE_COMMAND}"
    "-DEVO_SIM=$<TARGET_FILE:evo-sim>" "-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/cli-integration"
    -P "${CMAKE_CURRENT_SOURCE_DIR}/tests/cli_integration.cmake")
  set_tests_properties(simulation-contracts cli-replay cli-file-integration PROPERTIES TIMEOUT 60)
endif()

add_executable(evo-benchmark tests/benchmarks/simulation_benchmark.cpp)
target_link_libraries(evo-benchmark PRIVATE evo::simulation)
evo_owned_target(evo-benchmark)

if(EVO_BUILD_FUZZER)
  if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR MSVC OR EVO_SANITIZER STREQUAL "thread")
    message(FATAL_ERROR "Fuzzing requires Linux Clang and is incompatible with TSan")
  endif()
  # Compile the kernel itself with coverage and sanitizer instrumentation.
  add_executable(evo-snapshot-fuzz tests/fuzz/snapshot_fuzz.cpp ${evo_simulation_sources})
  target_include_directories(evo-snapshot-fuzz PRIVATE include)
  evo_owned_target(evo-snapshot-fuzz)
  target_compile_options(evo-snapshot-fuzz PRIVATE -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer)
  target_link_options(evo-snapshot-fuzz PRIVATE -fsanitize=fuzzer,address,undefined)
endif()

find_program(EVO_CLANG_FORMAT NAMES clang-format-19 clang-format)
add_custom_target(format-check COMMAND "${CMAKE_COMMAND}"
  "-DFORMATTER=${EVO_CLANG_FORMAT}" "-DSOURCE_ROOT=${CMAKE_CURRENT_SOURCE_DIR}"
  -P "${CMAKE_CURRENT_SOURCE_DIR}/tools/check-format.cmake" VERBATIM)
