Re: Compile sndio as static library

From: Alexandre Ratchov <alex_at_caoua.org>
Date: Fri, 15 Mar 2024 17:25:05 +0100
On Wed, Dec 13, 2023 at 04:31:07PM +0800, Fuzhou Chen wrote:
> Hi Jan,
> 
> It does. See source code:
> https://github.com/ratchov/sndio/blob/3f8bdefa5b4a63915ddd8335f01308756e021a4d/libsndio/sio_alsa.c#L32
> I also attached my readelf and ldd output when querying
> /usr/lib/libsndio.so.7.2, on the Manjaro unstable branch. You can see it
> depends on libasound.so.2, which is part of alsa-lib package.
> 
> If you are running on other systems, it's also possible that the system
> distributed libsndio.so is built using other backends, maybe OSS?
> 
> Actually, the different output from your machine and mine shows a perfect
> example to support my previous scenario description, that why a game
> developer should not depend on OS provided libraries.
> 
> Thanks,
> Fuzhou
> 
> ==== Output of library needed ====
> fuzhouch_at_fcdvh01 ~/p/c/s/libsndio (feature-staticlib)> readelf -a -W
> /usr/lib/libsndio.so.7.2 |grep NEEDED
>  0x0000000000000001 (NEEDED)             Shared library: [libasound.so.2]
>  0x0000000000000001 (NEEDED)             Shared library: [libbsd.so.0]
>  0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
> 
> fuzhouch_at_fcdvh01 ~/p/c/s/libsndio (feature-staticlib)> ldd
> /usr/lib/libsndio.so.7.2
> linux-vdso.so.1 (0x00007ffc93d26000)
> libasound.so.2 => /usr/lib/libasound.so.2 (0x00007f318171f000)
> libbsd.so.0 => /usr/lib/libbsd.so.0 (0x00007f3181709000)
> libc.so.6 => /usr/lib/libc.so.6 (0x00007f3181527000)
> libm.so.6 => /usr/lib/libm.so.6 (0x00007f318143a000)
> /usr/lib64/ld-linux-x86-64.so.2 (0x00007f3181851000)
> libmd.so.0 => /usr/lib/libmd.so.0 (0x00007f318142a000)
> 
> 

Hi,

Sorry for the delay. Here's a diff to select between dynamic and
static libraries, with the minimal ./configure bits. Could you confirm
it works for you?

Is so and nobody objects, I'll commit this change.

diff --git a/configure b/configure
index 6aef806..6f768e2 100755
--- a/configure
+++ b/configure
_at_@ -24,6 +24,8 @@ Usage: configure [options]
 --disable-rmidi			disable character device midi backend
 --enable-umidi			enable usb-midi backend [$umidi]
 --disable-umidi			disable usb-midi backend
