1*62c56f98SSadaf Ebrahimi#! /usr/bin/env sh 2*62c56f98SSadaf Ebrahimi 3*62c56f98SSadaf Ebrahimi# Copyright The Mbed TLS Contributors 4*62c56f98SSadaf Ebrahimi# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5*62c56f98SSadaf Ebrahimi# 6*62c56f98SSadaf Ebrahimi# Purpose 7*62c56f98SSadaf Ebrahimi# 8*62c56f98SSadaf Ebrahimi# Check if generated files are up-to-date. 9*62c56f98SSadaf Ebrahimi 10*62c56f98SSadaf Ebrahimiset -eu 11*62c56f98SSadaf Ebrahimi 12*62c56f98SSadaf Ebrahimiif [ $# -ne 0 ] && [ "$1" = "--help" ]; then 13*62c56f98SSadaf Ebrahimi cat <<EOF 14*62c56f98SSadaf Ebrahimi$0 [-l | -u] 15*62c56f98SSadaf EbrahimiThis script checks that all generated file are up-to-date. If some aren't, by 16*62c56f98SSadaf Ebrahimidefault the scripts reports it and exits in error; with the -u option, it just 17*62c56f98SSadaf Ebrahimiupdates them instead. 18*62c56f98SSadaf Ebrahimi 19*62c56f98SSadaf Ebrahimi -u Update the files rather than return an error for out-of-date files. 20*62c56f98SSadaf Ebrahimi -l List generated files, but do not update them. 21*62c56f98SSadaf EbrahimiEOF 22*62c56f98SSadaf Ebrahimi exit 23*62c56f98SSadaf Ebrahimifi 24*62c56f98SSadaf Ebrahimi 25*62c56f98SSadaf Ebrahimiif [ -d library -a -d include -a -d tests ]; then :; else 26*62c56f98SSadaf Ebrahimi echo "Must be run from Mbed TLS root" >&2 27*62c56f98SSadaf Ebrahimi exit 1 28*62c56f98SSadaf Ebrahimifi 29*62c56f98SSadaf Ebrahimi 30*62c56f98SSadaf EbrahimiUPDATE= 31*62c56f98SSadaf EbrahimiLIST= 32*62c56f98SSadaf Ebrahimiwhile getopts lu OPTLET; do 33*62c56f98SSadaf Ebrahimi case $OPTLET in 34*62c56f98SSadaf Ebrahimi l) LIST=1;; 35*62c56f98SSadaf Ebrahimi u) UPDATE=1;; 36*62c56f98SSadaf Ebrahimi esac 37*62c56f98SSadaf Ebrahimidone 38*62c56f98SSadaf Ebrahimi 39*62c56f98SSadaf Ebrahimi# check SCRIPT FILENAME[...] 40*62c56f98SSadaf Ebrahimi# check SCRIPT DIRECTORY 41*62c56f98SSadaf Ebrahimi# Run SCRIPT and check that it does not modify any of the specified files. 42*62c56f98SSadaf Ebrahimi# In the first form, there can be any number of FILENAMEs, which must be 43*62c56f98SSadaf Ebrahimi# regular files. 44*62c56f98SSadaf Ebrahimi# In the second form, there must be a single DIRECTORY, standing for the 45*62c56f98SSadaf Ebrahimi# list of files in the directory. Running SCRIPT must not modify any file 46*62c56f98SSadaf Ebrahimi# in the directory and must not add or remove files either. 47*62c56f98SSadaf Ebrahimi# If $UPDATE is empty, abort with an error status if a file is modified. 48*62c56f98SSadaf Ebrahimicheck() 49*62c56f98SSadaf Ebrahimi{ 50*62c56f98SSadaf Ebrahimi SCRIPT=$1 51*62c56f98SSadaf Ebrahimi shift 52*62c56f98SSadaf Ebrahimi 53*62c56f98SSadaf Ebrahimi if [ -n "$LIST" ]; then 54*62c56f98SSadaf Ebrahimi printf '%s\n' "$@" 55*62c56f98SSadaf Ebrahimi return 56*62c56f98SSadaf Ebrahimi fi 57*62c56f98SSadaf Ebrahimi 58*62c56f98SSadaf Ebrahimi directory= 59*62c56f98SSadaf Ebrahimi if [ -d "$1" ]; then 60*62c56f98SSadaf Ebrahimi directory="$1" 61*62c56f98SSadaf Ebrahimi rm -f "$directory"/*.bak 62*62c56f98SSadaf Ebrahimi set -- "$1"/* 63*62c56f98SSadaf Ebrahimi fi 64*62c56f98SSadaf Ebrahimi 65*62c56f98SSadaf Ebrahimi for FILE in "$@"; do 66*62c56f98SSadaf Ebrahimi if [ -e "$FILE" ]; then 67*62c56f98SSadaf Ebrahimi cp -p "$FILE" "$FILE.bak" 68*62c56f98SSadaf Ebrahimi else 69*62c56f98SSadaf Ebrahimi rm -f "$FILE.bak" 70*62c56f98SSadaf Ebrahimi fi 71*62c56f98SSadaf Ebrahimi done 72*62c56f98SSadaf Ebrahimi 73*62c56f98SSadaf Ebrahimi "$SCRIPT" 74*62c56f98SSadaf Ebrahimi 75*62c56f98SSadaf Ebrahimi # Compare the script output to the old files and remove backups 76*62c56f98SSadaf Ebrahimi for FILE in "$@"; do 77*62c56f98SSadaf Ebrahimi if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then 78*62c56f98SSadaf Ebrahimi # Move the original file back so that $FILE's timestamp doesn't 79*62c56f98SSadaf Ebrahimi # change (avoids spurious rebuilds with make). 80*62c56f98SSadaf Ebrahimi mv "$FILE.bak" "$FILE" 81*62c56f98SSadaf Ebrahimi else 82*62c56f98SSadaf Ebrahimi echo "'$FILE' was either modified or deleted by '$SCRIPT'" 83*62c56f98SSadaf Ebrahimi if [ -z "$UPDATE" ]; then 84*62c56f98SSadaf Ebrahimi exit 1 85*62c56f98SSadaf Ebrahimi else 86*62c56f98SSadaf Ebrahimi rm -f "$FILE.bak" 87*62c56f98SSadaf Ebrahimi fi 88*62c56f98SSadaf Ebrahimi fi 89*62c56f98SSadaf Ebrahimi done 90*62c56f98SSadaf Ebrahimi 91*62c56f98SSadaf Ebrahimi if [ -n "$directory" ]; then 92*62c56f98SSadaf Ebrahimi old_list="$*" 93*62c56f98SSadaf Ebrahimi set -- "$directory"/* 94*62c56f98SSadaf Ebrahimi new_list="$*" 95*62c56f98SSadaf Ebrahimi # Check if there are any new files 96*62c56f98SSadaf Ebrahimi if [ "$old_list" != "$new_list" ]; then 97*62c56f98SSadaf Ebrahimi echo "Files were deleted or created by '$SCRIPT'" 98*62c56f98SSadaf Ebrahimi echo "Before: $old_list" 99*62c56f98SSadaf Ebrahimi echo "After: $new_list" 100*62c56f98SSadaf Ebrahimi if [ -z "$UPDATE" ]; then 101*62c56f98SSadaf Ebrahimi exit 1 102*62c56f98SSadaf Ebrahimi fi 103*62c56f98SSadaf Ebrahimi fi 104*62c56f98SSadaf Ebrahimi fi 105*62c56f98SSadaf Ebrahimi} 106*62c56f98SSadaf Ebrahimi 107*62c56f98SSadaf Ebrahimi# Note: if the format of calls to the "check" function changes, update 108*62c56f98SSadaf Ebrahimi# scripts/code_style.py accordingly. For generated C source files (*.h or *.c), 109*62c56f98SSadaf Ebrahimi# the format must be "check SCRIPT FILENAME...". For other source files, 110*62c56f98SSadaf Ebrahimi# any shell syntax is permitted (including e.g. command substitution). 111*62c56f98SSadaf Ebrahimi 112*62c56f98SSadaf Ebrahimi# Note: Instructions to generate those files are replicated in: 113*62c56f98SSadaf Ebrahimi# - **/Makefile (to (re)build them with make) 114*62c56f98SSadaf Ebrahimi# - **/CMakeLists.txt (to (re)build them with cmake) 115*62c56f98SSadaf Ebrahimi# - scripts/make_generated_files.bat (to generate them under Windows) 116*62c56f98SSadaf Ebrahimi 117*62c56f98SSadaf Ebrahimicheck scripts/generate_errors.pl library/error.c 118*62c56f98SSadaf Ebrahimicheck scripts/generate_query_config.pl programs/test/query_config.c 119*62c56f98SSadaf Ebrahimicheck scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.h library/psa_crypto_driver_wrappers_no_static.c 120*62c56f98SSadaf Ebrahimicheck scripts/generate_features.pl library/version_features.c 121*62c56f98SSadaf Ebrahimicheck scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c 122*62c56f98SSadaf Ebrahimi# generate_visualc_files enumerates source files (library/*.c). It doesn't 123*62c56f98SSadaf Ebrahimi# care about their content, but the files must exist. So it must run after 124*62c56f98SSadaf Ebrahimi# the step that creates or updates these files. 125*62c56f98SSadaf Ebrahimicheck scripts/generate_visualc_files.pl visualc/VS2013 126*62c56f98SSadaf Ebrahimicheck scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c 127*62c56f98SSadaf Ebrahimicheck tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) 128*62c56f98SSadaf Ebrahimicheck tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list) 129*62c56f98SSadaf Ebrahimicheck tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) 130