mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-09-13 13:49:55 +00:00
CMake improvements: relocatable, transformation, and more test
Signed-off-by: Yi Wu <[email protected]>
This commit is contained in:
+53
-13
@@ -143,25 +143,54 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||
FORCE)
|
||||
endif()
|
||||
|
||||
# Make MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE into PATHs
|
||||
# Make configuration file options 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_BASE_FILE "" CACHE FILEPATH "Mbed TLS config to copy and optionally transform.")
|
||||
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)
|
||||
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
|
||||
NOT "${MBEDTLS_CONFIG_SET}" STREQUAL "" OR
|
||||
NOT "${MBEDTLS_CONFIG_UNSET}" STREQUAL ""))
|
||||
message(FATAL_ERROR
|
||||
"MBEDTLS_CONFIG_FILE cannot be combined with MBEDTLS_CONFIG_BASE_FILE, "
|
||||
"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
|
||||
NOT "${MBEDTLS_CONFIG_UNSET}" STREQUAL "")
|
||||
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
message(FATAL_ERROR
|
||||
"MBEDTLS_CONFIG_BASE_FILE, MBEDTLS_CONFIG_NAME, "
|
||||
"MBEDTLS_CONFIG_SET and MBEDTLS_CONFIG_UNSET are not supported "
|
||||
"in an in-tree build.")
|
||||
endif()
|
||||
|
||||
if(MBEDTLS_CONFIG_TRANSFORMED AND
|
||||
"${MBEDTLS_PYTHON_EXECUTABLE}" STREQUAL "")
|
||||
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 "${MBEDTLS_CONFIG_BASE_FILE}" STREQUAL "")
|
||||
set(MBEDTLS_BASE_CONFIG_FILE "${MBEDTLS_CONFIG_BASE_FILE}")
|
||||
if(NOT EXISTS "${MBEDTLS_BASE_CONFIG_FILE}")
|
||||
file(RELATIVE_PATH MBEDTLS_CONFIG_FILE_RELATIVE
|
||||
"${CMAKE_CURRENT_BINARY_DIR}" "${MBEDTLS_CONFIG_FILE}")
|
||||
file(RELATIVE_PATH MBEDTLS_CONFIG_BASE_FILE_RELATIVE
|
||||
"${CMAKE_CURRENT_BINARY_DIR}" "${MBEDTLS_CONFIG_BASE_FILE}")
|
||||
set(MBEDTLS_BASE_CONFIG_FILE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${MBEDTLS_CONFIG_FILE_RELATIVE}")
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${MBEDTLS_CONFIG_BASE_FILE_RELATIVE}")
|
||||
endif()
|
||||
else()
|
||||
set(MBEDTLS_BASE_CONFIG_FILE
|
||||
@@ -171,18 +200,28 @@ if(MBEDTLS_CONFIG_NAME OR MBEDTLS_CONFIG_SET OR MBEDTLS_CONFIG_UNSET)
|
||||
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}")
|
||||
set(MBEDTLS_GENERATED_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/include")
|
||||
set(MBEDTLS_GENERATED_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls/mbedtls_config.h")
|
||||
file(MAKE_DIRECTORY "${MBEDTLS_GENERATED_CONFIG_DIR}/mbedtls")
|
||||
configure_file("${MBEDTLS_BASE_CONFIG_FILE}"
|
||||
"${MBEDTLS_GENERATED_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")
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/psa/crypto_config.h"
|
||||
"${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}" COPYONLY)
|
||||
endif()
|
||||
|
||||
function(mbedtls_configure_generated_file)
|
||||
execute_process(
|
||||
COMMAND
|
||||
${MBEDTLS_PYTHON_EXECUTABLE}
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py"
|
||||
--file "${MBEDTLS_GENERATED_CONFIG_FILE}"
|
||||
--cryptofile "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}"
|
||||
${ARGN}
|
||||
RESULT_VARIABLE result
|
||||
)
|
||||
@@ -191,7 +230,7 @@ if(MBEDTLS_CONFIG_NAME OR MBEDTLS_CONFIG_SET OR MBEDTLS_CONFIG_UNSET)
|
||||
endif()
|
||||
endfunction(mbedtls_configure_generated_file)
|
||||
|
||||
if(MBEDTLS_CONFIG_NAME)
|
||||
if(NOT "${MBEDTLS_CONFIG_NAME}" STREQUAL "")
|
||||
mbedtls_configure_generated_file("${MBEDTLS_CONFIG_NAME}")
|
||||
endif()
|
||||
foreach(option IN LISTS MBEDTLS_CONFIG_UNSET)
|
||||
@@ -209,7 +248,8 @@ if(MBEDTLS_CONFIG_NAME OR MBEDTLS_CONFIG_SET OR MBEDTLS_CONFIG_UNSET)
|
||||
endif()
|
||||
endforeach(option)
|
||||
|
||||
set(MBEDTLS_CONFIG_FILE "${MBEDTLS_GENERATED_CONFIG_FILE}")
|
||||
set(MBEDTLS_CONFIG_FILE
|
||||
"$<BUILD_INTERFACE:${MBEDTLS_GENERATED_CONFIG_FILE}>$<INSTALL_INTERFACE:mbedtls/mbedtls_config.h>")
|
||||
endif()
|
||||
|
||||
# Create a symbolic link from ${base_name} in the binary directory
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
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.
|
||||
* Add the `MBEDTLS_CONFIG_BASE_FILE`, `MBEDTLS_CONFIG_NAME`,
|
||||
`MBEDTLS_CONFIG_SET` and `MBEDTLS_CONFIG_UNSET` CMake options for
|
||||
selecting and customizing compile-time configurations. Fixes #10838.
|
||||
|
||||
@@ -5,6 +5,15 @@ if(INSTALL_MBEDTLS_HEADERS)
|
||||
file(GLOB headers "mbedtls/*.h")
|
||||
file(GLOB psa_headers "psa/*.h")
|
||||
|
||||
if(NOT "${MBEDTLS_GENERATED_CONFIG_FILE}" STREQUAL "")
|
||||
list(REMOVE_ITEM headers
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/mbedtls/mbedtls_config.h")
|
||||
endif()
|
||||
if(NOT "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}" STREQUAL "")
|
||||
list(REMOVE_ITEM psa_headers
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/psa/crypto_config.h")
|
||||
endif()
|
||||
|
||||
install(FILES ${headers}
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mbedtls
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
||||
@@ -13,6 +22,17 @@ if(INSTALL_MBEDTLS_HEADERS)
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/psa
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
||||
|
||||
if(NOT "${MBEDTLS_GENERATED_CONFIG_FILE}" STREQUAL "")
|
||||
install(FILES "${MBEDTLS_GENERATED_CONFIG_FILE}"
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mbedtls
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
||||
endif()
|
||||
if(NOT "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}" STREQUAL "")
|
||||
install(FILES "${MBEDTLS_GENERATED_CRYPTO_CONFIG_FILE}"
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/psa
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
||||
endif()
|
||||
|
||||
endif(INSTALL_MBEDTLS_HEADERS)
|
||||
|
||||
# Make mbedtls_config.h available in an out-of-source build. ssl-opt.sh requires it.
|
||||
|
||||
@@ -342,6 +342,10 @@ endif(USE_SHARED_MBEDTLS_LIBRARY)
|
||||
|
||||
foreach(target IN LISTS target_libraries)
|
||||
add_library(MbedTLS::${target} ALIAS ${target}) # add_subdirectory support
|
||||
if(MBEDTLS_CONFIG_TRANSFORMED)
|
||||
target_include_directories(${target}
|
||||
PUBLIC $<BUILD_INTERFACE:${MBEDTLS_GENERATED_CONFIG_DIR}>)
|
||||
endif()
|
||||
# Include public header files from /include and other directories
|
||||
# declared by /3rdparty/**/CMakeLists.txt. Include private header files
|
||||
# from /library and others declared by /3rdparty/**/CMakeLists.txt.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Simple program to test an installed Mbed TLS configuration.
|
||||
*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "mbedtls/build_info.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -323,19 +323,63 @@ support_build_cmake_custom_config_file () {
|
||||
component_build_cmake_config_options () {
|
||||
MBEDTLS_ROOT_DIR="$PWD"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$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
|
||||
make query_compile_time_config
|
||||
programs/test/query_compile_time_config MBEDTLS_TEST_HOOKS
|
||||
|
||||
cd "$MBEDTLS_ROOT_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$OUT_OF_SOURCE_DIR"
|
||||
|
||||
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 '^MBEDTLS_CONFIG_FILE:FILEPATH=$' CMakeCache.txt
|
||||
|
||||
cd "$MBEDTLS_ROOT_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$OUT_OF_SOURCE_DIR"
|
||||
|
||||
msg "configure: cmake with MBEDTLS_CONFIG_BASE_FILE only"
|
||||
cmake -DMBEDTLS_CONFIG_BASE_FILE=configs/config-ccm-psk-tls1_2.h \
|
||||
"$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"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$OUT_OF_SOURCE_DIR"
|
||||
|
||||
msg "configure: reject MBEDTLS_CONFIG_FILE with transformations"
|
||||
not cmake -DMBEDTLS_CONFIG_FILE=configs/config-ccm-psk-tls1_2.h \
|
||||
-DMBEDTLS_CONFIG_SET=MBEDTLS_DEBUG_C "$MBEDTLS_ROOT_DIR"
|
||||
|
||||
cd "$MBEDTLS_ROOT_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$OUT_OF_SOURCE_DIR"
|
||||
|
||||
msg "configure: reject a missing base configuration"
|
||||
not cmake -DMBEDTLS_CONFIG_BASE_FILE=configs/does-not-exist.h \
|
||||
"$MBEDTLS_ROOT_DIR"
|
||||
|
||||
cd "$MBEDTLS_ROOT_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR"
|
||||
mkdir "$OUT_OF_SOURCE_DIR"
|
||||
cd "$OUT_OF_SOURCE_DIR"
|
||||
|
||||
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 \
|
||||
cp "$MBEDTLS_ROOT_DIR/configs/config-ccm-psk-tls1_2.h" base_config.before
|
||||
cmake -DMBEDTLS_CONFIG_BASE_FILE=configs/config-ccm-psk-tls1_2.h \
|
||||
'-DMBEDTLS_CONFIG_UNSET=MBEDTLS_SSL_SRV_C;PSA_WANT_ALG_CMAC;PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128' \
|
||||
'-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
|
||||
@@ -345,11 +389,48 @@ component_build_cmake_config_options () {
|
||||
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
|
||||
not programs/test/query_compile_time_config PSA_WANT_ALG_CMAC
|
||||
[ "$(programs/test/query_compile_time_config MBEDTLS_SSL_IN_CONTENT_LEN)" = \
|
||||
"12000" ]
|
||||
cmp base_config.before \
|
||||
"$MBEDTLS_ROOT_DIR/configs/config-ccm-psk-tls1_2.h"
|
||||
|
||||
msg "install: generated configurations are relocatable"
|
||||
install_dir="$OUT_OF_SOURCE_DIR.install"
|
||||
cmake -DCMAKE_INSTALL_PREFIX="$install_dir" .
|
||||
cmake --build . --target install
|
||||
cmp generated/include/mbedtls/mbedtls_config.h \
|
||||
"$install_dir/include/mbedtls/mbedtls_config.h"
|
||||
cmp generated/include/psa/crypto_config.h \
|
||||
"$install_dir/include/psa/crypto_config.h"
|
||||
|
||||
# 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 <<EOF
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(consumer C)
|
||||
find_package(MbedTLS REQUIRED CONFIG)
|
||||
add_executable(consumer
|
||||
"$MBEDTLS_ROOT_DIR/programs/test/cmake_package_install/cmake_package_config.c")
|
||||
target_link_libraries(consumer PRIVATE
|
||||
MbedTLS::mbedtls MbedTLS::mbedx509 MbedTLS::mbedcrypto)
|
||||
EOF
|
||||
mkdir consumer-build
|
||||
cd consumer-build
|
||||
cmake -DCMAKE_PREFIX_PATH="$install_dir" ../consumer
|
||||
cmake --build .
|
||||
cd ..
|
||||
|
||||
cd "$MBEDTLS_ROOT_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR"
|
||||
rm -rf "$OUT_OF_SOURCE_DIR" "$OUT_OF_SOURCE_DIR.moved" "$install_dir"
|
||||
|
||||
msg "configure: reject transformations in an in-tree build"
|
||||
not cmake -DMBEDTLS_CONFIG_SET=NO .
|
||||
rm -rf CMakeCache.txt CMakeFiles
|
||||
}
|
||||
|
||||
support_build_cmake_config_options () {
|
||||
|
||||
Reference in New Issue
Block a user