cmake_minimum_required(VERSION 3.20)
project(cloudsign_openapi VERSION 0.8.0 LANGUAGES C)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
find_package(CURL 7.85 REQUIRED)
find_package(OpenSSL 3.0 REQUIRED COMPONENTS Crypto)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CJSON REQUIRED IMPORTED_TARGET libcjson>=1.7.18)
pkg_check_modules(PCRE2 REQUIRED IMPORTED_TARGET libpcre2-8>=10.42)
option(CLOUDSIGN_BUILD_TESTS "Build isolated contract and transport test programs" OFF)
option(CLOUDSIGN_SANITIZERS "Enable AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
option(CLOUDSIGN_BUILD_EXAMPLE "Build the server-side account example" OFF)
set(CS_SOURCES src/client.c src/schema.c src/generated.c src/helpers.c src/webhook.c src/download.c src/upload.c src/uploader.c src/models.c src/model_fields.c src/requests.c src/request_fields.c)
function(cs_configure target)
    target_compile_features(${target} PUBLIC c_std_11)
    target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
    target_link_libraries(${target} PUBLIC CURL::libcurl OpenSSL::Crypto PkgConfig::CJSON PkgConfig::PCRE2)
    if(UNIX)
        target_link_libraries(${target} PUBLIC m)
    endif()
    set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
    if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
        target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -Werror)
        if(CLOUDSIGN_SANITIZERS)
            target_compile_options(${target} PUBLIC -fsanitize=address,undefined -fno-omit-frame-pointer)
            target_link_options(${target} PUBLIC -fsanitize=address,undefined)
        endif()
    endif()
endfunction()
add_library(cloudsign_openapi STATIC ${CS_SOURCES})
add_library(cloudsign::openapi ALIAS cloudsign_openapi)
set_target_properties(cloudsign_openapi PROPERTIES EXPORT_NAME openapi OUTPUT_NAME cloudsign-openapi)
cs_configure(cloudsign_openapi)
if(CLOUDSIGN_BUILD_TESTS)
    enable_testing()
    add_library(cloudsign_openapi_testing STATIC ${CS_SOURCES})
    cs_configure(cloudsign_openapi_testing)
    target_compile_definitions(cloudsign_openapi_testing PRIVATE CLOUDSIGN_TESTING=1)
    foreach(name unit wire helpers webhook download upload uploader models requests)
        add_executable(cloudsign_c_${name} tests/${name}.c)
        target_compile_definitions(cloudsign_c_${name} PRIVATE CLOUDSIGN_TESTING=1)
        target_include_directories(cloudsign_c_${name} PRIVATE src)
        target_link_libraries(cloudsign_c_${name} PRIVATE cloudsign_openapi_testing)
    endforeach()
    add_test(NAME cloudsign_c_unit COMMAND cloudsign_c_unit ${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures)
endif()
if(CLOUDSIGN_BUILD_EXAMPLE)
    add_executable(cloudsign_example examples/account.c)
    target_link_libraries(cloudsign_example PRIVATE cloudsign::openapi)
endif()
install(TARGETS cloudsign_openapi EXPORT CloudsignOpenApiTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(DIRECTORY include/cloudsign DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT CloudsignOpenApiTargets NAMESPACE cloudsign:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CloudsignOpenApi)
configure_package_config_file(cmake/CloudsignOpenApiConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/CloudsignOpenApiConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CloudsignOpenApi)
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/CloudsignOpenApiConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CloudsignOpenApiConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/CloudsignOpenApiConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CloudsignOpenApi)
