blob: 718788e0b7af1b9ee94b12a1c851fbc039d93d78 [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() {
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +020039 $debug && echo "Configuring: bb_common_bufsiz1[] in bss, 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 Vlasenko5b8c89d2016-06-29 15:00:52 +020042 echo "extern char bb_common_bufsiz1[];"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020043 echo "#define setup_common_bufsiz() ((void)0)"
44 } | regenerate "$common_bufsiz_h"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020045 echo "$2" >"$common_bufsiz_h.method"
46 $exitcmd
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020047}
48
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020049generate_1k_and_exit() {
50 generate_big_and_exit 1024 "1k"
51}
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020052
Denys Vlasenko4c8fa342016-04-24 14:13:35 +020053round_down_COMMON_BUFSIZE() {
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +020054 COMMON_BUFSIZE=1024
55 test "$1" -le 32 && return
56 COMMON_BUFSIZE=$(( ($1-32) & 0x0ffffff0 ))
Denys Vlasenko4c8fa342016-04-24 14:13:35 +020057 COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE ))
58}
59
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020060# 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))
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +020096 test $PAGE_SIZE -lt 1024 && exit 1
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020097
98 # How much space between _end[] and next page?
99 PAGE_MASK=$((PAGE_SIZE-1))
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200100 TAIL_SIZE=$(( (-END) & PAGE_MASK ))
101 $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200102
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +0200103 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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200115 fi
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200116fi
117
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200118# Based on past success/fail of 1k build, decide next build type
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200119
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200120if test -f "$common_bufsiz_h.1k.OK"; then
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +0200121 # previous 1k build succeeded
122 oldcfg=`tail -n2 -- "$common_bufsiz_h.1k.OK"`
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200123 curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config`
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +0200124 # config did not change
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200125 if test x"$oldcfg" = x"$curcfg"; then
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +0200126 # 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"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200131 fi
Denys Vlasenko5b8c89d2016-06-29 15:00:52 +0200132 # config did change
133 rm -rf -- "$common_bufsiz_h.1k.OK"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200134fi
Denys Vlasenko4c8fa342016-04-24 14:13:35 +0200135
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200136# There was no 1k build yet. Try it.
137generate_1k_and_exit