6 printf "%s\n" "Output of:" >$EXE.out
7 printf "%s\n" "$*" >>$EXE.out
8 printf "%s\n" "==========" >>$EXE.out
9 $debug && echo "Trying: $*"
22 # Sanitize lib list (dups, extra spaces etc)
23 LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
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"
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 \
36 echo "Failed: $* $l_list"
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"
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'"
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 \
58 echo "Library $one is not needed"
62 echo "Library $one is needed"
65 # All libs were needed, can't remove any
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
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
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 \
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).
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 \
109 sharedlib_dir="0_lib"
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"
117 ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
119 EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
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" \
128 echo "Linking $EXE failed"
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"
137 if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
138 EXE="$sharedlib_dir/busybox_unstripped"
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 \
144 -L"$sharedlib_dir" -lbusybox \
146 echo "Linking $EXE failed"
150 strip -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
151 echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
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 \
160 while read name main junk; do
163 void bbox_prepare_main(char **argv);
164 int $main(int argc, char **argv);
166 const char *applet_name = \"$name\";
168 int main(int argc, char **argv)
170 bbox_prepare_main(argv);
171 return $main(argc, argv);
173 " >"$sharedlib_dir/applet.c"
175 EXE="$sharedlib_dir/$name"
176 try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
178 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
179 -L"$sharedlib_dir" -lbusybox \
181 echo "Linking $EXE failed"
185 rm -- "$sharedlib_dir/applet.c" $EXE.out
186 strip -s --remove-section=.note --remove-section=.comment $EXE
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