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 | 7ff7965 | 2023-11-03 12:04:52 +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 |
| 14 | $0 [-u] |
| 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. |
| 20 | EOF |
| 21 | exit |
| 22 | fi |
| 23 | |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 24 | if [ -d library -a -d include -a -d tests ]; then :; else |
Gilles Peskine | f08ca83 | 2023-09-12 19:21:54 +0200 | [diff] [blame] | 25 | echo "Must be run from Mbed TLS root" >&2 |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 26 | exit 1 |
| 27 | fi |
| 28 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 29 | UPDATE= |
| 30 | if [ $# -ne 0 ] && [ "$1" = "-u" ]; then |
| 31 | shift |
| 32 | UPDATE='y' |
| 33 | fi |
| 34 | |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 35 | # check SCRIPT FILENAME[...] |
| 36 | # check SCRIPT DIRECTORY |
| 37 | # Run SCRIPT and check that it does not modify any of the specified files. |
| 38 | # In the first form, there can be any number of FILENAMEs, which must be |
| 39 | # regular files. |
| 40 | # In the second form, there must be a single DIRECTORY, standing for the |
| 41 | # list of files in the directory. Running SCRIPT must not modify any file |
| 42 | # in the directory and must not add or remove files either. |
| 43 | # 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] | 44 | check() |
| 45 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 46 | SCRIPT=$1 |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 47 | shift |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 48 | |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 49 | directory= |
| 50 | if [ -d "$1" ]; then |
| 51 | directory="$1" |
| 52 | set -- "$1"/* |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 53 | fi |
| 54 | |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 55 | for FILE in "$@"; do |
Gilles Peskine | b5c4382 | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 56 | cp -p "$FILE" "$FILE.bak" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 57 | done |
| 58 | |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 59 | "$SCRIPT" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 60 | |
| 61 | # Compare the script output to the old files and remove backups |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 62 | for FILE in "$@"; do |
Gilles Peskine | b5c4382 | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 63 | if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then |
| 64 | # Move the original file back so that $FILE's timestamp doesn't |
| 65 | # change (avoids spurious rebuilds with make). |
| 66 | mv "$FILE.bak" "$FILE" |
| 67 | else |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 68 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 69 | if [ -z "$UPDATE" ]; then |
| 70 | exit 1 |
Gilles Peskine | b5c4382 | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 71 | else |
| 72 | rm "$FILE.bak" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 73 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 74 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 75 | done |
| 76 | |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 77 | if [ -n "$directory" ]; then |
| 78 | old_list="$*" |
| 79 | set -- "$directory"/* |
| 80 | new_list="$*" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 81 | # Check if there are any new files |
Gilles Peskine | 30ccba4 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 82 | if [ "$old_list" != "$new_list" ]; then |
| 83 | echo "Files were deleted or created by '$SCRIPT'" |
| 84 | echo "Before: $old_list" |
| 85 | echo "After: $new_list" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 86 | if [ -z "$UPDATE" ]; then |
| 87 | exit 1 |
| 88 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 89 | fi |
| 90 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Gilles Peskine | 4ca54d4 | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 93 | # Note: if the format of calls to the "check" function changes, update |
| 94 | # scripts/code_style.py accordingly. For generated C source files (*.h or *.c), |
| 95 | # the format must be "check SCRIPT FILENAME...". For other source files, |
| 96 | # any shell syntax is permitted (including e.g. command substitution). |
| 97 | |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 98 | check scripts/generate_errors.pl library/error.c |
Jaeden Amero | 7cb47de | 2019-02-28 11:37:23 +0000 | [diff] [blame] | 99 | check scripts/generate_query_config.pl programs/test/query_config.c |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 100 | check scripts/generate_features.pl library/version_features.c |
| 101 | check scripts/generate_visualc_files.pl visualc/VS2010 |
Cameron Nemo | e18d09d | 2020-09-22 10:37:26 -0700 | [diff] [blame] | 102 | check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c |
Gilles Peskine | b7119c5 | 2024-01-04 16:46:00 +0100 | [diff] [blame^] | 103 | check tests/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c |
Werner Lewis | 545911f | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 104 | check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) |
Gilles Peskine | 0298bda | 2021-03-10 02:34:37 +0100 | [diff] [blame] | 105 | check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) |