From 82a61a6b60e67205af30460d8baf0f04d14d85d8 Mon Sep 17 00:00:00 2001 From: Matheus Gabriel Werny <46067952+Spixmaster@users.noreply.github.com> Date: Mon, 2 Mar 2026 00:00:36 +0100 Subject: [PATCH] [CMake] New compoments `MbedTLS` and `wolfSSL` (#2360) * [CMake] New component MbedTLS New component MbedTLS. * Fix case Fix case: HTTPLIB_REQUIRE_OPENSSL=OFF; HTTPLIB_REQUIRE_MBEDTLS=ON * [CMake] Test target MbedTLS::tfpsacrypto [CMake] Test target MbedTLS::tfpsacrypto. * [CMake] Test MbedTLS::mbedx509 [CMake] Test MbedTLS::mbedx509. * Revert "[CMake] Test MbedTLS::mbedx509" This reverts commit 1d0b91f59a0989034d9b01be039cbcf8337b3d8b. * Revert "[CMake] Test target MbedTLS::tfpsacrypto" This reverts commit bf099f62644daaacaaaa05df6fb88f14f87146d0. * Fix problem caused by the recent performance improvement * wolfSSL support wolfSSL support. Partly solve https://github.com/yhirose/cpp-httplib/issues/2371. Only meson is missing. * Solve https://github.com/yhirose/cpp-httplib/issues/2361 Solve https://github.com/yhirose/cpp-httplib/issues/2361. Apply `WARNING`. * Fix variable Fix variable. * [CMake] Solve incompatibilities with loop Solve incompatibilities with loop. * Fix Fix. * Remove debug prints Remove debug prints. * [CMake] Fix bug Prevent a bug aus the required and if available libraries are checked independently from each other. A could be chosen in required but B could be chosen in if available and everything would pass. * Remove debug print Remove debug print. * Restore change Restore change. --------- Co-authored-by: yhirose --- CMakeLists.txt | 51 ++++++++++++++++++++++++++++++++++-- cmake/httplibConfig.cmake.in | 13 +++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96ede13..ae236c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,11 +2,15 @@ Build options: * Standard BUILD_SHARED_LIBS is supported and sets HTTPLIB_SHARED default value. * HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on) + * HTTPLIB_USE_WOLFSSL_IF_AVAILABLE (default off) + * HTTPLIB_USE_MBEDTLS_IF_AVAILABLE (default off) * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on) * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) * HTTPLIB_USE_ZSTD_IF_AVAILABLE (default on) * HTTPLIB_BUILD_MODULES (default off) * HTTPLIB_REQUIRE_OPENSSL (default off) + * HTTPLIB_REQUIRE_WOLFSSL (default off) + * HTTPLIB_REQUIRE_MBEDTLS (default off) * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_REQUIRE_ZSTD (default off) @@ -21,7 +25,7 @@ ------------------------------------------------------------------------------- - After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL ZLIB Brotli zstd) is available. + After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL wolfssl MbedTLS ZLIB Brotli zstd) is available. This creates a httplib::httplib target (if found and if listed components are supported). It can be linked like so: @@ -48,6 +52,8 @@ These variables are available after you run find_package(httplib) * HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h). * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled. + * HTTPLIB_IS_USING_WOLFSSL - a bool for if wolfSSL support is enabled. + * HTTPLIB_IS_USING_MBEDTLS - a bool for if MbedTLS support is enabled. * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. * HTTPLIB_IS_USING_ZSTD - a bool for if ZSTD support is enabled. @@ -95,10 +101,14 @@ set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0") option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) # Allow for a build to require OpenSSL to pass, instead of just being optional option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF) +option(HTTPLIB_REQUIRE_WOLFSSL "Requires wolfSSL to be found & linked, or fails build." OFF) +option(HTTPLIB_REQUIRE_MBEDTLS "Requires MbedTLS to be found & linked, or fails build." OFF) option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF) # Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found. # Make these options so their automatic use can be specifically disabled (as needed) option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON) +option(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE "Uses wolfSSL (if available) to enable HTTPS support." OFF) +option(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE "Uses MbedTLS (if available) to enable HTTPS support." OFF) option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON) # Lets you compile the program as a regular library instead of header-only option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF) @@ -120,6 +130,23 @@ else() message(WARNING "HTTPLIB_BUILD_MODULES requires CMake 3.28 or later. Current version is ${CMAKE_VERSION}. Modules support has been disabled.") endif() endif() + +# Incompatibility between TLS libraries +set(TLS_LIBRARIES_USED_TMP 0) + +foreach(tls_library OPENSSL WOLFSSL MBEDTLS) + set(TLS_REQUIRED ${HTTPLIB_REQUIRE_${tls_library}}) + set(TLS_IF_AVAILABLE ${HTTPLIB_USE_${tls_library}_IF_AVAILABLE}) + + if(TLS_REQUIRED OR TLS_IF_AVAILABLE) + math(EXPR TLS_LIBRARIES_USED_TMP "${TLS_LIBRARIES_USED_TMP} + 1") + endif() +endforeach() + +if(TLS_LIBRARIES_USED_TMP GREATER 1) + message(FATAL_ERROR "TLS libraries are mutually exclusive.") +endif() + # Defaults to static library but respects standard BUILD_SHARED_LIBS if set include(CMakeDependentOption) cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only." @@ -139,7 +166,7 @@ endif() if(CMAKE_SYSTEM_NAME MATCHES "Windows") if(CMAKE_SYSTEM_VERSION) if(${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") - message(SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") + message(WARNING "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") endif() else() set(CMAKE_SYSTEM_VERSION "10.0.19041.0") @@ -176,6 +203,22 @@ elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE) endif() endif() +if(HTTPLIB_REQUIRE_WOLFSSL) + find_package(wolfssl REQUIRED) + set(HTTPLIB_IS_USING_WOLFSSL TRUE) +elseif(HTTPLIB_USE_WOLFSSL_IF_AVAILABLE) + find_package(wolfssl QUIET) + set(HTTPLIB_IS_USING_WOLFSSL ${wolfssl_FOUND}) +endif() + +if(HTTPLIB_REQUIRE_MBEDTLS) + find_package(MbedTLS REQUIRED) + set(HTTPLIB_IS_USING_MBEDTLS TRUE) +elseif(HTTPLIB_USE_MBEDTLS_IF_AVAILABLE) + find_package(MbedTLS QUIET) + set(HTTPLIB_IS_USING_MBEDTLS ${MbedTLS_FOUND}) +endif() + if(HTTPLIB_REQUIRE_ZLIB) find_package(ZLIB REQUIRED) set(HTTPLIB_IS_USING_ZLIB TRUE) @@ -330,6 +373,8 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:zstd::libzstd> $<$:OpenSSL::SSL> $<$:OpenSSL::Crypto> + $<$:wolfssl::wolfssl> + $<$:MbedTLS::mbedtls> ) # Set the definitions to enable optional features @@ -339,6 +384,8 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_ZSTD_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> + $<$:CPPHTTPLIB_WOLFSSL_SUPPORT> + $<$:CPPHTTPLIB_MBEDTLS_SUPPORT> $<$,$>:CPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES> $<$:CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO> ) diff --git a/cmake/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in index 8ca8b99..933862a 100644 --- a/cmake/httplibConfig.cmake.in +++ b/cmake/httplibConfig.cmake.in @@ -4,6 +4,8 @@ # Setting these here so they're accessible after install. # Might be useful for some users to check which settings were used. set(HTTPLIB_IS_USING_OPENSSL @HTTPLIB_IS_USING_OPENSSL@) +set(HTTPLIB_IS_USING_WOLFSSL @HTTPLIB_IS_USING_WOLFSSL@) +set(HTTPLIB_IS_USING_MBEDTLS @HTTPLIB_IS_USING_MBEDTLS@) set(HTTPLIB_IS_USING_ZLIB @HTTPLIB_IS_USING_ZLIB@) set(HTTPLIB_IS_COMPILED @HTTPLIB_COMPILE@) set(HTTPLIB_IS_USING_BROTLI @HTTPLIB_IS_USING_BROTLI@) @@ -25,11 +27,22 @@ if(@HTTPLIB_IS_USING_OPENSSL@) endif() set(httplib_OpenSSL_FOUND ${OpenSSL_FOUND}) endif() + if(@HTTPLIB_IS_USING_ZLIB@) find_dependency(ZLIB) set(httplib_ZLIB_FOUND ${ZLIB_FOUND}) endif() +if(@HTTPLIB_IS_USING_WOLFSSL@) + find_dependency(wolfssl) + set(httplib_wolfssl_FOUND ${wolfssl_FOUND}) +endif() + +if(@HTTPLIB_IS_USING_MBEDTLS@) + find_dependency(MbedTLS) + set(httplib_MbedTLS_FOUND ${MbedTLS_FOUND}) +endif() + if(@HTTPLIB_IS_USING_BROTLI@) # Needed so we can use our own FindBrotli.cmake in this file. # Note that the FindBrotli.cmake file is installed in the same dir as this file.