From f07fc278ca943107b9bec6fe269d968a88c85a71 Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Wed, 3 Jun 2026 10:20:59 +0000 Subject: [PATCH] Build Makefile tests apps and tools with CMake --- CMakeLists.txt | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 9 +++ 2 files changed, 154 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b2abf71..dcd5cb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,15 @@ cmake_minimum_required(VERSION 3.16) project(matrixssl VERSION 4.6.0 LANGUAGES C) include(GNUInstallDirs) +include(CTest) option(MATRIXSSL_BUILD_SHARED "Build MatrixSSL as a shared library" ON) option(MATRIXSSL_BUILD_STATIC "Build MatrixSSL as a static library" OFF) option(MATRIXSSL_DISABLE_TLS13 "Disable TLS 1.3 in the generated CMake configuration" ON) option(MATRIXSSL_BUILD_OPENSSL_COMPAT "Build the mars-nwe reduced OpenSSL compatibility layer" ON) +option(MATRIXSSL_BUILD_TESTS "Build MatrixSSL test programs matching the GNUmakefile tests target" ON) +option(MATRIXSSL_BUILD_APPS "Build MatrixSSL example applications matching the GNUmakefile apps target" ON) +option(MATRIXSSL_BUILD_TOOLS "Build auxiliary tools available from the legacy makefiles" ON) set(MATRIXSSL_CONFIG "default" CACHE STRING "MatrixSSL configuration directory under configs/") set(MATRIXSSL_LIBRARY_PREFIX "" CACHE STRING "Prefix added to installed library output names, e.g. 'nw' for libnwmatrixssl") set(MATRIXSSL_INSTALL_INCLUDE_SUBDIR "matrixssl" CACHE STRING "Header install subdirectory below CMAKE_INSTALL_INCLUDEDIR") @@ -318,6 +322,53 @@ if(MATRIXSSL_BUILD_STATIC) endif() endif() +if((MATRIXSSL_BUILD_TESTS OR MATRIXSSL_BUILD_APPS) AND NOT TARGET matrixssl_static) + add_library(matrixssl_static STATIC EXCLUDE_FROM_ALL) + matrixssl_configure_target(matrixssl_static) + set_target_properties(matrixssl_static PROPERTIES OUTPUT_NAME "${MATRIXSSL_LIBRARY_PREFIX}matrixssl") +endif() + + +function(matrixssl_configure_program target_name) + target_include_directories(${target_name} + PRIVATE + ${MATRIXSSL_COMMON_INCLUDE_DIRS} + "${CMAKE_CURRENT_SOURCE_DIR}/apps/common" + "${CMAKE_CURRENT_SOURCE_DIR}/apps/ssl" + "${CMAKE_CURRENT_SOURCE_DIR}/apps/dtls" + "${CMAKE_CURRENT_SOURCE_DIR}/core/include/testsupp") + target_compile_definitions(${target_name} PRIVATE MATRIX_CONFIGURATION_INCDIR_FIRST) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64|i[3-6]86)$") + target_compile_options(${target_name} PRIVATE -maes) + endif() + if(UNIX AND NOT APPLE) + target_link_libraries(${target_name} PRIVATE pthread m) + endif() +endfunction() + +function(matrixssl_link_program target_name) + matrixssl_configure_program(${target_name}) + if(TARGET matrixssl_static) + target_link_libraries(${target_name} PRIVATE matrixssl_static) + else() + target_link_libraries(${target_name} PRIVATE matrixssl) + endif() +endfunction() + +function(matrixssl_add_test_program target_name) + add_executable(${target_name} ${ARGN}) + matrixssl_link_program(${target_name}) + set_target_properties(${target_name} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests") +endfunction() + +function(matrixssl_add_app_program target_name) + add_executable(${target_name} ${ARGN}) + matrixssl_link_program(${target_name}) + set_target_properties(${target_name} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/apps") +endfunction() + if(MATRIXSSL_BUILD_OPENSSL_COMPAT) if(MATRIXSSL_BUILD_SHARED AND NOT TARGET OpenSSL::SSL) add_library(OpenSSL::SSL ALIAS matrixssl) @@ -331,6 +382,100 @@ if(MATRIXSSL_BUILD_OPENSSL_COMPAT) endif() endif() + + +if(MATRIXSSL_BUILD_TESTS) + add_custom_target(matrixssl_tests ALL) + + matrixssl_add_test_program(matrixssl_algorithmTest crypto/test/algorithmTest.c) + matrixssl_add_test_program(matrixssl_throughputTest crypto/test/throughputTest.c) + matrixssl_add_test_program(matrixssl_cryptoOpen crypto/test/cryptoOpen.c) + matrixssl_add_test_program(matrixssl_eccTest crypto/test/eccTest.c) + matrixssl_add_test_program(matrixssl_rsaTest crypto/test/rsaTest.c) + matrixssl_add_test_program(matrixssl_hmacTest crypto/test/hmacTest.c) + matrixssl_add_test_program(matrixssl_rsaperf crypto/test/rsaperf/rsaperf.c) + matrixssl_add_test_program(matrixssl_eccperf crypto/test/eccperf/eccperf.c) + matrixssl_add_test_program(matrixssl_dhperf crypto/test/dhperf/dhperf.c) + + matrixssl_add_test_program(matrixssl_sslTest matrixssl/test/sslTest.c) + matrixssl_add_test_program(matrixssl_certValidate matrixssl/test/certValidate.c) + matrixssl_add_test_program(matrixssl_pkcs12Test matrixssl/test/pkcs12Test.c) + target_compile_definitions(matrixssl_pkcs12Test PRIVATE USE_CL_PKCS USE_CL_CERTLIB) + + add_dependencies(matrixssl_tests + matrixssl_algorithmTest matrixssl_throughputTest matrixssl_cryptoOpen + matrixssl_eccTest matrixssl_rsaTest matrixssl_hmacTest + matrixssl_rsaperf matrixssl_eccperf matrixssl_dhperf + matrixssl_sslTest matrixssl_certValidate matrixssl_pkcs12Test) + + if(BUILD_TESTING) + add_test(NAME cryptoOpen COMMAND matrixssl_cryptoOpen) + add_test(NAME eccTest COMMAND matrixssl_eccTest) + add_test(NAME rsaTest COMMAND matrixssl_rsaTest) + add_test(NAME hmacTest COMMAND matrixssl_hmacTest) + set_tests_properties(cryptoOpen eccTest rsaTest hmacTest PROPERTIES + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + endif() +endif() + +if(MATRIXSSL_BUILD_APPS) + add_custom_target(matrixssl_apps ALL) + + add_library(matrixssl_client_common STATIC + apps/common/client_common.c + apps/common/clientconfig.c + apps/common/load_keys.c) + matrixssl_link_program(matrixssl_client_common) + target_compile_definitions(matrixssl_client_common PRIVATE ID_RSA) + set_target_properties(matrixssl_client_common PROPERTIES + OUTPUT_NAME matrixssl_client_common + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/apps") + + matrixssl_add_app_program(matrixssl_server apps/ssl/server.c apps/ssl/http.c) + matrixssl_add_app_program(matrixssl_client apps/ssl/client.c apps/ssl/http.c) + target_link_libraries(matrixssl_client PRIVATE matrixssl_client_common) + matrixssl_add_app_program(matrixssl_simpleClient apps/ssl/simpleClient.c) + matrixssl_add_app_program(matrixssl_simpleServer apps/ssl/simpleServer.c) + matrixssl_add_app_program(matrixssl_interactiveClient apps/ssl/interactiveClient.c apps/ssl/interactiveCommon.c) + matrixssl_add_app_program(matrixssl_interactiveServer apps/ssl/interactiveServer.c apps/ssl/interactiveCommon.c) + matrixssl_add_app_program(matrixssl_tlsDtlsServer apps/ssl/tlsDtlsServer.c apps/ssl/http.c) + + matrixssl_add_app_program(matrixssl_dtlsServer apps/dtls/dtlsServer.c apps/dtls/dtlsCommon.c) + matrixssl_add_app_program(matrixssl_dtlsClient apps/dtls/dtlsClient.c apps/dtls/dtlsCommon.c) + target_link_libraries(matrixssl_dtlsClient PRIVATE matrixssl_client_common) + + foreach(_matrixssl_app IN ITEMS + matrixssl_client_common matrixssl_server matrixssl_client + matrixssl_simpleClient matrixssl_simpleServer + matrixssl_interactiveClient matrixssl_interactiveServer + matrixssl_tlsDtlsServer + matrixssl_dtlsServer matrixssl_dtlsClient) + target_compile_definitions(${_matrixssl_app} PRIVATE ID_RSA) + endforeach() + + add_dependencies(matrixssl_apps + matrixssl_server matrixssl_client matrixssl_simpleClient matrixssl_simpleServer + matrixssl_interactiveClient matrixssl_interactiveServer matrixssl_tlsDtlsServer + matrixssl_dtlsServer matrixssl_dtlsClient) +endif() + +if(MATRIXSSL_BUILD_TOOLS) + add_custom_target(matrixssl_tools ALL) + + if(UNIX AND NOT APPLE) + add_library(matrixssl_wrap_malloc MODULE core/wrapper/wrap-malloc.c) + set_target_properties(matrixssl_wrap_malloc PROPERTIES + PREFIX "" + OUTPUT_NAME "wrap-malloc" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tools") + target_compile_options(matrixssl_wrap_malloc PRIVATE -Wall -Werror) + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tools") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/tools/wrap-malloc-include.txt" + "EXTRA_CFLAGS=\"-include ${CMAKE_CURRENT_SOURCE_DIR}/core/wrapper/wrap-malloc.h\" EXTRA_LDFLAGS=\"${CMAKE_CURRENT_BINARY_DIR}/tools/wrap-malloc.so\"\n") + add_dependencies(matrixssl_tools matrixssl_wrap_malloc) + endif() +endif() + if(MATRIXSSL_BUILD_SHARED) install(TARGETS matrixssl EXPORT matrixsslTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/README.md b/README.md index 06114fb..eb1f13c 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,19 @@ Build notes: ```sh cmake -S . -B build -DMATRIXSSL_BUILD_OPENSSL_COMPAT=ON cmake --build build +ctest --test-dir build --output-on-failure ``` `MATRIXSSL_BUILD_OPENSSL_COMPAT` defaults to `ON` in this fork. Disable it with `-DMATRIXSSL_BUILD_OPENSSL_COMPAT=OFF` to build only the native MatrixSSL API. +The CMake build also mirrors the legacy top-level Makefile more closely: + +- `MATRIXSSL_BUILD_TESTS=ON` builds the crypto and MatrixSSL test programs from `crypto/test`, `crypto/test/*perf`, and `matrixssl/test`. +- `MATRIXSSL_BUILD_APPS=ON` builds the SSL/DTLS example applications from `apps/common`, `apps/ssl`, and `apps/dtls`. +- `MATRIXSSL_BUILD_TOOLS=ON` builds the malloc wrapper helper from `core/wrapper` on Unix-like systems. + +Only safe smoke tests are registered with CTest. Interactive, benchmark, network server/client, and certificate-file tests are built but are not run automatically. + Security note: this compatibility layer is intentionally small and mars-nwe-specific. It should not be presented as a general OpenSSL replacement. In particular, the FTK-facing X509/BIO helper surface is limited to the calls that code uses. ---