blob: 6f92ecd311f3bcfd57c85ff72b0d5ea36f6c6853 [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 Daubneyd3f84432023-11-14 11:53:14 +000025# Detect whether we are in one of Mbed TLS or TF-PSA-Crypto and exit if not
26if [ -d library -a -d include -a -d tests ]; then :;
Thomas Daubneyc9f83862023-11-13 10:03:56 +000027elif [ -d core -a -d include -a -d tests ]; then :;
28else
29 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000030 exit 1
31fi
32
Thomas Daubneyd3f84432023-11-14 11:53:14 +000033# Now we know we are in one of Mbed TLS or TF-PSA-Crypto, determine which one
34in_mbedtls_build_dir () {
35 test -d library
36}
37
38if in_mbedtls_build_dir; then
39 library_dir='library'
40else
41 library_dir='core'
42fi
43
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020044UPDATE=
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020045LIST=
46while getopts lu OPTLET; do
47 case $OPTLET in
48 l) LIST=1;;
49 u) UPDATE=1;;
50 esac
51done
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020052
Gilles Peskineae4c4602021-04-21 16:11:50 +020053# check SCRIPT FILENAME[...]
54# check SCRIPT DIRECTORY
55# Run SCRIPT and check that it does not modify any of the specified files.
56# In the first form, there can be any number of FILENAMEs, which must be
57# regular files.
58# In the second form, there must be a single DIRECTORY, standing for the
59# list of files in the directory. Running SCRIPT must not modify any file
60# in the directory and must not add or remove files either.
61# If $UPDATE is empty, abort with an error status if a file is modified.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000062check()
63{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000064 SCRIPT=$1
Gilles Peskineae4c4602021-04-21 16:11:50 +020065 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000066
Gilles Peskine1fe01ac2021-07-01 11:13:29 +020067 if [ -n "$LIST" ]; then
68 printf '%s\n' "$@"
69 return
70 fi
71
Gilles Peskineae4c4602021-04-21 16:11:50 +020072 directory=
73 if [ -d "$1" ]; then
74 directory="$1"
75 rm -f "$directory"/*.bak
76 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000077 fi
78
Gilles Peskineae4c4602021-04-21 16:11:50 +020079 for FILE in "$@"; do
Gilles Peskineb32966d2021-05-18 14:58:09 +020080 if [ -e "$FILE" ]; then
Gilles Peskineebfee6e2022-04-05 14:08:09 +020081 cp -p "$FILE" "$FILE.bak"
Gilles Peskineb32966d2021-05-18 14:58:09 +020082 else
83 rm -f "$FILE.bak"
84 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000085 done
86
Gilles Peskineae4c4602021-04-21 16:11:50 +020087 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000088
89 # Compare the script output to the old files and remove backups
Gilles Peskineae4c4602021-04-21 16:11:50 +020090 for FILE in "$@"; do
Gilles Peskineebfee6e2022-04-05 14:08:09 +020091 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
92 # Move the original file back so that $FILE's timestamp doesn't
93 # change (avoids spurious rebuilds with make).
94 mv "$FILE.bak" "$FILE"
95 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000096 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020097 if [ -z "$UPDATE" ]; then
98 exit 1
Gilles Peskineebfee6e2022-04-05 14:08:09 +020099 else
100 rm -f "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200101 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000102 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000103 done
104
Gilles Peskineae4c4602021-04-21 16:11:50 +0200105 if [ -n "$directory" ]; then
106 old_list="$*"
107 set -- "$directory"/*
108 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000109 # Check if there are any new files
Gilles Peskineae4c4602021-04-21 16:11:50 +0200110 if [ "$old_list" != "$new_list" ]; then
111 echo "Files were deleted or created by '$SCRIPT'"
112 echo "Before: $old_list"
113 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +0200114 if [ -z "$UPDATE" ]; then
115 exit 1
116 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000117 fi
118 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +0000119}
120
Gilles Peskine9a3771e2022-12-19 00:48:58 +0100121# Note: if the format of calls to the "check" function changes, update
122# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
123# the format must be "check SCRIPT FILENAME...". For other source files,
124# any shell syntax is permitted (including e.g. command substitution).
125
Gilles Peskine3b56d292022-12-19 00:56:44 +0100126# Note: Instructions to generate those files are replicated in:
127# - **/Makefile (to (re)build them with make)
128# - **/CMakeLists.txt (to (re)build them with cmake)
129# - scripts/make_generated_files.bat (to generate them under Windows)
130
Thomas Daubneyc9f83862023-11-13 10:03:56 +0000131# These checks are common to Mbed TLS and TF PSA Crypto
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700132check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Werner Lewis8b2df742022-07-08 13:54:57 +0100133check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
Gabor Mezei95ecaaf2023-01-16 16:53:29 +0100134check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100135check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
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 Daubneyd3f84432023-11-14 11:53:14 +0000139if in_mbedtls_build_dir; 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
144 # generate_visualc_files enumerates source files (library/*.c). It doesn't
145 # care about their content, but the files must exist. So it must run after
146 # the step that creates or updates these files.
147 check scripts/generate_visualc_files.pl visualc/VS2013
148fi