cmake_minimum_required(VERSION 3.21)
project(itch_book CXX)

add_library(itch_book INTERFACE)
target_include_directories(itch_book INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(itch_book INTERFACE cxx_std_23)

include(FetchContent)
FetchContent_Declare(fixed_decimal
  URL https://github.com/groovg/fixed-decimal/archive/a25e15801e32977df9abbea6a1819eecc9befaa9.tar.gz
  URL_HASH SHA256=aedd636c8ba9874f7fdeda5b7e7d08f4a6a38c9cb0cc330153865bfe7202d5fa
  SOURCE_SUBDIR cpp)
FetchContent_MakeAvailable(fixed_decimal)
target_link_libraries(itch_book INTERFACE fixed_decimal)

if(PROJECT_IS_TOP_LEVEL)
  enable_testing()
  add_executable(parse_test test/parse_test.cpp)
  target_link_libraries(parse_test PRIVATE itch_book)
  target_compile_options(parse_test PRIVATE -Wall -Wextra -Werror)
  add_test(NAME parse COMMAND parse_test)

  add_executable(stream_test test/stream_test.cpp)
  target_link_libraries(stream_test PRIVATE itch_book)
  target_compile_options(stream_test PRIVATE -Wall -Wextra -Werror)
  add_test(NAME stream COMMAND stream_test)

  add_executable(book_test test/book_test.cpp)
  target_link_libraries(book_test PRIVATE itch_book)
  target_compile_options(book_test PRIVATE -Wall -Wextra -Werror)
  add_test(NAME book COMMAND book_test)

  add_executable(steady_state_test test/steady_state_test.cpp)
  target_link_libraries(steady_state_test PRIVATE itch_book)
  target_compile_options(steady_state_test PRIVATE -Wall -Wextra -Werror)
  add_test(NAME steady_state COMMAND steady_state_test)

  add_executable(differential_test test/differential_test.cpp)
  target_link_libraries(differential_test PRIVATE itch_book)
  target_compile_options(differential_test PRIVATE -Wall -Wextra -Werror)
  add_test(NAME differential COMMAND differential_test)

  add_executable(gen-synthetic tools/gen_synthetic.cpp)
  target_link_libraries(gen-synthetic PRIVATE itch_book)
  target_include_directories(gen-synthetic PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test)
  target_compile_options(gen-synthetic PRIVATE -Wall -Wextra -Werror)

  add_executable(parse_throughput bench/parse_throughput.cpp)
  target_link_libraries(parse_throughput PRIVATE itch_book)
  target_compile_options(parse_throughput PRIVATE -Wall -Wextra -Werror)

  add_executable(book_throughput bench/book_throughput.cpp)
  target_link_libraries(book_throughput PRIVATE itch_book)
  target_include_directories(book_throughput PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test)
  target_compile_options(book_throughput PRIVATE -Wall -Wextra -Werror)
  if(WIN32)
    target_link_libraries(book_throughput PRIVATE psapi)
  endif()

  option(ITCH_BENCH_LATENCY "Build the apply-latency bench (fetches tsc-latency, x86 only)" OFF)
  if(ITCH_BENCH_LATENCY)
    FetchContent_Declare(tsc_latency
      GIT_REPOSITORY https://github.com/groovg/tsc-latency
      GIT_TAG 8b432562323a9c2c5f9f54064d7c1127139daa4c)
    FetchContent_MakeAvailable(tsc_latency)
    add_executable(apply_latency bench/apply_latency.cpp)
    target_link_libraries(apply_latency PRIVATE itch_book tsclat)
    target_compile_options(apply_latency PRIVATE -Wall -Wextra -Werror)
  endif()

  add_executable(itch-replay src/replay.cpp)
  target_link_libraries(itch-replay PRIVATE itch_book)
  target_compile_options(itch-replay PRIVATE -Wall -Wextra -Werror)
endif()
