cmake_minimum_required(VERSION 3.16)
project(rean_sdk VERSION 0.1.0 LANGUAGES C)

option(REAN_WITH_CURL "Build the libcurl HTTP transport" ON)
option(REAN_BUILD_TESTS "Build the tests" OFF)
option(BUILD_SHARED_LIBS "Build a shared library" OFF)

add_library(rean src/rean.c src/rean_json.c src/rean_transport_curl.c)
target_include_directories(rean PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
                                PRIVATE src)
set_target_properties(rean PROPERTIES C_STANDARD 99 C_STANDARD_REQUIRED ON)
if(BUILD_SHARED_LIBS)
  target_compile_definitions(rean PRIVATE REAN_BUILD_SHARED INTERFACE REAN_USE_SHARED)
endif()
if(NOT WIN32)
  target_compile_definitions(rean PRIVATE _POSIX_C_SOURCE=200809L)
endif()
if(REAN_WITH_CURL)
  find_package(CURL REQUIRED)
  target_compile_definitions(rean PUBLIC REAN_WITH_CURL)
  target_link_libraries(rean PUBLIC CURL::libcurl)
endif()
if(MSVC)
  target_compile_options(rean PRIVATE /W4)
else()
  target_compile_options(rean PRIVATE -Wall -Wextra -pedantic)
endif()

install(TARGETS rean)
install(DIRECTORY include/ DESTINATION include)

if(REAN_BUILD_TESTS)
  enable_testing()
  add_executable(test_json tests/test_json.c src/rean_json.c)
  target_include_directories(test_json PRIVATE src)
  target_link_libraries(test_json PRIVATE rean)
  add_test(NAME json COMMAND test_json)
  if(REAN_WITH_CURL)
    add_executable(test_e2e tests/test_e2e.c)
    target_link_libraries(test_e2e PRIVATE rean)
    target_compile_definitions(test_e2e PRIVATE _POSIX_C_SOURCE=200809L)
  endif()
endif()
