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