de60a2cf726757f5d73e7b6537b995818480a02c
[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     cat $EXE.out
13     return $exitcode
14 }
15
16 EXE="$1"
17 CC="$2"
18 LDFLAGS="$3"
19 O_FILES="$4"
20 A_FILES="$5"
21 LDLIBS="$6"
22
23 # Sanitize lib list (dups, extra spaces etc)
24 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
25
26 # First link with all libs. If it fails, bail out
27 echo "Trying libraries: $LDLIBS"
28 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
29 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
30 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
31 try $CC $LDFLAGS \
32         -o $EXE -Wl,-Map -Wl,$EXE.map \
33         -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
34         -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
35         $l_list \
36         >/dev/null \
37 || {
38     echo "Failed: $* $l_list"
39     cat $EXE.out
40     exit 1
41 }
42
43 # Now try to remove each lib and build without it.
44 # Stop when no lib can be removed.
45 while test "$LDLIBS"; do
46     $debug && echo "Trying libraries: $LDLIBS"
47     all_needed=true
48     for one in $LDLIBS; do
49         without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
50         # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
51         l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
52         test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
53         $debug && echo "Trying -l options: '$l_list'"
54         try $CC $LDFLAGS \
55                 -o $EXE -Wl,-Map -Wl,$EXE.map \
56                 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
57                 -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
58                 $l_list \
59                 >/dev/null
60         if test $? = 0; then
61             echo "Library $one is not needed"
62             LDLIBS="$without_one"
63             all_needed=false
64         else
65             echo "Library $one is needed"
66         fi
67     done
68     # All libs were needed, can't remove any
69     $all_needed && break
70     # If there is no space char, the list has just one lib.
71     # I'm not sure that in this case lib really is 100% needed.
72     # Let's try linking without it anyway... thus commented out.
73     #{ echo "$LDLIBS" | grep -q ' '; } || break
74 done
75
76 # Make the binary with final, minimal list of libs
77 echo "Final link with: $LDLIBS"
78 l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
79 test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose"
80 # --verbose gives us gobs of info to stdout (e.g. linker script used)
81 if ! test -f busybox_ldscript; then
82     try $CC $LDFLAGS \
83             -o $EXE -Wl,-Map -Wl,$EXE.map \
84             -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
85             -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
86             $l_list -Wl,--verbose \
87             >/dev/null
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             >/dev/null
102 fi
103
104 . .config
105
106 sharedlib_dir="0_lib"
107
108 if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
109     mkdir "$sharedlib_dir" 2>/dev/null
110     test -d "$sharedlib_dir" || {
111         echo "Cannot make directory $sharedlib_dir"
112         exit 1
113     }
114     ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
115
116     EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
117     try $CC $LDFLAGS \
118         -o $EXE -Wl,-Map -Wl,$EXE.map \
119         -shared -fPIC -Wl,--enable-new-dtags \
120         -Wl,--start-group -Wl,--whole-archive $A_FILES -Wl,--no-whole-archive -Wl,--end-group \
121         $l_list -Wl,--verbose \
122         -Wl,-soname="libbusybox.so.$BB_VER" \
123         -Wl,-z,combreloc \
124         >/dev/null \
125     || {
126         echo "Linking $EXE failed"
127         cat $EXE.out
128         exit 1
129     }
130     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
131     echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
132 fi
133
134 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
135     EXE="$sharedlib_dir/busybox_unstripped"
136     try $CC $LDFLAGS \
137         -o $EXE -Wl,-Map -Wl,$EXE.map \
138         -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
139         -Wl,--start-group $O_FILES -Wl,--end-group \
140         $l_list -Wl,--verbose \
141         -L"$sharedlib_dir" -lbusybox \
142         >/dev/null \
143     || {
144         echo "Linking $EXE failed"
145         cat $EXE.out
146         exit 1
147     }
148     strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
149     echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
150 fi