blob: 18d10c0a3f7d9b189529529bd6f30ee4af157715 [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 Rodgman7ff79652023-11-03 12:04:52 +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
14$0 [-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.
20EOF
21 exit
22fi
23
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000024if [ -d library -a -d include -a -d tests ]; then :; else
Gilles Peskinef08ca832023-09-12 19:21:54 +020025 echo "Must be run from Mbed TLS root" >&2
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000026 exit 1
27fi
28
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020029UPDATE=
30if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
31 shift
32 UPDATE='y'
33fi
34
Gilles Peskine30ccba42021-04-21 16:11:50 +020035# check SCRIPT FILENAME[...]
36# check SCRIPT DIRECTORY
37# Run SCRIPT and check that it does not modify any of the specified files.
38# In the first form, there can be any number of FILENAMEs, which must be
39# regular files.
40# In the second form, there must be a single DIRECTORY, standing for the
41# list of files in the directory. Running SCRIPT must not modify any file
42# in the directory and must not add or remove files either.
43# If $UPDATE is empty, abort with an error status if a file is modified.
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000044check()
45{
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000046 SCRIPT=$1
Gilles Peskine30ccba42021-04-21 16:11:50 +020047 shift
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000048
Gilles Peskine30ccba42021-04-21 16:11:50 +020049 directory=
50 if [ -d "$1" ]; then
51 directory="$1"
52 set -- "$1"/*
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000053 fi
54
Gilles Peskine30ccba42021-04-21 16:11:50 +020055 for FILE in "$@"; do
Gilles Peskineb5c43822022-04-05 14:08:09 +020056 cp -p "$FILE" "$FILE.bak"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000057 done
58
Gilles Peskine30ccba42021-04-21 16:11:50 +020059 "$SCRIPT"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000060
61 # Compare the script output to the old files and remove backups
Gilles Peskine30ccba42021-04-21 16:11:50 +020062 for FILE in "$@"; do
Gilles Peskineb5c43822022-04-05 14:08:09 +020063 if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
64 # Move the original file back so that $FILE's timestamp doesn't
65 # change (avoids spurious rebuilds with make).
66 mv "$FILE.bak" "$FILE"
67 else
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000068 echo "'$FILE' was either modified or deleted by '$SCRIPT'"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020069 if [ -z "$UPDATE" ]; then
70 exit 1
Gilles Peskineb5c43822022-04-05 14:08:09 +020071 else
72 rm "$FILE.bak"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020073 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000074 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000075 done
76
Gilles Peskine30ccba42021-04-21 16:11:50 +020077 if [ -n "$directory" ]; then
78 old_list="$*"
79 set -- "$directory"/*
80 new_list="$*"
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000081 # Check if there are any new files
Gilles Peskine30ccba42021-04-21 16:11:50 +020082 if [ "$old_list" != "$new_list" ]; then
83 echo "Files were deleted or created by '$SCRIPT'"
84 echo "Before: $old_list"
85 echo "After: $new_list"
Manuel Pégourié-Gonnard2774fc42020-07-16 10:40:13 +020086 if [ -z "$UPDATE" ]; then
87 exit 1
88 fi
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000089 fi
90 fi
Manuel Pégourié-Gonnardb3b8e432015-02-13 14:52:19 +000091}
92
Gilles Peskine4ca54d42022-12-19 00:48:58 +010093# Note: if the format of calls to the "check" function changes, update
94# scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
95# the format must be "check SCRIPT FILENAME...". For other source files,
96# any shell syntax is permitted (including e.g. command substitution).
97
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +000098check scripts/generate_errors.pl library/error.c
Jaeden Amero7cb47de2019-02-28 11:37:23 +000099check scripts/generate_query_config.pl programs/test/query_config.c
Andres Amaya Garcia4c1e2ec2018-01-10 11:03:45 +0000100check scripts/generate_features.pl library/version_features.c
101check scripts/generate_visualc_files.pl visualc/VS2010
Cameron Nemoe18d09d2020-09-22 10:37:26 -0700102check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
Gilles Peskineb7119c52024-01-04 16:46:00 +0100103check tests/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c
Werner Lewis545911f2022-07-08 13:54:57 +0100104check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
Gilles Peskineb19fa4e2024-06-26 20:12:34 +0200105check tests/scripts/generate_config_tests.py $(tests/scripts/generate_bignum_tests.py --list)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100106check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)