blob: ea0aadd110b3ecfbfba30e4f7400cfcf86dc80c9 [file] [log] [blame]
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +00001#! /usr/bin/env sh
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +00002
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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 Garcia4c1e2ec2018-01-10 11:03:45 +000018# Purpose
19#
20# Check if generated files are up-to-date.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000021
22set -eu
23
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020024if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
25 cat <<EOF
26$0 [-u]
27This script checks that all generated file are up-to-date. If some aren't, by
28default the scripts reports it and exits in error; with the -u option, it just
29updates them instead.
30
31 -u Update the files rather than return an error for out-of-date files.
32EOF
33 exit
34fi
35
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000036if [ -d library -a -d include -a -d tests ]; then :; else
37 echo "Must be run from mbed TLS root" >&2
38 exit 1
39fi
40
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020041UPDATE=
42if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
43 shift
44 UPDATE='y'
45fi
46
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000047check()
48{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000049 SCRIPT=$1
50 TO_CHECK=$2
51 PATTERN=""
Andres AGc4ec7162018-04-11 21:13:20 -050052 FILES=""
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000053
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000054 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
Gilles Peskineb32966d2021-05-18 14:58:09 +020063 if [ -e "$FILE" ]; then
64 cp "$FILE" "$FILE.bak"
65 else
66 rm -f "$FILE.bak"
67 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000068 done
69
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000070 $SCRIPT
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000071
72 # Compare the script output to the old files and remove backups
73 for FILE in $FILES; do
74 if ! diff $FILE $FILE.bak >/dev/null 2>&1; then
75 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020076 if [ -z "$UPDATE" ]; then
77 exit 1
78 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000079 fi
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020080 if [ -z "$UPDATE" ]; then
81 mv $FILE.bak $FILE
82 else
Gilles Peskineb32966d2021-05-18 14:58:09 +020083 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020084 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000085
86 if [ -d $TO_CHECK ]; then
87 # Create a grep regular expression that we can check against the
88 # directory contents to test whether new files have been created
89 if [ -z $PATTERN ]; then
90 PATTERN="$(basename $FILE)"
91 else
92 PATTERN="$PATTERN\|$(basename $FILE)"
93 fi
94 fi
95 done
96
97 if [ -d $TO_CHECK ]; then
98 # Check if there are any new files
99 if ls -1 $TO_CHECK | grep -v "$PATTERN" >/dev/null 2>&1; then
100 echo "Files were created by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200101 if [ -z "$UPDATE" ]; then
102 exit 1
103 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000104 fi
105 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000106}
107
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000108check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +0000109check scripts/generate_query_config.pl programs/test/query_config.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000110check scripts/generate_features.pl library/version_features.c
111check scripts/generate_visualc_files.pl visualc/VS2010
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700112check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Gilles Peskine0298bda2021-03-10 02:34:37 +0100113check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)