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 |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 18 | # Purpose |
| 19 | # |
| 20 | # Check if generated files are up-to-date. |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 21 | |
| 22 | set -eu |
| 23 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 24 | if [ $# -ne 0 ] && [ "$1" = "--help" ]; then |
| 25 | cat <<EOF |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 26 | $0 [-l | -u] |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 27 | This script checks that all generated file are up-to-date. If some aren't, by |
| 28 | default the scripts reports it and exits in error; with the -u option, it just |
| 29 | updates them instead. |
| 30 | |
| 31 | -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] | 32 | -l List generated files, but do not update them. |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 33 | EOF |
| 34 | exit |
| 35 | fi |
| 36 | |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 37 | if [ -d library -a -d include -a -d tests ]; then :; else |
| 38 | echo "Must be run from mbed TLS root" >&2 |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 42 | UPDATE= |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 43 | LIST= |
| 44 | while getopts lu OPTLET; do |
| 45 | case $OPTLET in |
| 46 | l) LIST=1;; |
| 47 | u) UPDATE=1;; |
| 48 | esac |
| 49 | done |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 50 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 51 | # check SCRIPT FILENAME[...] |
| 52 | # check SCRIPT DIRECTORY |
| 53 | # Run SCRIPT and check that it does not modify any of the specified files. |
| 54 | # In the first form, there can be any number of FILENAMEs, which must be |
| 55 | # regular files. |
| 56 | # In the second form, there must be a single DIRECTORY, standing for the |
| 57 | # list of files in the directory. Running SCRIPT must not modify any file |
| 58 | # in the directory and must not add or remove files either. |
| 59 | # 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] | 60 | check() |
| 61 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 62 | SCRIPT=$1 |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 63 | shift |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 64 | |
Gilles Peskine | 1fe01ac | 2021-07-01 11:13:29 +0200 | [diff] [blame] | 65 | if [ -n "$LIST" ]; then |
| 66 | printf '%s\n' "$@" |
| 67 | return |
| 68 | fi |
| 69 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 70 | directory= |
| 71 | if [ -d "$1" ]; then |
| 72 | directory="$1" |
| 73 | rm -f "$directory"/*.bak |
| 74 | set -- "$1"/* |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 75 | fi |
| 76 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 77 | for FILE in "$@"; do |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 78 | if [ -e "$FILE" ]; then |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 79 | cp -p "$FILE" "$FILE.bak" |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 80 | else |
| 81 | rm -f "$FILE.bak" |
| 82 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 83 | done |
| 84 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 85 | "$SCRIPT" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 86 | |
| 87 | # Compare the script output to the old files and remove backups |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 88 | for FILE in "$@"; do |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 89 | if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then |
| 90 | # Move the original file back so that $FILE's timestamp doesn't |
| 91 | # change (avoids spurious rebuilds with make). |
| 92 | mv "$FILE.bak" "$FILE" |
| 93 | else |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 94 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 95 | if [ -z "$UPDATE" ]; then |
| 96 | exit 1 |
Gilles Peskine | ebfee6e | 2022-04-05 14:08:09 +0200 | [diff] [blame] | 97 | else |
| 98 | rm -f "$FILE.bak" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 99 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 100 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 101 | done |
| 102 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 103 | if [ -n "$directory" ]; then |
| 104 | old_list="$*" |
| 105 | set -- "$directory"/* |
| 106 | new_list="$*" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 107 | # Check if there are any new files |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 108 | if [ "$old_list" != "$new_list" ]; then |
| 109 | echo "Files were deleted or created by '$SCRIPT'" |
| 110 | echo "Before: $old_list" |
| 111 | echo "After: $new_list" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 112 | if [ -z "$UPDATE" ]; then |
| 113 | exit 1 |
| 114 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 115 | fi |
| 116 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Gilles Peskine | 9a3771e | 2022-12-19 00:48:58 +0100 | [diff] [blame] | 119 | # Note: if the format of calls to the "check" function changes, update |
| 120 | # scripts/code_style.py accordingly. For generated C source files (*.h or *.c), |
| 121 | # the format must be "check SCRIPT FILENAME...". For other source files, |
| 122 | # any shell syntax is permitted (including e.g. command substitution). |
| 123 | |
Gilles Peskine | 3b56d29 | 2022-12-19 00:56:44 +0100 | [diff] [blame] | 124 | # Note: Instructions to generate those files are replicated in: |
| 125 | # - **/Makefile (to (re)build them with make) |
| 126 | # - **/CMakeLists.txt (to (re)build them with cmake) |
| 127 | # - scripts/make_generated_files.bat (to generate them under Windows) |
| 128 | |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 129 | check scripts/generate_errors.pl library/error.c |
Jaeden Amero | 7cb47de | 2019-02-28 11:37:23 +0000 | [diff] [blame] | 130 | check scripts/generate_query_config.pl programs/test/query_config.c |
Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 131 | check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 132 | check scripts/generate_features.pl library/version_features.c |
Gilles Peskine | ccbc318 | 2021-12-15 12:55:37 +0100 | [diff] [blame] | 133 | 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] | 134 | # generate_visualc_files enumerates source files (library/*.c). It doesn't |
| 135 | # care about their content, but the files must exist. So it must run after |
| 136 | # the step that creates or updates these files. |
Dave Rodgman | 378ecdd | 2023-01-10 15:08:30 +0000 | [diff] [blame] | 137 | check scripts/generate_visualc_files.pl visualc/VS2013 |
Cameron Nemo | e18d09d | 2020-09-22 10:37:26 -0700 | [diff] [blame] | 138 | 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] | 139 | 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^] | 140 | 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] | 141 | check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) |