[patch] build shared libraries

Bernhard Fischer rep.nop at aon.at
Mon Sep 5 09:54:23 UTC 2005


Hi,

Attached patch adds CONFIG_BUILD_LIBRARIES.

If set, lib*.a will be also build as shared libraries:
./libbb/libbb.so
./libpwdgrp/libpwdgrp.so
./archival/libunarchive/libunarchive.so
./networking/libiproute/libiproute.so
./coreutils/libcoreutils/libcoreutils.so

If CONFIG_STATIC is set (which is the default), the busybox binary will
use the static libs as before.

If CONFIG_STATIC is not set, busybox will link against the shared
variants of the libraries (1).

make install installs (currently, i'm not against changing this) both
dynamic and static variants of the libraries into $PREFIX/lib[64].

This patch is ment as an RFC. It worked flawlessly in my cursory
testing so far.
Comments?



1)
# ldd bin/busybox
	libunarchive.so => /lib/libunarchive.so (0x40018000)
	libiproute.so => /lib/libiproute.so (0x400e2000)
	libpwdgrp.so => /lib/libpwdgrp.so (0x400f0000)
	libcoreutils.so => /lib/libcoreutils.so (0x400f3000)
	libbb.so => /lib/libbb.so (0x400f5000)
	libdl.so.2 => /lib/tls/libdl.so.2 (0x40105000)
	libm.so.6 => /lib/tls/libm.so.6 (0x40108000)
	libcrypt.so.1 => /lib/tls/libcrypt.so.1 (0x4012b000)
	libc.so.6 => /lib/tls/libc.so.6 (0x40158000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
# ls -l bin/busybox
-rwxr-xr-x    1 0      0        739044 Sep  5 09:38 bin/busybox

Annotations:
a) There are two files in libbb which use libcrypt: correct_password.c
and pw_encrypt.c. Because of this libbb.so itself has to link against
libcrypt. Is this intended, given that there is libpwdgrp?


b) every single Makefile.in seems to "handcode" the rule to build static
archives ($(AR) $(ARFLAGS) $@). The patch adds a generic rule to build
archives. This rule would be used if one specifies just the
libname.a: myobj1.o myobj2.o
dependencies without the redundant "$(AR) $(ARFLAGS)..." lines in all
Makefile.in's. If a Makefile still has a separate Rule for building the
archives, this specific Rule would be used instead of the generic one
from Rules.mk.
I changed the Makefiles for the libs which now are also built dynamically
and left the other alone for now. I, personally, don't see a good reason
not to ditch all remaining specific rules for building the archives but
would suggest to add the generic rule in one place (like this patch
does) and peruse this rule.



-------------- next part --------------
diff -X excl -rduNp busybox.oorig/applets/install.sh busybox/applets/install.sh
--- busybox.oorig/applets/install.sh	2005-09-02 13:35:48.000000000 +0200
+++ busybox/applets/install.sh	2005-09-05 10:04:33.196838505 +0200
@@ -4,7 +4,8 @@ export LC_ALL=POSIX
 export LC_CTYPE=POSIX
 
 prefix=$1
-if [ "$prefix" = "" ]; then
+# Some versions of [ can't deal with empty strings for the '=' operation
+if [ "x$prefix" = "x" ]; then
     echo "No installation directory, aborting."
     exit 1;
 fi
@@ -15,10 +16,20 @@ else
 fi
 h=`sort busybox.links | uniq`
 
+# get the target dir for the libs; This is an incorrect/incomplete list for now
+case $(uname -m) in
+x86_64|ppc64*|sparc64*|ia64*|hppa*64*) libdir=/lib64 ;;
+*) libdir=/lib ;;
+esac
 
 rm -f $prefix/bin/busybox || exit 1
 mkdir -p $prefix/bin || exit 1
+mkdir -p $prefix/$libdir || exit 1
 install -m 755 busybox $prefix/bin/busybox || exit 1
+for i in $(find ./ -type f -name \*.a ; find ./ -type f -name \*.so)
+do
+	install -m 644 $i $prefix/$libdir/
+done
 
 for i in $h ; do
 	appdir=`dirname $i`
