include(ExternalProject)

set(AWS_LC_BUILD_OPTS -DFIPS=${FIPS} -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR})

if(APPLE)
    if (IOS)
        set(AWS_LC_BUILD_OPTS ${AWS_LC_BUILD_OPTS} -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT} -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET} -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} -DCMAKE_SYSTEM_NAME=iOS)
    else ()
        set(AWS_LC_BUILD_OPTS ${AWS_LC_BUILD_OPTS} -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET} -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES})
    endif ()
elseif (ANDROID)
    set(AWS_LC_BUILD_OPTS ${AWS_LC_BUILD_OPTS} -DANDROID_ABI=${ANDROID_ABI} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL})
endif ()

set(AWS_LC_VERSION "AWS-LC-FIPS-2.0.16")
set(AWS_LC_HASH "3850079c48f82e8541d07cad59f5a4b8a51cdc156bda0ffc705fd12071cd3f29")

ExternalProject_add(
    openssl
    URL https://github.com/awslabs/aws-lc/archive/refs/tags/${AWS_LC_VERSION}.tar.gz
    URL_HASH SHA256=${AWS_LC_HASH}
    CMAKE_ARGS ${AWS_LC_BUILD_OPTS}
    CMAKE_GENERATOR Ninja
    PATCH_COMMAND ${AWS_LC_PATCH_COMMAND}
)

set(OPENSSL_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
set(OPENSSL_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include" CACHE INTERNAL "")

if(APPLE)
    set(OPENSSL_CRYPTO_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.dylib" CACHE INTERNAL "")
    set(OPENSSL_CRYPTO_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.dylib" CACHE INTERNAL "")
    set(OPENSSL_SSL_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.dylib" CACHE INTERNAL "")
    set(OPENSSL_SSL_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.dylib" CACHE INTERNAL "")
elseif(WIN32)
    set(OPENSSL_CRYPTO_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/openssl-prefix/src/openssl-build/crypto/crypto.lib" CACHE INTERNAL "")
    set(OPENSSL_SSL_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/openssl-prefix/src/openssl-build/ssl/ssl.lib" CACHE INTERNAL "")
    set(OPENSSL_CRYPTO_SHARED "${CMAKE_CURRENT_BINARY_DIR}/openssl-prefix/src/openssl-build/crypto/crypto.dll" CACHE INTERNAL "")
    set(OPENSSL_SSL_SHARED "${CMAKE_CURRENT_BINARY_DIR}/openssl-prefix/src/openssl-build/ssl/ssl.dll" CACHE INTERNAL "")
elseif(ANDROID)
    set(OPENSSL_CRYPTO_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.so" CACHE INTERNAL "")
    set(OPENSSL_SSL_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.so" CACHE INTERNAL "")
    set(OPENSSL_CRYPTO_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.so" CACHE INTERNAL "")
    set(OPENSSL_SSL_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.so" CACHE INTERNAL "")
elseif(UNIX)
    set(OPENSSL_CRYPTO_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.so" CACHE INTERNAL "")
    set(OPENSSL_SSL_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.so" CACHE INTERNAL "")
    set(OPENSSL_CRYPTO_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libcrypto.so" CACHE INTERNAL "")
    set(OPENSSL_SSL_SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib/libssl.so" CACHE INTERNAL "")
endif()
