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