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 |
| 26 | $0 [-u] |
| 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. |
| 32 | EOF |
| 33 | exit |
| 34 | fi |
| 35 | |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 36 | if [ -d library -a -d include -a -d tests ]; then :; else |
| 37 | echo "Must be run from mbed TLS root" >&2 |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 41 | UPDATE= |
| 42 | if [ $# -ne 0 ] && [ "$1" = "-u" ]; then |
| 43 | shift |
| 44 | UPDATE='y' |
| 45 | fi |
| 46 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 47 | # check SCRIPT FILENAME[...] |
| 48 | # check SCRIPT DIRECTORY |
| 49 | # Run SCRIPT and check that it does not modify any of the specified files. |
| 50 | # In the first form, there can be any number of FILENAMEs, which must be |
| 51 | # regular files. |
| 52 | # In the second form, there must be a single DIRECTORY, standing for the |
| 53 | # list of files in the directory. Running SCRIPT must not modify any file |
| 54 | # in the directory and must not add or remove files either. |
| 55 | # 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] | 56 | check() |
| 57 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 58 | SCRIPT=$1 |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 59 | shift |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 60 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 61 | directory= |
| 62 | if [ -d "$1" ]; then |
| 63 | directory="$1" |
| 64 | rm -f "$directory"/*.bak |
| 65 | set -- "$1"/* |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 66 | fi |
| 67 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 68 | for FILE in "$@"; do |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 69 | if [ -e "$FILE" ]; then |
| 70 | cp "$FILE" "$FILE.bak" |
| 71 | else |
| 72 | rm -f "$FILE.bak" |
| 73 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 74 | done |
| 75 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 76 | "$SCRIPT" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 77 | |
| 78 | # Compare the script output to the old files and remove backups |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 79 | for FILE in "$@"; do |
| 80 | if ! diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 81 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 82 | if [ -z "$UPDATE" ]; then |
| 83 | exit 1 |
| 84 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 85 | fi |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 86 | if [ -z "$UPDATE" ]; then |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 87 | mv "$FILE.bak" "$FILE" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 88 | else |
Gilles Peskine | b32966d | 2021-05-18 14:58:09 +0200 | [diff] [blame] | 89 | rm -f "$FILE.bak" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 90 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 91 | done |
| 92 | |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 93 | if [ -n "$directory" ]; then |
| 94 | old_list="$*" |
| 95 | set -- "$directory"/* |
| 96 | new_list="$*" |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 97 | # Check if there are any new files |
Gilles Peskine | ae4c460 | 2021-04-21 16:11:50 +0200 | [diff] [blame] | 98 | if [ "$old_list" != "$new_list" ]; then |
| 99 | echo "Files were deleted or created by '$SCRIPT'" |
| 100 | echo "Before: $old_list" |
| 101 | echo "After: $new_list" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 102 | if [ -z "$UPDATE" ]; then |
| 103 | exit 1 |
| 104 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 105 | fi |
| 106 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 109 | check scripts/generate_errors.pl library/error.c |
Jaeden Amero | 7cb47de | 2019-02-28 11:37:23 +0000 | [diff] [blame] | 110 | check scripts/generate_query_config.pl programs/test/query_config.c |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 111 | check scripts/generate_features.pl library/version_features.c |
Gilles Peskine | 94230ea | 2021-05-18 15:48:56 +0200 | [diff] [blame] | 112 | # generate_visualc_files enumerates source files (library/*.c). It doesn't |
| 113 | # care about their content, but the files must exist. So it must run after |
| 114 | # the step that creates or updates these files. |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 115 | check scripts/generate_visualc_files.pl visualc/VS2010 |
Cameron Nemo | e18d09d | 2020-09-22 10:37:26 -0700 | [diff] [blame] | 116 | check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c |
Gilles Peskine | 0298bda | 2021-03-10 02:34:37 +0100 | [diff] [blame] | 117 | check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) |