check-generated-files.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #! /usr/bin/env sh
  2. # Copyright The Mbed TLS Contributors
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # Purpose
  18. #
  19. # Check if generated files are up-to-date.
  20. set -eu
  21. if [ $# -ne 0 ] && [ "$1" = "--help" ]; then
  22. cat <<EOF
  23. $0 [-u]
  24. This script checks that all generated file are up-to-date. If some aren't, by
  25. default the scripts reports it and exits in error; with the -u option, it just
  26. updates them instead.
  27. -u Update the files rather than return an error for out-of-date files.
  28. EOF
  29. exit
  30. fi
  31. if [ -d library -a -d include -a -d tests ]; then :; else
  32. echo "Must be run from mbed TLS root" >&2
  33. exit 1
  34. fi
  35. UPDATE=
  36. if [ $# -ne 0 ] && [ "$1" = "-u" ]; then
  37. shift
  38. UPDATE='y'
  39. fi
  40. # check SCRIPT FILENAME[...]
  41. # check SCRIPT DIRECTORY
  42. # Run SCRIPT and check that it does not modify any of the specified files.
  43. # In the first form, there can be any number of FILENAMEs, which must be
  44. # regular files.
  45. # In the second form, there must be a single DIRECTORY, standing for the
  46. # list of files in the directory. Running SCRIPT must not modify any file
  47. # in the directory and must not add or remove files either.
  48. # If $UPDATE is empty, abort with an error status if a file is modified.
  49. check()
  50. {
  51. SCRIPT=$1
  52. shift
  53. directory=
  54. if [ -d "$1" ]; then
  55. directory="$1"
  56. set -- "$1"/*
  57. fi
  58. for FILE in "$@"; do
  59. cp "$FILE" "$FILE.bak"
  60. done
  61. "$SCRIPT"
  62. # Compare the script output to the old files and remove backups
  63. for FILE in "$@"; do
  64. if ! diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
  65. echo "'$FILE' was either modified or deleted by '$SCRIPT'"
  66. if [ -z "$UPDATE" ]; then
  67. exit 1
  68. fi
  69. fi
  70. if [ -z "$UPDATE" ]; then
  71. mv "$FILE.bak" "$FILE"
  72. else
  73. rm "$FILE.bak"
  74. fi
  75. done
  76. if [ -n "$directory" ]; then
  77. old_list="$*"
  78. set -- "$directory"/*
  79. new_list="$*"
  80. # Check if there are any new files
  81. if [ "$old_list" != "$new_list" ]; then
  82. echo "Files were deleted or created by '$SCRIPT'"
  83. echo "Before: $old_list"
  84. echo "After: $new_list"
  85. if [ -z "$UPDATE" ]; then
  86. exit 1
  87. fi
  88. fi
  89. fi
  90. }
  91. check scripts/generate_errors.pl library/error.c
  92. check scripts/generate_query_config.pl programs/test/query_config.c
  93. check scripts/generate_features.pl library/version_features.c
  94. check scripts/generate_visualc_files.pl visualc/VS2010
  95. check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
  96. check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)