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 [-l | -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.
20  -l    List generated files, but do not update them.
21EOF
22    exit
23fi
24
25if [ -d library -a -d include -a -d tests ]; then :; else
26    echo "Must be run from Mbed TLS root" >&2
27    exit 1
28fi
29
30UPDATE=
31LIST=
32while getopts lu OPTLET; do
33    case $OPTLET in
34      l) LIST=1;;
35      u) UPDATE=1;;
36    esac
37done
38
39# check SCRIPT FILENAME[...]
40# check SCRIPT DIRECTORY
41# Run SCRIPT and check that it does not modify any of the specified files.
42# In the first form, there can be any number of FILENAMEs, which must be
43# regular files.
44# In the second form, there must be a single DIRECTORY, standing for the
45# list of files in the directory. Running SCRIPT must not modify any file
46# in the directory and must not add or remove files either.
47# If $UPDATE is empty, abort with an error status if a file is modified.
48check()
49{
50    SCRIPT=$1
51    shift
52
53    if [ -n "$LIST" ]; then
54        printf '%s\n' "$@"
55        return
56    fi
57
58    directory=
59    if [ -d "$1" ]; then
60        directory="$1"
61        rm -f "$directory"/*.bak
62        set -- "$1"/*
63    fi
64
65    for FILE in "$@"; do
66        if [ -e "$FILE" ]; then
67            cp -p "$FILE" "$FILE.bak"
68        else
69            rm -f "$FILE.bak"
70        fi
71    done
72
73    "$SCRIPT"
74
75    # Compare the script output to the old files and remove backups
76    for FILE in "$@"; do
77        if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
78            # Move the original file back so that $FILE's timestamp doesn't
79            # change (avoids spurious rebuilds with make).
80            mv "$FILE.bak" "$FILE"
81        else
82            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
83            if [ -z "$UPDATE" ]; then
84                exit 1
85            else
86                rm -f "$FILE.bak"
87            fi
88        fi
89    done
90
91    if [ -n "$directory" ]; then
92        old_list="$*"
93        set -- "$directory"/*
94        new_list="$*"
95        # Check if there are any new files
96        if [ "$old_list" != "$new_list" ]; then
97            echo "Files were deleted or created by '$SCRIPT'"
98            echo "Before: $old_list"
99            echo "After: $new_list"
100            if [ -z "$UPDATE" ]; then
101                exit 1
102            fi
103        fi
104    fi
105}
106
107# Note: if the format of calls to the "check" function changes, update
108# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
109# the format must be "check SCRIPT FILENAME...". For other source files,
110# any shell syntax is permitted (including e.g. command substitution).
111
112# Note: Instructions to generate those files are replicated in:
113#   - **/Makefile (to (re)build them with make)
114#   - **/CMakeLists.txt (to (re)build them with cmake)
115#   - scripts/make_generated_files.bat (to generate them under Windows)
116
117check scripts/generate_errors.pl library/error.c
118check scripts/generate_query_config.pl programs/test/query_config.c
119check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.h library/psa_crypto_driver_wrappers_no_static.c
120check scripts/generate_features.pl library/version_features.c
121check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
122# generate_visualc_files enumerates source files (library/*.c). It doesn't
123# care about their content, but the files must exist. So it must run after
124# the step that creates or updates these files.
125check scripts/generate_visualc_files.pl visualc/VS2013
126check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
127check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
128check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list)
129check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
130