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