trylink: trivial fixes
[platform/upstream/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                 >/dev/null
58         if test $? = 0; then
59             echo "Library $one is not needed"
60             LDLIBS="$without_one"
61             all_needed=false
62         else
63             echo "Library $one is needed"
64         fi
65     done
66     # All libs were needed, can't remove any
67     $all_needed && break
68     # If there is no space char, the list has just one lib.
69     # I'm not sure that in this case lib really is 100% needed.
70     # Let's try linking without it anyway... thus commented out.
71     #{ echo "$LDLIBS" | grep -q ' '; } || break
72 done
73
74 # Make the binary with final, minimal list of libs
75 echo "Final link with: $LDLIBS"
76 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
77 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
78 # --verbose gives us gobs of info to stdout (e.g. linker script used)
79 if ! test -f busybox_ldscript; then
80     try $CC $LDFLAGS \
81             -o $EXE -Wl,-Map -Wl,$EXE.map \
82             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
83             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
84             $l_list -Wl,--verbose \
85             >/dev/null
86 else
87     echo "Custom linker script 'busybox_ldscript' found, using it"
88     # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
89     #  .rodata         : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
90     #  *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
91     #  *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
92     # This will eliminate most of the padding (~3kb).
93     try $CC $LDFLAGS \
94             -o $EXE -Wl,-Map -Wl,$EXE.map \
95             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
96             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
97             $l_list -Wl,--verbose \
98             -Wl,-T -Wl,busybox_ldscript \
99             >/dev/null
100 fi
101
102 . .config
103
104 sharedlib_dir="0_lib"
105
106 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
107     mkdir "$sharedlib_dir" 2>/dev/null
108     test -d "$sharedlib_dir" || {
109         echo "Cannot make directory $sharedlib_dir"
110         exit 1
111     }
112     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
113
114     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
115     try $CC $LDFLAGS \
116         -o $EXE -Wl,-Map -Wl,$EXE.map \
117         -shared -fPIC -Wl,--enable-new-dtags \
118         -Wl,--start-group -Wl,--whole-archive $A_FILES -Wl,--no-whole-archive -Wl,--end-group \
119         $l_list -Wl,--verbose \
120         -Wl,-soname="libbusybox.so.$BB_VER" \
121         -Wl,-z,combreloc \
122     || {
123         echo "Linking $EXE failed"
124         cat $EXE.out
125         exit 1
126     }
127     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
128     chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
129     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
130 fi
131
132 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
133     EXE="$sharedlib_dir/busybox_unstripped"
134     try $CC $LDFLAGS \
135         -o $EXE -Wl,-Map -Wl,$EXE.map \
136         -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
137         -Wl,--start-group $O_FILES -Wl,--end-group \
138         -Wl,--verbose \
139         -L"$sharedlib_dir" -lbusybox \
140     || {
141         echo "Linking $EXE failed"
142         cat $EXE.out
143         exit 1
144     }
145     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
146     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
147 fi
148
149 if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
150     echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
151     gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
152     | grep -v "^#" \
153     | grep -v "^$" \
154     > applet_lst.tmp
155     while read name main junk; do
156
157         echo "\
158 void bbox_prepare_main(char **argv);
159 int $main(int argc, char **argv);
160
161 const char *applet_name = \"$name\";
162
163 int main(int argc, char **argv)
164 {
165         bbox_prepare_main(argv);
166         return $main(argc, argv);
167 }
168 " >"$sharedlib_dir/applet.c"
169
170         EXE="$sharedlib_dir/$name"
171         try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
172             -o $EXE \
173             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
174             -L"$sharedlib_dir" -lbusybox \
175         || {
176             echo "Linking $EXE failed"
177             cat $EXE.out
178             exit 1
179         }
180         rm -- "$sharedlib_dir/applet.c" $EXE.out
181         strip -s --remove-section=.note --remove-section=.comment $EXE
182
183     done <applet_lst.tmp
184 fi
185
186 # libbusybox.so is needed only for -lbusybox at link time,
187 # it is not needed at runtime. Deleting to reduce confusion.
188 rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
189 exit 0 # or else we may confuse make