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 "$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            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
91            if [ -z "$UPDATE" ]; then
92                exit 1
93            fi
94        fi
95        if [ -z "$UPDATE" ]; then
96            mv "$FILE.bak" "$FILE"
97        else
98            rm -f "$FILE.bak"
99        fi
100    done
101
102    if [ -n "$directory" ]; then
103        old_list="$*"
104        set -- "$directory"/*
105        new_list="$*"
106        # Check if there are any new files
107        if [ "$old_list" != "$new_list" ]; then
108            echo "Files were deleted or created by '$SCRIPT'"
109            echo "Before: $old_list"
110            echo "After: $new_list"
111            if [ -z "$UPDATE" ]; then
112                exit 1
113            fi
114        fi
115    fi
116}
117
118check scripts/generate_errors.pl library/error.c
119check scripts/generate_query_config.pl programs/test/query_config.c
120check scripts/generate_features.pl library/version_features.c
121# generate_visualc_files enumerates source files (library/*.c). It doesn't
122# care about their content, but the files must exist. So it must run after
123# the step that creates or updates these files.
124check scripts/generate_visualc_files.pl visualc/VS2010
125check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
126check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
127