diff -X excl -rduNp busybox.oorig/archival/libunarchive/Makefile busybox/archival/libunarchive/Makefile
--- busybox.oorig/archival/libunarchive/Makefile	2005-09-02 13:35:31.000000000 +0200
+++ busybox/archival/libunarchive/Makefile	2005-09-05 10:04:33.203837601 +0200
@@ -28,5 +28,5 @@ all: $(libraries-y)
 -include $(top_builddir)/.depend
 
 clean:
-	rm -f *.o *.a $(AR_TARGET)
+	rm -f *.o *.a *.so $(AR_TARGET)
 
diff -X excl -rduNp busybox.oorig/archival/libunarchive/Makefile.in busybox/archival/libunarchive/Makefile.in
--- busybox.oorig/archival/libunarchive/Makefile.in	2005-09-02 13:35:31.000000000 +0200
+++ busybox/archival/libunarchive/Makefile.in	2005-09-05 10:04:33.209836827 +0200
@@ -18,6 +18,7 @@
 #
 
 LIBUNARCHIVE_AR:=libunarchive.a
+LIBUNARCHIVE_LD:=libunarchive.so
 ifndef $(LIBUNARCHIVE_DIR)
 LIBUNARCHIVE_DIR:=$(top_builddir)/archival/libunarchive/
 endif
@@ -74,11 +75,18 @@ LIBUNARCHIVE-$(CONFIG_FEATURE_TAR_COMPRE
 LIBUNARCHIVE-$(CONFIG_UNCOMPRESS) += decompress_uncompress.o
 LIBUNARCHIVE-$(CONFIG_UNZIP) += $(GUNZIP_FILES)
 
-libraries-y+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR)
-
 $(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR): $(patsubst %,$(LIBUNARCHIVE_DIR)%, $(LIBUNARCHIVE-y))
-	$(AR) $(ARFLAGS) $@ $(patsubst %,$(LIBUNARCHIVE_DIR)%, $(LIBUNARCHIVE-y))
 
-$(LIBUNARCHIVA_DIR)%.o: $(srcdir)/%.c
+$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_LD): $(patsubst %,$(LIBUNARCHIVE_DIR)%, $(LIBUNARCHIVE-y))
+
+$(LIBUNARCHIVE_DIR)%.o: $(srcdir)/%.c
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
 
+ifeq ($(CONFIG_STATIC),y)
+libraries-y+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR)
+libraries-m+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_LD)
+else
+libraries-m+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR)
+libraries-y+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_LD)
+endif
+
diff -X excl -rduNp busybox.oorig/coreutils/libcoreutils/Makefile busybox/coreutils/libcoreutils/Makefile
--- busybox.oorig/coreutils/libcoreutils/Makefile	2005-09-02 13:35:35.000000000 +0200
+++ busybox/coreutils/libcoreutils/Makefile	2005-09-05 10:04:33.215836052 +0200
@@ -29,5 +29,5 @@ all: $(libraries-y)
 -include $(top_builddir)/.depend
 
 clean:
-	rm -f *.o *.a $(AR_TARGET)
+	rm -f *.o *.a *.so $(AR_TARGET)
 
diff -X excl -rduNp busybox.oorig/coreutils/libcoreutils/Makefile.in busybox/coreutils/libcoreutils/Makefile.in
--- busybox.oorig/coreutils/libcoreutils/Makefile.in	2005-09-02 13:35:35.000000000 +0200
+++ busybox/coreutils/libcoreutils/Makefile.in	2005-09-05 10:04:33.221835278 +0200
@@ -18,6 +18,7 @@
 #
 
 LIBCOREUTILS_AR:=libcoreutils.a
+LIBCOREUTILS_LD:=libcoreutils.so
 ifndef $(LIBCOREUTILS_DIR)
 LIBCOREUTILS_DIR:=$(top_builddir)/coreutils/libcoreutils/
 endif
@@ -27,11 +28,18 @@ LIBCOREUTILS_SRC:= cp_mv_stat.c getopt_m
 
 LIBCOREUTILS_OBJS=$(patsubst %.c,$(LIBCOREUTILS_DIR)%.o, $(LIBCOREUTILS_SRC))
 
-libraries-y+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR)
-
 $(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR): $(LIBCOREUTILS_OBJS)
