blob: 3108aa7b9280379b239b6dc4059006c0280e0fe2 [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
Gilles Peskine236e05d2024-09-11 17:51:11 +020068 cmake -D CMAKE_BUILD_TYPE:String=Check -D GEN_FILES=ON -D TEST_CPP=1 "$MBEDTLS_ROOT_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +010069 make
70
71 msg "test: cmake 'out-of-source' build"
72 make test
73 # Check that ssl-opt.sh can find the test programs.
74 # Also ensure that there are no error messages such as
75 # "No such file or directory", which would indicate that some required
76 # file is missing (ssl-opt.sh tolerates the absence of some files so
77 # may exit with status 0 but emit errors).
78 ./tests/ssl-opt.sh -f 'Default' >ssl-opt.out 2>ssl-opt.err
79 grep PASS ssl-opt.out
80 cat ssl-opt.err >&2
81 # If ssl-opt.err is non-empty, record an error and keep going.
82 [ ! -s ssl-opt.err ]
83 rm ssl-opt.out ssl-opt.err
84 cd "$MBEDTLS_ROOT_DIR"
85 rm -rf "$OUT_OF_SOURCE_DIR"
86}
87
88component_test_cmake_as_subdirectory () {
89 # Remove existing generated files so that we use the ones CMake
90 # generates
91 make neat
92
93 msg "build: cmake 'as-subdirectory' build"
94 cd programs/test/cmake_subproject
95 # Note: Explicitly generate files as these are turned off in releases
96 cmake -D GEN_FILES=ON .
97 make
98 ./cmake_subproject
99}
100
101support_test_cmake_as_subdirectory () {
102 support_test_cmake_out_of_source
103}
104
105component_test_cmake_as_package () {
106 # Remove existing generated files so that we use the ones CMake
107 # generates
108 make neat
109
110 msg "build: cmake 'as-package' build"
Bill Roberts10ff4172024-03-25 08:52:47 -0500111 root_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100112 cd programs/test/cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500113 build_variant_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100114 cmake .
115 make
116 ./cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500117 if [[ "$OSTYPE" == linux* ]]; then
Ronald Cron4870e612024-10-17 17:49:57 +0200118 PKG_CONFIG_PATH="${build_variant_dir}/mbedtls/pkgconfig" \
Valerio Settiba8500b2025-01-09 14:27:42 +0100119 ${root_dir}/framework/scripts/pkgconfig.sh \
Ronald Cron4870e612024-10-17 17:49:57 +0200120 mbedtls mbedx509 mbedcrypto
121 # These are the EXPECTED package names. Renaming these could break
122 # consumers of pkg-config, consider carefully.
Bill Roberts10ff4172024-03-25 08:52:47 -0500123 fi
Minos Galanakis5357def2024-07-26 14:18:23 +0100124}
125
126support_test_cmake_as_package () {
127 support_test_cmake_out_of_source
128}
129
130component_test_cmake_as_package_install () {
131 # Remove existing generated files so that we use the ones CMake
132 # generates
133 make neat
134
135 msg "build: cmake 'as-installed-package' build"
136 cd programs/test/cmake_package_install
137 cmake .
138 make
139 ./cmake_package_install
140}
141
142support_test_cmake_as_package_install () {
143 support_test_cmake_out_of_source
144}
145
146component_build_cmake_custom_config_file () {
147 # Make a copy of config file to use for the in-tree test
148 cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
Minos Galanakis473b9602024-10-01 17:15:27 +0100149 cp "$CRYPTO_CONFIG_H" include/mbedtls_crypto_config_in_tree_copy.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100150
151 MBEDTLS_ROOT_DIR="$PWD"
152 mkdir "$OUT_OF_SOURCE_DIR"
153 cd "$OUT_OF_SOURCE_DIR"
154
155 # Build once to get the generated files (which need an intact config file)
156 cmake "$MBEDTLS_ROOT_DIR"
157 make
158
159 msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100160 cd "$MBEDTLS_ROOT_DIR"
161 scripts/config.py full
162 cp include/mbedtls/mbedtls_config.h $OUT_OF_SOURCE_DIR/full_config.h
163 cp tf-psa-crypto/include/psa/crypto_config.h $OUT_OF_SOURCE_DIR/full_crypto_config.h
164 cd "$OUT_OF_SOURCE_DIR"
Minos Galanakis5357def2024-07-26 14:18:23 +0100165 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000166 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 +0100167 make
168
Minos Galanakis473241e2024-12-02 17:53:01 +0000169 msg "build: cmake with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_USER_CONFIG_FILE"
Minos Galanakis5357def2024-07-26 14:18:23 +0100170 # In the user config, disable one feature (for simplicity, pick a feature
171 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100172 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000173 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100174
Minos Galanakis473241e2024-12-02 17:53:01 +0000175 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 +0100176 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100177 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000178 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100179
Minos Galanakis473b9602024-10-01 17:15:27 +0100180 rm -f user_config.h full_config.h full_crypto_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100181
182 cd "$MBEDTLS_ROOT_DIR"
183 rm -rf "$OUT_OF_SOURCE_DIR"
184
185 # Now repeat the test for an in-tree build:
186
187 # Restore config for the in-tree test
188 mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
Minos Galanakis473b9602024-10-01 17:15:27 +0100189 mv include/mbedtls_crypto_config_in_tree_copy.h "$CRYPTO_CONFIG_H"
Minos Galanakis5357def2024-07-26 14:18:23 +0100190
191 # Build once to get the generated files (which need an intact config)
192 cmake .
193 make
194
195 msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
Minos Galanakis473b9602024-10-01 17:15:27 +0100196 cp include/mbedtls/mbedtls_config.h full_config.h
197 cp tf-psa-crypto/include/psa/crypto_config.h full_crypto_config.h
198
Minos Galanakis5357def2024-07-26 14:18:23 +0100199 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
Minos Galanakis473241e2024-12-02 17:53:01 +0000200 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 +0100201 make
202
Minos Galanakis473241e2024-12-02 17:53:01 +0000203 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 +0100204 # In the user config, disable one feature (for simplicity, pick a feature
205 # that nothing else depends on).
Minos Galanakis981d7d62024-10-10 02:16:12 +0100206 echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
Minos Galanakis473241e2024-12-02 17:53:01 +0000207 echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
Minos Galanakis5357def2024-07-26 14:18:23 +0100208
Minos Galanakis473241e2024-12-02 17:53:01 +0000209 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 +0100210 make
Minos Galanakis981d7d62024-10-10 02:16:12 +0100211 not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
Minos Galanakis473241e2024-12-02 17:53:01 +0000212 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
Minos Galanakis5357def2024-07-26 14:18:23 +0100213
214 rm -f user_config.h full_config.h
215}
216
217support_build_cmake_custom_config_file () {
218 support_test_cmake_out_of_source
219}
220
221component_build_cmake_programs_no_testing () {
222 # Verify that the type of builds performed by oss-fuzz don't get accidentally broken
223 msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"
224 cmake -DENABLE_PROGRAMS=ON -DENABLE_TESTING=OFF .
225 make
226}
227support_build_cmake_programs_no_testing () {
228 support_test_cmake_out_of_source
229}