summaryrefslogtreecommitdiff
path: root/scripts/generate_BUFSIZ.sh (plain)
blob: 718788e0b7af1b9ee94b12a1c851fbc039d93d78
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
8debug=false
9#debug=true
10
11postcompile=false
12test x"$1" = x"--post" && { postcompile=true; shift; }
13
14common_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
19exitcmd="exit 0"
20
21regenerate() {
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() {
28 $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
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"
34 echo "std" >"$common_bufsiz_h.method"
35 $exitcmd
36}
37
38generate_big_and_exit() {
39 $debug && echo "Configuring: bb_common_bufsiz1[] in bss, COMMON_BUFSIZE = $1"
40 {
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"
46 $exitcmd
47}
48
49generate_1k_and_exit() {
50 generate_big_and_exit 1024 "1k"
51}
52
53round_down_COMMON_BUFSIZE() {
54 COMMON_BUFSIZE=1024
55 test "$1" -le 32 && return
56 COMMON_BUFSIZE=$(( ($1-32) & 0x0ffffff0 ))
57 COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE ))
58}
59
60# User does not want any funky stuff?
61test 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
68if $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))
96 test $PAGE_SIZE -lt 1024 && exit 1
97
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"
102
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"
115 fi
116fi
117
118# Based on past success/fail of 1k build, decide next build type
119
120if 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"
131 fi
132 # config did change
133 rm -rf -- "$common_bufsiz_h.1k.OK"
134fi
135
136# There was no 1k build yet. Try it.
137generate_1k_and_exit
138