-	$(AR) $(ARFLAGS) $@ $(LIBCOREUTILS_OBJS)
+
+$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_LD): $(LIBCOREUTILS_OBJS)
 
 $(LIBCOREUTILS_DIR)%.o: $(srcdir)/%.c
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
 
+ifeq ($(CONFIG_STATIC),y)
+libraries-y+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR)
+libraries-m+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_LD)
+else
+libraries-m+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR)
+libraries-y+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_LD)
+endif
+
diff -X excl -rduNp busybox.oorig/libbb/Makefile busybox/libbb/Makefile
--- busybox.oorig/libbb/Makefile	2005-09-02 13:35:41.000000000 +0200
+++ busybox/libbb/Makefile	2005-09-05 10:04:33.276828177 +0200
@@ -28,5 +28,5 @@ all: $(libraries-y)
 -include $(top_builddir)/.depend
 
 clean:
-	rm -f *.o *.a $(AR_TARGET)
+	rm -f *.o *.a *.so $(AR_TARGET)
 
diff -X excl -rduNp busybox.oorig/libbb/Makefile.in busybox/libbb/Makefile.in
--- busybox.oorig/libbb/Makefile.in	2005-09-02 13:35:41.000000000 +0200
+++ busybox/libbb/Makefile.in	2005-09-05 10:20:10.573587351 +0200
@@ -17,6 +17,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 LIBBB_AR:=libbb.a
+LIBBB_LD:=libbb.so
 ifndef $(LIBBB_DIR)
 LIBBB_DIR:=$(top_builddir)/libbb/
 endif
@@ -81,12 +82,13 @@ LIBBB_MOBJS2=$(patsubst %,$(LIBBB_DIR)%,
 LIBBB_MOBJS3=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ3))
 LIBBB_MOBJS4=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ4))
 
