blob: cc5db9730a6b51082433daca84041c20123aea2f [file] [log] [blame]
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +00001#! /usr/bin/env sh
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +00002
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +00003# Copyright (c) 2018, ARM Limited, All Rights Reserved
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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# This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000019#
20# Purpose
21#
22# Check if generated files are up-to-date.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000023
24set -eu
25
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020026if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
27 cat <<EOF
28$0 [-u]
29This script checks that all generated file are up-to-date. If some aren't, by
30default the scripts reports it and exits in error; with the -u option, it just
31updates them instead.
32
33 -u Update the files rather than return an error for out-of-date files.
34EOF
35 exit
36fi
37
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000038if [ -d library -a -d include -a -d tests ]; then :; else
39 echo "Must be run from mbed TLS root" >&2
40 exit 1
41fi
42
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020043UPDATE=
44if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
45 shift
46 UPDATE='y'
47fi
48
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000049check()
50{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000051 SCRIPT=$1
52 TO_CHECK=$2
53 PATTERN=""
Andres AGc4ec7162018-04-11 21:13:20 -050054 FILES=""
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000055
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000056 if [ -d $TO_CHECK ]; then
57 for FILE in $TO_CHECK/*; do
58 FILES="$FILE $FILES"
59 done
60 else
61 FILES=$TO_CHECK
62 fi
63
64 for FILE in $FILES; do
65 cp $FILE $FILE.bak
66 done
67
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000068 $SCRIPT
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000069
70 # Compare the script output to the old files and remove backups
71 for FILE in $FILES; do
72 if ! diff $FILE $FILE.bak >/dev/null 2>&1; then
73 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020074 if [ -z "$UPDATE" ]; then
75 exit 1
76 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000077 fi
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020078 if [ -z "$UPDATE" ]; then
79 mv $FILE.bak $FILE
80 else
81 rm $FILE.bak
82 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000083
84 if [ -d $TO_CHECK ]; then
85 # Create a grep regular expression that we can check against the
86 # directory contents to test whether new files have been created
87 if [ -z $PATTERN ]; then
88 PATTERN="$(basename $FILE)"
89 else
90 PATTERN="$PATTERN\|$(basename $FILE)"
91 fi
92 fi
93 done
94
95 if [ -d $TO_CHECK ]; then
96 # Check if there are any new files
97 if ls -1 $TO_CHECK | grep -v "$PATTERN" >/dev/null 2>&1; then
98 echo "Files were created by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020099 if [ -z "$UPDATE" ]; then
100 exit 1
101 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000102 fi
103 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000104}
105
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000106check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +0000107check scripts/generate_query_config.pl programs/test/query_config.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000108check scripts/generate_features.pl library/version_features.c
109check scripts/generate_visualc_files.pl visualc/VS2010