2 # Called from top-level directory a-la
4 # scripts/generate_BUFSIZ.sh include/common_bufsiz.h
12 test x"$1" = x"--post" && { postcompile=true; shift; }
16 test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
17 test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
23 test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
27 generate_std_and_exit() {
28 $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
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"
34 echo "std" >"$common_bufsiz_h.method"
38 generate_big_and_exit() {
39 $debug && echo "Configuring: bb_common_bufsiz1[] in bss, COMMON_BUFSIZE = $1"
41 echo "enum { COMMON_BUFSIZE = $1 };"
42 echo "extern char bb_common_bufsiz1[];"
43 echo "#define setup_common_bufsiz() ((void)0)"
44 } | regenerate "$common_bufsiz_h"
45 echo "$2" >"$common_bufsiz_h.method"
49 generate_1k_and_exit() {
50 generate_big_and_exit 1024 "1k"
53 round_down_COMMON_BUFSIZE() {
55 test "$1" -le 32 && return
56 COMMON_BUFSIZE=$(( ($1-32) & 0x0ffffff0 ))
57 COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE ))
60 # User does not want any funky stuff?
61 test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit
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.
69 # Postcompile needs to create/delete OK/FAIL files
71 test -f busybox_unstripped || exit 1
72 test -f "$common_bufsiz_h.method" || exit 1
74 # How the build was done?
75 method=`cat -- "$common_bufsiz_h.method"`
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))"
85 echo "#include <sys/user.h>"
86 echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0"
87 echo "char page_size[PAGE_SIZE];"
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))
96 test $PAGE_SIZE -lt 1024 && exit 1
98 # How much space between _end[] and next page?
99 PAGE_MASK=$((PAGE_SIZE-1))
100 TAIL_SIZE=$(( (-END) & PAGE_MASK ))
101 $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes"
103 if test x"$method" = x"1k"; then
106 md5sum <.config | cut -d' ' -f1
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"
118 # Based on past success/fail of 1k build, decide next build type
120 if test -f "$common_bufsiz_h.1k.OK"; then
121 # previous 1k build succeeded
122 oldcfg=`tail -n2 -- "$common_bufsiz_h.1k.OK"`
123 curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config`
124 # config did not change
125 if test x"$oldcfg" = x"$curcfg"; then
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"
133 rm -rf -- "$common_bufsiz_h.1k.OK"
136 # There was no 1k build yet. Try it.