cmake_minimum_required(VERSION 3.16)
project(ngram_fetch LANGUAGES CXX)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

find_package(CURL 7.75 REQUIRED)
add_executable(ngram_fetch ngram_fetch.cpp)
target_compile_features(ngram_fetch PRIVATE cxx_std_17)
target_link_libraries(ngram_fetch PRIVATE CURL::libcurl)
target_compile_options(ngram_fetch PRIVATE -Wall -Wextra -Wpedantic)

find_package(OpenSSL REQUIRED COMPONENTS Crypto)
find_package(Threads REQUIRED)
add_executable(s3_upload s3_upload.cpp)
target_compile_features(s3_upload PRIVATE cxx_std_17)
target_link_libraries(s3_upload PRIVATE CURL::libcurl OpenSSL::Crypto Threads::Threads)
target_compile_options(s3_upload PRIVATE -Wall -Wextra -Wpedantic)

find_package(LibXml2 REQUIRED)
add_executable(s3_download s3_download.cpp)
target_compile_features(s3_download PRIVATE cxx_std_17)
target_link_libraries(s3_download PRIVATE CURL::libcurl OpenSSL::Crypto Threads::Threads LibXml2::LibXml2)
target_compile_options(s3_download PRIVATE -Wall -Wextra -Wpedantic)

add_executable(ngram_s3_benchmark ngram_s3_benchmark.cpp)
target_compile_features(ngram_s3_benchmark PRIVATE cxx_std_17)
target_link_libraries(ngram_s3_benchmark PRIVATE CURL::libcurl OpenSSL::Crypto Threads::Threads)
target_compile_options(ngram_s3_benchmark PRIVATE -Wall -Wextra -Wpedantic)

include(CTest)
if(BUILD_TESTING)
  find_package(Python3 COMPONENTS Interpreter QUIET)
  if(Python3_Interpreter_FOUND)
    add_test(NAME integration
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/integration.py"
              "$<TARGET_FILE:ngram_fetch>")
    set_tests_properties(integration PROPERTIES TIMEOUT 60)
    add_test(NAME upload_integration
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/upload_integration.py"
              "$<TARGET_FILE:s3_upload>")
    set_tests_properties(upload_integration PROPERTIES TIMEOUT 60)
    add_test(NAME download_integration
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/download_integration.py"
              "$<TARGET_FILE:s3_download>")
    set_tests_properties(download_integration PROPERTIES TIMEOUT 90)
    add_test(NAME upload_summary
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/upload_summary.py")
    # This test-only executable deliberately slows payload hashing to prove that
    # neither PUT nor GET timings include local checksum work.
    add_executable(benchmark_slow_hash tests/benchmark_slow_hash.cpp)
    target_compile_features(benchmark_slow_hash PRIVATE cxx_std_17)
    target_link_libraries(benchmark_slow_hash PRIVATE CURL::libcurl OpenSSL::Crypto Threads::Threads)
    target_compile_options(benchmark_slow_hash PRIVATE -Wall -Wextra -Wpedantic)
    add_test(NAME benchmark_integration
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/benchmark_integration.py"
              "$<TARGET_FILE:ngram_s3_benchmark>" "$<TARGET_FILE:benchmark_slow_hash>")
    set_tests_properties(benchmark_integration PROPERTIES TIMEOUT 90)
  endif()
endif()
