blob: 1736f24d25480456f271febeca001e8b0b040a77 [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
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
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#
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000018# Purpose
19#
20# Check if generated files are up-to-date.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000021
22set -eu
23
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020024if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
25 cat <<EOF
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020026$0 [-l | -u]
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020027This 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.
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020032 -l List generated files, but do not update them.
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020033EOF
34 exit
35fi
36
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000037if [ -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
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020042UPDATE=
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020043LIST=
44while getopts lu OPTLET; do
45 case $OPTLET in
46 l) LIST=1;;
47 u) UPDATE=1;;
48 esac
49done
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020050
Gilles Peskineae4c4602021-04-21 16:11:50 +020051# 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.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000060check()
61{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000062 SCRIPT=$1
Gilles Peskineae4c4602021-04-21 16:11:50 +020063 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000064
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020065 if [ -n "$LIST" ]; then
66 printf '%s\n' "$@"
67 return
68 fi
69
Gilles Peskineae4c4602021-04-21 16:11:50 +020070 directory=
71 if [ -d "$1" ]; then
72 directory="$1"
73 rm -f "$directory"/*.bak
74 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000075 fi
76
Gilles Peskineae4c4602021-04-21 16:11:50 +020077 for FILE in "$@"; do
Gilles Peskineb32966d2021-05-18 14:58:09 +020078 if [ -e "$FILE" ]; then
Gilles Peskineebfee6e2022-04-05 14:08:09 +020079 cp -p "$FILE" "$FILE.bak"
Gilles Peskineb32966d2021-05-18 14:58:09 +020080 else
81 rm -f "$FILE.bak"
82 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000083 done
84
Gilles Peskineae4c4602021-04-21 16:11:50 +020085 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000086
87 # Compare the script output to the old files and remove backups
Gilles Peskineae4c4602021-04-21 16:11:50 +020088 for FILE in "$@"; do
Gilles Peskineebfee6e2022-04-05 14:08:09 +020089 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
90 # Move the original file back so that $FILE's timestamp doesn't
91 # change (avoids spurious rebuilds with make).
92 mv "$FILE.bak" "$FILE"
93 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000094 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020095 if [ -z "$UPDATE" ]; then
96 exit 1
Gilles Peskineebfee6e2022-04-05 14:08:09 +020097 else
98 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020099 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000100 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000101 done
102
Gilles Peskineae4c4602021-04-21 16:11:50 +0200103 if [ -n "$directory" ]; then
104 old_list="$*"
105 set -- "$directory"/*
106 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000107 # Check if there are any new files
Gilles Peskineae4c4602021-04-21 16:11:50 +0200108 if [ "$old_list" != "$new_list" ]; then
109 echo "Files were deleted or created by '$SCRIPT'"
110 echo "Before: $old_list"
111 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200112 if [ -z "$UPDATE" ]; then
113 exit 1
114 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000115 fi
116 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000117}
118
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000119check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +0000120check scripts/generate_query_config.pl programs/test/query_config.c
Archana6f21e452021-11-23 14:46:51 +0530121check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000122check scripts/generate_features.pl library/version_features.c
Gilles Peskineccbc3182021-12-15 12:55:37 +0100123check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
Gilles Peskine94230ea2021-05-18 15:48:56 +0200124# generate_visualc_files enumerates source files (library/*.c). It doesn't
125# care about their content, but the files must exist. So it must run after
126# the step that creates or updates these files.
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000127check scripts/generate_visualc_files.pl visualc/VS2010
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700128check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Gilles Peskine0298bda2021-03-10 02:34:37 +0100129check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)