-libraries-y+=$(LIBBB_DIR)$(LIBBB_AR)
-
+# use implicit rules from Rules.mk
 $(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
 	$(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
-	$(AR) $(ARFLAGS) $@ $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
-		$(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
+
+# correct_password() and pw_encrypt() call crypt()..
+$(LIBBB_DIR)$(LIBBB_LD): -lcrypt $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
+	$(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
 
 $(LIBBB_DIR)%.o: $(srcdir)/%.c
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
@@ -106,3 +108,14 @@ $(LIBBB_MOBJS3): $(LIBBB_MSRC3)
 $(LIBBB_MOBJS4): $(LIBBB_MSRC4)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
 
+
+
+
+ifeq ($(CONFIG_STATIC),y)
+libraries-y+=$(LIBBB_DIR)$(LIBBB_AR)
+libraries-m+=$(LIBBB_DIR)$(LIBBB_LD)
+else
+libraries-m+=$(LIBBB_DIR)$(LIBBB_AR)
+libraries-y+=$(LIBBB_DIR)$(LIBBB_LD)
+endif
+
diff -X excl -rduNp busybox.oorig/libpwdgrp/Makefile busybox/libpwdgrp/Makefile
--- busybox.oorig/libpwdgrp/Makefile	2005-09-02 13:35:47.000000000 +0200
+++ busybox/libpwdgrp/Makefile	2005-09-05 10:04:33.288826628 +0200
@@ -28,5 +28,5 @@ all: $(libraries-y)
 -include $(top_builddir)/.depend
 
 clean:
-	rm -f *.o *.a $(AR_TARGET)
+	rm -f *.o *.a *.so $(AR_TARGET)
 
diff -X excl -rduNp busybox.oorig/libpwdgrp/Makefile.in busybox/libpwdgrp/Makefile.in
--- busybox.oorig/libpwdgrp/Makefile.in	2005-09-02 13:35:47.000000000 +0200
+++ busybox/libpwdgrp/Makefile.in	2005-09-05 10:04:33.294825853 +0200
@@ -18,6 +18,7 @@
 #
 
 LIBPWDGRP_AR:=libpwdgrp.a
+LIBPWDGRP_LD:=libpwdgrp.so
 ifndef $(LIBPWDGRP_DIR)
 LIBPWDGRP_DIR:=$(top_builddir)/libpwdgrp/
 endif
@@ -39,10 +40,9 @@ LIBPWDGRP_MOBJ1-$(CONFIG_USE_BB_PWD_GRP)
 	putspent.o __parsespent.o # getspuid_r.o getspuid.o
 LIBPWDGRP_MOBJS1=$(patsubst %,$(LIBPWDGRP_DIR)%, $(LIBPWDGRP_MOBJ1-y))
 
-libraries-y+=$(LIBPWDGRP_DIR)$(LIBPWDGRP_AR)
-
 $(LIBPWDGRP_DIR)$(LIBPWDGRP_AR): $(LIBPWDGRP_MOBJS0) $(LIBPWDGRP_MOBJS1)
-	$(AR) $(ARFLAGS) $@ $(LIBPWDGRP_MOBJS0) $(LIBPWDGRP_MOBJS1)
+
+$(LIBPWDGRP_DIR)$(LIBPWDGRP_LD): $(LIBPWDGRP_MOBJS0) $(LIBPWDGRP_MOBJS1)
 
 $(LIBPWDGRP_MOBJS0): $(LIBPWDGRP_MSRC0)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
@@ -50,4 +50,11 @@ $(LIBPWDGRP_MOBJS0): $(LIBPWDGRP_MSRC0)
 $(LIBPWDGRP_MOBJS1): $(LIBPWDGRP_MSRC1)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
 
+ifeq ($(CONFIG_STATIC),y)
+libraries-y+=$(LIBPWDGRP_DIR)$(LIBPWDGRP_AR)
+libraries-m+=$(LIBPWDGRP_DIR)$(LIBPWDGRP_LD)
+else
+libraries-m+=$(LIBPWDGRP_DIR)$(LIBPWDGRP_AR)
+libraries-y+=$(LIBPWDGRP_DIR)$(LIBPWDGRP_LD)
+endif
 
diff -X excl -rduNp busybox.oorig/Makefile busybox/Makefile
--- busybox.oorig/Makefile	2005-09-02 13:35:51.000000000 +0200
+++ busybox/Makefile	2005-09-05 10:04:33.302824820 +0200
@@ -122,7 +122,7 @@ $(ALL_MAKEFILES): %/Makefile: $(top_srcd
 include $(patsubst %,%/Makefile.in, $(SRC_DIRS))
 -include $(top_builddir)/.depend
 
-busybox: $(ALL_MAKEFILES) .depend include/bb_config.h $(libraries-y)
+busybox: $(ALL_MAKEFILES) .depend include/bb_config.h $(libraries-y) $(libraries-m)
 	$(CC) $(LDFLAGS) -o $@ -Wl,--start-group $(libraries-y) $(LIBRARIES) -Wl,--end-group
 	$(STRIPCMD) $@
 
@@ -289,6 +289,7 @@ clean:
 	- find . -name .\*.flags -exec rm -f {} \;
 	- find . -name \*.o -exec rm -f {} \;
 	- find . -name \*.a -exec rm -f {} \;
+	- find . -name \*.so -exec rm -f {} \;
 
 distclean: clean
 	- rm -f scripts/split-include scripts/mkdep
diff -X excl -rduNp busybox.oorig/networking/libiproute/Makefile busybox/networking/libiproute/Makefile
--- busybox.oorig/networking/libiproute/Makefile	2005-09-02 13:35:26.000000000 +0200
+++ busybox/networking/libiproute/Makefile	2005-09-05 10:04:33.308824045 +0200
@@ -28,5 +28,5 @@ all: $(libraries-y)
 -include $(top_builddir)/.depend
 
 clean:
-	rm -f *.o *.a $(AR_TARGET)
+	rm -f *.o *.a *.so $(AR_TARGET)
 
diff -X excl -rduNp busybox.oorig/networking/libiproute/Makefile.in busybox/networking/libiproute/Makefile.in
--- busybox.oorig/networking/libiproute/Makefile.in	2005-09-02 13:35:25.000000000 +0200
+++ busybox/networking/libiproute/Makefile.in	2005-09-05 10:04:33.314823271 +0200
@@ -18,6 +18,7 @@
 #
 
 LIBIPROUTE_AR:=libiproute.a
+LIBIPROUTE_LD:=libiproute.so
 ifndef $(LIBIPROUTE_DIR)
 LIBIPROUTE_DIR:=$(top_builddir)/networking/libiproute/
 endif
@@ -74,10 +75,17 @@ LIBIPROUTE-$(CONFIG_IPTUNNEL) += \
 	rt_names.o \
 	utils.o
 
+ifeq ($(CONFIG_STATIC),y)
 libraries-y+=$(LIBIPROUTE_DIR)$(LIBIPROUTE_AR)
+libraries-m+=$(LIBIPROUTE_DIR)$(LIBIPROUTE_LD)
+else
+libraries-m+=$(LIBIPROUTE_DIR)$(LIBIPROUTE_AR)
+libraries-y+=$(LIBIPROUTE_DIR)$(LIBIPROUTE_LD)
+endif
 
 $(LIBIPROUTE_DIR)$(LIBIPROUTE_AR): $(patsubst %,$(LIBIPROUTE_DIR)%, $(LIBIPROUTE-y))
-	$(AR) $(ARFLAGS) $@ $(patsubst %,$(LIBIPROUTE_DIR)%, $(LIBIPROUTE-y))
+
+$(LIBIPROUTE_DIR)$(LIBIPROUTE_LD): $(patsubst %,$(LIBIPROUTE_DIR)%, $(LIBIPROUTE-y))
 
 $(LIBIPROUTE_DIR)%.o: $(srcdir)/%.c
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
diff -X excl -rduNp busybox.oorig/Rules.mak busybox/Rules.mak
--- busybox.oorig/Rules.mak	2005-09-02 13:35:51.000000000 +0200
+++ busybox/Rules.mak	2005-09-05 10:18:47.029405332 +0200
@@ -165,15 +168,22 @@ else
 endif
 ifeq ($(strip $(CONFIG_DEBUG)),y)
     CFLAGS  +=$(WARNINGS) -g -D_GNU_SOURCE
-    LDFLAGS +=-Wl,-warn-common
+    LDFLAGS +=-Wl,-warn-common -Wl,--no-undefined-version
     STRIPCMD:=/bin/true -Not_stripping_since_we_are_debugging
 else
     CFLAGS+=$(WARNINGS) $(OPTIMIZATIONS) -D_GNU_SOURCE -DNDEBUG
-    LDFLAGS += -Wl,-warn-common
-    STRIPCMD:=$(STRIP) -s --remove-section=.note --remove-section=.comment
+    LDFLAGS += -Wl,-warn-common -Wl,--sort-common
+    STRIPCMD:=$(STRIP) -s --remove-section=.note --remove-section=.comment \
+	--remove-section=.version
 endif
 ifeq ($(strip $(CONFIG_STATIC)),y)
-    LDFLAGS += --static
+    LDFLAGS += -static
+else
+    LIBRARIES += -ldl
+endif
+
+ifeq ($(strip $(CONFIG_BUILD_LIBRARIES)),y)
+#   CFLAGS += -fpic
 endif
 
 ifeq ($(strip $(PREFIX)),)
@@ -200,4 +210,19 @@ endif
 # have a chance of winning.
 CFLAGS += $(CFLAGS_EXTRA)
 
+# Rule to build shared objects
+%.so:
+	$(CC) -shared -Wl,-soname=$(@F) -Wl,-rpath=$@ \
+	-Wl,--enable-new-dtags \
+	-Wl,--reduce-memory-overheads \
+	-o $@ $^
+
+# Rule to build static objects
+%.a:
+	$(AR) $(ARFLAGS) $@ $^
+
+ifeq ($(CONFIG_BUILD_LIBRARIES),y)
+all: $(libraries-y) $(libraries-m)
+endif
+
 .PHONY: dummy
diff -X excl -rduNp busybox.oorig/sysdeps/linux/Config.in busybox/sysdeps/linux/Config.in
--- busybox.oorig/sysdeps/linux/Config.in	2005-09-02 13:35:47.000000000 +0200
+++ busybox/sysdeps/linux/Config.in	2005-09-05 10:04:33.336820430 +0200
@@ -164,6 +164,14 @@ config CONFIG_STATIC
 
 	  Most people will leave this set to 'N'.
 
+config CONFIG_BUILD_LIBRARIES
+	bool "Build libraries"
+	default y
+	help
+	  Build static as well as shared libs. If you disable this,
+	  you will not be able to peruse the BusyBox libraries for
+	  your applications.
+
 config CONFIG_LFS
 	bool "Build with Large File Support (for accessing files > 2 GB)"
 	default n



More information about the busybox mailing list