blob: 2255deee7699bce9300c588319cb229098c1ee13 [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001#!/bin/sh
2
Denys Vlasenko1f9ed022021-02-23 23:09:49 +01003#debug=true
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00004debug=false
5
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +00006# Linker flags used:
7#
8# Informational:
9# --warn-common
10# -Map $EXE.map
11# --verbose
12#
13# Optimizations:
14# --sort-common reduces padding
15# --sort-section alignment reduces padding
16# --gc-sections throws out unused sections,
17# does not work for shared libs
Denis Vlasenko42e41822007-10-09 18:01:13 +000018# -On Not used, maybe useful?
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000019#
20# List of files to link:
21# $l_list == --start-group -llib1 -llib2 --end-group
22# --start-group $O_FILES $A_FILES --end-group
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000023#
24# Shared library link:
25# -shared self-explanatory
26# -fPIC position-independent code
Denis Vlasenko724d1962007-10-10 14:41:07 +000027# --enable-new-dtags ?
28# -z,combreloc ?
29# -soname="libbusybox.so.$BB_VER"
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030# --undefined=lbb_main Seed name to start pulling from
Denis Vlasenko724d1962007-10-10 14:41:07 +000031# (otherwise we'll need --whole-archive)
32# -static Not used, but may be useful! manpage:
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +000033# "... This option can be used with -shared.
34# Doing so means that a shared library
35# is being created but that all of the library's
36# external references must be resolved by pulling
37# in entries from static libraries."
38
39
Denis Vlasenko018e0852007-02-25 00:40:37 +000040try() {
Denis Vlasenko32404742007-10-07 17:05:22 +000041 printf "%s\n" "Output of:" >$EXE.out
42 printf "%s\n" "$*" >>$EXE.out
43 printf "%s\n" "==========" >>$EXE.out
44 $debug && echo "Trying: $*"
Bernhard Reutner-Fischer2dfd2952008-10-23 13:49:21 +000045 $@ >>$EXE.out 2>&1
46 return $?
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000047}
48
Denis Vlasenko130f5592007-11-13 17:36:12 +000049check_cc() {
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010050 tempname="$(mktemp tmp.XXXXXXXXXX)"
Denys Vlasenko51340102015-10-20 16:16:16 +020051 echo "int main(int argc,char**argv){return argv?argc:0;}" >"$tempname".c
Denis Vlasenko0a4624a2008-02-13 07:47:37 +000052 # Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :(
Denys Vlasenko51340102015-10-20 16:16:16 +020053 # Was using "-xc /dev/null", but we need a valid C program.
Mike Frysinger77e2bde2015-12-01 11:25:10 -050054 $CC $CFLAGS $LDFLAGS $1 "$tempname".c -o "$tempname" >/dev/null 2>&1
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010055 exitcode=$?
Denys Vlasenko51340102015-10-20 16:16:16 +020056 rm -f "$tempname" "$tempname".c "$tempname".o
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010057 return $exitcode
Denis Vlasenko130f5592007-11-13 17:36:12 +000058}
59
Denis Vlasenko3f9c8482007-12-28 17:04:42 +000060check_libc_is_glibc() {
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010061 tempname="$(mktemp tmp.XXXXXXXXXX)"
Denis Vlasenko3f9c8482007-12-28 17:04:42 +000062 echo "\
63 #include <stdlib.h>
64 /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
65 #if defined(__GLIBC__) && !defined(__UCLIBC__)
66 syntax error here
67 #endif
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +000068 " >"$tempname".c
Denys Vlasenko58d0e202015-10-20 16:40:43 +020069 ! $CC $CFLAGS "$tempname".c -c -o "$tempname".o >/dev/null 2>&1
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010070 exitcode=$?
Denys Vlasenko51340102015-10-20 16:16:16 +020071 rm -f "$tempname" "$tempname".c "$tempname".o
Denys Vlasenkobca4ea82017-01-24 20:52:42 +010072 return $exitcode
Denis Vlasenko3f9c8482007-12-28 17:04:42 +000073}
74
Denis Vlasenko32404742007-10-07 17:05:22 +000075EXE="$1"
76CC="$2"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +000077CFLAGS="$3"
78LDFLAGS="$4"
79O_FILES="$5"
80A_FILES="$6"
Denys Vlasenko1f9ed022021-02-23 23:09:49 +010081# We try to drop libraries from LDLIBS if build works without them,
82# but ones from CONFIG_EXTRA_LDLIBS are always linked in.
83# (For example, musl has stub utmp implementation, and if you link with
84# a real utmp library in LDLIBS, dropping it "works" but resulting binary
85# does not work properly).
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +000086LDLIBS="$7"
Denys Vlasenko1f9ed022021-02-23 23:09:49 +010087CONFIG_EXTRA_LDLIBS="$8"
Denis Vlasenko32404742007-10-07 17:05:22 +000088
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +000089# The --sort-section option is not supported by older versions of ld
Denys Vlasenko51340102015-10-20 16:16:16 +020090SORT_SECTION="-Wl,--sort-section,alignment"
91if ! check_cc "-Wl,--sort-section,alignment"; then
92 echo "Your linker does not support --sort-section,alignment"
93 SORT_SECTION=""
94fi
Denis Vlasenko130f5592007-11-13 17:36:12 +000095
Dan Fandrichebeac162010-06-18 22:36:10 -070096START_GROUP="-Wl,--start-group"
97END_GROUP="-Wl,--end-group"
Denys Vlasenko367a55c2017-07-15 14:52:26 +020098INFO_OPTS() {
99 echo "-Wl,--warn-common -Wl,-Map,$EXE.map -Wl,--verbose"
100}
Dan Fandrichebeac162010-06-18 22:36:10 -0700101
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200102# gold may not support --sort-common (yet)
Denys Vlasenko51340102015-10-20 16:16:16 +0200103SORT_COMMON="-Wl,--sort-common"
104if ! check_cc "-Wl,--sort-common"; then
105 echo "Your linker does not support --sort-common"
106 SORT_COMMON=""
107fi
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200108
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000109# Static linking against glibc produces buggy executables
110# (glibc does not cope well with ld --gc-sections).
111# See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
112# Note that glibc is unsuitable for static linking anyway.
113# We are removing -Wl,--gc-sections from link command line.
Denys Vlasenko51340102015-10-20 16:16:16 +0200114GC_SECTIONS="-Wl,--gc-sections"
115if (. ./.config && test x"$CONFIG_STATIC" = x"y") then
116 if check_libc_is_glibc; then
117 echo "Static linking against glibc, can't use --gc-sections"
118 GC_SECTIONS=""
119 fi
Denis Vlasenko3f9c8482007-12-28 17:04:42 +0000120fi
Denys Vlasenkoeb1cda22009-06-27 00:24:35 +0200121# The --gc-sections option is not supported by older versions of ld
122if test -n "$GC_SECTIONS"; then
Denys Vlasenko51340102015-10-20 16:16:16 +0200123 if ! check_cc "$GC_SECTIONS"; then
124 echo "Your linker does not support $GC_SECTIONS"
125 GC_SECTIONS=""
126 fi
Denys Vlasenkoeb1cda22009-06-27 00:24:35 +0200127fi
128
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000129# Sanitize lib list (dups, extra spaces etc)
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000130LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000131
132# First link with all libs. If it fails, bail out
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000133echo "Trying libraries: $LDLIBS"
Denis Vlasenko32404742007-10-07 17:05:22 +0000134# "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Denys Vlasenko1f9ed022021-02-23 23:09:49 +0100135l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
136test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000137try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000138 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200139 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000140 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000141 $GC_SECTIONS \
Dan Fandrichebeac162010-06-18 22:36:10 -0700142 $START_GROUP $O_FILES $A_FILES $END_GROUP \
Denis Vlasenko32404742007-10-07 17:05:22 +0000143 $l_list \
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000144|| {
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000145 echo "Failed: $l_list"
Denis Vlasenko32404742007-10-07 17:05:22 +0000146 cat $EXE.out
Denys Vlasenko056e1f52016-04-03 15:38:53 +0200147 echo 'Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.'
148 echo 'Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"'
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000149 exit 1
150}
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000151
Denis Vlasenko8274e062007-08-06 03:41:08 +0000152# Now try to remove each lib and build without it.
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000153# Stop when no lib can be removed.
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000154while test "$LDLIBS"; do
155 $debug && echo "Trying libraries: $LDLIBS"
Denys Vlasenko32511da2018-11-19 20:36:16 +0100156 dropped_non_first_lib=false
157 first_lib=true
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000158 for one in $LDLIBS; do
159 without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
Denis Vlasenko32404742007-10-07 17:05:22 +0000160 # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
Denys Vlasenko1f9ed022021-02-23 23:09:49 +0100161 l_list=`echo " $without_one $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
Dan Fandrichebeac162010-06-18 22:36:10 -0700162 test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
Bernhard Reutner-Fischer8d91c132007-09-02 14:51:54 +0000163 $debug && echo "Trying -l options: '$l_list'"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000164 try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000165 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200166 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000167 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000168 $GC_SECTIONS \
Dan Fandrichebeac162010-06-18 22:36:10 -0700169 $START_GROUP $O_FILES $A_FILES $END_GROUP \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +0000170 $l_list
Denis Vlasenko32404742007-10-07 17:05:22 +0000171 if test $? = 0; then
Denis Vlasenkob522d692008-08-26 20:09:08 +0000172 echo " Library $one is not needed, excluding it"
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000173 LDLIBS="$without_one"
Denys Vlasenko32511da2018-11-19 20:36:16 +0100174 $first_lib || dropped_non_first_lib=true
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000175 else
Denis Vlasenkob522d692008-08-26 20:09:08 +0000176 echo " Library $one is needed, can't exclude it (yet)"
Denys Vlasenko32511da2018-11-19 20:36:16 +0100177 first_lib=false
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000178 fi
179 done
Denys Vlasenko32511da2018-11-19 20:36:16 +0100180 # We can stop trying to drop libs if either all libs were needed,
181 # or we excluded only the _first_ few.
182 # (else: we dropped some intermediate lib(s), maybe now we can succeed
183 # in dropping some of the preceding ones)
184 $dropped_non_first_lib || break
Denis Vlasenko150d2fa2007-07-17 20:39:27 +0000185done
186
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000187# Make the binary with final, minimal list of libs
Mike Frysinger49d15892007-11-18 06:42:56 +0000188echo "Final link with: ${LDLIBS:-<none>}"
Denys Vlasenko1f9ed022021-02-23 23:09:49 +0100189l_list=`echo " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'`
190test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000191# --verbose gives us gobs of info to stdout (e.g. linker script used)
192if ! test -f busybox_ldscript; then
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000193 try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000194 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200195 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000196 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000197 $GC_SECTIONS \
Dan Fandrichebeac162010-06-18 22:36:10 -0700198 $START_GROUP $O_FILES $A_FILES $END_GROUP \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000199 $l_list \
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200200 `INFO_OPTS` \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +0000201 || {
202 cat $EXE.out
203 exit 1
204 }
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000205else
206 echo "Custom linker script 'busybox_ldscript' found, using it"
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000207 # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000208 # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
209 # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
210 # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
Denis Vlasenko76a6b232007-10-07 17:05:42 +0000211 # This will eliminate most of the padding (~3kb).
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000212 # Hmm, "ld --sort-section alignment" should do it too.
Denys Vlasenko663d1da2016-04-22 02:00:04 +0200213 #
214 # There is a ld hack which is meant to decrease disk usage
215 # at the cost of more RAM usage (??!!) in standard ld script:
216 # /* Adjust the address for the data segment. We want to adjust up to
217 # the same address within the page on the next page up. */
218 # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
219 # Replace it with:
220 # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
221 # to unconditionally align .data to the next page boundary,
222 # instead of "next page, plus current offset in this page"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000223 try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000224 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200225 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000226 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000227 $GC_SECTIONS \
Bernhard Reutner-Fischer50dbed92008-05-09 12:43:04 +0000228 -Wl,-T,busybox_ldscript \
Dan Fandrichebeac162010-06-18 22:36:10 -0700229 $START_GROUP $O_FILES $A_FILES $END_GROUP \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000230 $l_list \
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200231 `INFO_OPTS` \
Denis Vlasenkod19b87e2007-10-09 13:08:02 +0000232 || {
233 cat $EXE.out
234 exit 1
235 }
Denis Vlasenko9862e6b2007-09-03 11:28:14 +0000236fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000237
Denis Vlasenko42e41822007-10-09 18:01:13 +0000238. ./.config
Denis Vlasenko32404742007-10-07 17:05:22 +0000239
Denis Vlasenkodef88982007-10-07 17:06:01 +0000240sharedlib_dir="0_lib"
241
242if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
243 mkdir "$sharedlib_dir" 2>/dev/null
244 test -d "$sharedlib_dir" || {
245 echo "Cannot make directory $sharedlib_dir"
246 exit 1
247 }
248 ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
249
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200250 # Yes, "ld -shared -static" is a thing. It's a shared library which is itself static.
251 LBB_STATIC=""
252 test "$CONFIG_FEATURE_LIBBUSYBOX_STATIC" = y && LBB_STATIC="-Wl,-static"
253
Denis Vlasenkodef88982007-10-07 17:06:01 +0000254 EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000255 try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000256 -o $EXE \
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200257 -shared -fPIC $LBB_STATIC \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000258 -Wl,--enable-new-dtags \
259 -Wl,-z,combreloc \
260 -Wl,-soname="libbusybox.so.$BB_VER" \
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000261 -Wl,--undefined=lbb_main \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200262 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000263 $SORT_SECTION \
Dan Fandrichebeac162010-06-18 22:36:10 -0700264 $START_GROUP $A_FILES $END_GROUP \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000265 $l_list \
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200266 `INFO_OPTS` \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000267 || {
268 echo "Linking $EXE failed"
269 cat $EXE.out
270 exit 1
271 }
Denis Vlasenko141750e2007-10-10 10:05:35 +0000272 $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000273 chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
Denis Vlasenkodef88982007-10-07 17:06:01 +0000274 echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
275fi
Denis Vlasenko32404742007-10-07 17:05:22 +0000276
Denis Vlasenkodef88982007-10-07 17:06:01 +0000277if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
278 EXE="$sharedlib_dir/busybox_unstripped"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000279 try $CC $CFLAGS $LDFLAGS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000280 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200281 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000282 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000283 $GC_SECTIONS \
Dan Fandrichebeac162010-06-18 22:36:10 -0700284 $START_GROUP $O_FILES $END_GROUP \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000285 -L"$sharedlib_dir" -lbusybox \
Steve Iribarneed607a82011-05-09 01:42:12 +0200286 $l_list \
Denys Vlasenko367a55c2017-07-15 14:52:26 +0200287 `INFO_OPTS` \
Denis Vlasenkodef88982007-10-07 17:06:01 +0000288 || {
289 echo "Linking $EXE failed"
290 cat $EXE.out
291 exit 1
292 }
Denis Vlasenko141750e2007-10-10 10:05:35 +0000293 $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
Denis Vlasenkodef88982007-10-07 17:06:01 +0000294 echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
295fi
Denis Vlasenkof545be02007-10-07 17:06:26 +0000296
297if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000298 echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200299 gcc -DNAME_MAIN -E -include include/autoconf.h include/applets.h \
Denis Vlasenkof545be02007-10-07 17:06:26 +0000300 | grep -v "^#" \
Denys Vlasenkoea9ebc02016-06-20 12:23:35 +0200301 | grep -v "^ *$" \
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000302 > applet_lst.tmp
Denis Vlasenko642a52d2007-10-07 21:00:41 +0000303 while read name main junk; do
Denis Vlasenkof545be02007-10-07 17:06:26 +0000304
305 echo "\
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000306void lbb_prepare(const char *applet, char **argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000307int $main(int argc, char **argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000308
Denis Vlasenkof545be02007-10-07 17:06:26 +0000309int main(int argc, char **argv)
310{
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000311 lbb_prepare(\"$name\", argv);
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000312 return $main(argc, argv);
Denis Vlasenkof545be02007-10-07 17:06:26 +0000313}
314" >"$sharedlib_dir/applet.c"
315
316 EXE="$sharedlib_dir/$name"
Denis Vlasenkof1d93ec2008-02-14 12:24:14 +0000317 try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000318 -o $EXE \
Bernhard Reutner-Fischera88585a2010-05-21 12:11:34 +0200319 $SORT_COMMON \
Denis Vlasenko130f5592007-11-13 17:36:12 +0000320 $SORT_SECTION \
Denis Vlasenkoa2dcb502008-04-30 00:15:56 +0000321 $GC_SECTIONS \
Denis Vlasenko01f3b2c2007-10-09 13:49:26 +0000322 -L"$sharedlib_dir" -lbusybox \
323 -Wl,--warn-common \
Denis Vlasenkof545be02007-10-07 17:06:26 +0000324 || {
325 echo "Linking $EXE failed"
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000326 cat $EXE.out
Denis Vlasenkof545be02007-10-07 17:06:26 +0000327 exit 1
328 }
329 rm -- "$sharedlib_dir/applet.c" $EXE.out
Denis Vlasenko141750e2007-10-10 10:05:35 +0000330 $STRIP -s --remove-section=.note --remove-section=.comment $EXE
Denys Vlasenkoe3366d62014-05-03 16:35:15 +0200331 # Let user see that we do something - list the names of created binaries:
332 echo "$EXE"
Denis Vlasenkof545be02007-10-07 17:06:26 +0000333
Denis Vlasenkoe9fd69c2007-10-08 22:16:14 +0000334 done <applet_lst.tmp
Denis Vlasenkof545be02007-10-07 17:06:26 +0000335fi
Denis Vlasenkod62fd842007-10-07 20:46:34 +0000336
337# libbusybox.so is needed only for -lbusybox at link time,
338# it is not needed at runtime. Deleting to reduce confusion.
Denis Vlasenko8d755ad2007-10-09 10:15:41 +0000339rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
340exit 0 # or else we may confuse make