From 5c906f7fa0eeeff031beeed281b2716cc3677e9f Mon Sep 17 00:00:00 2001 From: Yi Wu Date: Wed, 19 Aug 2026 15:56:46 +0100 Subject: [PATCH] compatible with PSA configs and test improvements Signed-off-by: Yi Wu --- CMakeLists.txt | 46 +++++++++++++++++------- tests/scripts/components-build-system.sh | 33 ++++++++++++++--- 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36e1e6dd13..c834c0fbe4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,6 +139,13 @@ set(MBEDTLS_CONFIG_NAME "" CACHE STRING "Named Mbed TLS configuration (see confi set(MBEDTLS_CONFIG_SET "" CACHE STRING "Options to set, separated by semicolons (OPTION or OPTION=VALUE).") set(MBEDTLS_CONFIG_UNSET "" CACHE STRING "Options to unset, separated by semicolons.") +set(MBEDTLS_CONFIG_TRANSFORMED FALSE) +if(NOT "${MBEDTLS_CONFIG_NAME}" STREQUAL "" OR + NOT "${MBEDTLS_CONFIG_SET}" STREQUAL "" OR + NOT "${MBEDTLS_CONFIG_UNSET}" STREQUAL "") + set(MBEDTLS_CONFIG_TRANSFORMED TRUE) +endif() + if(NOT "${MBEDTLS_CONFIG_FILE}" STREQUAL "" AND (NOT "${MBEDTLS_CONFIG_BASE_FILE}" STREQUAL "" OR NOT "${MBEDTLS_CONFIG_NAME}" STREQUAL "" OR @@ -149,6 +156,19 @@ if(NOT "${MBEDTLS_CONFIG_FILE}" STREQUAL "" AND "MBEDTLS_CONFIG_NAME, MBEDTLS_CONFIG_SET or MBEDTLS_CONFIG_UNSET.") endif() +if(MBEDTLS_CONFIG_TRANSFORMED AND + (NOT "${TF_PSA_CRYPTO_CONFIG_FILE}" STREQUAL "" OR + NOT "${TF_PSA_CRYPTO_CONFIG_BASE_FILE}" STREQUAL "" OR + NOT "${TF_PSA_CRYPTO_CONFIG_NAME}" STREQUAL "" OR + NOT "${TF_PSA_CRYPTO_CONFIG_SET}" STREQUAL "" OR + NOT "${TF_PSA_CRYPTO_CONFIG_UNSET}" STREQUAL "")) + message(FATAL_ERROR + "TF_PSA_CRYPTO_CONFIG_FILE, TF_PSA_CRYPTO_CONFIG_BASE_FILE, " + "TF_PSA_CRYPTO_CONFIG_NAME, TF_PSA_CRYPTO_CONFIG_SET and " + "TF_PSA_CRYPTO_CONFIG_UNSET cannot be combined with " + "MBEDTLS_CONFIG_NAME, MBEDTLS_CONFIG_SET or MBEDTLS_CONFIG_UNSET.") +endif() + if(NOT "${MBEDTLS_CONFIG_BASE_FILE}" STREQUAL "" OR NOT "${MBEDTLS_CONFIG_NAME}" STREQUAL "" OR NOT "${MBEDTLS_CONFIG_SET}" STREQUAL "" OR @@ -185,16 +205,21 @@ if(NOT "${MBEDTLS_CONFIG_BASE_FILE}" STREQUAL "" OR set(MBEDTLS_GENERATED_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/include") set(MBEDTLS_GENERATED_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls/mbedtls_config.h") - set(MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_DIR}/psa/crypto_config.h") file(MAKE_DIRECTORY "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls") - file(MAKE_DIRECTORY "${MBEDTLS_GENERATED_CONFIG_DIR}/psa") configure_file("${MBEDTLS_BASE_CONFIG_FILE}" "${MBEDTLS_GENERATED_CONFIG_FILE}" COPYONLY) - # config.py handles the Mbed TLS and PSA configurations together. Give it - # a build-tree PSA configuration so that it never modifies the source tree. - configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/tf-psa-crypto/include/psa/crypto_config.h" - "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}" COPYONLY) + + if(MBEDTLS_CONFIG_TRANSFORMED) + set(MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE + "${MBEDTLS_GENERATED_CONFIG_DIR}/psa/crypto_config.h") + file(MAKE_DIRECTORY "${MBEDTLS_GENERATED_CONFIG_DIR}/psa") + # config.py handles the Mbed TLS and PSA configurations together. Give + # it a build-tree PSA configuration so that it never modifies the + # source tree. + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/tf-psa-crypto/include/psa/crypto_config.h" + "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}" COPYONLY) + endif() function(mbedtls_configure_generated_file) execute_process( @@ -231,12 +256,9 @@ if(NOT "${MBEDTLS_CONFIG_BASE_FILE}" STREQUAL "" OR set(MBEDTLS_CONFIG_FILE "$$") - if("${TF_PSA_CRYPTO_CONFIG_FILE}" STREQUAL "" AND - "${TF_PSA_CRYPTO_CONFIG_NAME}" STREQUAL "") - # Named configurations can adjust both configuration files. + if(MBEDTLS_CONFIG_TRANSFORMED) set(TF_PSA_CRYPTO_CONFIG_FILE - "$$" - CACHE STRING "TF-PSA-Crypto config file (overrides default)." FORCE) + "$$") endif() endif() diff --git a/tests/scripts/components-build-system.sh b/tests/scripts/components-build-system.sh index cd1db7c9e6..e65b8c9d4d 100644 --- a/tests/scripts/components-build-system.sh +++ b/tests/scripts/components-build-system.sh @@ -303,6 +303,8 @@ component_build_cmake_config_options () { msg "configure: cmake with a false-like option name" cmake -DMBEDTLS_CONFIG_SET=NO "$MBEDTLS_ROOT_DIR" grep '^#define NO$' generated/include/mbedtls/mbedtls_config.h + cmake -DMBEDTLS_CONFIG_SET= . + grep '^TF_PSA_CRYPTO_CONFIG_FILE:FILEPATH=$' CMakeCache.txt cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" @@ -314,6 +316,7 @@ component_build_cmake_config_options () { "$MBEDTLS_ROOT_DIR" cmp "$MBEDTLS_ROOT_DIR/configs/config-ccm-psk-tls1_2.h" \ generated/include/mbedtls/mbedtls_config.h + not test -e generated/include/psa/crypto_config.h cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" @@ -325,6 +328,24 @@ component_build_cmake_config_options () { -DMBEDTLS_CONFIG_SET=MBEDTLS_DEBUG_C "$MBEDTLS_ROOT_DIR" cd "$MBEDTLS_ROOT_DIR" + for option in \ + "TF_PSA_CRYPTO_CONFIG_FILE=$MBEDTLS_ROOT_DIR/tf-psa-crypto/include/psa/crypto_config.h" \ + "TF_PSA_CRYPTO_CONFIG_BASE_FILE=$MBEDTLS_ROOT_DIR/tf-psa-crypto/include/psa/crypto_config.h" \ + "TF_PSA_CRYPTO_CONFIG_NAME=full" \ + "TF_PSA_CRYPTO_CONFIG_SET=PSA_WANT_ALG_SHA_256" \ + "TF_PSA_CRYPTO_CONFIG_UNSET=PSA_WANT_ALG_CMAC" + do + rm -rf "$OUT_OF_SOURCE_DIR" + mkdir "$OUT_OF_SOURCE_DIR" + cd "$OUT_OF_SOURCE_DIR" + + msg "configure: reject $option with Mbed TLS transformations" + not cmake "-D$option" -DMBEDTLS_CONFIG_SET=MBEDTLS_DEBUG_C \ + "$MBEDTLS_ROOT_DIR" + + cd "$MBEDTLS_ROOT_DIR" + done + rm -rf "$OUT_OF_SOURCE_DIR" mkdir "$OUT_OF_SOURCE_DIR" cd "$OUT_OF_SOURCE_DIR" @@ -358,7 +379,7 @@ component_build_cmake_config_options () { "$MBEDTLS_ROOT_DIR/configs/config-ccm-psk-tls1_2.h" msg "install: generated configurations are relocatable" - install_dir="$OUT_OF_SOURCE_DIR/install" + install_dir="$OUT_OF_SOURCE_DIR.install" cmake -DCMAKE_INSTALL_PREFIX="$install_dir" . cmake --build . --target install cmp generated/include/mbedtls/mbedtls_config.h \ @@ -366,9 +387,11 @@ component_build_cmake_config_options () { cmp generated/include/psa/crypto_config.h \ "$install_dir/include/psa/crypto_config_mbedtls.h" - # The installed targets must not refer to configuration headers in the - # build tree. - mv generated generated.moved + # The installed targets must not refer to the build tree. + cd "$MBEDTLS_ROOT_DIR" + mv "$OUT_OF_SOURCE_DIR" "$OUT_OF_SOURCE_DIR.moved" + mkdir "$OUT_OF_SOURCE_DIR" + cd "$OUT_OF_SOURCE_DIR" mkdir consumer cat >consumer/CMakeLists.txt <