CMake: support custom base configurations

Signed-off-by: Yi Wu <yi.wu2@arm.com>
This commit is contained in:
Yi Wu 2026-08-06 16:21:07 +01:00
parent bf92cc769c
commit 346ece94cc
3 changed files with 29 additions and 14 deletions

View File

@ -139,22 +139,33 @@ set(MBEDTLS_CONFIG_SET "" CACHE STRING "Options to set, separated by semicolons
set(MBEDTLS_CONFIG_UNSET "" CACHE STRING "Options to unset, separated by semicolons.")
if(MBEDTLS_CONFIG_NAME OR MBEDTLS_CONFIG_SET OR MBEDTLS_CONFIG_UNSET)
if(MBEDTLS_CONFIG_FILE)
message(FATAL_ERROR
"MBEDTLS_CONFIG_FILE cannot be combined with MBEDTLS_CONFIG_NAME, "
"MBEDTLS_CONFIG_SET or MBEDTLS_CONFIG_UNSET.")
endif()
if(NOT MBEDTLS_PYTHON_EXECUTABLE)
message(FATAL_ERROR "Python 3 is required to generate an Mbed TLS configuration file.")
endif()
if(MBEDTLS_CONFIG_FILE)
set(MBEDTLS_BASE_CONFIG_FILE "${MBEDTLS_CONFIG_FILE}")
if(NOT EXISTS "${MBEDTLS_BASE_CONFIG_FILE}")
file(RELATIVE_PATH MBEDTLS_CONFIG_FILE_RELATIVE
"${CMAKE_CURRENT_BINARY_DIR}" "${MBEDTLS_CONFIG_FILE}")
set(MBEDTLS_BASE_CONFIG_FILE
"${CMAKE_CURRENT_SOURCE_DIR}/${MBEDTLS_CONFIG_FILE_RELATIVE}")
endif()
else()
set(MBEDTLS_BASE_CONFIG_FILE
"${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/mbedtls_config.h")
endif()
if(NOT EXISTS "${MBEDTLS_BASE_CONFIG_FILE}")
message(FATAL_ERROR "Mbed TLS configuration file not found: ${MBEDTLS_BASE_CONFIG_FILE}")
endif()
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")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/mbedtls_config.h"
DESTINATION "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls")
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.
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tf-psa-crypto/include/psa/crypto_config.h"

View File

@ -1,4 +1,5 @@
Features
* Add the `MBEDTLS_CONFIG_NAME`, `MBEDTLS_CONFIG_SET` and
`MBEDTLS_CONFIG_UNSET` CMake options for selecting and customizing
compile-time configurations. Fixes #10838.
compile-time configurations, including configurations based on a custom
`MBEDTLS_CONFIG_FILE`. Fixes #10838.

View File

@ -299,15 +299,18 @@ component_build_cmake_config_options () {
mkdir "$OUT_OF_SOURCE_DIR"
cd "$OUT_OF_SOURCE_DIR"
msg "build: cmake with MBEDTLS_CONFIG_SET and MBEDTLS_CONFIG_UNSET"
cmake '-DMBEDTLS_CONFIG_UNSET=MBEDTLS_SSL_ALL_ALERT_MESSAGES;MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE' \
'-DMBEDTLS_CONFIG_SET=MBEDTLS_SSL_PROTO_TLS1_3;MBEDTLS_SSL_ALL_ALERT_MESSAGES;MBEDTLS_SSL_IN_CONTENT_LEN=12000' \
msg "build: cmake with a base config, MBEDTLS_CONFIG_SET and MBEDTLS_CONFIG_UNSET"
cmake -DMBEDTLS_CONFIG_FILE=configs/config-ccm-psk-tls1_2.h \
-DMBEDTLS_CONFIG_UNSET=MBEDTLS_SSL_SRV_C \
'-DMBEDTLS_CONFIG_SET=MBEDTLS_SSL_RENEGOTIATION;MBEDTLS_DEBUG_C;MBEDTLS_ERROR_C;MBEDTLS_SSL_IN_CONTENT_LEN=12000' \
"$MBEDTLS_ROOT_DIR"
make query_compile_time_config
programs/test/query_compile_time_config MBEDTLS_SSL_PROTO_TLS1_3
programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
not programs/test/query_compile_time_config MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE
programs/test/query_compile_time_config MBEDTLS_SSL_PROTO_TLS1_2
programs/test/query_compile_time_config MBEDTLS_SSL_RENEGOTIATION
programs/test/query_compile_time_config MBEDTLS_DEBUG_C
programs/test/query_compile_time_config MBEDTLS_ERROR_C
not programs/test/query_compile_time_config MBEDTLS_SSL_SRV_C
[ "$(programs/test/query_compile_time_config MBEDTLS_SSL_IN_CONTENT_LEN)" = \
"12000" ]