blob: ce923b5cc41238b64bdd4fdd7983842daf17ef6c [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
Ronald Cron401f20f2025-09-02 14:50:10 +020014 $MAKE_COMMAND 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
Ronald Cron401f20f2025-09-02 14:50:10 +020061 $MAKE_COMMAND neat
Minos Galanakis5357def2024-07-26 14:18:23 +010062
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
Ronald Cron401f20f2025-09-02 14:50:10 +020093 $MAKE_COMMAND neat
Minos Galanakis5357def2024-07-26 14:18:23 +010094
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
Ronald Cron401f20f2025-09-02 14:50:10 +0200110 $MAKE_COMMAND neat
Minos Galanakis5357def2024-07-26 14:18:23 +0100111
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
Ronald Cron401f20f2025-09-02 14:50:10 +0200135 $MAKE_COMMAND neat
Minos Galanakis5357def2024-07-26 14:18:23 +0100136
137 msg "build: cmake 'as-installed-package' build"
138 cd programs/test/cmake_package_install
139 cmake .
140 make
Ronald Cron35d59c62025-09-19 17:16:01 +0200141
142 if ! cmp -s "mbedtls/lib/libtfpsacrypto.a" "mbedtls/lib/libmbedcrypto.a"; then
143 echo "Error: Crypto static libraries are different or one of them is missing/unreadable." >&2
144 exit 1
145 fi
146 if ! cmp -s "mbedtls/lib/libtfpsacrypto.so" "mbedtls/lib/libmbedcrypto.so"; then
147 echo "Error: Crypto shared libraries are different or one of them is missing/unreadable." >&2
148 exit 1
149 fi
150
Minos Galanakis5357def2024-07-26 14:18:23 +0100151 ./cmake_package_install
152}
153
154support_test_cmake_as_package_install () {
155 support_test_cmake_out_of_source
156}
157
158component_build_cmake_custom_config_file () {
159 # Make a copy of config file to use for the in-tree test
160 cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
Minos Galanakis473b9602024-10-01 17:15:27 +0100161 cp "$CRYPTO_CONFIG_H" include/mbedtls_crypto_config_in_tree_copy.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100162
163 MBEDTLS_ROOT_DIR="$PWD"
164 mkdir "$OUT_OF_SOURCE_DIR"
165 cd "$OUT_OF_SOURCE_DIR"
166
167 # Build once to get the generated files (which need an intact config file)
168 cmake "$MBEDTLS_ROOT_DIR"
169 make
170
171 msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100172 cd "$MBEDTLS_ROOT_DIR"
173 scripts/config.py full
174 cp include/mbedtls/mbedtls_config.h $OUT_OF_SOURCE_DIR/full_config.h
175 cp tf-psa-crypto/include/psa/crypto_config.h $OUT_OF_SOURCE_DIR/full_crypto_config.h
176 cd "$OUT_OF_SOURCE_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +0100177 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000178 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 +0100179 make
180
Minos Galanakis473241e2024-12-02 17:53:01 +0000181 msg "build: cmake with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_USER_CONFIG_FILE"
Minos Galanakis5357def2024-07-26 14:18:23 +0100182 # In the user config, disable one feature (for simplicity, pick a feature
183 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100184 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000185 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100186
Minos Galanakis473241e2024-12-02 17:53:01 +0000187 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 +0100188 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100189 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000190 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100191
Minos Galanakis473b9602024-10-01 17:15:27 +0100192 rm -f user_config.h full_config.h full_crypto_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100193
194 cd "$MBEDTLS_ROOT_DIR"
195 rm -rf "$OUT_OF_SOURCE_DIR"
196
197 # Now repeat the test for an in-tree build:
198
199 # Restore config for the in-tree test
200 mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
Minos Galanakis473b9602024-10-01 17:15:27 +0100201 mv include/mbedtls_crypto_config_in_tree_copy.h "$CRYPTO_CONFIG_H"
Minos Galanakis5357def2024-07-26 14:18:23 +0100202
203 # Build once to get the generated files (which need an intact config)
204 cmake .
205 make
206
207 msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100208 cp include/mbedtls/mbedtls_config.h full_config.h
209 cp tf-psa-crypto/include/psa/crypto_config.h full_crypto_config.h
210
Minos Galanakis5357def2024-07-26 14:18:23 +0100211 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000212 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 +0100213 make
214
Minos Galanakis473241e2024-12-02 17:53:01 +0000215 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 +0100216 # In the user config, disable one feature (for simplicity, pick a feature
217 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100218 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000219 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100220
Minos Galanakis473241e2024-12-02 17:53:01 +0000221 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 +0100222 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100223 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000224 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100225
226 rm -f user_config.h full_config.h
227}
228
229support_build_cmake_custom_config_file () {
230 support_test_cmake_out_of_source
231}
232
233component_build_cmake_programs_no_testing () {
234 # Verify that the type of builds performed by oss-fuzz don't get accidentally broken
235 msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"
236 cmake -DENABLE_PROGRAMS=ON -DENABLE_TESTING=OFF .
237 make
238}
239support_build_cmake_programs_no_testing () {
240 support_test_cmake_out_of_source
241}