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 |
| 31 | } |
| 32 | if in_mbedtls_repo; then |
| 33 | library_dir='library' |
| 34 | elif in_tf_psa_crypto_repo; then |
| 35 | library_dir='core' |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 36 | else |
| 37 | 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] | 38 | exit 1 |
| 39 | fi |
| 40 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 41 | UPDATE= |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 42 | LIST= |
| 43 | while getopts lu OPTLET; do |
| 44 | case $OPTLET in |
| 45 | l) LIST=1;; |
| 46 | u) UPDATE=1;; |
| 47 | esac |
| 48 | done |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 49 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 50 | # check SCRIPT FILENAME[...] |
| 51 | # check SCRIPT DIRECTORY |
| 52 | # Run SCRIPT and check that it does not modify any of the specified files. |
| 53 | # In the first form, there can be any number of FILENAMEs, which must be |
| 54 | # regular files. |
| 55 | # In the second form, there must be a single DIRECTORY, standing for the |
| 56 | # list of files in the directory. Running SCRIPT must not modify any file |
| 57 | # in the directory and must not add or remove files either. |
| 58 | # 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] | 59 | check() |
| 60 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 61 | SCRIPT=$1 |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 62 | shift |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 63 | |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 64 | if [ -n "$LIST" ]; then |
| 65 | printf '%s\n' "$@" |
| 66 | return |
| 67 | fi |
| 68 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 69 | directory= |
| 70 | if [ -d "$1" ]; then |
| 71 | directory="$1" |
| 72 | rm -f "$directory"/*.bak |
| 73 | set -- "$1"/* |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 74 | fi |
| 75 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 76 | for FILE in "$@"; do |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 77 | if [ -e "$FILE" ]; then |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 78 | cp -p "$FILE" "$FILE.bak" |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 79 | else |
| 80 | rm -f "$FILE.bak" |
| 81 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 82 | done |
| 83 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 84 | "$SCRIPT" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 85 | |
| 86 | # Compare the script output to the old files and remove backups |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 87 | for FILE in "$@"; do |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 88 | if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then |
| 89 | # Move the original file back so that $FILE's timestamp doesn't |
| 90 | # change (avoids spurious rebuilds with make). |
| 91 | mv "$FILE.bak" "$FILE" |
| 92 | else |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 93 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 94 | if [ -z "$UPDATE" ]; then |
| 95 | exit 1 |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 96 | else |
| 97 | rm -f "$FILE.bak" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 98 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 99 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 100 | done |
| 101 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 102 | if [ -n "$directory" ]; then |
| 103 | old_list="$*" |
| 104 | set -- "$directory"/* |
| 105 | new_list="$*" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 106 | # Check if there are any new files |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 107 | if [ "$old_list" != "$new_list" ]; then |
| 108 | echo "Files were deleted or created by '$SCRIPT'" |
| 109 | echo "Before: $old_list" |
| 110 | echo "After: $new_list" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 111 | if [ -z "$UPDATE" ]; then |
| 112 | exit 1 |
| 113 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 114 | fi |
| 115 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 118 | # Note: if the format of calls to the "check" function changes, update |
| 119 | # scripts/code_style.py accordingly. For generated C source files (*.h or *.c), |
| 120 | # the format must be "check SCRIPT FILENAME...". For other source files, |
| 121 | # any shell syntax is permitted (including e.g. command substitution). |
| 122 | |
Gilles Peskine | 3b56d29 | 2022-12-19 00:56:44 +0100 | [diff] [blame] | 123 | # Note: Instructions to generate those files are replicated in: |
| 124 | # - **/Makefile (to (re)build them with make) |
| 125 | # - **/CMakeLists.txt (to (re)build them with cmake) |
| 126 | # - scripts/make_generated_files.bat (to generate them under Windows) |
| 127 | |
Thomas Daubney | d289b8b | 2023-11-14 15:30:07 +0000 | [diff] [blame^] | 128 | # These checks are common to Mbed TLS and TF-PSA-Crypto |
Cameron Nemo | e18d09d | 2020-09-22 10:37:26 -0700 | [diff] [blame] | 129 | check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 130 | check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) |
Gabor Mezei | 95ecaaf | 2023-01-16 16:53:29 +0100 | [diff] [blame] | 131 | check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list) |
Gilles Peskine | 0298bda | 2021-03-10 02:34:37 +0100 | [diff] [blame] | 132 | check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) |
Thomas Daubney | d3f8443 | 2023-11-14 11:53:14 +0000 | [diff] [blame] | 133 | check scripts/generate_driver_wrappers.py $library_dir/psa_crypto_driver_wrappers.h $library_dir/psa_crypto_driver_wrappers_no_static.c |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 134 | |
| 135 | # Additional checks for Mbed TLS only |
Thomas Daubney | d3f8443 | 2023-11-14 11:53:14 +0000 | [diff] [blame] | 136 | if in_mbedtls_build_dir; then |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 137 | check scripts/generate_errors.pl library/error.c |
| 138 | check scripts/generate_query_config.pl programs/test/query_config.c |
Thomas Daubney | c9f8386 | 2023-11-13 10:03:56 +0000 | [diff] [blame] | 139 | check scripts/generate_features.pl library/version_features.c |
| 140 | check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c |
| 141 | # generate_visualc_files enumerates source files (library/*.c). It doesn't |
| 142 | # care about their content, but the files must exist. So it must run after |
| 143 | # the step that creates or updates these files. |
| 144 | check scripts/generate_visualc_files.pl visualc/VS2013 |
| 145 | fi |