build system: don't use -o /dev/null, old gcc can delete /dev/null!
[oweals/busybox.git] / scripts / trylink
1 #!/bin/sh
2
3 debug=false
4
5 # Linker flags used:
6 #
7 # Informational:
8 # --warn-common
9 # -Map $EXE.map
10 # --verbose
11 #
12 # Optimizations:
13 # --sort-common                 reduces padding
14 # --sort-section alignment      reduces padding
15 # --gc-sections                 throws out unused sections,
16 #                               does not work for shared libs
17 # -On                           Not used, maybe useful?
18 #
19 # List of files to link:
20 # $l_list                       == --start-group -llib1 -llib2 --end-group
21 # --start-group $O_FILES $A_FILES --end-group
22 #
23 # Shared library link:
24 # -shared                       self-explanatory
25 # -fPIC                         position-independent code
26 # --enable-new-dtags            ?
27 # -z,combreloc                  ?
28 # -soname="libbusybox.so.$BB_VER"
29 # --undefined=lbb_main          Seed name to start pulling from
30 #                               (otherwise we'll need --whole-archive)
31 # -static                       Not used, but may be useful! manpage:
32 #                               "... This option can be used with -shared.
33 #                               Doing so means that a shared library
34 #                               is being created but that all of the library's
35 #                               external references must be resolved by pulling
36 #                               in entries from static libraries."
37
38
39 try() {
40     printf "%s\n" "Output of:" >$EXE.out
41     printf "%s\n" "$*" >>$EXE.out
42     printf "%s\n" "==========" >>$EXE.out
43     $debug && echo "Trying: $*"
44     "$@" >>$EXE.out 2>&1
45     exitcode=$?
46     return $exitcode
47 }
48
49 check_cc() {
50     local tempname="/tmp/temp.$$.$RANDOM"
51     # Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :(
52     # "-xc": C language. "/dev/null" is an empty source file.
53     if $CC $1 -shared -xc /dev/null -o "$tempname".o >/dev/null 2>&1; then
54         echo "$1";
55     else
56         echo "$2";
57     fi
58     rm "$tempname".o 2>/dev/null
59 }
60
61 check_libc_is_glibc() {
62     local tempname="/tmp/temp.$$.$RANDOM"
63     echo "\
64         #include <stdlib.h>
65         /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
66         #if defined(__GLIBC__) && !defined(__UCLIBC__)
67         syntax error here
68         #endif
69         " >"$tempname"
70     if $CC "$tempname".c -c -o "$tempname".o >/dev/null 2>&1; then
71         echo "$2";
72     else
73         echo "$1";
74     fi
75     rm "$tempname".c "$tempname".o 2>/dev/null
76 }
77
78 EXE="$1"
79 CC="$2"
80 LDFLAGS="$3"
81 O_FILES="$4"
82 A_FILES="$5"
83 LDLIBS="$6"
84
85 # The -Wl,--sort-section option is not supported by older versions of ld
86 SORT_SECTION=`check_cc "-Wl,--sort-section -Wl,alignment" ""`
87
88 # Static linking against glibc produces buggy executables
89 # (glibc does not cope well with ld --gc-sections).
90 # See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
91 # Note that glibc is unsuitable for static linking anyway.
92 # We are removing -Wl,--gc-sections from link command line.
93 GC_SECTION=`(
94 . ./.config
95 if test x"$CONFIG_STATIC" = x"y"; then
96     check_libc_is_glibc "" "-Wl,--gc-sections"
97 else
98     echo "-Wl,--gc-sections"
99 fi
100 )`
101
102 # Sanitize lib list (dups, extra spaces etc)
103 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
104
105 # First link with all libs. If it fails, bail out
106 echo "Trying libraries: $LDLIBS"
107 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
108 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
109 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
110 try $CC $LDFLAGS \
111         -o $EXE \
112         -Wl,--sort-common \
113         $SORT_SECTION \
114         $GC_SECTION \
115         -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
116         $l_list \
117 || {
118     echo "Failed: $* $l_list"
119     cat $EXE.out
120     exit 1
121 }
122
123 # Now try to remove each lib and build without it.
124 # Stop when no lib can be removed.
125 while test "$LDLIBS"; do
126     $debug && echo "Trying libraries: $LDLIBS"
127     all_needed=true
128     for one in $LDLIBS; do
129         without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
130         # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
131         l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
132         test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
133         $debug && echo "Trying -l options: '$l_list'"
134         try $CC $LDFLAGS \
135                 -o $EXE \
136                 -Wl,--sort-common \
137                 $SORT_SECTION \
138                 $GC_SECTION \
139                 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
140                 $l_list
141         if test $? = 0; then
142             echo " Library $one is not needed"
143             LDLIBS="$without_one"
144             all_needed=false
145         else
146             echo " Library $one is needed"
147         fi
148     done
149     # All libs were needed, can't remove any
150     $all_needed && break
151     # If there is no space char, the list has just one lib.
152     # I'm not sure that in this case lib really is 100% needed.
153     # Let's try linking without it anyway... thus commented out.
154     #{ echo "$LDLIBS" | grep -q ' '; } || break
155 done
156
157 # Make the binary with final, minimal list of libs
158 echo "Final link with: ${LDLIBS:-<none>}"
159 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
160 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
161 # --verbose gives us gobs of info to stdout (e.g. linker script used)
162 if ! test -f busybox_ldscript; then
163     try $CC $LDFLAGS \
164             -o $EXE \
165             -Wl,--sort-common \
166             $SORT_SECTION \
167             $GC_SECTION \
168             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
169             $l_list \
170             -Wl,--warn-common \
171             -Wl,-Map -Wl,$EXE.map \
172             -Wl,--verbose \
173     || {
174         cat $EXE.out
175         exit 1
176     }
177 else
178     echo "Custom linker script 'busybox_ldscript' found, using it"
179     # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
180     #  .rodata         : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
181     #  *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
182     #  *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
183     # This will eliminate most of the padding (~3kb).
184     # Hmm, "ld --sort-section alignment" should do it too.
185     try $CC $LDFLAGS \
186             -o $EXE \
187             -Wl,--sort-common \
188             $SORT_SECTION \
189             $GC_SECTION \
190             -Wl,-T -Wl,busybox_ldscript \
191             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
192             $l_list \
193             -Wl,--warn-common \
194             -Wl,-Map -Wl,$EXE.map \
195             -Wl,--verbose \
196     || {
197         cat $EXE.out
198         exit 1
199     }
200 fi
201
202 . ./.config
203
204 sharedlib_dir="0_lib"
205
206 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
207     mkdir "$sharedlib_dir" 2>/dev/null
208     test -d "$sharedlib_dir" || {
209         echo "Cannot make directory $sharedlib_dir"
210         exit 1
211     }
212     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
213
214     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
215     try $CC $LDFLAGS \
216             -o $EXE \
217             -shared -fPIC \
218             -Wl,--enable-new-dtags \
219             -Wl,-z,combreloc \
220             -Wl,-soname="libbusybox.so.$BB_VER" \
221             -Wl,--undefined=lbb_main \
222             -Wl,--sort-common \
223             $SORT_SECTION \
224             -Wl,--start-group $A_FILES -Wl,--end-group \
225             $l_list \
226             -Wl,--warn-common \
227             -Wl,-Map -Wl,$EXE.map \
228             -Wl,--verbose \
229     || {
230         echo "Linking $EXE failed"
231         cat $EXE.out
232         exit 1
233     }
234     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
235     chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
236     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
237 fi
238
239 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
240     EXE="$sharedlib_dir/busybox_unstripped"
241     try $CC $LDFLAGS \
242             -o $EXE \
243             -Wl,--sort-common \
244             $SORT_SECTION \
245             $GC_SECTION \
246             -Wl,--start-group $O_FILES -Wl,--end-group \
247             -L"$sharedlib_dir" -lbusybox \
248             -Wl,--warn-common \
249             -Wl,-Map -Wl,$EXE.map \
250             -Wl,--verbose \
251     || {
252         echo "Linking $EXE failed"
253         cat $EXE.out
254         exit 1
255     }
256     $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
257     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
258 fi
259
260 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
261     echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
262     gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
263     | grep -v "^#" \
264     | grep -v "^$" \
265     > applet_lst.tmp
266     while read name main junk; do
267
268         echo "\
269 void lbb_prepare(const char *applet, char **argv);
270 int $main(int argc, char **argv);
271
272 int main(int argc, char **argv)
273 {
274         lbb_prepare(\"$name\", argv);
275         return $main(argc, argv);
276 }
277 " >"$sharedlib_dir/applet.c"
278
279         EXE="$sharedlib_dir/$name"
280         try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
281                 -o $EXE \
282                 -Wl,--sort-common \
283                 $SORT_SECTION \
284                 $GC_SECTION \
285                 -L"$sharedlib_dir" -lbusybox \
286                 -Wl,--warn-common \
287         || {
288             echo "Linking $EXE failed"
289             cat $EXE.out
290             exit 1
291         }
292         rm -- "$sharedlib_dir/applet.c" $EXE.out
293         $STRIP -s --remove-section=.note --remove-section=.comment $EXE
294
295     done <applet_lst.tmp
296 fi
297
298 # libbusybox.so is needed only for -lbusybox at link time,
299 # it is not needed at runtime. Deleting to reduce confusion.
300 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
301 exit 0 # or else we may confuse make