blob: e533cdf0f97e491bf2b793b273c413498ae4106f [file] [log] [blame]
Minos Galanakis6aab5b72024-07-25 14:24:37 +01001# components-build-system.sh
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
Minos Galanakis609f7492024-07-31 16:39:28 +01006# This file contains test components that are executed by all.sh
Minos Galanakis6aab5b72024-07-25 14:24:37 +01007
8################################################################
9#### Build System Testing
10################################################################
11
Minos Galanakis5357def2024-07-26 14:18:23 +010012component_test_make_shared () {
13 msg "build/test: make shared" # ~ 40s
Gilles Peskine62ee8fd2024-06-06 22:12:06 +020014 make SHARED=1 TEST_CPP=1 all check
Minos Galanakis5357def2024-07-26 14:18:23 +010015 ldd programs/util/strerror | grep libmbedcrypto
Harry Ramseyd0967932025-02-12 20:29:33 +000016 $FRAMEWORK/tests/programs/dlopen_demo.sh
Minos Galanakis5357def2024-07-26 14:18:23 +010017}
Minos Galanakis6aab5b72024-07-25 14:24:37 +010018
Minos Galanakis5357def2024-07-26 14:18:23 +010019component_test_cmake_shared () {
20 msg "build/test: cmake shared" # ~ 2min
21 cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On .
22 make
Ronald Cron8126a682024-10-25 17:34:23 +020023 ldd programs/util/strerror | grep libtfpsacrypto
Minos Galanakis5357def2024-07-26 14:18:23 +010024 make test
Harry Ramseyd0967932025-02-12 20:29:33 +000025 $FRAMEWORK/tests/programs/dlopen_demo.sh
Minos Galanakis5357def2024-07-26 14:18:23 +010026}
27
28support_test_cmake_out_of_source () {
29 distrib_id=""
30 distrib_ver=""
31 distrib_ver_minor=""
32 distrib_ver_major=""
33
34 # Attempt to parse lsb-release to find out distribution and version. If not
35 # found this should fail safe (test is supported).
36 if [[ -f /etc/lsb-release ]]; then
37
38 while read -r lsb_line; do
39 case "$lsb_line" in
40 "DISTRIB_ID"*) distrib_id=${lsb_line/#DISTRIB_ID=};;
41 "DISTRIB_RELEASE"*) distrib_ver=${lsb_line/#DISTRIB_RELEASE=};;
42 esac
43 done < /etc/lsb-release
44
45 distrib_ver_major="${distrib_ver%%.*}"
46 distrib_ver="${distrib_ver#*.}"
47 distrib_ver_minor="${distrib_ver%%.*}"
48 fi
49
50 # Running the out of source CMake test on Ubuntu 16.04 using more than one
51 # processor (as the CI does) can create a race condition whereby the build
52 # fails to see a generated file, despite that file actually having been
53 # generated. This problem appears to go away with 18.04 or newer, so make
54 # the out of source tests unsupported on Ubuntu 16.04.
55 [ "$distrib_id" != "Ubuntu" ] || [ "$distrib_ver_major" -gt 16 ]
56}
57
58component_test_cmake_out_of_source () {
59 # Remove existing generated files so that we use the ones cmake
60 # generates
61 make neat
62
63 msg "build: cmake 'out-of-source' build"
64 MBEDTLS_ROOT_DIR="$PWD"
65 mkdir "$OUT_OF_SOURCE_DIR"
66 cd "$OUT_OF_SOURCE_DIR"
67 # Note: Explicitly generate files as these are turned off in releases
Valerio Settib13d29e2025-04-18 18:11:17 +020068 # Note: Use Clang compiler also for C++ (C uses it by default)
69 CXX=clang++ cmake -D CMAKE_BUILD_TYPE:String=Check -D GEN_FILES=ON \
70 -D TEST_CPP=1 "$MBEDTLS_ROOT_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +010071 make
72
73 msg "test: cmake 'out-of-source' build"
74 make test
75 # Check that ssl-opt.sh can find the test programs.
76 # Also ensure that there are no error messages such as
77 # "No such file or directory", which would indicate that some required
78 # file is missing (ssl-opt.sh tolerates the absence of some files so
79 # may exit with status 0 but emit errors).
80 ./tests/ssl-opt.sh -f 'Default' >ssl-opt.out 2>ssl-opt.err
81 grep PASS ssl-opt.out
82 cat ssl-opt.err >&2
83 # If ssl-opt.err is non-empty, record an error and keep going.
84 [ ! -s ssl-opt.err ]
85 rm ssl-opt.out ssl-opt.err
86 cd "$MBEDTLS_ROOT_DIR"
87 rm -rf "$OUT_OF_SOURCE_DIR"
88}
89
90component_test_cmake_as_subdirectory () {
91 # Remove existing generated files so that we use the ones CMake
92 # generates
93 make neat
94
95 msg "build: cmake 'as-subdirectory' build"
96 cd programs/test/cmake_subproject
97 # Note: Explicitly generate files as these are turned off in releases
98 cmake -D GEN_FILES=ON .
99 make
100 ./cmake_subproject
101}
102
103support_test_cmake_as_subdirectory () {
104 support_test_cmake_out_of_source
105}
106
107component_test_cmake_as_package () {
108 # Remove existing generated files so that we use the ones CMake
109 # generates
110 make neat
111
112 msg "build: cmake 'as-package' build"
Bill Roberts10ff4172024-03-25 08:52:47 -0500113 root_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100114 cd programs/test/cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500115 build_variant_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100116 cmake .
117 make
118 ./cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500119 if [[ "$OSTYPE" == linux* ]]; then
Ronald Cron4870e612024-10-17 17:49:57 +0200120 PKG_CONFIG_PATH="${build_variant_dir}/mbedtls/pkgconfig" \
Valerio Settiba8500b2025-01-09 14:27:42 +0100121 ${root_dir}/framework/scripts/pkgconfig.sh \
Ronald Cron4870e612024-10-17 17:49:57 +0200122 mbedtls mbedx509 mbedcrypto
123 # These are the EXPECTED package names. Renaming these could break
124 # consumers of pkg-config, consider carefully.
Bill Roberts10ff4172024-03-25 08:52:47 -0500125 fi
Minos Galanakis5357def2024-07-26 14:18:23 +0100126}
127
128support_test_cmake_as_package () {
129 support_test_cmake_out_of_source
130}
131
132component_test_cmake_as_package_install () {
133 # Remove existing generated files so that we use the ones CMake
134 # generates
135 make neat
136
137 msg "build: cmake 'as-installed-package' build"
138 cd programs/test/cmake_package_install
139 cmake .
140 make
141 ./cmake_package_install
142}
143
144support_test_cmake_as_package_install () {
145 support_test_cmake_out_of_source
146}
147
148component_build_cmake_custom_config_file () {
149 # Make a copy of config file to use for the in-tree test
150 cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
Minos Galanakis473b9602024-10-01 17:15:27 +0100151 cp "$CRYPTO_CONFIG_H" include/mbedtls_crypto_config_in_tree_copy.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100152
153 MBEDTLS_ROOT_DIR="$PWD"
154 mkdir "$OUT_OF_SOURCE_DIR"
155 cd "$OUT_OF_SOURCE_DIR"
156
157 # Build once to get the generated files (which need an intact config file)
158 cmake "$MBEDTLS_ROOT_DIR"
159 make
160
161 msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100162 cd "$MBEDTLS_ROOT_DIR"
163 scripts/config.py full
164 cp include/mbedtls/mbedtls_config.h $OUT_OF_SOURCE_DIR/full_config.h
165 cp tf-psa-crypto/include/psa/crypto_config.h $OUT_OF_SOURCE_DIR/full_crypto_config.h
166 cd "$OUT_OF_SOURCE_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +0100167 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000168 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h "$MBEDTLS_ROOT_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +0100169 make
170
Minos Galanakis473241e2024-12-02 17:53:01 +0000171 msg "build: cmake with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_USER_CONFIG_FILE"
Minos Galanakis5357def2024-07-26 14:18:23 +0100172 # In the user config, disable one feature (for simplicity, pick a feature
173 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100174 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000175 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100176
Minos Galanakis473241e2024-12-02 17:53:01 +0000177 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h -DTF_PSA_CRYPTO_USER_CONFIG_FILE=crypto_user_config.h "$MBEDTLS_ROOT_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +0100178 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100179 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000180 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100181
Minos Galanakis473b9602024-10-01 17:15:27 +0100182 rm -f user_config.h full_config.h full_crypto_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100183
184 cd "$MBEDTLS_ROOT_DIR"
185 rm -rf "$OUT_OF_SOURCE_DIR"
186
187 # Now repeat the test for an in-tree build:
188
189 # Restore config for the in-tree test
190 mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
Minos Galanakis473b9602024-10-01 17:15:27 +0100191 mv include/mbedtls_crypto_config_in_tree_copy.h "$CRYPTO_CONFIG_H"
Minos Galanakis5357def2024-07-26 14:18:23 +0100192
193 # Build once to get the generated files (which need an intact config)
194 cmake .
195 make
196
197 msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100198 cp include/mbedtls/mbedtls_config.h full_config.h
199 cp tf-psa-crypto/include/psa/crypto_config.h full_crypto_config.h
200
Minos Galanakis5357def2024-07-26 14:18:23 +0100201 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000202 cmake -DGEN_FILES=OFF -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h -DMBEDTLS_CONFIG_FILE=full_config.h .
Minos Galanakis5357def2024-07-26 14:18:23 +0100203 make
204
Minos Galanakis473241e2024-12-02 17:53:01 +0000205 msg "build: cmake (in-tree) with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_USER_CONFIG_FILE"
Minos Galanakis5357def2024-07-26 14:18:23 +0100206 # In the user config, disable one feature (for simplicity, pick a feature
207 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100208 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000209 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100210
Minos Galanakis473241e2024-12-02 17:53:01 +0000211 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h -DTF_PSA_CRYPTO_USER_CONFIG_FILE=crypto_user_config.h .
Minos Galanakis5357def2024-07-26 14:18:23 +0100212 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100213 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000214 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100215
216 rm -f user_config.h full_config.h
217}
218
219support_build_cmake_custom_config_file () {
220 support_test_cmake_out_of_source
221}
222
223component_build_cmake_programs_no_testing () {
224 # Verify that the type of builds performed by oss-fuzz don't get accidentally broken
225 msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"
226 cmake -DENABLE_PROGRAMS=ON -DENABLE_TESTING=OFF .
227 make
228}
229support_build_cmake_programs_no_testing () {
230 support_test_cmake_out_of_source
231}