blob: 8aa0174a6bf0d6e8c5a82ce3f0f838394154c180 [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
9
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020010postcompile=false
11test x"$1" = x"--post" && { postcompile=true; shift; }
12
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020013common_bufsiz_h=$1
14
15test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm"
16test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc"
17
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020018exitcmd="exit 0"
19
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020020regenerate() {
21 cat >"$1.$$"
22 test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return
23 mv "$1.$$" "$1"
24}
25
26generate_std_and_exit() {
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020027 $debug && echo "Configuring: bb_common_bufsiz1[] in bss"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020028 {
29 echo "enum { COMMON_BUFSIZE = 1024 };"
30 echo "extern char bb_common_bufsiz1[];"
31 echo "#define setup_common_bufsiz() ((void)0)"
32 } | regenerate "$common_bufsiz_h"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020033 echo "std" >"$common_bufsiz_h.method"
34 $exitcmd
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020035}
36
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020037generate_big_and_exit() {
38 $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020039 {
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020040 echo "enum { COMMON_BUFSIZE = $1 };"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020041 echo "extern char _end[]; /* linker-provided label */"
42 echo "#define bb_common_bufsiz1 _end"
43 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 Vlasenkoe6a2f4c2016-04-21 16:26:30 +020053
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020054generate_malloc_and_exit() {
55 $debug && echo "Configuring: bb_common_bufsiz1[] is malloced"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020056 {
57 echo "enum { COMMON_BUFSIZE = 1024 };"
Denys Vlasenko93e1aaa2016-04-21 21:47:45 +020058 echo "extern char *const bb_common_bufsiz1;"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020059 echo "void setup_common_bufsiz(void);"
60 } | regenerate "$common_bufsiz_h"
Denys Vlasenkod7b502c2016-04-21 23:52:35 +020061 echo "malloc" >"$common_bufsiz_h.method"
62 $exitcmd
63}
64
65# User does not want any funky stuff?
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
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200122 fi
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200123fi
124
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200125# Based on past success/fail of 1k build, decide next build type
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200126
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200127if test -f "$common_bufsiz_h.1k.OK"; then
128 # Previous build succeeded fitting 1k into _end[].
129 # Try bigger COMMON_BUFSIZE if possible.
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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200136fi
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200137if 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
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200151fi
Denys Vlasenkod7b502c2016-04-21 23:52:35 +0200152# There was no 1k build yet. Try it.
153generate_1k_and_exit