Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | debug=false |
| 4 | |
Denis Vlasenko | 018e085 | 2007-02-25 00:40:37 +0000 | [diff] [blame] | 5 | try() { |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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=$? |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 12 | return $exitcode |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 13 | } |
| 14 | |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 15 | EXE="$1" |
| 16 | CC="$2" |
| 17 | LDFLAGS="$3" |
| 18 | O_FILES="$4" |
| 19 | A_FILES="$5" |
| 20 | LDLIBS="$6" |
| 21 | |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 22 | # Sanitize lib list (dups, extra spaces etc) |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 23 | LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs` |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 24 | |
| 25 | # First link with all libs. If it fails, bail out |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 26 | echo "Trying libraries: $LDLIBS" |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 27 | # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3" |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 28 | l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` |
Bernhard Reutner-Fischer | d2bb2d3 | 2007-09-02 15:28:30 +0000 | [diff] [blame] | 29 | test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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 \ |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 35 | || { |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 36 | echo "Failed: $* $l_list" |
| 37 | cat $EXE.out |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 38 | exit 1 |
| 39 | } |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 41 | # Now try to remove each lib and build without it. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 42 | # Stop when no lib can be removed. |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 43 | while test "$LDLIBS"; do |
| 44 | $debug && echo "Trying libraries: $LDLIBS" |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 45 | all_needed=true |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 46 | for one in $LDLIBS; do |
| 47 | without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs` |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 48 | # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3" |
Bernhard Reutner-Fischer | d2bb2d3 | 2007-09-02 15:28:30 +0000 | [diff] [blame] | 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" |
Bernhard Reutner-Fischer | 8d91c13 | 2007-09-02 14:51:54 +0000 | [diff] [blame] | 51 | $debug && echo "Trying -l options: '$l_list'" |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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" |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 60 | LDLIBS="$without_one" |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 61 | all_needed=false |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 62 | else |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 63 | echo "Library $one is needed" |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 64 | fi |
| 65 | done |
| 66 | # All libs were needed, can't remove any |
| 67 | $all_needed && break |
Denis Vlasenko | 8274e06 | 2007-08-06 03:41:08 +0000 | [diff] [blame] | 68 | # If there is no space char, the list has just one lib. |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 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. |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 71 | #{ echo "$LDLIBS" | grep -q ' '; } || break |
Denis Vlasenko | 150d2fa | 2007-07-17 20:39:27 +0000 | [diff] [blame] | 72 | done |
| 73 | |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 74 | # Make the binary with final, minimal list of libs |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 75 | echo "Final link with: $LDLIBS" |
| 76 | l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'` |
Denis Vlasenko | 8d755ad | 2007-10-09 10:15:41 +0000 | [diff] [blame^] | 77 | test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group" |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 78 | # --verbose gives us gobs of info to stdout (e.g. linker script used) |
| 79 | if ! test -f busybox_ldscript; then |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 86 | else |
| 87 | echo "Custom linker script 'busybox_ldscript' found, using it" |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 88 | # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out): |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 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.*) |
Denis Vlasenko | 76a6b23 | 2007-10-07 17:05:42 +0000 | [diff] [blame] | 92 | # This will eliminate most of the padding (~3kb). |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 9862e6b | 2007-09-03 11:28:14 +0000 | [diff] [blame] | 100 | fi |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 102 | . .config |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 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 \ |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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 \ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 120 | -Wl,-soname="libbusybox.so.$BB_VER" \ |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 121 | -Wl,-z,combreloc \ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 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" |
Denis Vlasenko | d62fd84 | 2007-10-07 20:46:34 +0000 | [diff] [blame] | 128 | chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER" |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 129 | echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER" |
| 130 | fi |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 132 | if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then |
| 133 | EXE="$sharedlib_dir/busybox_unstripped" |
| 134 | try $CC $LDFLAGS \ |
Denis Vlasenko | 3240474 | 2007-10-07 17:05:22 +0000 | [diff] [blame] | 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 \ |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 138 | -Wl,--verbose \ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 139 | -L"$sharedlib_dir" -lbusybox \ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 148 | |
| 149 | if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then |
Denis Vlasenko | e9fd69c | 2007-10-08 22:16:14 +0000 | [diff] [blame] | 150 | echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)" |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 151 | gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \ |
| 152 | | grep -v "^#" \ |
| 153 | | grep -v "^$" \ |
Denis Vlasenko | e9fd69c | 2007-10-08 22:16:14 +0000 | [diff] [blame] | 154 | > applet_lst.tmp |
Denis Vlasenko | 642a52d | 2007-10-07 21:00:41 +0000 | [diff] [blame] | 155 | while read name main junk; do |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 156 | |
| 157 | echo "\ |
Denis Vlasenko | 642a52d | 2007-10-07 21:00:41 +0000 | [diff] [blame] | 158 | void bbox_prepare_main(char **argv); |
Denis Vlasenko | d62fd84 | 2007-10-07 20:46:34 +0000 | [diff] [blame] | 159 | int $main(int argc, char **argv); |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 160 | |
| 161 | const char *applet_name = \"$name\"; |
| 162 | |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 163 | int main(int argc, char **argv) |
| 164 | { |
Denis Vlasenko | 642a52d | 2007-10-07 21:00:41 +0000 | [diff] [blame] | 165 | bbox_prepare_main(argv); |
Denis Vlasenko | d62fd84 | 2007-10-07 20:46:34 +0000 | [diff] [blame] | 166 | return $main(argc, argv); |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 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 \ |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 175 | || { |
| 176 | echo "Linking $EXE failed" |
Denis Vlasenko | 8d755ad | 2007-10-09 10:15:41 +0000 | [diff] [blame^] | 177 | cat $EXE.out |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 178 | exit 1 |
| 179 | } |
| 180 | rm -- "$sharedlib_dir/applet.c" $EXE.out |
| 181 | strip -s --remove-section=.note --remove-section=.comment $EXE |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 182 | |
Denis Vlasenko | e9fd69c | 2007-10-08 22:16:14 +0000 | [diff] [blame] | 183 | done <applet_lst.tmp |
Denis Vlasenko | f545be0 | 2007-10-07 17:06:26 +0000 | [diff] [blame] | 184 | fi |
Denis Vlasenko | d62fd84 | 2007-10-07 20:46:34 +0000 | [diff] [blame] | 185 | |
| 186 | # libbusybox.so is needed only for -lbusybox at link time, |
| 187 | # it is not needed at runtime. Deleting to reduce confusion. |
Denis Vlasenko | 8d755ad | 2007-10-09 10:15:41 +0000 | [diff] [blame^] | 188 | rm "$sharedlib_dir"/libbusybox.so 2>/dev/null |
| 189 | exit 0 # or else we may confuse make |