+--enable-dynamic		build the dynamic library [$dynamic]
+--enable-static			build the static library [$static]
 --with-libbsd			use the libbsd rather than bsd-compat/*
 --without-libbsd		don't use libbsd
 --default-dev=DEV		set default device [$dev]
_at_@ -36,11 +38,15 @@ END
 version=1.9.0				# package version (used by pkg-config)
 prefix=/usr/local			# where to install sndio
 so="libsndio.so.\${MAJ}.\${MIN}"	# shared libs to build
+so_cflags="-fPIC"			# clags to build shared objects
+static_lib="libsndio.a"			# static libs to build
 alsa=no					# do we want alsa support ?
 sun=no					# do we want sun support ?
 oss=no					# do we want oss support ?
 rmidi=no				# do we want support for raw char dev ?
 umidi=no				# do we want support for umidi ?
+dynamic=yes				# do we build libsndio.so and links
+static=no				# do we build libsndio.a
 precision=16				# sndiod default device bit-depth
 user=_sndio				# non-privileged user for sndio daemon
 libbsd=no				# use libbsd?
_at_@ -63,7 +69,7 @@ unset dev
 case `uname` in
 	Linux)
 		alsa=yes
-		ldadd="-lrt"
+		ldadd="-lrt -lm"
 		user=sndiod
 		so_link="libsndio.so libsndio.so.\${MAJ} libsndio.so.\${MAJ}.0"
 		so_ldflags="-Wl,-soname=libsndio.so.\${MAJ}"
_at_@ -71,7 +77,7 @@ case `uname` in
 		;;
 	GNU/kFreeBSD) # OSS output support on kFreeBSD, but otherwise like linux
 		oss=yes
-		ldadd="-lrt"
+		ldadd="-lrt -lm"
 		user=sndiod
 		so_ldflags="-Wl,-soname=libsndio.so.\${MAJ}"
 		so_link="libsndio.so libsndio.so.\${MAJ} libsndio.so.\${MAJ}.0"
_at_@ -80,7 +86,7 @@ case `uname` in
 		;;
 	GNU) # No output support on Hurd, but otherwise like linux
 		oss=no
-		ldadd="-lrt"
+		ldadd="-lrt -lm"
 		user=sndiod
 		so_ldflags="-Wl,-soname=libsndio.so.\${MAJ}"
 		so_link="libsndio.so libsndio.so.\${MAJ} libsndio.so.\${MAJ}.0"
_at_@ -155,6 +161,14 @@ for i; do
 	--mandir=*)
 		mandir="${i#--mandir=}"
 		shift;;
+	--enable-dynamic)
+		dynamic=yes
+		static=no
+		shift;;
+	--enable-static)
+		static=yes
+		dynamic=no
+		shift;;
 	--enable-alsa)
 		alsa=yes
 		shift;;
_at_@ -204,7 +218,7 @@ for i; do
 	--default-dev=*)
 		dev="${i#--default-dev=}"
 		shift;;
-	CC=*|CFLAGS=*|LDFLAGS=*)
+	CC=*|CFLAGS=*|LDFLAGS=*|AR=*)
 		vars="$vars$i$nl"
 		shift;;
 	*)
_at_@ -225,6 +239,18 @@ libdir="${libdir:-$exec_prefix/lib}"
 pkgconfdir="${pkgconfdir:-$libdir/pkgconfig}"
 mandir="${mandir:-$prefix/share/man}"
 
+#
+# if we build the static library, exclude the dynamic one from the build
+#
+if [ $static = yes ]; then
+	so=
+	so_link=
+	so_cflags=
+	so_ldflags=
+else
+	static_lib=
+fi
+
 #
 # umidi implies rmidi
 #
_at_@ -237,7 +263,7 @@ fi
 #
 if [ $alsa = yes ]; then
 	defs="$defs -DUSE_ALSA"
-	ldadd="$ldadd -lasound"
+	ldadd="-lasound $ldadd"
 fi
 
 #
_at_@ -301,7 +327,9 @@ do
 	-e "s:_at_ldadd@:$ldadd:" \
 	-e "s:_at_so@:$so:" \
 	-e "s:_at_so_link@:$so_link:" \
+	-e "s:_at_so_cflags@:$so_cflags:" \
 	-e "s:_at_so_ldflags@:$so_ldflags:" \
+	-e "s:_at_static_lib@:$static_lib:" \
 	-e "s:_at_vars@:${vars}:" \
 	-e "s:_at_precision@:$precision:" \
 	-e "s:_at_user@:$user:" \
_at_@ -347,6 +375,7 @@ oss...................... $oss
 sun...................... $sun
 rmidi.................... $rmidi
 umidi.................... $umidi
+static................... $static
 
 Do "make && make install" to compile and install sndio
 
diff --git a/libsndio/Makefile.in b/libsndio/Makefile.in
index 6225544..45fa895 100644
--- a/libsndio/Makefile.in
+++ b/libsndio/Makefile.in
_at_@ -11,7 +11,7 @@ DEFS = -DDEBUG @defs@
 LDADD = _at_ldadd@
 
 # extra compiler flags to produce objects for shared library
-SO_CFLAGS = -fPIC
+SO_CFLAGS = _at_so_cflags@
 
 # extra compiler flags to produce a shared library with the given name
 SO_LDFLAGS = -shared _at_so_ldflags@
_at_@ -50,8 +50,9 @@ MAJ = 7
 MIN = 2
 SO = _at_so@
 SO_LINK = _at_so_link@
+STATIC_LIB = _at_static_lib@
 
-all:		${SO}
+all:		${STATIC_LIB} ${SO}
 		for i in ${SO_LINK}; do ln -sf ${SO} $$i; done
 
 install:
_at_@ -61,7 +62,7 @@ install:
 		mkdir -p ${DESTDIR}${MAN3_DIR}
 		mkdir -p ${DESTDIR}${MAN7_DIR}
 		cp sndio.h ${DESTDIR}${INCLUDE_DIR}
-		cp -R ${SO} ${SO_LINK} ${DESTDIR}${LIB_DIR}
+		cp -R ${STATIC_LIB} ${SO} ${SO_LINK} ${DESTDIR}${LIB_DIR}
 		cp sndio.pc ${DESTDIR}${PKGCONF_DIR}
 		cp sio_open.3 ${DESTDIR}${MAN3_DIR}
 		ln -sf sio_open.3 ${DESTDIR}${MAN3_DIR}/sio_close.3
_at_@ -101,13 +102,13 @@ install:
 
 uninstall:
 		rm -f ${DESTDIR}${INCLUDE_DIR}/sndio.h
-		cd ${DESTDIR}${LIB_DIR} && rm -f ${SO} ${SO_LINK}
+		cd ${DESTDIR}${LIB_DIR} && rm -f ${STATIC_LIB} ${SO} ${SO_LINK}
 		cd ${DESTDIR}${PKGCONF_DIR} && rm -f sndio.pc
 		cd ${DESTDIR}${MAN3_DIR} && rm -f ${MAN3}
 		cd ${DESTDIR}${MAN7_DIR} && rm -f ${MAN7}
 
 clean:
-		rm -f -- ${SO} ${SO_LINK} *.o
+		rm -f -- ${STATIC_LIB} ${SO} ${SO_LINK} *.o
 
 # ---------------------------------------------------------- dependencies ---
 
_at_@ -127,6 +128,9 @@ getpeereid.o issetugid.o strlcat.o strlcpy.o strtonum.o clock_gettime.o
 ${SO}:		${OBJS}
 		${CC} ${LDFLAGS} ${SO_CFLAGS} ${SO_LDFLAGS} -o ${SO} ${OBJS} ${LDADD}
 
+libsndio.a:	${OBJS}
+		${AR} rcs libsndio.a ${OBJS}
+
 getpeereid.o:	../bsd-compat/getpeereid.c
 		${CC} ${CFLAGS} ${SO_CFLAGS} ${INCLUDE} ${DEFS} -c -o getpeereid.o ../bsd-compat/getpeereid.c
 
Received on Fri Mar 15 2024 - 17:25:05 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 16 2024 - 01:30:01 CET