summaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-01-27 14:22:36 -0800
committerGarrett D'Amore <garrett@damore.org>2024-01-27 16:55:14 -0800
commitf48959f1347d97a9bf4d5edb4eb0c46e4101d356 (patch)
treefc4ebd708026915db958c8c4167ab02bb2407cbf /cmake
parent2f90e27f0b64990b99df1f4afdd9ea82ff512e61 (diff)
downloadnng-f48959f1347d97a9bf4d5edb4eb0c46e4101d356.tar.gz
nng-f48959f1347d97a9bf4d5edb4eb0c46e4101d356.tar.bz2
nng-f48959f1347d97a9bf4d5edb4eb0c46e4101d356.zip
Mbed TLS CMake improvements.
Try to use the Mbed TLS cmake configuration data if present, and refactor our FindModule to adhere to the same basic API.
Diffstat (limited to 'cmake')
-rw-r--r--cmake/FindMbedTLS.cmake122
-rw-r--r--cmake/FindmbedTLS.cmake86
-rw-r--r--cmake/NNGHelpers.cmake8
3 files changed, 129 insertions, 87 deletions
diff --git a/cmake/FindMbedTLS.cmake b/cmake/FindMbedTLS.cmake
new file mode 100644
index 00000000..553a1953
--- /dev/null
+++ b/cmake/FindMbedTLS.cmake
@@ -0,0 +1,122 @@
+#
+# Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
+# Copyright 2017 Capitar IT Group BV <info@capitar.com>
+#
+# This software is supplied under the terms of the MIT License, a
+# copy of which should be located in the distribution where this
+# file was obtained (LICENSE.txt). A copy of the license may also be
+# found online at https://opensource.org/licenses/MIT.
+#
+
+#
+# Try to find the Mbed TLS libraries.
+# This tries to emulate the same expectations that the stock Mbed TLS
+# module uses in Mbed TLS v3.x.
+#
+# Sets the following:
+#
+# MbedTLS_FOUND - True if we found Mbed TLS.
+# MbedTLS_TARGET - Target of the mbedtls library.
+# MbedX509_TARGET - Target of the mbedx509 library.
+# MbedCrypto_TARGET - Target of the mbedcrypto library.
+# MbedTLS_VERSION - $major.$minor.$revision (e.g. ``2.6.0``).
+#
+# MBEDTLS_CRYPTO_LIBRARY - The mbedcrypto library.
+# MBEDTLS_X509_LIBRARY - The mbedx509 library.
+# MBEDTLS_TLS_LIBRARY - The mbedtls library.
+# MBEDTLS_LIBRARIES - List of all three Mbed TLS libraries.
+#
+# Hints:
+#
+# Set ``MBEDTLS_ROOT_DIR`` to the root directory of Mbed TLS installation.
+#
+
+set(_MBEDTLS_ROOT_HINTS ${MBEDTLS_ROOT_DIR} ENV MBEDTLS_ROOT_DIR)
+
+set(_MBED_REQUIRED_VARS MbedTLS_TARGET MbedX509_TARGET MbedCrypto_TARGET MbedTLS_VERSION)
+
+include(FindPackageHandleStandardArgs)
+include(CMakePushCheckState)
+
+find_path(_MBEDTLS_INCLUDE_DIR
+ NAMES mbedtls/ssl.h
+ HINTS ${_MBEDTLS_ROOT_HINTS}
+ # PATHS /usr/local
+ PATH_SUFFIXES include)
+
+find_library(_MBEDCRYPTO_LIBRARY
+ NAMES mbedcrypto
+ HINTS ${_MBEDTLS_ROOT_HINTS}
+ # PATHS /usr/local
+ # PATH_SUFFIXES lib
+ )
+
+find_library(_MBEDX509_LIBRARY
+ NAMES mbedx509
+ HINTS ${_MBEDTLS_ROOT_HINTS}
+ #PATHS /usr/local
+ # PATH_SUFFIXES lib
+ )
+
+find_library(_MBEDTLS_LIBRARY
+ NAMES mbedtls
+ HINTS ${_MBEDTLS_ROOT_HINTS}
+ #PATHS /usr/local
+ #PATH_SUFFIXES lib
+ )
+
+if ("${_MBEDTLS_TLS_LIBRARY}" STREQUAL "_MBEDTLS_TLS_LIBRARY-NOTFOUND")
+ message("Failed to find Mbed TLS library")
+else()
+
+ cmake_push_check_state(RESET)
+ set(CMAKE_REQUIRED_INCLUDES ${_MBEDTLS_INCLUDE_DIR} ${CMAKE_REQUIRED_INCLUDES_${BUILD_TYPE}})
+ list(APPEND CMAKE_REQUIRED_LIBRARIES ${_MBEDTLS_LIBRARY} ${_MBEDX509_LIBRARY} ${_MBEDCRYPTO_LIBRARY})
+ check_symbol_exists(mbedtls_ssl_init "mbedtls/ssl.h" _MBEDTLS_V2_OR_NEWER)
+ cmake_pop_check_state()
+
+ if (NOT _MBEDTLS_V2_OR_NEWER)
+ message("Mbed TLS too old (must be version 2 or newer) ${_MBEDTLS_V2_OR_NEWER} UP ${_MbedTLS_V2}")
+
+ else()
+ # Extract the version from the header... hopefully it matches the library.
+ if (EXISTS ${_MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h)
+ file(STRINGS ${_MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h _MBEDTLS_VERLINE
+ REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
+ else ()
+ file(STRINGS ${_MBEDTLS_INCLUDE_DIR}/mbedtls/version.h _MBEDTLS_VERLINE
+ REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
+ endif ()
+
+ string(REGEX REPLACE ".*MBEDTLS_VERSION_STRING[\t ]+\"(.*)\"" "\\1" MbedTLS_VERSION ${_MBEDTLS_VERLINE})
+ message("Mbed TLS version: ${MbedTLS_VERSION}")
+ endif()
+endif()
+
+
+add_library(MbedTLS::mbedtls UNKNOWN IMPORTED)
+add_library(MbedTLS::mbedx509 UNKNOWN IMPORTED)
+add_library(MbedTLS::mbedcrypto UNKNOWN IMPORTED)
+
+
+set_target_properties(MbedTLS::mbedtls PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_MBEDTLS_INCLUDE_DIR}")
+set_target_properties(MbedTLS::mbedx509 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_MBEDTLS_INCLUDE_DIR}")
+set_target_properties(MbedTLS::mbedcrypto PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_MBEDTLS_INCLUDE_DIR}")
+
+if (WIN32)
+ set_target_properties(MbedTLS::mbedtls PROPERTIES IMPORTED_IMPLIB "${_MBEDTLS_LIBRARY}")
+ set_target_properties(MbedTLS::mbedx509 PROPERTIES IMPORTED_IMPLIB "${_MBEDX509_LIBRARY}")
+ set_target_properties(MbedTLS::mbedcrypto PROPERTIES IMPORTED_IMPLIB "${_MBEDCRYPTO_LIBRARY}")
+else()
+ set_target_properties(MbedTLS::mbedtls PROPERTIES IMPORTED_LOCATION "${_MBEDTLS_LIBRARY}")
+ set_target_properties(MbedTLS::mbedx509 PROPERTIES IMPORTED_LOCATION "${_MBEDX509_LIBRARY}")
+ set_target_properties(MbedTLS::mbedcrypto PROPERTIES IMPORTED_LOCATION "${_MBEDCRYPTO_LIBRARY}")
+endif()
+
+set(MbedTLS_TARGET MbedTLS::mbedtls)
+set(MbedX509_TARGET MbedTLS::mbedx509)
+set(MbedCrypto_TARGET MbedTLS::mbedcrypto)
+
+find_package_handle_standard_args(MbedTLS REQUIRED_VARS ${_MBED_REQUIRED_VARS})
+mark_as_advanced(${_MBED_REQUIRED_VARS})
+
diff --git a/cmake/FindmbedTLS.cmake b/cmake/FindmbedTLS.cmake
deleted file mode 100644
index 804a0392..00000000
--- a/cmake/FindmbedTLS.cmake
+++ /dev/null
@@ -1,86 +0,0 @@
-#
-# Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
-# Copyright 2017 Capitar IT Group BV <info@capitar.com>
-#
-# This software is supplied under the terms of the MIT License, a
-# copy of which should be located in the distribution where this
-# file was obtained (LICENSE.txt). A copy of the license may also be
-# found online at https://opensource.org/licenses/MIT.
-#
-
-#
-# Try to find the Mbed TLS libraries.
-#
-# Sets the following:
-#
-# MBEDTLS_INCLUDE_DIR - Where to find mbedtls/ssl.h, etc.
-# MBEDTLS_FOUND - True if we found Mbed TLS.
-# MBEDTLS_CRYPTO_LIBRARY - The mbedcrypto library.
-# MBEDTLS_X509_LIBRARY - The mbedx509 library.
-# MBEDTLS_TLS_LIBRARY - The mbedtls library.
-# MBEDTLS_LIBRARIES - List of all three Mbed TLS libraries.
-# MBEDTLS_VERSION - $major.$minor.$revision (e.g. ``2.6.0``).
-#
-# Hints:
-#
-# Set ``MBEDTLS_ROOT_DIR`` to the root directory of Mbed TLS installation.
-#
-
-set(_MBEDTLS_ROOT_HINTS ${MBEDTLS_ROOT_DIR} ENV MBEDTLS_ROOT_DIR)
-
-include(FindPackageHandleStandardArgs)
-
-find_path(MBEDTLS_INCLUDE_DIR
- NAMES mbedtls/ssl.h
- HINTS ${_MBEDTLS_ROOT_HINTS}
- PATHS /usr/local
- PATH_SUFFIXES include)
-
-find_library(MBEDTLS_CRYPTO_LIBRARY
- NAMES mbedcrypto
- HINTS ${_MBEDTLS_ROOT_HINTS}
- PATHS /usr/local
- PATH_SUFFIXES lib)
-
-find_library(MBEDTLS_X509_LIBRARY
- NAMES mbedx509
- HINTS ${_MBEDTLS_ROOT_HINTS}
- PATHS /usr/local
- PATH_SUFFIXES lib)
-
-find_library(MBEDTLS_TLS_LIBRARY
- NAMES mbedtls
- HINTS ${_MBEDTLS_ROOT_HINTS}
- PATHS /usr/local
- PATH_SUFFIXES lib)
-
-set(MBEDTLS_LIBRARIES
- ${MBEDTLS_TLS_LIBRARY}
- ${MBEDTLS_X509_LIBRARY}
- ${MBEDTLS_CRYPTO_LIBRARY})
-
-if (${MBEDTLS_TLS_LIBRARY-NOTFOUND})
- message(FATAL_ERROR "Failed to find Mbed TLS library")
-endif ()
-
-mark_as_advanced(
- MBEDSSL_INCLUDE_DIR
- MBEDTLS_LIBRARIES
- MBEDTLS_CRYPTO_LIBRARY
- MBEDTLS_X509_LIBRARY
- MBEDTLS_TLS_LIBRARY)
-
-# Extract the version from the header... hopefully it matches the library.
-if (EXISTS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h)
- file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h _MBEDTLS_VERLINE
- REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
-else ()
- file(STRINGS ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h _MBEDTLS_VERLINE
- REGEX "^#define[ \t]+MBEDTLS_VERSION_STRING[\t ].*")
-endif ()
-
-string(REGEX REPLACE ".*MBEDTLS_VERSION_STRING[\t ]+\"(.*)\"" "\\1" MBEDTLS_VERSION ${_MBEDTLS_VERLINE})
-
-find_package_handle_standard_args(mbedTLS
- REQUIRED_VARS MBEDTLS_TLS_LIBRARY MBEDTLS_CRYPTO_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES VERSION_VAR MBEDTLS_VERSION)
-
diff --git a/cmake/NNGHelpers.cmake b/cmake/NNGHelpers.cmake
index d97d800c..b2daa1dc 100644
--- a/cmake/NNGHelpers.cmake
+++ b/cmake/NNGHelpers.cmake
@@ -1,5 +1,5 @@
#
-# Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+# Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
#
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
@@ -55,6 +55,12 @@ function(nng_link_libraries)
target_link_libraries(nng_testing PRIVATE ${ARGN})
endfunction()
+function(nng_link_libraries_public)
+ target_link_libraries(nng PRIVATE ${ARGN})
+ target_link_libraries(nng_testing PRIVATE ${ARGN})
+endfunction()
+
+
# nng_include_directories adds include directories.
function(nng_include_directories)
target_include_directories(nng PRIVATE ${ARGN})