cmake_minimum_required(VERSION 3.16)
project(build_curl)
include(FetchContent)

if (NOT DEFINED CURL_MIRROR_LINK)
    set(CURL_MIRROR_LINK https://curl.se/download/curl-8.8.0.tar.gz)
endif ()
if (NOT DEFINED LIBRESSL_MIRROR_LINK)
    set(LIBRESSL_MIRROR_LINK https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-3.9.2.tar.gz)
endif ()

set(LIBRESSL_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(
    libressl
    URL ${LIBRESSL_MIRROR_LINK}
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=Release
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
)
FetchContent_MakeAvailable(libressl)

set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
set(BUILD_STATIC_LIBS ON CACHE BOOL "Build static libraries" FORCE)
set(BUILD_STATIC_CURL ON CACHE BOOL "Build curl executable with static libcurl" FORCE)
set(CURL_DISABLE_TESTS ON CACHE BOOL "Do no build tests" FORCE)

set(CURL_USE_LIBRESSL ON CACHE BOOL "Use LibreSSL instead of OpenSSL" FORCE)
set(OPENSSL_ROOT_DIR ${libressl_SOURCE_DIR} CACHE PATH "Path to LibreSSL root directory" FORCE)
if (WIN32)
    set(OPENSSL_CRYPTO_LIBRARY ${libressl_BINARY_DIR}/crypto/release/crypto.lib CACHE FILEPATH "Path to LibreSSL crypto library" FORCE)
    set(OPENSSL_SSL_LIBRARY ${libressl_BINARY_DIR}/ssl/release/ssl.lib CACHE FILEPATH "Path to LibreSSL SSL library" FORCE)
else ()
    set(OPENSSL_CRYPTO_LIBRARY ${libressl_BINARY_DIR}/crypto/libcrypto.a CACHE FILEPATH "Path to LibreSSL crypto library" FORCE)
    set(OPENSSL_SSL_LIBRARY ${libressl_BINARY_DIR}/ssl/libssl.a CACHE FILEPATH "Path to LibreSSL SSL library" FORCE)
endif ()
set(OPENSSL_INCLUDE_DIR ${libressl_SOURCE_DIR}/include CACHE PATH "Path to LibreSSL include directory" FORCE)

FetchContent_Declare(
    curl
    URL ${CURL_MIRROR_LINK}
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=Release
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
)
FetchContent_MakeAvailable(curl)