blob: 3047e762520141071b819409e98a88cd8176f9ed [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
16 programs/test/dlopen_demo.sh
17}
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
23 ldd programs/util/strerror | grep libmbedcrypto
24 make test
25 programs/test/dlopen_demo.sh
26}
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
Minos Galanakis308c7372024-08-07 15:00:28 +010088component_test_cmake_tf_psa_crypto_out_of_source () {
89 # Remove existing generated files so that we use the ones cmake
90 # generates
91 make neat
92 msg "build: cmake tf-psa-crypto 'out-of-source' build"
93 MBEDTLS_ROOT_DIR="$PWD"
94 cd tf-psa-crypto
95 TF_PSA_CRYPTO_ROOT_DIR="$PWD"
96 mkdir "$OUT_OF_SOURCE_DIR"
97 cd "$OUT_OF_SOURCE_DIR"
98 # Note: Explicitly generate files as these are turned off in releases
99 cmake -D CMAKE_BUILD_TYPE:String=Check -D GEN_FILES=ON "$TF_PSA_CRYPTO_ROOT_DIR"
100 make
101 msg "test: cmake tf-psa-crypto 'out-of-source' build"
102 make test
103 cd "$TF_PSA_CRYPTO_ROOT_DIR"
104 rm -rf "$OUT_OF_SOURCE_DIR"
105 cd "$MBEDTLS_ROOT_DIR"
106}
107
Minos Galanakis5357def2024-07-26 14:18:23 +0100108component_test_cmake_as_subdirectory () {
109 # Remove existing generated files so that we use the ones CMake
110 # generates
111 make neat
112
113 msg "build: cmake 'as-subdirectory' build"
114 cd programs/test/cmake_subproject
115 # Note: Explicitly generate files as these are turned off in releases
116 cmake -D GEN_FILES=ON .
117 make
118 ./cmake_subproject
119}
120
121support_test_cmake_as_subdirectory () {
122 support_test_cmake_out_of_source
123}
124
125component_test_cmake_as_package () {
126 # Remove existing generated files so that we use the ones CMake
127 # generates
128 make neat
129
130 msg "build: cmake 'as-package' build"
Bill Roberts10ff4172024-03-25 08:52:47 -0500131 root_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100132 cd programs/test/cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500133 build_variant_dir="$(pwd)"
Minos Galanakis5357def2024-07-26 14:18:23 +0100134 cmake .
135 make
136 ./cmake_package
Bill Roberts10ff4172024-03-25 08:52:47 -0500137 if [[ "$OSTYPE" == linux* ]]; then
Ronald Cron4870e612024-10-17 17:49:57 +0200138 PKG_CONFIG_PATH="${build_variant_dir}/mbedtls/pkgconfig" \
139 ${root_dir}/tests/scripts/pkgconfig.sh \
140 mbedtls mbedx509 mbedcrypto
141 # These are the EXPECTED package names. Renaming these could break
142 # consumers of pkg-config, consider carefully.
Bill Roberts10ff4172024-03-25 08:52:47 -0500143 fi
Minos Galanakis5357def2024-07-26 14:18:23 +0100144}
145
Ronald Cron4cd797e2024-10-17 17:50:32 +0200146component_test_tf_psa_crypto_cmake_as_package () {
147 # Remove existing generated files so that we use the ones CMake
148 # generates
149 make neat
150
151 msg "build: cmake 'as-package' build"
152 root_dir="$(pwd)"
153 cd tf-psa-crypto/programs/test/cmake_package
154 build_variant_dir="$(pwd)"
155 cmake .
156 make
157 ./cmake_package
158 if [[ "$OSTYPE" == linux* ]]; then
159 PKG_CONFIG_PATH="${build_variant_dir}/tf-psa-crypto/pkgconfig" \
160 ${root_dir}/tests/scripts/pkgconfig.sh \
161 tfpsacrypto
162 # This is the EXPECTED package name. Renaming it could break consumers
163 # of pkg-config, consider carefully.
164 fi
165}
166
Minos Galanakis5357def2024-07-26 14:18:23 +0100167support_test_cmake_as_package () {
168 support_test_cmake_out_of_source
169}
170
171component_test_cmake_as_package_install () {
172 # Remove existing generated files so that we use the ones CMake
173 # generates
174 make neat
175
176 msg "build: cmake 'as-installed-package' build"
177 cd programs/test/cmake_package_install
178 cmake .
179 make
180 ./cmake_package_install
181}
182
183support_test_cmake_as_package_install () {
184 support_test_cmake_out_of_source
185}
186
187component_build_cmake_custom_config_file () {
188 # Make a copy of config file to use for the in-tree test
189 cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
190
191 MBEDTLS_ROOT_DIR="$PWD"
192 mkdir "$OUT_OF_SOURCE_DIR"
193 cd "$OUT_OF_SOURCE_DIR"
194
195 # Build once to get the generated files (which need an intact config file)
196 cmake "$MBEDTLS_ROOT_DIR"
197 make
198
199 msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
200 scripts/config.py -w full_config.h full
201 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
202 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h "$MBEDTLS_ROOT_DIR"
203 make
204
205 msg "build: cmake with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
206 # In the user config, disable one feature (for simplicity, pick a feature
207 # that nothing else depends on).
208 echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
209
210 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h "$MBEDTLS_ROOT_DIR"
211 make
212 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
213
214 rm -f user_config.h full_config.h
215
216 cd "$MBEDTLS_ROOT_DIR"
217 rm -rf "$OUT_OF_SOURCE_DIR"
218
219 # Now repeat the test for an in-tree build:
220
221 # Restore config for the in-tree test
222 mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
223
224 # Build once to get the generated files (which need an intact config)
225 cmake .
226 make
227
228 msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
229 scripts/config.py -w full_config.h full
230 echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
231 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h .
232 make
233
234 msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
235 # In the user config, disable one feature (for simplicity, pick a feature
236 # that nothing else depends on).
237 echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
238
239 cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h .
240 make
241 not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
242
243 rm -f user_config.h full_config.h
244}
245
246support_build_cmake_custom_config_file () {
247 support_test_cmake_out_of_source
248}
249
250component_build_cmake_programs_no_testing () {
251 # Verify that the type of builds performed by oss-fuzz don't get accidentally broken
252 msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"
253 cmake -DENABLE_PROGRAMS=ON -DENABLE_TESTING=OFF .
254 make
255}
256support_build_cmake_programs_no_testing () {
257 support_test_cmake_out_of_source
258}