header_guards.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. if [ ! -f "version.py" ]; then
  3. echo "Warning: This script is intended to be run from the root of the Godot repository."
  4. echo "Some of the paths checks may not work as intended from a different folder."
  5. fi
  6. for file in $(find -name "thirdparty" -prune -o -name "*.h" -print); do
  7. # Skip *.gen.h and *-so_wrap.h, they're generated.
  8. if [[ "$file" == *".gen.h" || "$file" == *"-so_wrap.h" ]]; then continue; fi
  9. # Has important define before normal header guards.
  10. if [[ "$file" == *"thread.h" || "$file" == *"platform_config.h" ]]; then continue; fi
  11. bname=$(basename $file .h)
  12. # Add custom prefix or suffix for generic filenames with a well-defined namespace.
  13. prefix=
  14. if [[ "$file" == "./modules/gdnative/"*"/register_types.h" ]]; then
  15. module=$(echo $file | sed "s@.*modules/gdnative/\([^/]*\).*@\1@")
  16. prefix="${module^^}_"
  17. elif [[ "$file" == "./modules/"*"/register_types.h" ]]; then
  18. module=$(echo $file | sed "s@.*modules/\([^/]*\).*@\1@")
  19. prefix="${module^^}_"
  20. fi
  21. if [[ "$file" == "./platform/"*"/api/api.h" || "$file" == "./platform/"*"/export/"* ]]; then
  22. platform=$(echo $file | sed "s@.*platform/\([^/]*\).*@\1@")
  23. prefix="${platform^^}_"
  24. fi
  25. if [[ "$file" == "./modules/mono/utils/"* && "$bname" != *"mono"* ]]; then prefix="MONO_"; fi
  26. if [[ "$file" == "./modules/gdnative/include/gdnative/"* ]]; then prefix="GDNATIVE_"; fi
  27. suffix=
  28. if [[ "$file" == *"ustring.h" ]]; then suffix="_GODOT"; fi
  29. # ^^ is bash builtin for UPPERCASE.
  30. guard="${prefix}${bname^^}${suffix}_H"
  31. # Replaces guards to use computed name.
  32. # We also add some \n to make sure there's a proper separation.
  33. sed -i $file -e "0,/ifndef/s/#ifndef.*/\n#ifndef $guard/"
  34. sed -i $file -e "0,/define/s/#define.*/#define $guard\n/"
  35. sed -i $file -e "$ s/#endif.*/\n#endif \/\/ $guard/"
  36. # Removes redundant \n added before, if they weren't needed.
  37. sed -i $file -e "/^$/N;/^\n$/D"
  38. done
  39. diff=$(git diff --color)
  40. # If no diff has been generated all is OK, clean up, and exit.
  41. if [ -z "$diff" ] ; then
  42. printf "Files in this commit comply with the header guards formatting rules.\n"
  43. exit 0
  44. fi
  45. # A diff has been created, notify the user, clean up, and exit.
  46. printf "\n*** The following differences were found between the code "
  47. printf "and the header guards formatting rules:\n\n"
  48. echo "$diff"
  49. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  50. exit 1