1# 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
6# This file contains test components that are executed by all.sh
7
8################################################################
9#### Build System Testing
10################################################################
11
12component_test_make_shared () {
13    msg "build/test: make shared" # ~ 40s
14    make SHARED=1 TEST_CPP=1 all check
15    ldd programs/util/strerror | grep libmbedcrypto
16    $FRAMEWORK/tests/programs/dlopen_demo.sh
17}
18
19component_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 libtfpsacrypto
24    make test
25    $FRAMEWORK/tests/programs/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
68    # 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"
71    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"
113    root_dir="$(pwd)"
114    cd programs/test/cmake_package
115    build_variant_dir="$(pwd)"
116    cmake .
117    make
118    ./cmake_package
119    if [[ "$OSTYPE" == linux* ]]; then
120        PKG_CONFIG_PATH="${build_variant_dir}/mbedtls/pkgconfig" \
121        ${root_dir}/framework/scripts/pkgconfig.sh \
122        mbedtls mbedx509 mbedcrypto
123        # These are the EXPECTED package names. Renaming these could break
124        # consumers of pkg-config, consider carefully.
125    fi
126}
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
151    cp "$CRYPTO_CONFIG_H" include/mbedtls_crypto_config_in_tree_copy.h
152
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"
162    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"
167    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
168    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h "$MBEDTLS_ROOT_DIR"
169    make
170
171    msg "build: cmake with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_USER_CONFIG_FILE"
172    # In the user config, disable one feature (for simplicity, pick a feature
173    # that nothing else depends on).
174    echo '#undef MBEDTLS_SSL_ALL_ALERT_MESSAGES' >user_config.h
175    echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
176
177    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"
178    make
179    not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
180    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
181
182    rm -f user_config.h full_config.h full_crypto_config.h
183
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"
191    mv include/mbedtls_crypto_config_in_tree_copy.h "$CRYPTO_CONFIG_H"
192
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"
198    cp include/mbedtls/mbedtls_config.h full_config.h
199    cp tf-psa-crypto/include/psa/crypto_config.h full_crypto_config.h
200
201    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
202    cmake -DGEN_FILES=OFF -DTF_PSA_CRYPTO_CONFIG_FILE=full_crypto_config.h -DMBEDTLS_CONFIG_FILE=full_config.h .
203    make
204
205    msg "build: cmake (in-tree) with -DMBEDTLS/TF_PSA_CRYPTO_CONFIG_FILE + -DMBEDTLS/TF_PSA_CRYPTO_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_SSL_ALL_ALERT_MESSAGES' >user_config.h
209    echo '#undef MBEDTLS_NIST_KW_C' >crypto_user_config.h
210
211    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 .
212    make
213    not programs/test/query_compile_time_config MBEDTLS_SSL_ALL_ALERT_MESSAGES
214    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
215
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}
232