0829121abac926b93c88b2d7b7a1de64d84b1db5
[oweals/busybox.git] / scripts / trylink
1 #!/bin/sh
2
3 debug=false
4
5 try() {
6     printf "%s\n" "Output of:" >$EXE.out
7     printf "%s\n" "$*" >>$EXE.out
8     printf "%s\n" "==========" >>$EXE.out
9     $debug && echo "Trying: $*"
10     "$@" >>$EXE.out 2>&1
11     exitcode=$?
12     return $exitcode
13 }
14
15 EXE="$1"
16 CC="$2"
17 LDFLAGS="$3"
18 O_FILES="$4"
19 A_FILES="$5"
20 LDLIBS="$6"
21
22 # Sanitize lib list (dups, extra spaces etc)
23 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
24
25 # First link with all libs. If it fails, bail out
26 echo "Trying libraries: $LDLIBS"
27 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
28 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
29 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
30 try $CC $LDFLAGS \
31         -o $EXE -Wl,-Map -Wl,$EXE.map \
32         -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
33         -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
34         $l_list \
35 || {
36     echo "Failed: $* $l_list"
37     cat $EXE.out
38     exit 1
39 }
40
41 # Now try to remove each lib and build without it.
42 # Stop when no lib can be removed.
43 while test "$LDLIBS"; do
44     $debug && echo "Trying libraries: $LDLIBS"
45     all_needed=true
46     for one in $LDLIBS; do
47         without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
48         # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
49         l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
50         test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
51         $debug && echo "Trying -l options: '$l_list'"
52         try $CC $LDFLAGS \
53                 -o $EXE -Wl,-Map -Wl,$EXE.map \
54                 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
55                 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
56                 $l_list
57         if test $? = 0; then
58             echo "Library $one is not needed"
59             LDLIBS="$without_one"
60             all_needed=false
61         else
62             echo "Library $one is needed"
63         fi
64     done
65     # All libs were needed, can't remove any
66     $all_needed && break
67     # If there is no space char, the list has just one lib.
68     # I'm not sure that in this case lib really is 100% needed.
69     # Let's try linking without it anyway... thus commented out.
70     #{ echo "$LDLIBS" | grep -q ' '; } || break
71 done
72
73 # Make the binary with final, minimal list of libs
74 echo "Final link with: $LDLIBS"
75 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
76 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
77 # --verbose gives us gobs of info to stdout (e.g. linker script used)
78 if ! test -f busybox_ldscript; then
79     try $CC $LDFLAGS \
80             -o $EXE -Wl,-Map -Wl,$EXE.map \
81             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
82             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
83             $l_list -Wl,--verbose \
84     || {
85         cat $EXE.out
86         exit 1
87     }
88 else
89     echo "Custom linker script 'busybox_ldscript' found, using it"
90     # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
91     #  .rodata         : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
92     #  *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
93     #  *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
94     # This will eliminate most of the padding (~3kb).
95     try $CC $LDFLAGS \
96             -o $EXE -Wl,-Map -Wl,$EXE.map \
97             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
98             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
99             $l_list -Wl,--verbose \
100             -Wl,-T -Wl,busybox_ldscript \
101     || {
102         cat $EXE.out
103         exit 1
104     }
105 fi
106
107 . .config
108
109 sharedlib_dir="0_lib"
110
111 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
112     mkdir "$sharedlib_dir" 2>/dev/null
113     test -d "$sharedlib_dir" || {
114         echo "Cannot make directory $sharedlib_dir"
115         exit 1
116     }
117     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
118
119     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
120     try $CC $LDFLAGS \
121         -o $EXE -Wl,-Map -Wl,$EXE.map \
122         -shared -fPIC -Wl,--enable-new-dtags \
123         -Wl,--start-group -Wl,--whole-archive $A_FILES -Wl,--no-whole-archive -Wl,--end-group \
124         $l_list -Wl,--verbose \
125         -Wl,-soname="libbusybox.so.$BB_VER" \
126         -Wl,-z,combreloc \
127     || {
128         echo "Linking $EXE failed"
129         cat $EXE.out
130         exit 1
131     }
132     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
133     chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
134     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
135 fi
136
137 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
138     EXE="$sharedlib_dir/busybox_unstripped"
139     try $CC $LDFLAGS \
140         -o $EXE -Wl,-Map -Wl,$EXE.map \
141         -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
142         -Wl,--start-group $O_FILES -Wl,--end-group \
143         -Wl,--verbose \
144         -L"$sharedlib_dir" -lbusybox \
145     || {
146         echo "Linking $EXE failed"
147         cat $EXE.out
148         exit 1
149     }
150     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
151     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
152 fi
153
154 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
155     echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
156     gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
157     | grep -v "^#" \
158     | grep -v "^$" \
159     > applet_lst.tmp
160     while read name main junk; do
161
162         echo "\
163 void bbox_prepare_main(char **argv);
164 int $main(int argc, char **argv);
165
166 const char *applet_name = \"$name\";
167
168 int main(int argc, char **argv)
169 {
170         bbox_prepare_main(argv);
171         return $main(argc, argv);
172 }
173 " >"$sharedlib_dir/applet.c"
174
175         EXE="$sharedlib_dir/$name"
176         try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
177             -o $EXE \
178             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
179             -L"$sharedlib_dir" -lbusybox \
180         || {
181             echo "Linking $EXE failed"
182             cat $EXE.out
183             exit 1
184         }
185         rm -- "$sharedlib_dir/applet.c" $EXE.out
186         strip -s --remove-section=.note --remove-section=.comment $EXE
187
188     done <applet_lst.tmp
189 fi
190
191 # libbusybox.so is needed only for -lbusybox at link time,
192 # it is not needed at runtime. Deleting to reduce confusion.
193 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
194 exit 0 # or else we may confuse make