3
0

gen_build_files.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. local src="$1" dst="$2" header="$3" insert="$4"
  17. #chk "${dst}"
  18. (
  19. # Need to use printf: different shells have inconsistent
  20. # rules re handling of "\n" in echo params,
  21. # and ${insert} definitely contains "\n".
  22. # Therefore, echo "${header}" would not work:
  23. printf "%s\n" "${header}"
  24. if grep -qs '^INSERT$' "${src}"; then
  25. sed -n '1,/^INSERT$/p' "${src}"
  26. printf "%s\n" "${insert}"
  27. sed -n '/^INSERT$/,$p' "${src}"
  28. else
  29. if [ -n "${insert}" ]; then
  30. printf "%s\n" "ERROR: INSERT line missing in: ${src}" 1>&2
  31. fi
  32. cat "${src}"
  33. fi
  34. ) | sed '/^INSERT$/d' > "${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. s=`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. "${s}"
  49. # (Re)generate include/usage.h
  50. # We add line continuation backslash after each line,
  51. # and insert empty line before each line which doesn't start
  52. # with space or tab
  53. # (note: we need to use \\\\ because of ``)
  54. s=`sed -n -e 's@^//usage:\([ \t].*\)$@\1 \\\\@p' -e 's@^//usage:\([^ \t].*\)$@\n\1 \\\\@p' "$srctree"/*/*.c "$srctree"/*/*/*.c`
  55. generate \
  56. "$srctree/include/usage.src.h" \
  57. "include/usage.h" \
  58. "/* DO NOT EDIT. This file is generated from usage.src.h */" \
  59. "${s}"
  60. # (Re)generate */Kbuild and */Config.in
  61. { cd -- "$srctree" && find . -type d; } | while read -r d; do
  62. d="${d#./}"
  63. src="$srctree/$d/Kbuild.src"
  64. dst="$d/Kbuild"
  65. if test -f "$src"; then
  66. mkdir -p -- "$d" 2>/dev/null
  67. s=`sed -n 's@^//kbuild:@@p' "$srctree/$d"/*.c`
  68. generate \
  69. "${src}" "${dst}" \
  70. "# DO NOT EDIT. This file is generated from Kbuild.src" \
  71. "${s}"
  72. fi
  73. src="$srctree/$d/Config.src"
  74. dst="$d/Config.in"
  75. if test -f "$src"; then
  76. mkdir -p -- "$d" 2>/dev/null
  77. s=`sed -n 's@^//config:@@p' "$srctree/$d"/*.c`
  78. generate \
  79. "${src}" "${dst}" \
  80. "# DO NOT EDIT. This file is generated from Config.src" \
  81. "${s}"
  82. fi
  83. done
  84. # Last read failed. This is normal. Don't exit with its error code:
  85. exit 0