• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env sh
2
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5#
6# Purpose
7#
8# Check if generated files are up-to-date.
9
10set -eu
11
12if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
13    cat <<EOF
14$0 [-u]
15This script checks that all generated file are up-to-date. If some aren't, by
16default the scripts reports it and exits in error; with the -u option, it just
17updates them instead.
18
19  -u    Update the files rather than return an error for out-of-date files.
20EOF
21    exit
22fi
23
24if [ -d library -a -d include -a -d tests ]; then :; else
25    echo "Must be run from Mbed TLS root" >&2
26    exit 1
27fi
28
29UPDATE=
30if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
31    shift
32    UPDATE='y'
33fi
34
35# check SCRIPT FILENAME[...]
36# check SCRIPT DIRECTORY
37# Run SCRIPT and check that it does not modify any of the specified files.
38# In the first form, there can be any number of FILENAMEs, which must be
39# regular files.
40# In the second form, there must be a single DIRECTORY, standing for the
41# list of files in the directory. Running SCRIPT must not modify any file
42# in the directory and must not add or remove files either.
43# If $UPDATE is empty, abort with an error status if a file is modified.
44check()
45{
46    SCRIPT=$1
47    shift
48
49    directory=
50    if [ -d "$1" ]; then
51        directory="$1"
52        set -- "$1"/*
53    fi
54
55    for FILE in "$@"; do
56        cp -p "$FILE" "$FILE.bak"
57    done
58
59    "$SCRIPT"
60
61    # Compare the script output to the old files and remove backups
62    for FILE in "$@"; do
63        if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
64            # Move the original file back so that $FILE's timestamp doesn't
65            # change (avoids spurious rebuilds with make).
66            mv "$FILE.bak" "$FILE"
67        else
68            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
69            if [ -z "$UPDATE" ]; then
70                exit 1
71            else
72                rm "$FILE.bak"
73            fi
74        fi
75    done
76
77    if [ -n "$directory" ]; then
78        old_list="$*"
79        set -- "$directory"/*
80        new_list="$*"
81        # Check if there are any new files
82        if [ "$old_list" != "$new_list" ]; then
83            echo "Files were deleted or created by '$SCRIPT'"
84            echo "Before: $old_list"
85            echo "After: $new_list"
86            if [ -z "$UPDATE" ]; then
87                exit 1
88            fi
89        fi
90    fi
91}
92
93# Note: if the format of calls to the "check" function changes, update
94# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
95# the format must be "check SCRIPT FILENAME...". For other source files,
96# any shell syntax is permitted (including e.g. command substitution).
97
98check scripts/generate_errors.pl library/error.c
99check scripts/generate_query_config.pl programs/test/query_config.c
100check scripts/generate_features.pl library/version_features.c
101check scripts/generate_visualc_files.pl visualc/VS2010
102check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
103check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
104check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
105