blob: 844261906c2c8cc84d85390340c46be012c6190e [file] [log] [blame]
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +02001#!/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
8debug=false
Denys Vlasenko4c8fa342016-04-24 14:13:35 +02009#debug=true
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020010
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020011postcompile=false
12test x"$1" = x"--post" && { postcompile=true; shift; }
13
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020014common_bufsiz_h=$1
15
16test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
17test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
18
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020019exitcmd="exit 0"
20
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020021regenerate() {
22 cat >"$1.$$"
23 test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
24 mv "$1.$$" "$1"
25}
26
27generate_std_and_exit() {
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020028 $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020029 {
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 Vlasenkod7b502c2016-04-21 23:52:35 +020034 echo "std" >"$common_bufsiz_h.method"
35 $exitcmd
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020036}
37
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020038generate_big_and_exit() {
39 $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020040 {
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020041 echo "enum { COMMON_BUFSIZE = $1 };"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020042 echo "extern char _end[]; /* linker-provided label */"
43 echo "#define bb_common_bufsiz1 _end"
44 echo "#define setup_common_bufsiz() ((void)0)"
45 } | regenerate "$common_bufsiz_h"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020046 echo "$2" >"$common_bufsiz_h.method"
47 $exitcmd
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020048}
49
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020050generate_1k_and_exit() {
51 generate_big_and_exit 1024 "1k"
52}
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020053
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020054
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020055generate_malloc_and_exit() {
56 $debug && echo "Configuring: bb_common_bufsiz1[] is malloced"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020057 {
58 echo "enum { COMMON_BUFSIZE = 1024 };"
Denys Vlasenko93e1aaa2016-04-21 21:47:45 +020059 echo "extern char *const bb_common_bufsiz1;"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020060 echo "void setup_common_bufsiz(void);"
61 } | regenerate "$common_bufsiz_h"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020062 echo "malloc" >"$common_bufsiz_h.method"
63 $exitcmd
64}
65
Denys Vlasenko4c8fa342016-04-24 14:13:35 +020066round_down_COMMON_BUFSIZE() {
67 COMMON_BUFSIZE=$(( ($1-32) & 0xfffffe0 ))
68 COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE ))
69}
70
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020071# User does not want any funky stuff?
72test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit
73
74# The script is run two times: before compilation, when it needs to
75# (re)generate $common_bufsiz_h, and directly after successful build,
76# when it needs to assess whether the build is ok to use at all (not buggy),
77# and (re)generate $common_bufsiz_h for a future build.
78
79if $postcompile; then
80 # Postcompile needs to create/delete OK/FAIL files
81
82 test -f busybox_unstripped || exit 1
83 test -f "$common_bufsiz_h.method" || exit 1
84
85 # How the build was done?
86 method=`cat -- "$common_bufsiz_h.method"`
87
88 # Get _end address
89 END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1`
90 test x"$END" = x"" && generate_std_and_exit
91 $debug && echo "END:0x$END $((0x$END))"
92 END=$((0x$END))
93
94 # Get PAGE_SIZE
95 {
96 echo "#include <sys/user.h>"
97 echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0"
98 echo "char page_size[PAGE_SIZE];"
99 echo "#endif"
100 } >page_size_$$.c
101 $CC -c "page_size_$$.c" || exit 1
102 PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1`
103 rm "page_size_$$.c" "page_size_$$.o"
104 test x"$PAGE_SIZE" = x"" && exit 1
105 $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))"
106 PAGE_SIZE=$((0x$PAGE_SIZE))
107 test $PAGE_SIZE -lt 512 && exit 1
108
109 # How much space between _end[] and next page?
110 PAGE_MASK=$((PAGE_SIZE-1))
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200111 TAIL_SIZE=$(( (-END) & PAGE_MASK ))
112 $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200113
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200114 if test x"$method" = x"1k" || test x"$method" = x"big"; then
115 if test $TAIL_SIZE -lt 1024; then
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200116 # _end[] has no enough space for bb_common_bufsiz1[]
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200117 echo "Warning! Space in _end[] is too small ($TAIL_SIZE bytes)!"
118 echo "Rerun make to build a binary which doesn't use it!"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200119 rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null
120 { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL"
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200121 rm busybox_unstripped busybox 2>/dev/null
122# Note: here we can do either a "malloc" or "std" build.
123# "malloc" gives a bit bigger code:
124# text bss filename
125# 804355 5385 busybox.std
126# 804618 4361 busybox.malloc
127# but may have a smaller .bss (not guaranteed!). Use "pmap -x" to verify.
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200128 exitcmd="exit 1"
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200129 generate_malloc_and_exit
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200130 else
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200131 PREV_SIZE=1024
132 test x"$method" = x"big" && PREV_SIZE=`cat -- "$common_bufsiz_h.1k.OK"`
133 round_down_COMMON_BUFSIZE $PREV_SIZE
134 PREV_BUFSIZE=$COMMON_BUFSIZE
135
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200136 rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200137 echo $TAIL_SIZE >"$common_bufsiz_h.1k.OK"
138 round_down_COMMON_BUFSIZE $TAIL_SIZE
139 # emit message only if COMMON_BUFSIZE is indeed larger
140 test $COMMON_BUFSIZE -gt $PREV_BUFSIZE \
141 && echo "Rerun make to use larger COMMON_BUFSIZE ($COMMON_BUFSIZE)"
142#TODO: test $PREV_BUFSIZE -lt $TAIL_SIZE && PANIC!!!
143#Code size with COMMON_BUFSIZE > 1024 may be bigger than code with COMMON_BUFSIZE = 1024!
144#(currently we just hope "-32 and round down to 32" saves us)
145
146 test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit
147 generate_big_and_exit $COMMON_BUFSIZE "big"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200148 fi
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200149 fi
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200150fi
151
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200152# Based on past success/fail of 1k build, decide next build type
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200153
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200154if test -f "$common_bufsiz_h.1k.OK"; then
155 # Previous build succeeded fitting 1k into _end[].
156 # Try bigger COMMON_BUFSIZE if possible.
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200157 TAIL_SIZE=`cat -- "$common_bufsiz_h.1k.OK"`
158 round_down_COMMON_BUFSIZE $TAIL_SIZE
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200159 test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit
160 generate_big_and_exit $COMMON_BUFSIZE "big"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200161fi
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200162
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200163if test -f "$common_bufsiz_h.1k.FAIL"; then
164 # Previous build FAILED to fit 1k into _end[].
165 # Was it with same .config?
166 oldcfg=`cat -- "$common_bufsiz_h.1k.FAIL"`
167 curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config`
168 # If yes, then build a "malloced" version
169 if test x"$oldcfg" = x"$curcfg"; then
170 echo "Will not try 1k build, it failed before. Touch .config to override"
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200171# Note: here we can do either a "malloc" or "std" build.
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200172 generate_malloc_and_exit
173 fi
174 # else: try 1k version
175 echo "New .config, will try 1k build"
176 rm -- "$common_bufsiz_h.1k.FAIL"
177 generate_1k_and_exit
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200178fi
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200179
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200180# There was no 1k build yet. Try it.
181generate_1k_and_exit