check-generated-files.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 [-l | -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. -l List generated files, but do not update them.
  29. EOF
  30. exit
  31. fi
  32. if [ -d library -a -d include -a -d tests ]; then :; else
  33. echo "Must be run from mbed TLS root" >&2
  34. exit 1
  35. fi
  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. # check SCRIPT FILENAME[...]
  45. # check SCRIPT DIRECTORY
  46. # Run SCRIPT and check that it does not modify any of the specified files.
  47. # In the first form, there can be any number of FILENAMEs, which must be
  48. # regular files.
  49. # In the second form, there must be a single DIRECTORY, standing for the
  50. # list of files in the directory. Running SCRIPT must not modify any file
  51. # in the directory and must not add or remove files either.
  52. # If $UPDATE is empty, abort with an error status if a file is modified.
  53. check()
  54. {
  55. SCRIPT=$1
  56. shift
  57. if [ -n "$LIST" ]; then
  58. printf '%s\n' "$@"
  59. return
  60. fi
  61. directory=
  62. if [ -d "$1" ]; then
  63. directory="$1"
  64. rm -f "$directory"/*.bak
  65. set -- "$1"/*
  66. fi
  67. for FILE in "$@"; do
  68. if [ -e "$FILE" ]; then
  69. cp -p "$FILE" "$FILE.bak"
  70. else
  71. rm -f "$FILE.bak"
  72. fi
  73. done
  74. "$SCRIPT"
  75. # Compare the script output to the old files and remove backups
  76. for FILE in "$@"; do
  77. if diff "$FILE" "$FILE.bak" >/dev/null 2>&1; then
  78. # Move the original file back so that $FILE's timestamp doesn't
  79. # change (avoids spurious rebuilds with make).
  80. mv "$FILE.bak" "$FILE"
  81. else
  82. echo "'$FILE' was either modified or deleted by '$SCRIPT'"
  83. if [ -z "$UPDATE" ]; then
  84. exit 1
  85. else
  86. rm -f "$FILE.bak"
  87. fi
  88. fi
  89. done
  90. if [ -n "$directory" ]; then
  91. old_list="$*"
  92. set -- "$directory"/*
  93. new_list="$*"
  94. # Check if there are any new files
  95. if [ "$old_list" != "$new_list" ]; then
  96. echo "Files were deleted or created by '$SCRIPT'"
  97. echo "Before: $old_list"
  98. echo "After: $new_list"
  99. if [ -z "$UPDATE" ]; then
  100. exit 1
  101. fi
  102. fi
  103. fi
  104. }
  105. # Note: if the format of calls to the "check" function changes, update
  106. # scripts/code_style.py accordingly. For generated C source files (*.h or *.c),
  107. # the format must be "check SCRIPT FILENAME...". For other source files,
  108. # any shell syntax is permitted (including e.g. command substitution).
  109. # Note: Instructions to generate those files are replicated in:
  110. # - **/Makefile (to (re)build them with make)
  111. # - **/CMakeLists.txt (to (re)build them with cmake)
  112. # - scripts/make_generated_files.bat (to generate them under Windows)
  113. check scripts/generate_errors.pl library/error.c
  114. check scripts/generate_query_config.pl programs/test/query_config.c
  115. check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c
  116. check scripts/generate_features.pl library/version_features.c
  117. check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c
  118. # generate_visualc_files enumerates source files (library/*.c). It doesn't
  119. # care about their content, but the files must exist. So it must run after
  120. # the step that creates or updates these files.
  121. check scripts/generate_visualc_files.pl visualc/VS2013
  122. check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
  123. check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
  124. check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list)
  125. check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)