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