blob: f8a244928df18e88eb865c8f1aca062a2a7f774a [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
Gilles Peskine30ccba42021-04-21 16:11:50 +020047# 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é-Gonnardb3b8e432015-02-13 14:52:19 +000056check()
57{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000058 SCRIPT=$1
Gilles Peskine30ccba42021-04-21 16:11:50 +020059 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000060
Gilles Peskine30ccba42021-04-21 16:11:50 +020061 directory=
62 if [ -d "$1" ]; then
63 directory="$1"
64 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000065 fi
66
Gilles Peskine30ccba42021-04-21 16:11:50 +020067 for FILE in "$@"; do
Gilles Peskineb5c43822022-04-05 14:08:09 +020068 cp -p "$FILE" "$FILE.bak"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000069 done
70
Gilles Peskine30ccba42021-04-21 16:11:50 +020071 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000072
73 # Compare the script output to the old files and remove backups
Gilles Peskine30ccba42021-04-21 16:11:50 +020074 for FILE in "$@"; do
Gilles Peskineb5c43822022-04-05 14:08:09 +020075 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
76 # Move the original file back so that $FILE's timestamp doesn't
77 # change (avoids spurious rebuilds with make).
78 mv "$FILE.bak" "$FILE"
79 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000080 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020081 if [ -z "$UPDATE" ]; then
82 exit 1
Gilles Peskineb5c43822022-04-05 14:08:09 +020083 else
84 rm "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020085 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000086 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000087 done
88
Gilles Peskine30ccba42021-04-21 16:11:50 +020089 if [ -n "$directory" ]; then
90 old_list="$*"
91 set -- "$directory"/*
92 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000093 # Check if there are any new files
Gilles Peskine30ccba42021-04-21 16:11:50 +020094 if [ "$old_list" != "$new_list" ]; then
95 echo "Files were deleted or created by '$SCRIPT'"
96 echo "Before: $old_list"
97 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020098 if [ -z "$UPDATE" ]; then
99 exit 1
100 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000101 fi
102 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000103}
104
Gilles Peskine4ca54d42022-12-19 00:48:58 +0100105# Note: if the format of calls to the "check" function changes, update
106# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
107# the format must be "check SCRIPT FILENAME...". For other source files,
108# any shell syntax is permitted (including e.g. command substitution).
109
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000110check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +0000111check scripts/generate_query_config.pl programs/test/query_config.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000112check scripts/generate_features.pl library/version_features.c
113check scripts/generate_visualc_files.pl visualc/VS2010
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700114check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Werner Lewis545911f2022-07-08 13:54:57 +0100115check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100116check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)