3
0

generate_BUFSIZ.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. # Called from top-level directory a-la
  3. #
  4. # scripts/generate_BUFSIZ.sh include/common_bufsiz.h
  5. . ./.config || exit 1
  6. debug=false
  7. #debug=true
  8. postcompile=false
  9. test x"$1" = x"--post" && { postcompile=true; shift; }
  10. common_bufsiz_h=$1
  11. test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
  12. test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
  13. exitcmd="exit 0"
  14. regenerate() {
  15. cat >"$1.$$"
  16. test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
  17. mv "$1.$$" "$1"
  18. }
  19. generate_std_and_exit() {
  20. $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
  21. {
  22. echo "enum { COMMON_BUFSIZE = 1024 };"
  23. echo "extern char bb_common_bufsiz1[];"
  24. echo "#define setup_common_bufsiz() ((void)0)"
  25. } | regenerate "$common_bufsiz_h"
  26. echo "std" >"$common_bufsiz_h.method"
  27. $exitcmd
  28. }
  29. generate_big_and_exit() {
  30. $debug && echo "Configuring: bb_common_bufsiz1[] in bss, COMMON_BUFSIZE = $1"
  31. {
  32. echo "enum { COMMON_BUFSIZE = $1 };"
  33. echo "extern char bb_common_bufsiz1[];"
  34. echo "#define setup_common_bufsiz() ((void)0)"
  35. } | regenerate "$common_bufsiz_h"
  36. echo "$2" >"$common_bufsiz_h.method"
  37. $exitcmd
  38. }
  39. generate_1k_and_exit() {
  40. generate_big_and_exit 1024 "1k"
  41. }
  42. round_down_COMMON_BUFSIZE() {
  43. COMMON_BUFSIZE=1024
  44. test "$1" -le 32 && return
  45. COMMON_BUFSIZE=$(( ($1-32) & 0x0ffffff0 ))
  46. COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE ))
  47. }
  48. # User does not want any funky stuff?
  49. test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit
  50. # The script is run two times: before compilation, when it needs to
  51. # (re)generate $common_bufsiz_h, and directly after successful build,
  52. # when it needs to assess whether the build is ok to use at all (not buggy),
  53. # and (re)generate $common_bufsiz_h for a future build.
  54. if $postcompile; then
  55. # Postcompile needs to create/delete OK/FAIL files
  56. test -f busybox_unstripped || exit 1
  57. test -f "$common_bufsiz_h.method" || exit 1
  58. # How the build was done?
  59. method=`cat -- "$common_bufsiz_h.method"`
  60. # Get _end address
  61. END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1`
  62. test x"$END" = x"" && generate_std_and_exit
  63. $debug && echo "END:0x$END $((0x$END))"
  64. END=$((0x$END))
  65. # Get PAGE_SIZE
  66. {
  67. echo "#include <sys/user.h>"
  68. echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0"
  69. echo "char page_size[PAGE_SIZE];"
  70. echo "#endif"
  71. } >page_size_$$.c
  72. $CC -c "page_size_$$.c" || exit 1
  73. PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1`
  74. rm "page_size_$$.c" "page_size_$$.o"
  75. test x"$PAGE_SIZE" = x"" && exit 1
  76. $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))"
  77. PAGE_SIZE=$((0x$PAGE_SIZE))
  78. test $PAGE_SIZE -lt 1024 && exit 1
  79. # How much space between _end[] and next page?
  80. PAGE_MASK=$((PAGE_SIZE-1))
  81. TAIL_SIZE=$(( (-END) & PAGE_MASK ))
  82. $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes"
  83. if test x"$method" = x"1k"; then
  84. {
  85. echo $TAIL_SIZE
  86. md5sum <.config | cut -d' ' -f1
  87. stat -c "%Y" .config
  88. } >"$common_bufsiz_h.1k.OK"
  89. round_down_COMMON_BUFSIZE $((1024 + TAIL_SIZE))
  90. # emit message only if COMMON_BUFSIZE is indeed larger
  91. test $COMMON_BUFSIZE -gt 1024 \
  92. && echo "Rerun make to use larger COMMON_BUFSIZE ($COMMON_BUFSIZE)"
  93. test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit
  94. generate_big_and_exit $COMMON_BUFSIZE "big"
  95. fi
  96. fi
  97. # Based on past success/fail of 1k build, decide next build type
  98. if test -f "$common_bufsiz_h.1k.OK"; then
  99. # previous 1k build succeeded
  100. oldcfg=`tail -n2 -- "$common_bufsiz_h.1k.OK"`
  101. curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config`
  102. # config did not change
  103. if test x"$oldcfg" = x"$curcfg"; then
  104. # Try bigger COMMON_BUFSIZE if possible
  105. TAIL_SIZE=`head -n1 -- "$common_bufsiz_h.1k.OK"`
  106. round_down_COMMON_BUFSIZE $((1024 + TAIL_SIZE))
  107. test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit
  108. generate_big_and_exit $COMMON_BUFSIZE "big"
  109. fi
  110. # config did change
  111. rm -rf -- "$common_bufsiz_h.1k.OK"
  112. fi
  113. # There was no 1k build yet. Try it.
  114. generate_1k_and_exit