gen_build_files.sh 3.0 KB

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