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 10set -eu 11 12if [ $# -ne 0 ] && [ "$1" = "--help" ]; then 13 cat <<EOF 14$0 [-l | -u] 15This 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. 20 -l List generated files, but do not update them. 21EOF 22 exit 23fi 24 25in_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 31} 32 33if in_mbedtls_repo; then 34 library_dir='library' 35elif in_tf_psa_crypto_repo; then 36 library_dir='core' 37else 38 echo "Must be run from Mbed TLS root or TF-PSA-Crypto root" >&2 39 exit 1 40fi 41 42UPDATE= 43LIST= 44while getopts lu OPTLET; do 45 case $OPTLET in 46 l) LIST=1;; 47 u) UPDATE=1;; 48 esac 49done 50 51# 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. 60check() 61{ 62 SCRIPT=$1 63 shift 64 65 if [ -n "$LIST" ]; then 66 printf '%s\n' "$@" 67 return 68 fi 69 70 directory= 71 if [ -d "$1" ]; then 72 directory="$1" 73 rm -f "$directory"/*.bak 74 set -- "$1"/* 75 fi 76 77 for FILE in "$@"; do 78 if [ -e "$FILE" ]; then 79 cp -p "$FILE" "$FILE.bak" 80 else 81 rm -f "$FILE.bak" 82 fi 83 done 84 85 "$SCRIPT" 86 87 # Compare the script output to the old files and remove backups 88 for FILE in "$@"; do 89 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 94 echo "'$FILE' was either modified or deleted by '$SCRIPT'" 95 if [ -z "$UPDATE" ]; then 96 exit 1 97 else 98 rm -f "$FILE.bak" 99 fi 100 fi 101 done 102 103 if [ -n "$directory" ]; then 104 old_list="$*" 105 set -- "$directory"/* 106 new_list="$*" 107 # Check if there are any new files 108 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" 112 if [ -z "$UPDATE" ]; then 113 exit 1 114 fi 115 fi 116 fi 117} 118 119# 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 124# 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 129# These checks are common to Mbed TLS and TF-PSA-Crypto 130check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c 131check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) 132check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list) 133check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) 134check scripts/generate_driver_wrappers.py $library_dir/psa_crypto_driver_wrappers.h $library_dir/psa_crypto_driver_wrappers_no_static.c 135 136# Additional checks for Mbed TLS only 137if in_mbedtls_repo; then 138 check scripts/generate_errors.pl library/error.c 139 check scripts/generate_query_config.pl programs/test/query_config.c 140 check scripts/generate_features.pl library/version_features.c 141 check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c 142 # generate_visualc_files enumerates source files (library/*.c). It doesn't 143 # care about their content, but the files must exist. So it must run after 144 # the step that creates or updates these files. 145 check scripts/generate_visualc_files.pl visualc/VS2017 146fi 147 148# Generated files that are present in the repository even in the development 149# branch. (This is intended to be temporary, until the generator scripts are 150# fully reviewed and the build scripts support a generated header file.) 151check tests/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c 152