• 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# This file is provided under the Apache License 2.0, or the
7# GNU General Public License v2.0 or later.
8#
9# **********
10# Apache License 2.0:
11#
12# Licensed under the Apache License, Version 2.0 (the "License"); you may
13# not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16# http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
23#
24# **********
25#
26# **********
27# GNU General Public License v2.0 or later:
28#
29# This program is free software; you can redistribute it and/or modify
30# it under the terms of the GNU General Public License as published by
31# the Free Software Foundation; either version 2 of the License, or
32# (at your option) any later version.
33#
34# This program is distributed in the hope that it will be useful,
35# but WITHOUT ANY WARRANTY; without even the implied warranty of
36# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37# GNU General Public License for more details.
38#
39# You should have received a copy of the GNU General Public License along
40# with this program; if not, write to the Free Software Foundation, Inc.,
41# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42#
43# **********
44#
45# Purpose
46#
47# Check if generated files are up-to-date.
48
49set -eu
50
51if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
52    cat <<EOF
53$0 [-u]
54This script checks that all generated file are up-to-date. If some aren't, by
55default the scripts reports it and exits in error; with the -u option, it just
56updates them instead.
57
58  -u    Update the files rather than return an error for out-of-date files.
59EOF
60    exit
61fi
62
63if [ -d library -a -d include -a -d tests ]; then :; else
64    echo "Must be run from mbed TLS root" >&2
65    exit 1
66fi
67
68UPDATE=
69if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
70    shift
71    UPDATE='y'
72fi
73
74check()
75{
76    SCRIPT=$1
77    TO_CHECK=$2
78    PATTERN=""
79    FILES=""
80
81    if [ -d $TO_CHECK ]; then
82        for FILE in $TO_CHECK/*; do
83            FILES="$FILE $FILES"
84        done
85    else
86        FILES=$TO_CHECK
87    fi
88
89    for FILE in $FILES; do
90        cp $FILE $FILE.bak
91    done
92
93    $SCRIPT
94
95    # Compare the script output to the old files and remove backups
96    for FILE in $FILES; do
97        if ! diff $FILE $FILE.bak >/dev/null 2>&1; then
98            echo "'$FILE' was either modified or deleted by '$SCRIPT'"
99            if [ -z "$UPDATE" ]; then
100                exit 1
101            fi
102        fi
103        if [ -z "$UPDATE" ]; then
104            mv $FILE.bak $FILE
105        else
106            rm $FILE.bak
107        fi
108
109        if [ -d $TO_CHECK ]; then
110            # Create a grep regular expression that we can check against the
111            # directory contents to test whether new files have been created
112            if [ -z $PATTERN ]; then
113                PATTERN="$(basename $FILE)"
114            else
115                PATTERN="$PATTERN\|$(basename $FILE)"
116            fi
117        fi
118    done
119
120    if [ -d $TO_CHECK ]; then
121        # Check if there are any new files
122        if ls -1 $TO_CHECK | grep -v "$PATTERN" >/dev/null 2>&1; then
123            echo "Files were created by '$SCRIPT'"
124            if [ -z "$UPDATE" ]; then
125                exit 1
126            fi
127        fi
128    fi
129}
130
131check scripts/generate_errors.pl library/error.c
132check scripts/generate_query_config.pl programs/ssl/query_config.c
133check scripts/generate_features.pl library/version_features.c
134check scripts/generate_visualc_files.pl visualc/VS2010
135