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
47check()
48{
49    SCRIPT=$1
50    TO_CHECK=$2
51    PATTERN=""
52    FILES=""
53
54    if [ -d $TO_CHECK ]; then
55        for FILE in $TO_CHECK/*; do
56            FILES="$FILE $FILES"
57        done
58    else
59        FILES=$TO_CHECK
60    fi
61
62    for FILE in $FILES; do
63        cp $FILE $FILE.bak
64    done
65
66    $SCRIPT
67
68    # Compare the script output to the old files and remove backups
69    for FILE in $FILES; do
70        if ! diff $FILE $FILE.bak >/dev/null 2>&1; then
71            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
72            if [ -z "$UPDATE" ]; then
73                exit 1
74            fi
75        fi
76        if [ -z "$UPDATE" ]; then
77            mv $FILE.bak $FILE
78        else
79            rm $FILE.bak
80        fi
81
82        if [ -d $TO_CHECK ]; then
83            # Create a grep regular expression that we can check against the
84            # directory contents to test whether new files have been created
85            if [ -z $PATTERN ]; then
86                PATTERN="$(basename $FILE)"
87            else
88                PATTERN="$PATTERN\|$(basename $FILE)"
89            fi
90        fi
91    done
92
93    if [ -d $TO_CHECK ]; then
94        # Check if there are any new files
95        if ls -1 $TO_CHECK | grep -v "$PATTERN" >/dev/null 2>&1; then
96            echo "Files were created by '$SCRIPT'"
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
109