gen_build_files.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh
  2. test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  3. # cd to objtree
  4. cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  5. # In separate objtree build, include/ might not exist yet
  6. mkdir include 2>/dev/null
  7. srctree="$1"
  8. status() { printf ' %-8s%s\n' "$1" "$2"; }
  9. gen() { status "GEN" "$@"; }
  10. chk() { status "CHK" "$@"; }
  11. generate()
  12. {
  13. local src="$1" dst="$2" header="$3" insert="$4"
  14. #chk "${dst}"
  15. (
  16. # Need to use printf: different shells have inconsistent
  17. # rules re handling of "\n" in echo params,
  18. # and ${insert} definitely contains "\n".
  19. # Therefore, echo "${header}" would not work:
  20. printf "%s\n" "${header}"
  21. if grep -qs '^INSERT$' "${src}"; then
  22. sed -n '1,/^INSERT$/p' "${src}"
  23. printf "%s\n" "${insert}"
  24. sed -n '/^INSERT$/,$p' "${src}"
  25. else
  26. if [ -n "${insert}" ]; then
  27. printf "%s\n" "ERROR: INSERT line missing in: ${src}" 1>&2
  28. fi
  29. cat "${src}"
  30. fi
  31. ) | sed '/^INSERT$/d' > "${dst}.tmp"
  32. if ! cmp -s "${dst}" "${dst}.tmp"; then
  33. gen "${dst}"
  34. mv "${dst}.tmp" "${dst}"
  35. else
  36. rm -f "${dst}.tmp"
  37. fi
  38. }
  39. # (Re)generate include/applets.h
  40. s=`sed -n 's@^//applet:@@p' -- "$srctree"/*/*.c "$srctree"/*/*/*.c`
  41. generate \
  42. "$srctree/include/applets.src.h" \
  43. "include/applets.h" \
  44. "/* DO NOT EDIT. This file is generated from applets.src.h */" \
  45. "${s}"
  46. # (Re)generate include/usage.h
  47. # We add line continuation backslash after each line,
  48. # and insert empty line before each line which doesn't start
  49. # with space or tab
  50. # (note: we need to use \\\\ because of ``)
  51. s=`sed -n -e 's@^//usage:\([ \t].*\)$@\1 \\\\@p' -e 's@^//usage:\([^ \t].*\)$@\n\1 \\\\@p' -- "$srctree"/*/*.c "$srctree"/*/*/*.c`
  52. generate \
  53. "$srctree/include/usage.src.h" \
  54. "include/usage.h" \
  55. "/* DO NOT EDIT. This file is generated from usage.src.h */" \
  56. "${s}"
  57. # (Re)generate */Kbuild and */Config.in
  58. { cd -- "$srctree" && find . -type d; } | while read -r d; do
  59. d="${d#./}"
  60. src="$srctree/$d/Kbuild.src"
  61. dst="$d/Kbuild"
  62. if test -f "$src"; then
  63. mkdir -p -- "$d" 2>/dev/null
  64. s=`sed -n 's@^//kbuild:@@p' -- "$srctree/$d"/*.c`
  65. generate \
  66. "${src}" "${dst}" \
  67. "# DO NOT EDIT. This file is generated from Kbuild.src" \
  68. "${s}"
  69. fi
  70. src="$srctree/$d/Config.src"
  71. dst="$d/Config.in"
  72. if test -f "$src"; then
  73. mkdir -p -- "$d" 2>/dev/null
  74. s=`sed -n 's@^//config:@@p' -- "$srctree/$d"/*.c`
  75. generate \
  76. "${src}" "${dst}" \
  77. "# DO NOT EDIT. This file is generated from Config.src" \
  78. "${s}"
  79. fi
  80. done
  81. # Last read failed. This is normal. Don't exit with its error code:
  82. exit 0