aboutsummaryrefslogtreecommitdiff
path: root/scripts/generate_BUFSIZ.sh
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-04-21 23:52:35 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-04-21 23:52:35 +0200
commitd7b502c05911e1cf1d4fc31be71f3abccd0927a5 (patch)
tree29cf9754793c0bc4e9340af7b1f49a037621d7a3 /scripts/generate_BUFSIZ.sh
parent93e1aaa1c7e5ed6d2704262700ec28837bdfc9b7 (diff)
downloadbusybox-w32-d7b502c05911e1cf1d4fc31be71f3abccd0927a5.tar.gz
busybox-w32-d7b502c05911e1cf1d4fc31be71f3abccd0927a5.tar.bz2
busybox-w32-d7b502c05911e1cf1d4fc31be71f3abccd0927a5.zip
build system: fix generate_BUFSIZ.sh to not alternate 1k and malloc builds
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'scripts/generate_BUFSIZ.sh')
-rwxr-xr-xscripts/generate_BUFSIZ.sh180
1 files changed, 108 insertions, 72 deletions
diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh
index 1914fa0f5..8aa0174a6 100755
--- a/scripts/generate_BUFSIZ.sh
+++ b/scripts/generate_BUFSIZ.sh
@@ -7,11 +7,16 @@
7 7
8debug=false 8debug=false
9 9
10postcompile=false
11test x"$1" = x"--post" && { postcompile=true; shift; }
12
10common_bufsiz_h=$1 13common_bufsiz_h=$1
11 14
12test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" 15test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
13test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" 16test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
14 17
18exitcmd="exit 0"
19
15regenerate() { 20regenerate() {
16 cat >"$1.$$" 21 cat >"$1.$$"
17 test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return 22 test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
@@ -19,99 +24,130 @@ regenerate() {
19} 24}
20 25
21generate_std_and_exit() { 26generate_std_and_exit() {
22 $debug && echo "Default: bb_common_bufsiz1[] in bss" 27 $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
23 { 28 {
24 echo "enum { COMMON_BUFSIZE = 1024 };" 29 echo "enum { COMMON_BUFSIZE = 1024 };"
25 echo "extern char bb_common_bufsiz1[];" 30 echo "extern char bb_common_bufsiz1[];"
26 echo "#define setup_common_bufsiz() ((void)0)" 31 echo "#define setup_common_bufsiz() ((void)0)"
27 } | regenerate "$common_bufsiz_h" 32 } | regenerate "$common_bufsiz_h"
28 exit 0 33 echo "std" >"$common_bufsiz_h.method"
34 $exitcmd
29} 35}
30 36
31# User does not want any funky stuff? 37generate_big_and_exit() {
32test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit 38 $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1"
33
34test -f busybox_unstripped || {
35 # We did not try anything yet
36 $debug && echo "Will try to fit bb_common_bufsiz1[] into _end[]"
37 { 39 {
38 echo "enum { COMMON_BUFSIZE = 1024 };" 40 echo "enum { COMMON_BUFSIZE = $1 };"
39 echo "extern char _end[]; /* linker-provided label */" 41 echo "extern char _end[]; /* linker-provided label */"
40 echo "#define bb_common_bufsiz1 _end" 42 echo "#define bb_common_bufsiz1 _end"
41 echo "#define setup_common_bufsiz() ((void)0)" 43 echo "#define setup_common_bufsiz() ((void)0)"
42 } | regenerate "$common_bufsiz_h" 44 } | regenerate "$common_bufsiz_h"
43 echo 1024 >"$common_bufsiz_h.BUFSIZE" 45 echo "$2" >"$common_bufsiz_h.method"
44 exit 0 46 $exitcmd
45} 47}
46 48
47# Get _end address 49generate_1k_and_exit() {
48END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` 50 generate_big_and_exit 1024 "1k"
49test x"$END" = x"" && generate_std_and_exit 51}
50$debug && echo "END:0x$END $((0x$END))" 52
51END=$((0x$END)) 53
52 54generate_malloc_and_exit() {
53# Get PAGE_SIZE 55 $debug && echo "Configuring: bb_common_bufsiz1[] is malloced"
54echo "\
55#include <sys/user.h>
56#if defined(PAGE_SIZE) && PAGE_SIZE > 0
57char page_size[PAGE_SIZE];
58#else
59char page_size[1];
60#endif
61" >page_size_$$.c
62$CC -c "page_size_$$.c" || generate_std_and_exit
63PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1`
64rm "page_size_$$.c" "page_size_$$.o"
65test x"$PAGE_SIZE" = x"" && generate_std_and_exit
66$debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))"
67PAGE_SIZE=$((0x$PAGE_SIZE))
68test $PAGE_SIZE -lt 1024 && generate_std_and_exit
69
70# How much space between _end[] and next page?
71PAGE_MASK=$((PAGE_SIZE-1))
72REM=$(( (-END) & PAGE_MASK ))
73$debug && echo "REM:$REM"
74
75if test $REM -lt 1024; then
76 # _end[] has no enough space for bb_common_bufsiz1[],
77 # users will need to malloc it.
78 { 56 {
79 echo "enum { COMMON_BUFSIZE = 1024 };" 57 echo "enum { COMMON_BUFSIZE = 1024 };"
80 echo "extern char *const bb_common_bufsiz1;" 58 echo "extern char *const bb_common_bufsiz1;"
81 echo "void setup_common_bufsiz(void);" 59 echo "void setup_common_bufsiz(void);"
82 } | regenerate "$common_bufsiz_h" 60 } | regenerate "$common_bufsiz_h"
83 # Check that we aren't left with a buggy binary: 61 echo "malloc" >"$common_bufsiz_h.method"
84 if test -f "$common_bufsiz_h.BUFSIZE"; then 62 $exitcmd
85 rm "$common_bufsiz_h.BUFSIZE" 63}
86 echo "Warning! Space in _end[] is too small ($REM bytes)!" 64
87 echo "Rerun make to build a binary which doesn't use it!" 65# User does not want any funky stuff?
88 exit 1 66test 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
73if $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
89 fi 122 fi
90 echo "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced"
91 exit 0
92fi 123fi
93 124
94# _end[] has REM bytes for bb_common_bufsiz1[] 125# Based on past success/fail of 1k build, decide next build type
95OLD=1024
96test -f "$common_bufsiz_h.BUFSIZE" && OLD=`cat "$common_bufsiz_h.BUFSIZE"`
97$debug && echo "OLD:$OLD"
98{
99echo "enum { COMMON_BUFSIZE = $REM };"
100echo "extern char _end[]; /* linker-provided label */"
101echo "#define bb_common_bufsiz1 _end"
102echo "#define setup_common_bufsiz() ((void)0)"
103} | regenerate "$common_bufsiz_h"
104echo $REM >"$common_bufsiz_h.BUFSIZE"
105
106# Check that code did not grow too much and thus _end[] did not shrink:
107if test $OLD -gt $REM; then
108 echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!"
109 echo "Rerun make!"
110 exit 1
111fi
112 126
113if test $OLD != $REM; then 127if test -f "$common_bufsiz_h.1k.OK"; then
114 echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." 128 # Previous build succeeded fitting 1k into _end[].
115else 129 # Try bigger COMMON_BUFSIZE if possible.
116 echo "COMMON_BUFSIZE = $REM bytes" 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"
136fi
137if 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
117fi 151fi
152# There was no 1k build yet. Try it.
153generate_1k_and_exit