blob: 1b923328717503d5dfb3924981caad454edd3591 [file] [log] [blame]
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +00001#! /usr/bin/env sh
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +00002
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02003# Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02004# 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:
Bence Szépkúti09b4f192020-05-26 01:54:15 +020011#
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#
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020024# **********
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#
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +000045# Purpose
46#
47# Check if generated files are up-to-date.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000048
49set -eu
50
Manuel Pégourié-Gonnard961fb132020-07-16 10:40:13 +020051if [ $# -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
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000063if [ -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
Manuel Pégourié-Gonnard961fb132020-07-16 10:40:13 +020068UPDATE=
69if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
70 shift
71 UPDATE='y'
72fi
73
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000074check()
75{
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +000076 SCRIPT=$1
77 TO_CHECK=$2
78 PATTERN=""
Andres AGb7b420b2018-04-11 21:13:20 -050079 FILES=""
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000080
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +000081 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
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000093 $SCRIPT
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +000094
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'"
Manuel Pégourié-Gonnard961fb132020-07-16 10:40:13 +020099 if [ -z "$UPDATE" ]; then
100 exit 1
101 fi
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +0000102 fi
Manuel Pégourié-Gonnard961fb132020-07-16 10:40:13 +0200103 if [ -z "$UPDATE" ]; then
104 mv $FILE.bak $FILE
105 else
106 rm $FILE.bak
107 fi
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +0000108
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'"
Manuel Pégourié-Gonnard961fb132020-07-16 10:40:13 +0200124 if [ -z "$UPDATE" ]; then
125 exit 1
126 fi
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +0000127 fi
128 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000129}
130
Andres Amaya Garcia7dae1082018-01-10 11:03:45 +0000131check scripts/generate_errors.pl library/error.c
132check scripts/generate_features.pl library/version_features.c
133check scripts/generate_visualc_files.pl visualc/VS2010