blob: 4ee5d825597dd7bd943645e13e30a3aa6482325c [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001#!/bin/sh
2
3debug=false
4
Denis Vlasenko018e0852007-02-25 00:40:37 +00005try() {
Denis Vlasenko32404742007-10-07 17:05:22 +00006 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=$?
Denis Vlasenko32404742007-10-07 17:05:22 +000012 return $exitcode
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000013}
14
Denis Vlasenko32404742007-10-07 17:05:22 +000015EXE="$1"
16CC="$2"
17LDFLAGS="$3"
18O_FILES="$4"
19A_FILES="$5"
20LDLIBS="$6"
21
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000022# Sanitize lib list (dups, extra spaces etc)
Denis Vlasenko76a6b232007-10-07 17:05:42 +000023LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000024
25# First link with all libs. If it fails, bail out
Denis Vlasenko76a6b232007-10-07 17:05:42 +000026echo "Trying libraries: $LDLIBS"
Denis Vlasenko32404742007-10-07 17:05:22 +000027# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000028l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000029test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Denis Vlasenko32404742007-10-07 17:05:22 +000030try $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 \
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000035|| {
Denis Vlasenko32404742007-10-07 17:05:22 +000036 echo "Failed: $* $l_list"
37 cat $EXE.out
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000038 exit 1
39}
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000040
Denis Vlasenko8274e062007-08-06 03:41:08 +000041# Now try to remove each lib and build without it.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000042# Stop when no lib can be removed.
Denis Vlasenko76a6b232007-10-07 17:05:42 +000043while test "$LDLIBS"; do
44 $debug && echo "Trying libraries: $LDLIBS"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000045 all_needed=true
Denis Vlasenko76a6b232007-10-07 17:05:42 +000046 for one in $LDLIBS; do
47 without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
Denis Vlasenko32404742007-10-07 17:05:22 +000048 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Bernhard Reutner-Fischerd2bb2d32007-09-02 15:28:30 +000049 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"
Bernhard Reutner-Fischer8d91c132007-09-02 14:51:54 +000051 $debug && echo "Trying -l options: '$l_list'"
Denis Vlasenko32404742007-10-07 17:05:22 +000052 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"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000060 LDLIBS="$without_one"
Denis Vlasenko32404742007-10-07 17:05:22 +000061 all_needed=false
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000062 else
Denis Vlasenko32404742007-10-07 17:05:22 +000063 echo "Library $one is needed"
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000064 fi
65 done
66 # All libs were needed, can't remove any
67 $all_needed && break
Denis Vlasenko8274e062007-08-06 03:41:08 +000068 # If there is no space char, the list has just one lib.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000069 # I'm not sure that in this case lib really is 100% needed.
70 # Let's try linking without it anyway... thus commented out.
Denis Vlasenko76a6b232007-10-07 17:05:42 +000071 #{ echo "$LDLIBS" | grep -q ' '; } || break
Denis Vlasenko150d2fa2007-07-17 20:39:27 +000072done
73
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000074# Make the binary with final, minimal list of libs
Denis Vlasenko76a6b232007-10-07 17:05:42 +000075echo "Final link with: $LDLIBS"
76l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
Denis Vlasenko8d755ad2007-10-09 10:15:41 +000077test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000078# --verbose gives us gobs of info to stdout (e.g. linker script used)
79if ! test -f busybox_ldscript; then
Denis Vlasenko32404742007-10-07 17:05:22 +000080 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
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000086else
87 echo "Custom linker script 'busybox_ldscript' found, using it"
Denis Vlasenko76a6b232007-10-07 17:05:42 +000088 # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
Denis Vlasenko9862e6b2007-09-03 11:28:14 +000089 # .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.*)
Denis Vlasenko76a6b232007-10-07 17:05:42 +000092 # This will eliminate most of the padding (~3kb).
Denis Vlasenko32404742007-10-07 17:05:22 +000093 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
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000100fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000101
Denis Vlasenkodef88982007-10-07 17:06:01 +0000102. .config
Denis Vlasenko32404742007-10-07 17:05:22 +0000103
Denis Vlasenkodef88982007-10-07 17:06:01 +0000104sharedlib_dir="0_lib"
105
106if 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 \
Denis Vlasenko32404742007-10-07 17:05:22 +0000116 -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 \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000120 -Wl,-soname="libbusybox.so.$BB_VER" \
Denis Vlasenko32404742007-10-07 17:05:22 +0000121 -Wl,-z,combreloc \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000122 || {
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"
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000128 chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
Denis Vlasenkodef88982007-10-07 17:06:01 +0000129 echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
130fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000131
Denis Vlasenkodef88982007-10-07 17:06:01 +0000132if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
133 EXE="$sharedlib_dir/busybox_unstripped"
134 try $CC $LDFLAGS \
Denis Vlasenko32404742007-10-07 17:05:22 +0000135 -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 \
Denis Vlasenkof545be02007-10-07 17:06:26 +0000138 -Wl,--verbose \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000139 -L"$sharedlib_dir" -lbusybox \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000140 || {
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"
147fi
Denis Vlasenkof545be02007-10-07 17:06:26 +0000148
149if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000150 echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
Denis Vlasenkof545be02007-10-07 17:06:26 +0000151 gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
152 | grep -v "^#" \
153 | grep -v "^$" \
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000154 > applet_lst.tmp
Denis Vlasenko642a52d2007-10-07 21:00:41 +0000155 while read name main junk; do
Denis Vlasenkof545be02007-10-07 17:06:26 +0000156
157 echo "\
Denis Vlasenko642a52d2007-10-07 21:00:41 +0000158void bbox_prepare_main(char **argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000159int $main(int argc, char **argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000160
161const char *applet_name = \"$name\";
162
Denis Vlasenkof545be02007-10-07 17:06:26 +0000163int main(int argc, char **argv)
164{
Denis Vlasenko642a52d2007-10-07 21:00:41 +0000165 bbox_prepare_main(argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000166 return $main(argc, argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000167}
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 \
Denis Vlasenkof545be02007-10-07 17:06:26 +0000175 || {
176 echo "Linking $EXE failed"
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000177 cat $EXE.out
Denis Vlasenkof545be02007-10-07 17:06:26 +0000178 exit 1
179 }
180 rm -- "$sharedlib_dir/applet.c" $EXE.out
181 strip -s --remove-section=.note --remove-section=.comment $EXE
Denis Vlasenkof545be02007-10-07 17:06:26 +0000182
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000183 done <applet_lst.tmp
Denis Vlasenkof545be02007-10-07 17:06:26 +0000184fi
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000185
186# libbusybox.so is needed only for -lbusybox at link time,
187# it is not needed at runtime. Deleting to reduce confusion.
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000188rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
189exit 0 # or else we may confuse make