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 |
| 9 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 10 | postcompile=false |
| 11 | test x"$1" = x"--post" && { postcompile=true; shift; } |
| 12 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 13 | common_bufsiz_h=$1 |
| 14 | |
| 15 | test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" |
| 16 | test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" |
| 17 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 18 | exitcmd="exit 0" |
| 19 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 20 | regenerate() { |
| 21 | cat >"$1.$$" |
| 22 | test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return |
| 23 | mv "$1.$$" "$1" |
| 24 | } |
| 25 | |
| 26 | generate_std_and_exit() { |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 27 | $debug && echo "Configuring: bb_common_bufsiz1[] in bss" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 28 | { |
| 29 | echo "enum { COMMON_BUFSIZE = 1024 };" |
| 30 | echo "extern char bb_common_bufsiz1[];" |
| 31 | echo "#define setup_common_bufsiz() ((void)0)" |
| 32 | } | regenerate "$common_bufsiz_h" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 33 | echo "std" >"$common_bufsiz_h.method" |
| 34 | $exitcmd |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 37 | generate_big_and_exit() { |
| 38 | $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 39 | { |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 40 | echo "enum { COMMON_BUFSIZE = $1 };" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 41 | echo "extern char _end[]; /* linker-provided label */" |
| 42 | echo "#define bb_common_bufsiz1 _end" |
| 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 | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 53 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 54 | generate_malloc_and_exit() { |
| 55 | $debug && echo "Configuring: bb_common_bufsiz1[] is malloced" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 56 | { |
| 57 | echo "enum { COMMON_BUFSIZE = 1024 };" |
Denys Vlasenko | 93e1aaa | 2016-04-21 21:47:45 +0200 | [diff] [blame] | 58 | echo "extern char *const bb_common_bufsiz1;" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 59 | echo "void setup_common_bufsiz(void);" |
| 60 | } | regenerate "$common_bufsiz_h" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 61 | echo "malloc" >"$common_bufsiz_h.method" |
| 62 | $exitcmd |
| 63 | } |
| 64 | |
| 65 | # User does not want any funky stuff? |
| 66 | test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit |
| 67 | |
| 68 | # The script is run two times: before compilation, when it needs to |
| 69 | # (re)generate $common_bufsiz_h, and directly after successful build, |
| 70 | # when it needs to assess whether the build is ok to use at all (not buggy), |
| 71 | # and (re)generate $common_bufsiz_h for a future build. |
| 72 | |
| 73 | if $postcompile; then |
| 74 | # Postcompile needs to create/delete OK/FAIL files |
| 75 | |
| 76 | test -f busybox_unstripped || exit 1 |
| 77 | test -f "$common_bufsiz_h.method" || exit 1 |
| 78 | |
| 79 | # How the build was done? |
| 80 | method=`cat -- "$common_bufsiz_h.method"` |
| 81 | |
| 82 | # Get _end address |
| 83 | END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` |
| 84 | test x"$END" = x"" && generate_std_and_exit |
| 85 | $debug && echo "END:0x$END $((0x$END))" |
| 86 | END=$((0x$END)) |
| 87 | |
| 88 | # Get PAGE_SIZE |
| 89 | { |
| 90 | echo "#include <sys/user.h>" |
| 91 | echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0" |
| 92 | echo "char page_size[PAGE_SIZE];" |
| 93 | echo "#endif" |
| 94 | } >page_size_$$.c |
| 95 | $CC -c "page_size_$$.c" || exit 1 |
| 96 | PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` |
| 97 | rm "page_size_$$.c" "page_size_$$.o" |
| 98 | test x"$PAGE_SIZE" = x"" && exit 1 |
| 99 | $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" |
| 100 | PAGE_SIZE=$((0x$PAGE_SIZE)) |
| 101 | test $PAGE_SIZE -lt 512 && exit 1 |
| 102 | |
| 103 | # How much space between _end[] and next page? |
| 104 | PAGE_MASK=$((PAGE_SIZE-1)) |
| 105 | COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) |
| 106 | echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" |
| 107 | |
| 108 | if test x"$method" = x"1k"; then |
| 109 | if test $COMMON_BUFSIZE -lt 1024; then |
| 110 | # _end[] has no enough space for bb_common_bufsiz1[] |
| 111 | rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null |
| 112 | { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" |
| 113 | echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" |
| 114 | echo "Rerun make to build a binary which doesn't use it!" |
| 115 | rm busybox_unstripped |
| 116 | exitcmd="exit 1" |
| 117 | else |
| 118 | rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null |
| 119 | echo $COMMON_BUFSIZE >"$common_bufsiz_h.1k.OK" |
| 120 | test $COMMON_BUFSIZE -gt $((1024+32)) && echo "Rerun make to use larger COMMON_BUFSIZE" |
| 121 | fi |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 122 | fi |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 123 | fi |
| 124 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 125 | # Based on past success/fail of 1k build, decide next build type |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 126 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 127 | if test -f "$common_bufsiz_h.1k.OK"; then |
| 128 | # Previous build succeeded fitting 1k into _end[]. |
| 129 | # Try bigger COMMON_BUFSIZE if possible. |
| 130 | COMMON_BUFSIZE=`cat -- "$common_bufsiz_h.1k.OK"` |
| 131 | # Round down a bit |
| 132 | COMMON_BUFSIZE=$(( (COMMON_BUFSIZE-32) & 0xfffffe0 )) |
| 133 | COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) |
| 134 | test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit |
| 135 | generate_big_and_exit $COMMON_BUFSIZE "big" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 136 | fi |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 137 | if test -f "$common_bufsiz_h.1k.FAIL"; then |
| 138 | # Previous build FAILED to fit 1k into _end[]. |
| 139 | # Was it with same .config? |
| 140 | oldcfg=`cat -- "$common_bufsiz_h.1k.FAIL"` |
| 141 | curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config` |
| 142 | # If yes, then build a "malloced" version |
| 143 | if test x"$oldcfg" = x"$curcfg"; then |
| 144 | echo "Will not try 1k build, it failed before. Touch .config to override" |
| 145 | generate_malloc_and_exit |
| 146 | fi |
| 147 | # else: try 1k version |
| 148 | echo "New .config, will try 1k build" |
| 149 | rm -- "$common_bufsiz_h.1k.FAIL" |
| 150 | generate_1k_and_exit |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 151 | fi |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame^] | 152 | # There was no 1k build yet. Try it. |
| 153 | generate_1k_and_exit |