3
0

gen_build_files.sh 3.0 KB

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