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 | |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 47 | check() |
| 48 | { |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 49 | SCRIPT=$1 |
| 50 | TO_CHECK=$2 |
| 51 | PATTERN="" |
Andres AG | c4ec716 | 2018-04-11 21:13:20 -0500 | [diff] [blame] | 52 | FILES="" |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 53 | |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 54 | if [ -d $TO_CHECK ]; then |
| 55 | for FILE in $TO_CHECK/*; do |
| 56 | FILES="$FILE $FILES" |
| 57 | done |
| 58 | else |
| 59 | FILES=$TO_CHECK |
| 60 | fi |
| 61 | |
| 62 | for FILE in $FILES; do |
| 63 | cp $FILE $FILE.bak |
| 64 | done |
| 65 | |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 66 | $SCRIPT |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 67 | |
| 68 | # Compare the script output to the old files and remove backups |
| 69 | for FILE in $FILES; do |
| 70 | if ! diff $FILE $FILE.bak >/dev/null 2>&1; then |
| 71 | echo "'$FILE' was either modified or deleted by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 72 | if [ -z "$UPDATE" ]; then |
| 73 | exit 1 |
| 74 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 75 | fi |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 76 | if [ -z "$UPDATE" ]; then |
| 77 | mv $FILE.bak $FILE |
| 78 | else |
| 79 | rm $FILE.bak |
| 80 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 81 | |
| 82 | if [ -d $TO_CHECK ]; then |
| 83 | # Create a grep regular expression that we can check against the |
| 84 | # directory contents to test whether new files have been created |
| 85 | if [ -z $PATTERN ]; then |
| 86 | PATTERN="$(basename $FILE)" |
| 87 | else |
| 88 | PATTERN="$PATTERN\|$(basename $FILE)" |
| 89 | fi |
| 90 | fi |
| 91 | done |
| 92 | |
| 93 | if [ -d $TO_CHECK ]; then |
| 94 | # Check if there are any new files |
| 95 | if ls -1 $TO_CHECK | grep -v "$PATTERN" >/dev/null 2>&1; then |
| 96 | echo "Files were created by '$SCRIPT'" |
Manuel Pégourié-Gonnard | 2774fc4 | 2020-07-16 10:40:13 +0200 | [diff] [blame] | 97 | if [ -z "$UPDATE" ]; then |
| 98 | exit 1 |
| 99 | fi |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 100 | fi |
| 101 | fi |
Manuel Pégourié-Gonnard | b3b8e43 | 2015-02-13 14:52:19 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 104 | check scripts/generate_errors.pl library/error.c |
Jaeden Amero | 7cb47de | 2019-02-28 11:37:23 +0000 | [diff] [blame] | 105 | check scripts/generate_query_config.pl programs/test/query_config.c |
Andres Amaya Garcia | 4c1e2ec | 2018-01-10 11:03:45 +0000 | [diff] [blame] | 106 | check scripts/generate_features.pl library/version_features.c |
| 107 | check scripts/generate_visualc_files.pl visualc/VS2010 |