check-generated-files.sh (5380B)
1 #! /usr/bin/env sh 2 3 # Copyright The Mbed TLS Contributors 4 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5 # 6 # Purpose 7 # 8 # Check if generated files are up-to-date. 9 10 set -eu 11 12 if [ $# -ne 0 ] && [ "$1" = "--help" ]; then 13 cat <<EOF 14 $0 [-l | -u] 15 This script checks that all generated file are up-to-date. If some aren't, by 16 default the scripts reports it and exits in error; with the -u option, it just 17 updates them instead. 18 19 -u Update the files rather than return an error for out-of-date files. 20 -l List generated files, but do not update them. 21 EOF 22 exit 23 fi 24 25 . framework/scripts/project_detection.sh 26 27 if in_mbedtls_repo; then 28 library_dir='library' 29 elif in_tf_psa_crypto_repo; then 30 library_dir='core' 31 else 32 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2 33 exit 1 34 fi 35 36 UPDATE= 37 LIST= 38 while getopts lu OPTLET; do 39 case $OPTLET in 40 l) LIST=1;; 41 u) UPDATE=1;; 42 esac 43 done 44 45 # 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. 54 check() 55 { 56 SCRIPT=$1 57 shift 58 59 if [ -n "$LIST" ]; then 60 printf '%s\n' "$@" 61 return 62 fi 63 64 directory= 65 if [ -d "$1" ]; then 66 directory="$1" 67 rm -f "$directory"/*.bak 68 set -- "$1"/* 69 fi 70 71 for FILE in "$@"; do 72 if [ -e "$FILE" ]; then 73 cp -p "$FILE" "$FILE.bak" 74 else 75 rm -f "$FILE.bak" 76 fi 77 done 78 79 "$SCRIPT" 80 81 # Compare the script output to the old files and remove backups 82 for FILE in "$@"; do 83 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 88 echo "'$FILE' was either modified or deleted by '$SCRIPT'" 89 if [ -z "$UPDATE" ]; then 90 exit 1 91 else 92 rm -f "$FILE.bak" 93 fi 94 fi 95 done 96 97 if [ -n "$directory" ]; then 98 old_list="$*" 99 set -- "$directory"/* 100 new_list="$*" 101 # Check if there are any new files 102 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" 106 if [ -z "$UPDATE" ]; then 107 exit 1 108 fi 109 fi 110 fi 111 } 112 113 # Note: if the format of calls to the "check" function changes, update 114 # framework/scripts/code_style.py accordingly. For generated C source files (*.h or *.c), 115 # the format must be "check SCRIPT FILENAME...". For other source files, 116 # any shell syntax is permitted (including e.g. command substitution). 117 118 # 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 123 # These checks are common to Mbed TLS and TF-PSA-Crypto 124 check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c 125 check framework/scripts/generate_bignum_tests.py $(framework/scripts/generate_bignum_tests.py --list) 126 check framework/scripts/generate_config_tests.py $(framework/scripts/generate_config_tests.py --list) 127 check framework/scripts/generate_ecp_tests.py $(framework/scripts/generate_ecp_tests.py --list) 128 check framework/scripts/generate_psa_tests.py $(framework/scripts/generate_psa_tests.py --list) 129 check framework/scripts/generate_test_keys.py tests/include/test/test_keys.h 130 check scripts/generate_driver_wrappers.py $library_dir/psa_crypto_driver_wrappers.h $library_dir/psa_crypto_driver_wrappers_no_static.c 131 132 # Additional checks for Mbed TLS only 133 if in_mbedtls_repo; then 134 check scripts/generate_errors.pl library/error.c 135 check scripts/generate_query_config.pl programs/test/query_config.c 136 check scripts/generate_features.pl library/version_features.c 137 check framework/scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c 138 check framework/scripts/generate_tls_handshake_tests.py tests/opt-testcases/handshake-generated.sh 139 check framework/scripts/generate_tls13_compat_tests.py tests/opt-testcases/tls13-compat.sh 140 check framework/scripts/generate_test_cert_macros.py tests/include/test/test_certs.h 141 # generate_visualc_files enumerates source files (library/*.c). It doesn't 142 # care about their content, but the files must exist. So it must run after 143 # the step that creates or updates these files. 144 check scripts/generate_visualc_files.pl visualc/VS2017 145 fi 146 147 # Generated files that are present in the repository even in the development 148 # branch. (This is intended to be temporary, until the generator scripts are 149 # fully reviewed and the build scripts support a generated header file.) 150 check framework/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c