blob: 67dedeb26556ad998756c3e857796b61e891be44 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02005#
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +00006# Purpose
7#
8# Check if generated files are up-to-date.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +00009
10set -eu
11
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020012if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
13 cat <<EOF
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020014$0 [-l | -u]
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020015This script checks that all generated file are up-to-date. If some aren't, by
16default the scripts reports it and exits in error; with the -u option, it just
17updates them instead.
18
19 -u Update the files rather than return an error for out-of-date files.
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020020 -l List generated files, but do not update them.
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020021EOF
22 exit
23fi
24
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000025if [ -d library -a -d include -a -d tests ]; then :; else
Gilles Peskinee820c0a2023-08-03 17:45:20 +020026 echo "Must be run from Mbed TLS root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000027 exit 1
28fi
29
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020030UPDATE=
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020031LIST=
32while getopts lu OPTLET; do
33 case $OPTLET in
34 l) LIST=1;;
35 u) UPDATE=1;;
36 esac
37done
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020038
Gilles Peskineae4c4602021-04-21 16:11:50 +020039# check SCRIPT FILENAME[...]
40# check SCRIPT DIRECTORY
41# Run SCRIPT and check that it does not modify any of the specified files.
42# In the first form, there can be any number of FILENAMEs, which must be
43# regular files.
44# In the second form, there must be a single DIRECTORY, standing for the
45# list of files in the directory. Running SCRIPT must not modify any file
46# in the directory and must not add or remove files either.
47# If $UPDATE is empty, abort with an error status if a file is modified.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000048check()
49{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000050 SCRIPT=$1
Gilles Peskineae4c4602021-04-21 16:11:50 +020051 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000052
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020053 if [ -n "$LIST" ]; then
54 printf '%s\n' "$@"
55 return
56 fi
57
Gilles Peskineae4c4602021-04-21 16:11:50 +020058 directory=
59 if [ -d "$1" ]; then
60 directory="$1"
61 rm -f "$directory"/*.bak
62 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000063 fi
64
Gilles Peskineae4c4602021-04-21 16:11:50 +020065 for FILE in "$@"; do
Gilles Peskineb32966d2021-05-18 14:58:09 +020066 if [ -e "$FILE" ]; then
Gilles Peskineebfee6e2022-04-05 14:08:09 +020067 cp -p "$FILE" "$FILE.bak"
Gilles Peskineb32966d2021-05-18 14:58:09 +020068 else
69 rm -f "$FILE.bak"
70 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000071 done
72
Gilles Peskineae4c4602021-04-21 16:11:50 +020073 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000074
75 # Compare the script output to the old files and remove backups
Gilles Peskineae4c4602021-04-21 16:11:50 +020076 for FILE in "$@"; do
Gilles Peskineebfee6e2022-04-05 14:08:09 +020077 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
78 # Move the original file back so that $FILE's timestamp doesn't
79 # change (avoids spurious rebuilds with make).
80 mv "$FILE.bak" "$FILE"
81 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000082 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020083 if [ -z "$UPDATE" ]; then
84 exit 1
Gilles Peskineebfee6e2022-04-05 14:08:09 +020085 else
86 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020087 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000088 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000089 done
90
Gilles Peskineae4c4602021-04-21 16:11:50 +020091 if [ -n "$directory" ]; then
92 old_list="$*"
93 set -- "$directory"/*
94 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000095 # Check if there are any new files
Gilles Peskineae4c4602021-04-21 16:11:50 +020096 if [ "$old_list" != "$new_list" ]; then
97 echo "Files were deleted or created by '$SCRIPT'"
98 echo "Before: $old_list"
99 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200100 if [ -z "$UPDATE" ]; then
101 exit 1
102 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000103 fi
104 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000105}
106
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100107# Note: if the format of calls to the "check" function changes, update
108# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
109# the format must be "check SCRIPT FILENAME...". For other source files,
110# any shell syntax is permitted (including e.g. command substitution).
111
Gilles Peskine3b56d292022-12-19 00:56:44 +0100112# Note: Instructions to generate those files are replicated in:
113# - **/Makefile (to (re)build them with make)
114# - **/CMakeLists.txt (to (re)build them with cmake)
115# - scripts/make_generated_files.bat (to generate them under Windows)
116
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000117check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +0000118check scripts/generate_query_config.pl programs/test/query_config.c
Minos Galanakis3974b172023-10-02 16:49:14 +0100119check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.h library/psa_crypto_driver_wrappers_no_static.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000120check scripts/generate_features.pl library/version_features.c
Gilles Peskineccbc3182021-12-15 12:55:37 +0100121check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
Gilles Peskine94230ea2021-05-18 15:48:56 +0200122# generate_visualc_files enumerates source files (library/*.c). It doesn't
123# care about their content, but the files must exist. So it must run after
124# the step that creates or updates these files.
Dave Rodgman378ecdd2023-01-10 15:08:30 +0000125check scripts/generate_visualc_files.pl visualc/VS2013
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700126check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Werner Lewis8b2df742022-07-08 13:54:57 +0100127check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
Gabor Mezei95ecaaf2023-01-16 16:53:29 +0100128check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100129check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)