lint-makefiles.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. # The GNU C Library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. # The GNU C Library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Lesser General Public License for more details.
  12. # You should have received a copy of the GNU Lesser General Public
  13. # License along with the GNU C Library; if not, see
  14. # <https://www.gnu.org/licenses/>.
  15. # This script checks to see that all Makefiles in the source tree
  16. # conform to the sorted variable rules as defined by:
  17. # scripts/sort-makefile-lines.py.
  18. # Any difference is an error and should be corrected e.g. the lines
  19. # reordered to sort correctly.
  20. # The intent with this check is to ensure that changes made by
  21. # developers match the expected format for the project.
  22. export LC_ALL=C
  23. tmpfile="$(mktemp)"
  24. cleanup () {
  25. rm -f -- "$tmpfile"
  26. }
  27. trap cleanup 0
  28. PYTHON=$1
  29. # Absolute or relative path to the source directory.
  30. srcdir=$2
  31. # Must specify $PYTHON.
  32. if [ -z "$PYTHON" ]; then
  33. echo 'Please specify $PYTHON interpreter'
  34. exit 1
  35. fi
  36. # Absolute or relative $srcdir must exist and be a directory.
  37. if [ ! -d "$srcdir" ]; then
  38. echo 'Please specify $srcdir in which to look for Makefiles'
  39. exit 1
  40. fi
  41. linted=0
  42. failed=0
  43. for mfile in `find "$srcdir" -name Makefile`; do
  44. $PYTHON "${srcdir}/scripts/sort-makefile-lines.py" < "$mfile" > "$tmpfile"
  45. # Printed the expected -> actual difference on error.
  46. if ! diff -u --label "$mfile.expected" "$tmpfile" "$mfile"; then
  47. failed=$((failed+1))
  48. fi
  49. linted=$((linted+1))
  50. done
  51. # Must have linted at least the top-level Makefile.
  52. if [ $linted -lt 1 ]; then
  53. echo "Did not lint any Makefiles!"
  54. exit 1
  55. fi
  56. if [ $failed -gt 0 ]; then
  57. echo "---"
  58. echo "Tested $linted Makefiles and $failed were incorrectly sorted"
  59. echo 'Please use `patch -R -pN` and the output above to correct the sorting'
  60. exit 1
  61. fi
  62. # All Makefiles linted clean.
  63. exit 0