check-debug-flags.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. set -eo pipefail
  3. script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
  4. cd "${script_path}/.."
  5. MISSING_FLAGS=n
  6. find_all_source_files () {
  7. # We're in the middle of a pre-commit run, so we should only check the files that have
  8. # actually changed. The reason is that "git ls-files | grep" on the entire repo takes
  9. # about 100ms. That is perfectly fine during a CI run, but becomes noticeable during a
  10. # pre-commit hook. It is unnecessary to check the entire repository on every single
  11. # commit, so we save some time here.
  12. #
  13. # And see https://github.com/LadybirdBrowser/ladybird/issues/283; the reason this is
  14. # pulled out into separate function is, Bash 3.2 apparently has a parser bug which
  15. # makes it choke on the above comment if it's in the process substitution below.
  16. for file in "$@"; do
  17. if [[ ("${file}" =~ \.cpp || "${file}" =~ \.h || "${file}" =~ \.in) ]]; then
  18. echo "$file"
  19. fi
  20. done
  21. }
  22. # Check whether all_the_debug_macros.cmake sets all the flags used in C++ code.
  23. while IFS= read -r FLAG; do
  24. # We intentionally don't check for commented-out lines,
  25. # in order to keep track of false positives.
  26. if ! grep -qF "set(${FLAG} ON)" Meta/CMake/all_the_debug_macros.cmake ; then
  27. echo "'all_the_debug_macros.cmake' is missing ${FLAG}"
  28. MISSING_FLAGS=y
  29. fi
  30. done < <(
  31. if [ "$#" -eq "0" ]; then
  32. git ls-files -- \
  33. '*.cpp' \
  34. '*.h' \
  35. '*.in'
  36. else
  37. find_all_source_files "$@"
  38. fi \
  39. | xargs grep -E '(_DEBUG|DEBUG_)' \
  40. | sed -re 's,^.*[^a-zA-Z0-9_]([a-zA-Z0-9_]*DEBUG[a-zA-Z0-9_]*).*$,\1,' \
  41. | sort -u
  42. )
  43. if [ "n" != "${MISSING_FLAGS}" ] ; then
  44. echo "Some flags are missing for the ALL_THE_DEBUG_MACROS feature."
  45. echo "If you just added a new SOMETHING_DEBUG flag, that's great!"
  46. echo "We want to enable all of these in automated builds, so that the code doesn't rot."
  47. echo "Please add it to Meta/CMake/all_the_debug_macros.cmake"
  48. echo "Or perhaps it's not a debug flag?"
  49. echo "Please also add it to Meta/CMake/all_the_debug_macros.cmake"
  50. exit 1
  51. fi