1#! /usr/bin/env sh
2
3# Copyright The Mbed TLS Contributors
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#
18# Purpose
19#
20# Check if generated files are up-to-date.
21
22set -eu
23
24if [ $# -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
36if [ -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
41UPDATE=
42if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
43    shift
44    UPDATE='y'
45fi
46
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.
56check()
57{
58    SCRIPT=$1
59    shift
60
61    directory=
62    if [ -d "$1" ]; then
63        directory="$1"
64        set -- "$1"/*
65    fi
66
67    for FILE in "$@"; do
68        cp "$FILE" "$FILE.bak"
69    done
70
71    "$SCRIPT"
72
73    # Compare the script output to the old files and remove backups
74    for FILE in "$@"; do
75        if ! diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
76            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
77            if [ -z "$UPDATE" ]; then
78                exit 1
79            fi
80        fi
81        if [ -z "$UPDATE" ]; then
82            mv "$FILE.bak" "$FILE"
83        else
84            rm "$FILE.bak"
85        fi
86    done
87
88    if [ -n "$directory" ]; then
89        old_list="$*"
90        set -- "$directory"/*
91        new_list="$*"
92        # Check if there are any new files
93        if [ "$old_list" != "$new_list" ]; then
94            echo "Files were deleted or created by '$SCRIPT'"
95            echo "Before: $old_list"
96            echo "After: $new_list"
97            if [ -z "$UPDATE" ]; then
98                exit 1
99            fi
100        fi
101    fi
102}
103
104check scripts/generate_errors.pl library/error.c
105check scripts/generate_query_config.pl programs/test/query_config.c
106check scripts/generate_features.pl library/version_features.c
107check scripts/generate_visualc_files.pl visualc/VS2010
108check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
109check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
110