blob: b61c5ac172c9348799d9feb987de840afad3f1ba [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
Elena Uziunaitef1099cb2024-11-07 14:39:32 +000025. framework/scripts/project_detection.sh
Thomas Daubney0eb2dc12023-11-14 16:56:45 +000026
Thomas Daubneye58128e2023-11-14 15:25:52 +000027if in_mbedtls_repo; then
28 library_dir='library'
Thomas Daubney4291bc22023-11-14 18:05:19 +000029elif in_tf_psa_crypto_repo; then
Thomas Daubneye58128e2023-11-14 15:25:52 +000030 library_dir='core'
Thomas Daubneyc9f83862023-11-13 10:03:56 +000031else
32 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000033 exit 1
34fi
35
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020036UPDATE=
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020037LIST=
38while getopts lu OPTLET; do
39 case $OPTLET in
40 l) LIST=1;;
41 u) UPDATE=1;;
42 esac
43done
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020044
Gilles Peskineae4c4602021-04-21 16:11:50 +020045# check SCRIPT FILENAME[...]
46# check SCRIPT DIRECTORY
47# Run SCRIPT and check that it does not modify any of the specified files.
48# In the first form, there can be any number of FILENAMEs, which must be
49# regular files.
50# In the second form, there must be a single DIRECTORY, standing for the
51# list of files in the directory. Running SCRIPT must not modify any file
52# in the directory and must not add or remove files either.
53# If $UPDATE is empty, abort with an error status if a file is modified.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000054check()
55{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000056 SCRIPT=$1
Gilles Peskineae4c4602021-04-21 16:11:50 +020057 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000058
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020059 if [ -n "$LIST" ]; then
60 printf '%s\n' "$@"
61 return
62 fi
63
Gilles Peskineae4c4602021-04-21 16:11:50 +020064 directory=
65 if [ -d "$1" ]; then
66 directory="$1"
67 rm -f "$directory"/*.bak
68 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000069 fi
70
Gilles Peskineae4c4602021-04-21 16:11:50 +020071 for FILE in "$@"; do
Gilles Peskineb32966d2021-05-18 14:58:09 +020072 if [ -e "$FILE" ]; then
Gilles Peskineebfee6e2022-04-05 14:08:09 +020073 cp -p "$FILE" "$FILE.bak"
Gilles Peskineb32966d2021-05-18 14:58:09 +020074 else
75 rm -f "$FILE.bak"
76 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000077 done
78
Gilles Peskineae4c4602021-04-21 16:11:50 +020079 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000080
81 # Compare the script output to the old files and remove backups
Gilles Peskineae4c4602021-04-21 16:11:50 +020082 for FILE in "$@"; do
Gilles Peskineebfee6e2022-04-05 14:08:09 +020083 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
84 # Move the original file back so that $FILE's timestamp doesn't
85 # change (avoids spurious rebuilds with make).
86 mv "$FILE.bak" "$FILE"
87 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000088 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020089 if [ -z "$UPDATE" ]; then
90 exit 1
Gilles Peskineebfee6e2022-04-05 14:08:09 +020091 else
92 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020093 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000094 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000095 done
96
Gilles Peskineae4c4602021-04-21 16:11:50 +020097 if [ -n "$directory" ]; then
98 old_list="$*"
99 set -- "$directory"/*
100 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000101 # Check if there are any new files
Gilles Peskineae4c4602021-04-21 16:11:50 +0200102 if [ "$old_list" != "$new_list" ]; then
103 echo "Files were deleted or created by '$SCRIPT'"
104 echo "Before: $old_list"
105 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200106 if [ -z "$UPDATE" ]; then
107 exit 1
108 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000109 fi
110 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000111}
112
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100113# Note: if the format of calls to the "check" function changes, update
Elena Uziunaite993df662024-12-10 15:22:34 +0000114# framework/scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100115# the format must be "check SCRIPT FILENAME...". For other source files,
116# any shell syntax is permitted (including e.g. command substitution).
117
Gilles Peskine3b56d292022-12-19 00:56:44 +0100118# Note: Instructions to generate those files are replicated in:
119# - **/Makefile (to (re)build them with make)
120# - **/CMakeLists.txt (to (re)build them with cmake)
121# - scripts/make_generated_files.bat (to generate them under Windows)
122
Thomas Daubneyd289b8b2023-11-14 15:30:07 +0000123# These checks are common to Mbed TLS and TF-PSA-Crypto
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700124check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
David Horstmannea091522024-05-29 17:57:08 +0100125check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list)
Gilles Peskine06fb1802024-05-23 16:31:22 +0200126check framework/scripts/generate_config_tests.py $(framework/scripts/generate_config_tests.py --list)
David Horstmannea091522024-05-29 17:57:08 +0100127check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list)
128check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list)
Ronald Cron4eaf54e2024-12-09 18:46:11 +0100129check framework/scripts/generate_test_keys.py framework/tests/include/test/test_keys.h
Thomas Daubneyd3f84432023-11-14 11:53:14 +0000130check 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 +0000131
132# Additional checks for Mbed TLS only
Thomas Daubney0eb2dc12023-11-14 16:56:45 +0000133if in_mbedtls_repo; then
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000134 check scripts/generate_errors.pl library/error.c
135 check scripts/generate_query_config.pl programs/test/query_config.c
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000136 check scripts/generate_features.pl library/version_features.c
Elena Uziunaite7f5ec132024-10-07 17:40:21 +0100137 check framework/scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
Elena Uziunaite1d8a2252024-10-08 13:02:48 +0100138 check framework/scripts/generate_tls13_compat_tests.py tests/opt-testcases/tls13-compat.sh
David Horstmannea091522024-05-29 17:57:08 +0100139 check framework/scripts/generate_test_cert_macros.py tests/src/test_certs.h
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000140 # generate_visualc_files enumerates source files (library/*.c). It doesn't
141 # care about their content, but the files must exist. So it must run after
142 # the step that creates or updates these files.
Bence Szépkútifac11222024-03-12 16:38:23 +0100143 check scripts/generate_visualc_files.pl visualc/VS2017
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000144fi
Gilles Peskinee00150d2024-01-04 16:46:00 +0100145
146# Generated files that are present in the repository even in the development
147# branch. (This is intended to be temporary, until the generator scripts are
148# fully reviewed and the build scripts support a generated header file.)
David Horstmann501e9a92024-11-08 11:48:12 +0000149check framework/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c