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    programs/test/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 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
68    cmake -D CMAKE_BUILD_TYPE:String=Check -D GEN_FILES=ON -D TEST_CPP=1 "$MBEDTLS_ROOT_DIR"
69    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"
111    cd programs/test/cmake_package
112    cmake .
113    make
114    ./cmake_package
115}
116
117support_test_cmake_as_package () {
118    support_test_cmake_out_of_source
119}
120
121component_test_cmake_as_package_install () {
122    # Remove existing generated files so that we use the ones CMake
123    # generates
124    make neat
125
126    msg "build: cmake 'as-installed-package' build"
127    cd programs/test/cmake_package_install
128    cmake .
129    make
130    ./cmake_package_install
131}
132
133support_test_cmake_as_package_install () {
134    support_test_cmake_out_of_source
135}
136
137component_build_cmake_custom_config_file () {
138    # Make a copy of config file to use for the in-tree test
139    cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
140
141    MBEDTLS_ROOT_DIR="$PWD"
142    mkdir "$OUT_OF_SOURCE_DIR"
143    cd "$OUT_OF_SOURCE_DIR"
144
145    # Build once to get the generated files (which need an intact config file)
146    cmake "$MBEDTLS_ROOT_DIR"
147    make
148
149    msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
150    scripts/config.py -w full_config.h full
151    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
152    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h "$MBEDTLS_ROOT_DIR"
153    make
154
155    msg "build: cmake with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
156    # In the user config, disable one feature (for simplicity, pick a feature
157    # that nothing else depends on).
158    echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
159
160    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h "$MBEDTLS_ROOT_DIR"
161    make
162    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
163
164    rm -f user_config.h full_config.h
165
166    cd "$MBEDTLS_ROOT_DIR"
167    rm -rf "$OUT_OF_SOURCE_DIR"
168
169    # Now repeat the test for an in-tree build:
170
171    # Restore config for the in-tree test
172    mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
173
174    # Build once to get the generated files (which need an intact config)
175    cmake .
176    make
177
178    msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
179    scripts/config.py -w full_config.h full
180    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
181    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h .
182    make
183
184    msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
185    # In the user config, disable one feature (for simplicity, pick a feature
186    # that nothing else depends on).
187    echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
188
189    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h .
190    make
191    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
192
193    rm -f user_config.h full_config.h
194}
195
196support_build_cmake_custom_config_file () {
197    support_test_cmake_out_of_source
198}
199
200component_build_cmake_programs_no_testing () {
201    # Verify that the type of builds performed by oss-fuzz don't get accidentally broken
202    msg "build: cmake with -DENABLE_PROGRAMS=ON and -DENABLE_TESTING=OFF"
203    cmake -DENABLE_PROGRAMS=ON -DENABLE_TESTING=OFF .
204    make
205}
206
207support_build_cmake_programs_no_testing () {
208    support_test_cmake_out_of_source
209}
210
211