CMake: add support for config options

Signed-off-by: Yi Wu <[email protected]>
This commit is contained in:
Yi Wu
2026-08-10 08:58:34 +01:00
parent 35bc09db98
commit 1e8647ff92
3 changed files with 106 additions and 0 deletions
+65
View File
@@ -146,6 +146,71 @@ endif()
# Make MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE into PATHs
set(MBEDTLS_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS config file (overrides default).")
set(MBEDTLS_USER_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS user config file (appended to default).")
set(MBEDTLS_CONFIG_NAME "" CACHE STRING "Named Mbed TLS configuration (see config.py).")
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.")
if(MBEDTLS_CONFIG_NAME OR MBEDTLS_CONFIG_SET OR MBEDTLS_CONFIG_UNSET)
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/mbedtls")
set(MBEDTLS_GENERATED_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls_config.h")
file(MAKE_DIRECTORY "${MBEDTLS_GENERATED_CONFIG_DIR}")
configure_file("${MBEDTLS_BASE_CONFIG_FILE}"
"${MBEDTLS_GENERATED_CONFIG_FILE}" COPYONLY)
function(mbedtls_configure_generated_file)
execute_process(
COMMAND
${MBEDTLS_PYTHON_EXECUTABLE}
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py"
--file "${MBEDTLS_GENERATED_CONFIG_FILE}"
${ARGN}
RESULT_VARIABLE result
)
if(result)
message(FATAL_ERROR "Failed to configure the generated Mbed TLS configuration file")
endif()
endfunction(mbedtls_configure_generated_file)
if(MBEDTLS_CONFIG_NAME)
mbedtls_configure_generated_file("${MBEDTLS_CONFIG_NAME}")
endif()
foreach(option IN LISTS MBEDTLS_CONFIG_UNSET)
mbedtls_configure_generated_file(unset "${option}")
endforeach(option)
foreach(option IN LISTS MBEDTLS_CONFIG_SET)
string(FIND "${option}" "=" separator)
if(separator EQUAL -1)
mbedtls_configure_generated_file(--force set "${option}")
else()
string(SUBSTRING "${option}" 0 ${separator} name)
math(EXPR value_start "${separator} + 1")
string(SUBSTRING "${option}" ${value_start} -1 value)
mbedtls_configure_generated_file(--force set "${name}" "${value}")
endif()
endforeach(option)
set(MBEDTLS_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_FILE}")
endif()
# Create a symbolic link from ${base_name} in the binary directory
# to the corresponding path in the source directory.
+5
View File
@@ -0,0 +1,5 @@
Features
* Add the `MBEDTLS_CONFIG_NAME`, `MBEDTLS_CONFIG_SET` and
`MBEDTLS_CONFIG_UNSET` CMake options for selecting and customizing
compile-time configurations, including configurations based on a custom
`MBEDTLS_CONFIG_FILE`. Fixes #10838.
+36
View File
@@ -320,6 +320,42 @@ support_build_cmake_custom_config_file () {
support_test_cmake_out_of_source
}
component_build_cmake_config_options () {
MBEDTLS_ROOT_DIR="$PWD"
mkdir "$OUT_OF_SOURCE_DIR"
mkdir "$OUT_OF_SOURCE_DIR/config-name"
cd "$OUT_OF_SOURCE_DIR/config-name"
msg "configure: cmake with MBEDTLS_CONFIG_NAME"
cmake -DMBEDTLS_CONFIG_NAME=full "$MBEDTLS_ROOT_DIR"
grep '^#define MBEDTLS_SSL_PROTO_TLS1_3$' \
generated/include/mbedtls/mbedtls_config.h
mkdir "$OUT_OF_SOURCE_DIR/config-options"
cd "$OUT_OF_SOURCE_DIR/config-options"
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_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" ]
cd "$MBEDTLS_ROOT_DIR"
rm -rf "$OUT_OF_SOURCE_DIR"
}
support_build_cmake_config_options () {
support_test_cmake_out_of_source
}
component_build_cmake_programs_no_testing () {
# Verify that the type of builds performed by oss-fuzz don't get accidentally broken
msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"