
project(System.Security.Cryptography.Native)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# These are happening inside of OpenSSL-defined macros out of our control
add_compile_options(-Wno-cast-align)
add_compile_options(-Wno-used-but-marked-unused)

add_definitions(-DPIC=1 -DOPENSSL_API_COMPAT=0x10100000L)

if(CMAKE_STATIC_LIB_LINK)
   set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif(CMAKE_STATIC_LIB_LINK)

find_package(OpenSSL REQUIRED)
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})

set(NATIVECRYPTO_SOURCES
    apibridge.cpp
    openssl.cpp
    pal_asn1.cpp
    pal_asn1_print.cpp
    pal_bignum.cpp
    pal_bio.cpp
    pal_dsa.cpp
    pal_ecdsa.cpp
    pal_ecc_import_export.cpp
    pal_eckey.cpp
    pal_err.cpp
    pal_evp.cpp
    pal_evp_pkey.cpp
    pal_evp_pkey_dsa.cpp
    pal_evp_pkey_ecdh.cpp
    pal_evp_pkey_eckey.cpp
    pal_evp_pkey_rsa.cpp
    pal_evp_cipher.cpp
    pal_hmac.cpp
    pal_pkcs12.cpp
    pal_pkcs7.cpp
    pal_rsa.cpp
    pal_ssl.cpp
    pal_x509.cpp
    pal_x509_name.cpp
    pal_x509_root.cpp
    pal_x509ext.cpp
)

# Always build portable on macOS because OpenSSL is not a system component
# and our prebuilts should not assume a specific ABI version for the types
# that use OpenSSL at runtime.
if (APPLE)
    set(FEATURE_DISTRO_AGNOSTIC_SSL True)
endif()

if (FEATURE_DISTRO_AGNOSTIC_SSL)
    list(APPEND NATIVECRYPTO_SOURCES
        opensslshim.cpp
    )
    add_definitions(-DFEATURE_DISTRO_AGNOSTIC_SSL)
endif()

add_library(objlib OBJECT ${NATIVECRYPTO_SOURCES} ${VERSION_FILE_PATH})

add_library(System.Security.Cryptography.Native.OpenSsl
    SHARED
    $<TARGET_OBJECTS:objlib>
)

add_library(System.Security.Cryptography.Native.OpenSsl-Static
    STATIC
    $<TARGET_OBJECTS:objlib>
)

# Disable the "lib" prefix.
set_target_properties(System.Security.Cryptography.Native.OpenSsl PROPERTIES PREFIX "")

# Disable the "lib" prefix and override default name
set_target_properties(System.Security.Cryptography.Native.OpenSsl-Static PROPERTIES PREFIX "")
set_target_properties(System.Security.Cryptography.Native.OpenSsl-Static PROPERTIES OUTPUT_NAME System.Security.Cryptography.Native.OpenSsl CLEAN_DIRECT_OUTPUT 1)

if (FEATURE_DISTRO_AGNOSTIC_SSL)
    # on macOS the link step fails with undefined symbols, and the script doesn't run.
    # if the build succeeds, the script would succeed, except it uses a Linux-only command.
    #
    # on Linux, the build will succeed with undefined symbols, then the script reports them
    # and fails the build for us.
    if (NOT APPLE)
        add_custom_command(TARGET System.Security.Cryptography.Native.OpenSsl POST_BUILD
            COMMENT "Verifying System.Security.Cryptography.Native.OpenSsl.so dependencies"
            COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-so.sh 
                $<TARGET_FILE:System.Security.Cryptography.Native.OpenSsl> 
                "Verification failed. System.Security.Cryptography.Native.OpenSsl.so has undefined dependencies. These are likely OpenSSL APIs that need to be added to opensslshim.h"
            VERBATIM
        )
    endif()

    # Link with libdl.so to get the dlopen / dlsym / dlclose
    target_link_libraries(System.Security.Cryptography.Native.OpenSsl
      dl
    )
else()
    target_link_libraries(System.Security.Cryptography.Native.OpenSsl
      ${OPENSSL_CRYPTO_LIBRARY}
      ${OPENSSL_SSL_LIBRARY}
    )
endif()

include(configure.cmake)

install_library_and_symbols (System.Security.Cryptography.Native.OpenSsl)
install (TARGETS System.Security.Cryptography.Native.OpenSsl-Static DESTINATION .)
