add_library(examples_main STATIC main.cpp)
target_compile_features(examples_main PRIVATE cxx_std_20)
target_link_libraries(examples_main PRIVATE boost_redis_project_options)

macro(make_example EXAMPLE_NAME STANDARD)
  add_executable(${EXAMPLE_NAME} ${EXAMPLE_NAME}.cpp)
  target_link_libraries(${EXAMPLE_NAME} PRIVATE boost_redis_src)
  target_link_libraries(${EXAMPLE_NAME} PRIVATE boost_redis_project_options)
  target_compile_features(${EXAMPLE_NAME} PRIVATE cxx_std_${STANDARD})
  if (${STANDARD} STREQUAL "20")
    target_link_libraries(${EXAMPLE_NAME} PRIVATE examples_main)
  endif()
  if (${EXAMPLE_NAME} STREQUAL "cpp20_json")
    target_link_libraries(${EXAMPLE_NAME} PRIVATE Boost::json Boost::container_hash)
  endif()
endmacro()

macro(make_testable_example EXAMPLE_NAME STANDARD)
  make_example(${EXAMPLE_NAME} ${STANDARD})
  if (BOOST_REDIS_INTEGRATION_TESTS)
    add_test(${EXAMPLE_NAME} ${EXAMPLE_NAME})
  endif()
endmacro()

make_testable_example(cpp17_intro 17)
make_testable_example(cpp17_intro_sync 17)

make_testable_example(cpp20_intro 20)
make_testable_example(cpp20_containers 20)
make_testable_example(cpp20_json 20)
make_testable_example(cpp20_intro_tls 20)
make_testable_example(cpp20_unix_sockets 20)

make_example(cpp20_subscriber 20)
make_example(cpp20_streams 20)
make_example(cpp20_echo_server 20)
make_example(cpp20_resolve_with_sentinel 20)

# We test the protobuf example only on gcc.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  find_package(Protobuf)
  if (Protobuf_FOUND)
    protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS person.proto)
    make_testable_example(cpp20_protobuf 20)
    target_sources(cpp20_protobuf PUBLIC ${PROTO_SRCS} ${PROTO_HDRS})
    target_link_libraries(cpp20_protobuf PRIVATE  ${Protobuf_LIBRARIES})
    target_include_directories(cpp20_protobuf PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
  endif()
endif()

if (NOT MSVC)
  make_example(cpp20_chat_room 20)
endif()

# We build and test the spdlog integration example only if the library is found
find_package(spdlog)
if (spdlog_FOUND)
  make_testable_example(cpp17_spdlog 17)
  target_link_libraries(cpp17_spdlog PRIVATE spdlog::spdlog)
else()
  message(STATUS "Skipping the spdlog example because the spdlog package couldn't be found")
endif()
