blob: e3c8e08afe3516f3abfb47f59fc950a85590dde7 [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 Uziunaiteef401832024-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
Ronald Cron381247e2024-07-02 09:55:39 +020028 if [ -d tf-psa-crypto ]; then
29 crypto_core_dir='tf-psa-crypto/core'
30 builtin_drivers_dir='tf-psa-crypto/drivers/builtin/src'
31 else
32 crypto_core_dir='library'
33 builtin_drivers_dir='library'
34 fi
Thomas Daubney4291bc22023-11-14 18:05:19 +000035elif in_tf_psa_crypto_repo; then
Ronald Cron381247e2024-07-02 09:55:39 +020036 crypto_core_dir='core'
37 builtin_drivers_dir='drivers/builtin/src/'
Thomas Daubneyc9f83862023-11-13 10:03:56 +000038else
39 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000040 exit 1
41fi
42
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020043UPDATE=
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020044LIST=
45while getopts lu OPTLET; do
46 case $OPTLET in
47 l) LIST=1;;
48 u) UPDATE=1;;
49 esac
50done
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020051
Gilles Peskineae4c4602021-04-21 16:11:50 +020052# check SCRIPT FILENAME[...]
53# check SCRIPT DIRECTORY
54# Run SCRIPT and check that it does not modify any of the specified files.
55# In the first form, there can be any number of FILENAMEs, which must be
56# regular files.
57# In the second form, there must be a single DIRECTORY, standing for the
58# list of files in the directory. Running SCRIPT must not modify any file
59# in the directory and must not add or remove files either.
60# If $UPDATE is empty, abort with an error status if a file is modified.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000061check()
62{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000063 SCRIPT=$1
Gilles Peskineae4c4602021-04-21 16:11:50 +020064 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000065
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020066 if [ -n "$LIST" ]; then
67 printf '%s\n' "$@"
68 return
69 fi
70
Gilles Peskineae4c4602021-04-21 16:11:50 +020071 directory=
72 if [ -d "$1" ]; then
73 directory="$1"
74 rm -f "$directory"/*.bak
75 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000076 fi
77
Gilles Peskineae4c4602021-04-21 16:11:50 +020078 for FILE in "$@"; do
Gilles Peskineb32966d2021-05-18 14:58:09 +020079 if [ -e "$FILE" ]; then
Gilles Peskineebfee6e2022-04-05 14:08:09 +020080 cp -p "$FILE" "$FILE.bak"
Gilles Peskineb32966d2021-05-18 14:58:09 +020081 else
82 rm -f "$FILE.bak"
83 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000084 done
85
Ronald Cronc29dd982024-07-16 18:03:01 +020086 # In the case of the config tests, generate only the files to be checked
87 # by the caller as they are divided into Mbed TLS and TF-PSA-Crypto
88 # specific ones.
89 if [ "${SCRIPT##*/}" = "generate_config_tests.py" ]; then
90 "$SCRIPT" "$@"
91 else
92 "$SCRIPT"
93 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000094
95 # Compare the script output to the old files and remove backups
Gilles Peskineae4c4602021-04-21 16:11:50 +020096 for FILE in "$@"; do
Gilles Peskineebfee6e2022-04-05 14:08:09 +020097 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
98 # Move the original file back so that $FILE's timestamp doesn't
99 # change (avoids spurious rebuilds with make).
100 mv "$FILE.bak" "$FILE"
101 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000102 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200103 if [ -z "$UPDATE" ]; then
104 exit 1
Gilles Peskineebfee6e2022-04-05 14:08:09 +0200105 else
106 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200107 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000108 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000109 done
110
Gilles Peskineae4c4602021-04-21 16:11:50 +0200111 if [ -n "$directory" ]; then
112 old_list="$*"
113 set -- "$directory"/*
114 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000115 # Check if there are any new files
Gilles Peskineae4c4602021-04-21 16:11:50 +0200116 if [ "$old_list" != "$new_list" ]; then
117 echo "Files were deleted or created by '$SCRIPT'"
118 echo "Before: $old_list"
119 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200120 if [ -z "$UPDATE" ]; then
121 exit 1
122 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000123 fi
124 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000125}
126
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100127# Note: if the format of calls to the "check" function changes, update
Elena Uziunaitee0d3ffe2024-12-10 15:22:34 +0000128# framework/scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100129# the format must be "check SCRIPT FILENAME...". For other source files,
130# any shell syntax is permitted (including e.g. command substitution).
131
Gilles Peskine3b56d292022-12-19 00:56:44 +0100132# Note: Instructions to generate those files are replicated in:
133# - **/Makefile (to (re)build them with make)
134# - **/CMakeLists.txt (to (re)build them with cmake)
135# - scripts/make_generated_files.bat (to generate them under Windows)
136
Thomas Daubneyd289b8b2023-11-14 15:30:07 +0000137# These checks are common to Mbed TLS and TF-PSA-Crypto
Ronald Cronc29dd982024-07-16 18:03:01 +0200138
139# The first case is temporary for the hybrid situation with a tf-psa-crypto
140# directory in Mbed TLS that is not just a TF-PSA-Crypto submodule.
141if [ -d tf-psa-crypto ]; then
142 cd tf-psa-crypto
Harry Ramseycbd00b02024-12-02 14:43:49 +0000143 check scripts/generate_psa_constants.py ./programs/psa/psa_constant_names_generated.c
Ronald Cron81a674e2025-03-11 12:53:45 +0100144 check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list)
145 check framework/scripts/generate_config_tests.py $(framework/scripts/generate_config_tests.py --list)
146 check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list)
147 check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list)
Ronald Cronc29dd982024-07-16 18:03:01 +0200148 cd ..
Ronald Cronbced0c72024-12-09 17:58:04 +0100149 # Generated files that are present in the repository even in the development
150 # branch. (This is intended to be temporary, until the generator scripts are
151 # fully reviewed and the build scripts support a generated header file.)
152 check framework/scripts/generate_psa_wrappers.py tf-psa-crypto/tests/include/test/psa_test_wrappers.h tf-psa-crypto/tests/src/psa_test_wrappers.c
Ronald Cron6a2cbe72024-11-13 09:20:30 +0100153 check tf-psa-crypto/scripts/generate_driver_wrappers.py ${crypto_core_dir}/psa_crypto_driver_wrappers.h \
154 ${crypto_core_dir}/psa_crypto_driver_wrappers_no_static.c
Ronald Cronc29dd982024-07-16 18:03:01 +0200155 check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.mbedtls_boolean.data
156else
Ronald Cronce3bcf02024-12-09 11:04:38 +0100157 check scripts/generate_psa_constants.py ./programs/psa/psa_constant_names_generated.c
Ronald Cronc29dd982024-07-16 18:03:01 +0200158 check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list)
Ronald Cron3f8275e2024-07-19 16:51:33 +0200159 if in_tf_psa_crypto_repo; then
Ronald Cronc29dd982024-07-16 18:03:01 +0200160 check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.psa_boolean.data
Ronald Cron3f8275e2024-07-19 16:51:33 +0200161 else
162 check framework/scripts/generate_config_tests.py tests/suites/test_suite_config.mbedtls_boolean.data
Ronald Cronc29dd982024-07-16 18:03:01 +0200163 fi
164 check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list)
165 check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list)
Ronald Cron6a2cbe72024-11-13 09:20:30 +0100166 check scripts/generate_driver_wrappers.py ${crypto_core_dir}/psa_crypto_driver_wrappers.h \
167 ${crypto_core_dir}/psa_crypto_driver_wrappers_no_static.c
Ronald Cronbced0c72024-12-09 17:58:04 +0100168 # Generated files that are present in the repository even in the development
169 # branch. (This is intended to be temporary, until the generator scripts are
170 # fully reviewed and the build scripts support a generated header file.)
171 check framework/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c
Ronald Cronc29dd982024-07-16 18:03:01 +0200172fi
173
Ronald Cron99226e92025-02-14 15:43:22 +0100174check framework/scripts/generate_test_keys.py tests/include/test/test_keys.h
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000175
176# Additional checks for Mbed TLS only
Thomas Daubney0eb2dc12023-11-14 16:56:45 +0000177if in_mbedtls_repo; then
Harry Ramsey8b4b1522024-10-15 12:04:26 +0100178 check scripts/generate_errors.pl library/error.c
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000179 check scripts/generate_query_config.pl programs/test/query_config.c
Harry Ramsey468c0ae2024-09-27 14:38:53 +0100180 check scripts/generate_features.pl library/version_features.c
Elena Uziunaite09fee362024-10-07 17:40:21 +0100181 check framework/scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
Gilles Peskine47733332025-03-01 14:28:20 +0100182 check framework/scripts/generate_tls_handshake_tests.py tests/opt-testcases/handshake-generated.sh
Elena Uziunaiteb74c3ea2024-10-08 13:02:48 +0100183 check framework/scripts/generate_tls13_compat_tests.py tests/opt-testcases/tls13-compat.sh
Ronald Cron99226e92025-02-14 15:43:22 +0100184 check framework/scripts/generate_test_cert_macros.py tests/include/test/test_certs.h
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000185 # generate_visualc_files enumerates source files (library/*.c). It doesn't
186 # care about their content, but the files must exist. So it must run after
187 # the step that creates or updates these files.
Bence Szépkútifac11222024-03-12 16:38:23 +0100188 check scripts/generate_visualc_files.pl visualc/VS2017
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000189fi