blob: 09c850af7ad688ff0c746351244d98972786fdbb [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
Thomas Daubneye58128e2023-11-14 15:25:52 +000025in_mbedtls_repo () {
26 test -d include -a -d library -a -d programs -a -d tests
27}
28
29in_tf_psa_crypto_repo () {
30 test -d include -a -d core -a -d drivers -a -d programs -a -d tests
Thomas Daubney0eb2dc12023-11-14 16:56:45 +000031}
32
Thomas Daubneye58128e2023-11-14 15:25:52 +000033if in_mbedtls_repo; then
34 library_dir='library'
Thomas Daubney4291bc22023-11-14 18:05:19 +000035elif in_tf_psa_crypto_repo; then
Thomas Daubneye58128e2023-11-14 15:25:52 +000036 library_dir='core'
Thomas Daubneyc9f83862023-11-13 10:03:56 +000037else
38 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000039 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
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100119# Note: if the format of calls to the "check" function changes, update
120# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
121# the format must be "check SCRIPT FILENAME...". For other source files,
122# any shell syntax is permitted (including e.g. command substitution).
123
Gilles Peskine3b56d292022-12-19 00:56:44 +0100124# Note: Instructions to generate those files are replicated in:
125# - **/Makefile (to (re)build them with make)
126# - **/CMakeLists.txt (to (re)build them with cmake)
127# - scripts/make_generated_files.bat (to generate them under Windows)
128
Thomas Daubneyd289b8b2023-11-14 15:30:07 +0000129# These checks are common to Mbed TLS and TF-PSA-Crypto
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700130check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
David Horstmannea091522024-05-29 17:57:08 +0100131check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list)
Gilles Peskine06fb1802024-05-23 16:31:22 +0200132check framework/scripts/generate_config_tests.py $(framework/scripts/generate_config_tests.py --list)
David Horstmannea091522024-05-29 17:57:08 +0100133check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list)
134check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list)
135check framework/scripts/generate_test_keys.py tests/src/test_keys.h
Thomas Daubneyd3f84432023-11-14 11:53:14 +0000136check scripts/generate_driver_wrappers.py $library_dir/psa_crypto_driver_wrappers.h $library_dir/psa_crypto_driver_wrappers_no_static.c
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000137
138# Additional checks for Mbed TLS only
Thomas Daubney0eb2dc12023-11-14 16:56:45 +0000139if in_mbedtls_repo; then
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000140 check scripts/generate_errors.pl library/error.c
141 check scripts/generate_query_config.pl programs/test/query_config.c
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000142 check scripts/generate_features.pl library/version_features.c
143 check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
David Horstmannea091522024-05-29 17:57:08 +0100144 check framework/scripts/generate_test_cert_macros.py tests/src/test_certs.h
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000145 # generate_visualc_files enumerates source files (library/*.c). It doesn't
146 # care about their content, but the files must exist. So it must run after
147 # the step that creates or updates these files.
Bence Szépkútifac11222024-03-12 16:38:23 +0100148 check scripts/generate_visualc_files.pl visualc/VS2017
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000149fi
Gilles Peskinee00150d2024-01-04 16:46:00 +0100150
151# Generated files that are present in the repository even in the development
152# branch. (This is intended to be temporary, until the generator scripts are
153# fully reviewed and the build scripts support a generated header file.)
David Horstmannea091522024-05-29 17:57:08 +0100154check framework/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c