From 8663fefa5fc3b3b35f4338105344e1169ba47154 Mon Sep 17 00:00:00 2001 From: Nicholas Frizzell Date: Thu, 27 Jul 2023 16:46:49 -0400 Subject: [PATCH] Add cmake cache var to allow for use of system openssl Signed-off-by: Nicholas Frizzell --- .../OpenSSL/FindOpenSSL.cmake.Linux.template | 91 +++++++++++++++++++ package-system/OpenSSL/build_config.json | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 package-system/OpenSSL/FindOpenSSL.cmake.Linux.template diff --git a/package-system/OpenSSL/FindOpenSSL.cmake.Linux.template b/package-system/OpenSSL/FindOpenSSL.cmake.Linux.template new file mode 100644 index 00000000..157d6729 --- /dev/null +++ b/package-system/OpenSSL/FindOpenSSL.cmake.Linux.template @@ -0,0 +1,91 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +set(OPENSSL_O3DE_NAMESPACE "3rdParty::OpenSSL") +if (TARGET $${OPENSSL_O3DE_NAMESPACE}) + return() +endif() + +set(SSL_TARGETNAME "OpenSSL::SSL") +set(CRYPTO_TARGETNAME "OpenSSL::Crypto") + +if (LY_SYSTEM_PACKAGE_OpenSSL) + + # We need to temporarily override the CMAKE_MODULE_PATH to force it to search in the system directories + set(CMAKE_MODULE_PATH "") + find_package(OpenSSL) + unset(CMAKE_MODULE_PATH) + + if (NOT OpenSSL_FOUND) + message(FATAL_ERROR "Compiling on linux requires the development headers for OpenSSL. Try using your package manager to install the OpenSSL development libraries following https://wiki.openssl.org/index.php/Libssl_API") + else() + # OpenSSL targets should be considered as provided by the system + set_target_properties(OpenSSL::SSL OpenSSL::Crypto PROPERTIES LY_SYSTEM_LIBRARY TRUE) + + # Alias the O3DE name to the official name + add_library(3rdParty::OpenSSL ALIAS OpenSSL::SSL) + endif() + +else() + + # we're trying to be a drop-in replacement for the FindOpenSSL.cmake that is shipped + # with CMake itself, so we set the same variables with the same uppercase for compatibility + # for questions about these variables, see https://cmake.org/cmake/help/latest/module/FindOpenSSL.html + set(OPENSSL_FOUND True) + set(OPENSSL_INCLUDE_DIR $${CMAKE_CURRENT_LIST_DIR}/OpenSSL/include) + # c-only packages can be released containing only Release executables + set(OPENSSL_LIBS_DIR $${CMAKE_CURRENT_LIST_DIR}/OpenSSL/lib) + set(OPENSSL_CRYPTO_LIBRARY $${OPENSSL_LIBS_DIR}/libcrypto$${CMAKE_STATIC_LIBRARY_SUFFIX}) + set(OPENSSL_CRYPTO_LIBRARIES + $${OPENSSL_CRYPTO_LIBRARY} + ${CRYPTO_LIBRARY_DEPENDENCIES}) + set(OPENSSL_SSL_LIBRARY $${OPENSSL_LIBS_DIR}/libssl$${CMAKE_STATIC_LIBRARY_SUFFIX}) + set(OPENSSL_SSL_LIBRARIES + $${OPENSSL_SSL_LIBRARY} + $${OPENSSL_CRYPTO_LIBRARIES}) + set(OPENSSL_LIBRARIES $${OPENSSL_SSL_LIBRARIES}) + set(OPENSSL_VERSION "${OPENSSL_VERSION_STRING}") + + add_library($${CRYPTO_TARGETNAME} STATIC IMPORTED GLOBAL) + set_target_properties($${CRYPTO_TARGETNAME} PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C") + set_target_properties($${CRYPTO_TARGETNAME} PROPERTIES IMPORTED_LOCATION "$${OPENSSL_CRYPTO_LIBRARY}") + + # anyone who links to the CRYPTO target also links to its dependencies: + target_link_libraries($${CRYPTO_TARGETNAME} INTERFACE ${CRYPTO_LIBRARY_DEPENDENCIES}) + + add_library($${SSL_TARGETNAME} STATIC IMPORTED GLOBAL) + set_target_properties($${SSL_TARGETNAME} PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C") + set_target_properties($${SSL_TARGETNAME} PROPERTIES IMPORTED_LOCATION "$${OPENSSL_SSL_LIBRARY}") + + # anyone who links to the SSL target also links to CRYPTO since SSL depends on CRYPTO: + target_link_libraries($${SSL_TARGETNAME} INTERFACE $${CRYPTO_TARGETNAME}) + + # cmake < 3.21 and visual studio < 16.10 don't properly implement SYSTEM includes + # so we use O3DEs patched implementation if it is available and fallback to default if not. + # this is futureproof so that when O3DE no longer needs to define this and CMake's system + # works without fixes, O3DE can erase this implementation and this script will still function. + if (COMMAND ly_target_include_system_directories) + ly_target_include_system_directories(TARGET $${SSL_TARGETNAME} INTERFACE $${OPENSSL_INCLUDE_DIR}) + ly_target_include_system_directories(TARGET $${CRYPTO_TARGETNAME} INTERFACE $${OPENSSL_INCLUDE_DIR}) + else() + target_include_directories($${SSL_TARGETNAME} SYSTEM INTERFACE $${OPENSSL_INCLUDE_DIR}) + target_include_directories($${CRYPTO_TARGETNAME} SYSTEM INTERFACE $${OPENSSL_INCLUDE_DIR}) + endif() + + # alias the O3DE name to the official name: + add_library($${OPENSSL_O3DE_NAMESPACE} ALIAS $${SSL_TARGETNAME}) + + # if we're not in O3DE, it's also extremely helpful to show a message to logs that indicate that this + # library was successfully picked up, as opposed to the system one. + # A good way to know if you're in O3DE or not is that O3DE sets various cache variables before + # calling find_package, specifically, LY_VERSION_ENGINE_NAME is always set very early: + if (NOT LY_VERSION_ENGINE_NAME) + message(STATUS "Using O3DE's OpenSSL ($${OPENSSL_VERSION}) from $${CMAKE_CURRENT_LIST_DIR}") + endif() + +endif() # LY_SYSTEM_PACKAGE_OpenSSL diff --git a/package-system/OpenSSL/build_config.json b/package-system/OpenSSL/build_config.json index dc0cb9bd..0dd47929 100644 --- a/package-system/OpenSSL/build_config.json +++ b/package-system/OpenSSL/build_config.json @@ -10,7 +10,7 @@ "Platforms":{ "Linux":{ "Linux":{ - "cmake_find_template":"FindOpenSSL.cmake.template", + "cmake_find_template":"FindOpenSSL.cmake.Linux.template", "custom_build_cmd": [ "./build-linux.sh", "openssl_1_1_1t", @@ -31,7 +31,7 @@ } }, "Linux-aarch64":{ - "cmake_find_template":"FindOpenSSL.cmake.template", + "cmake_find_template":"FindOpenSSL.cmake.Linux.template", "custom_build_cmd": [ "./build-linux.sh", "openssl_1_1_1t",