gen_build_files.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh
  2. # Note: was using sed OPTS CMD -- FILES
  3. # but users complain that many sed implementations
  4. # are misinterpreting --.
  5. test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  6. # cd to objtree
  7. cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  8. # In separate objtree build, include/ might not exist yet
  9. mkdir include 2>/dev/null
  10. srctree="$1"
  11. status() { printf ' %-8s%s\n' "$1" "$2"; }
  12. gen() { status "GEN" "$@"; }
  13. chk() { status "CHK" "$@"; }
  14. generate()
  15. {
  16. # NB: data to be inserted at INSERT line is coming on stdin
  17. local src="$1" dst="$2" header="$3"
  18. #chk "${dst}"
  19. {
  20. # Need to use printf: different shells have inconsistent
  21. # rules re handling of "\n" in echo params.
  22. printf "%s\n" "${header}"
  23. # print everything up to INSERT line
  24. sed -n '/^INSERT$/ q; p' "${src}"
  25. # copy stdin to stdout
  26. cat
  27. # print everything after INSERT line
  28. sed -n '/^INSERT$/ {
  29. :l
  30. n
  31. p
  32. bl
  33. }' "${src}"
  34. } >"${dst}.tmp"
  35. if ! cmp -s "${dst}" "${dst}.tmp"; then
  36. gen "${dst}"
  37. mv "${dst}.tmp" "${dst}"
  38. else
  39. rm -f "${dst}.tmp"
  40. fi
  41. }
  42. # (Re)generate include/applets.h
  43. sed -n 's@^//applet:@@p' "$srctree"/*/*.c "$srctree"/*/*/*.c \
  44. | generate \
  45. "$srctree/include/applets.src.h" \
  46. "include/applets.h" \
  47. "/* DO NOT EDIT. This file is generated from applets.src.h */"
  48. # (Re)generate include/usage.h
  49. # We add line continuation backslash after each line,
  50. # and insert empty line before each line which doesn't start
  51. # with space or tab
  52. TAB="$(printf '\tX')"
  53. TAB="${TAB%X}"
  54. LF="$(printf '\nX')"
  55. LF="${LF%X}"
  56. sed -n -e 's@^//usage:\([ '"$TAB"'].*\)$@\1 \\@p' \
  57. -e 's@^//usage:\([^ '"$TAB"'].*\)$@\'"$LF"'\1 \\@p' \
  58. "$srctree"/*/*.c "$srctree"/*/*/*.c \
  59. | generate \
  60. "$srctree/include/usage.src.h" \
  61. "include/usage.h" \
  62. "/* DO NOT EDIT. This file is generated from usage.src.h */"
  63. # (Re)generate */Kbuild and */Config.in
  64. # We skip .dotdirs - makes git/svn/etc users happier
  65. { cd -- "$srctree" && find . -type d ! '(' -name '.?*' -prune ')'; } \
  66. | while read -r d; do
  67. d="${d#./}"
  68. src="$srctree/$d/Kbuild.src"
  69. dst="$d/Kbuild"
  70. if test -f "$src"; then
  71. mkdir -p -- "$d" 2>/dev/null
  72. sed -n 's@^//kbuild:@@p' "$srctree/$d"/*.c \
  73. | generate \
  74. "${src}" "${dst}" \
  75. "# DO NOT EDIT. This file is generated from Kbuild.src"
  76. fi
  77. src="$srctree/$d/Config.src"
  78. dst="$d/Config.in"
  79. if test -f "$src"; then
  80. mkdir -p -- "$d" 2>/dev/null
  81. sed -n 's@^//config:@@p' "$srctree/$d"/*.c \
  82. | generate \
  83. "${src}" "${dst}" \
  84. "# DO NOT EDIT. This file is generated from Config.src"
  85. fi
  86. done
  87. # Last read failed. This is normal. Don't exit with its error code:
  88. exit 0