Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env sh |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 2 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 5 | # |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 6 | # Purpose |
| 7 | # |
| 8 | # Check if generated files are up-to-date. |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 9 | |
| 10 | set -eu |
| 11 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 12 | if [ $# -ne 0 ] && [ "$1" = "--help" ]; then |
| 13 | cat <<EOF |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 14 | $0 [-l | -u] |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 15 | This script checks that all generated file are up-to-date. If some aren't, by |
| 16 | default the scripts reports it and exits in error; with the -u option, it just |
| 17 | updates them instead. |
| 18 | |
| 19 | -u Update the files rather than return an error for out-of-date files. |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 20 | -l List generated files, but do not update them. |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 21 | EOF |
| 22 | exit |
| 23 | fi |
| 24 | |
Elena Uziunaite | ef40183 | 2024-11-07 14:39:32 +0000 | [diff] [blame] | 25 | . framework/scripts/project_detection.sh |
Thomas Daubney | 0eb2dc1 | 2023-11-14 16:56:45 +0000 | [diff] [blame] | 26 | |
Thomas Daubney | e58128e | 2023-11-14 15:25:52 +0000 | [diff] [blame] | 27 | if in_mbedtls_repo; then |
Ronald Cron | 381247e | 2024-07-02 09:55:39 +0200 | [diff] [blame] | 28 | if [ -d tf-psa-crypto ]; then |
| 29 | crypto_core_dir='tf-psa-crypto/core' |
| 30 | builtin_drivers_dir='tf-psa-crypto/drivers/builtin/src' |
| 31 | else |
| 32 | crypto_core_dir='library' |
| 33 | builtin_drivers_dir='library' |
| 34 | fi |
Thomas Daubney | 4291bc2 | 2023-11-14 18:05:19 +0000 | [diff] [blame] | 35 | elif in_tf_psa_crypto_repo; then |
Ronald Cron | 381247e | 2024-07-02 09:55:39 +0200 | [diff] [blame] | 36 | crypto_core_dir='core' |
| 37 | builtin_drivers_dir='drivers/builtin/src/' |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 38 | else |
| 39 | echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2 |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 40 | exit 1 |
| 41 | fi |
| 42 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 43 | UPDATE= |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 44 | LIST= |
| 45 | while getopts lu OPTLET; do |
| 46 | case $OPTLET in |
| 47 | l) LIST=1;; |
| 48 | u) UPDATE=1;; |
| 49 | esac |
| 50 | done |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 51 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 52 | # check SCRIPT FILENAME[...] |
| 53 | # check SCRIPT DIRECTORY |
| 54 | # Run SCRIPT and check that it does not modify any of the specified files. |
| 55 | # In the first form, there can be any number of FILENAMEs, which must be |
| 56 | # regular files. |
| 57 | # In the second form, there must be a single DIRECTORY, standing for the |
| 58 | # list of files in the directory. Running SCRIPT must not modify any file |
| 59 | # in the directory and must not add or remove files either. |
| 60 | # If $UPDATE is empty, abort with an error status if a file is modified. |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 61 | check() |
| 62 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 63 | SCRIPT=$1 |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 64 | shift |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 65 | |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 66 | if [ -n "$LIST" ]; then |
| 67 | printf '%s\n' "$@" |
| 68 | return |
| 69 | fi |
| 70 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 71 | directory= |
| 72 | if [ -d "$1" ]; then |
| 73 | directory="$1" |
| 74 | rm -f "$directory"/*.bak |
| 75 | set -- "$1"/* |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 76 | fi |
| 77 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 78 | for FILE in "$@"; do |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 79 | if [ -e "$FILE" ]; then |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 80 | cp -p "$FILE" "$FILE.bak" |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 81 | else |
| 82 | rm -f "$FILE.bak" |
| 83 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 84 | done |
| 85 | |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 86 | # In the case of the config tests, generate only the files to be checked |
| 87 | # by the caller as they are divided into Mbed TLS and TF-PSA-Crypto |
| 88 | # specific ones. |
| 89 | if [ "${SCRIPT##*/}" = "generate_config_tests.py" ]; then |
| 90 | "$SCRIPT" "$@" |
| 91 | else |
| 92 | "$SCRIPT" |
| 93 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 94 | |
| 95 | # Compare the script output to the old files and remove backups |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 96 | for FILE in "$@"; do |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 97 | if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then |
| 98 | # Move the original file back so that $FILE's timestamp doesn't |
| 99 | # change (avoids spurious rebuilds with make). |
| 100 | mv "$FILE.bak" "$FILE" |
| 101 | else |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 102 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 103 | if [ -z "$UPDATE" ]; then |
| 104 | exit 1 |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 105 | else |
| 106 | rm -f "$FILE.bak" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 107 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 108 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 109 | done |
| 110 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 111 | if [ -n "$directory" ]; then |
| 112 | old_list="$*" |
| 113 | set -- "$directory"/* |
| 114 | new_list="$*" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 115 | # Check if there are any new files |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 116 | if [ "$old_list" != "$new_list" ]; then |
| 117 | echo "Files were deleted or created by '$SCRIPT'" |
| 118 | echo "Before: $old_list" |
| 119 | echo "After: $new_list" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 120 | if [ -z "$UPDATE" ]; then |
| 121 | exit 1 |
| 122 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 123 | fi |
| 124 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 127 | # Note: if the format of calls to the "check" function changes, update |
Elena Uziunaite | e0d3ffe | 2024-12-10 15:22:34 +0000 | [diff] [blame] | 128 | # framework/scripts/code_style.py accordingly. For generated C source files (*.h or *.c), |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 129 | # the format must be "check SCRIPT FILENAME...". For other source files, |
| 130 | # any shell syntax is permitted (including e.g. command substitution). |
| 131 | |
Gilles Peskine | 3b56d29 | 2022-12-19 00:56:44 +0100 | [diff] [blame] | 132 | # Note: Instructions to generate those files are replicated in: |
| 133 | # - **/Makefile (to (re)build them with make) |
| 134 | # - **/CMakeLists.txt (to (re)build them with cmake) |
| 135 | # - scripts/make_generated_files.bat (to generate them under Windows) |
| 136 | |
Thomas Daubney | d289b8b | 2023-11-14 15:30:07 +0000 | [diff] [blame] | 137 | # These checks are common to Mbed TLS and TF-PSA-Crypto |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 138 | |
| 139 | # The first case is temporary for the hybrid situation with a tf-psa-crypto |
| 140 | # directory in Mbed TLS that is not just a TF-PSA-Crypto submodule. |
| 141 | if [ -d tf-psa-crypto ]; then |
| 142 | cd tf-psa-crypto |
Harry Ramsey | cbd00b0 | 2024-12-02 14:43:49 +0000 | [diff] [blame] | 143 | check scripts/generate_psa_constants.py ./programs/psa/psa_constant_names_generated.c |
Ronald Cron | 81a674e | 2025-03-11 12:53:45 +0100 | [diff] [blame] | 144 | check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list) |
| 145 | check framework/scripts/generate_config_tests.py $(framework/scripts/generate_config_tests.py --list) |
| 146 | check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list) |
| 147 | check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list) |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 148 | cd .. |
Ronald Cron | bced0c7 | 2024-12-09 17:58:04 +0100 | [diff] [blame] | 149 | # Generated files that are present in the repository even in the development |
| 150 | # branch. (This is intended to be temporary, until the generator scripts are |
| 151 | # fully reviewed and the build scripts support a generated header file.) |
| 152 | check framework/scripts/generate_psa_wrappers.py tf-psa-crypto/tests/include/test/psa_test_wrappers.h tf-psa-crypto/tests/src/psa_test_wrappers.c |
Ronald Cron | 6a2cbe7 | 2024-11-13 09:20:30 +0100 | [diff] [blame] | 153 | check tf-psa-crypto/scripts/generate_driver_wrappers.py ${crypto_core_dir}/psa_crypto_driver_wrappers.h \ |
| 154 | ${crypto_core_dir}/psa_crypto_driver_wrappers_no_static.c |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 155 | check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.mbedtls_boolean.data |
| 156 | else |
Ronald Cron | ce3bcf0 | 2024-12-09 11:04:38 +0100 | [diff] [blame] | 157 | check scripts/generate_psa_constants.py ./programs/psa/psa_constant_names_generated.c |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 158 | check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list) |
Ronald Cron | 3f8275e | 2024-07-19 16:51:33 +0200 | [diff] [blame] | 159 | if in_tf_psa_crypto_repo; then |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 160 | check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.psa_boolean.data |
Ronald Cron | 3f8275e | 2024-07-19 16:51:33 +0200 | [diff] [blame] | 161 | else |
| 162 | check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.mbedtls_boolean.data |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 163 | fi |
| 164 | check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list) |
| 165 | check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list) |
Ronald Cron | 6a2cbe7 | 2024-11-13 09:20:30 +0100 | [diff] [blame] | 166 | check scripts/generate_driver_wrappers.py ${crypto_core_dir}/psa_crypto_driver_wrappers.h \ |
| 167 | ${crypto_core_dir}/psa_crypto_driver_wrappers_no_static.c |
Ronald Cron | bced0c7 | 2024-12-09 17:58:04 +0100 | [diff] [blame] | 168 | # Generated files that are present in the repository even in the development |
| 169 | # branch. (This is intended to be temporary, until the generator scripts are |
| 170 | # fully reviewed and the build scripts support a generated header file.) |
| 171 | check framework/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c |
Ronald Cron | c29dd98 | 2024-07-16 18:03:01 +0200 | [diff] [blame] | 172 | fi |
| 173 | |
Ronald Cron | 99226e9 | 2025-02-14 15:43:22 +0100 | [diff] [blame] | 174 | check framework/scripts/generate_test_keys.py tests/include/test/test_keys.h |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 175 | |
| 176 | # Additional checks for Mbed TLS only |
Thomas Daubney | 0eb2dc1 | 2023-11-14 16:56:45 +0000 | [diff] [blame] | 177 | if in_mbedtls_repo; then |
Harry Ramsey | 8b4b152 | 2024-10-15 12:04:26 +0100 | [diff] [blame] | 178 | check scripts/generate_errors.pl library/error.c |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 179 | check scripts/generate_query_config.pl programs/test/query_config.c |
Harry Ramsey | 468c0ae | 2024-09-27 14:38:53 +0100 | [diff] [blame] | 180 | check scripts/generate_features.pl library/version_features.c |
Elena Uziunaite | 09fee36 | 2024-10-07 17:40:21 +0100 | [diff] [blame] | 181 | check framework/scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c |
Gilles Peskine | 4773333 | 2025-03-01 14:28:20 +0100 | [diff] [blame] | 182 | check framework/scripts/generate_tls_handshake_tests.py tests/opt-testcases/handshake-generated.sh |
Elena Uziunaite | b74c3ea | 2024-10-08 13:02:48 +0100 | [diff] [blame] | 183 | check framework/scripts/generate_tls13_compat_tests.py tests/opt-testcases/tls13-compat.sh |
Ronald Cron | 99226e9 | 2025-02-14 15:43:22 +0100 | [diff] [blame] | 184 | check framework/scripts/generate_test_cert_macros.py tests/include/test/test_certs.h |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 185 | # generate_visualc_files enumerates source files (library/*.c). It doesn't |
| 186 | # care about their content, but the files must exist. So it must run after |
| 187 | # the step that creates or updates these files. |
Bence Szépkúti | fac1122 | 2024-03-12 16:38:23 +0100 | [diff] [blame] | 188 | check scripts/generate_visualc_files.pl visualc/VS2017 |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 189 | fi |