tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: "Sören Tempel" <soeren@soeren-tempel.net>
To: tech@mandoc.bsd.lv
Subject: [PATCH] Optionally allow building libmandoc as a shared library
Date: Sat, 25 Sep 2021 10:58:40 +0200	[thread overview]
Message-ID: <2L1YC2WD8JUE5.3QGU3CQ10EGCD@8pit.net> (raw)

Hi,

While upgrading the Alpine mandoc aport to 1.14.6, I encountered a patch
we are applying for mandoc which allows building libmandoc as a shared
library. Alpine is also targeting constrained embedded systems and thus
tries to reduce packages sizes whenever feasible. Building libmandoc as
a shared library allows reducing the size of mandoc quite a bit as
mandoc ships five binaries (mandoc, man.cgi, mandocd, catman, and
demandoc) which use this library. At Alpine, we only package mandoc and
demandoc and even which just two of the five binaries using a shared
library reduces the package size by roughly 50% from 520 KiB to 1104 KiB.

I cleaned up our patch a bit and made shared/static linking of libmandoc
configurable via a STATIC_LIB configuration variable which is can be set
in configure.local. By default, static linking continues to be used.
STATIC_LIB!=1 implies dynamic linking, causes the code to be compiled
with -fPIC, and forces INSTALL_LIBMANDOC=1. I understand that package
sizes are not a priority in the common case, and that static linking is
probably fine in most cases, but since the patch is relatively
non-invasive I wanted to suggest it for upstream inclusion. No hard
feelings if you are not interested in this patch, we can just continue
applying it downstream in that case.

On a side note, the patch links dynamically by just passing $(LIBMANDOC)
(i.e. libmandoc.so) to the linker, this seems to work with GCC 10.X
though using `-L. -lmandoc` instead might be more portable and will
also work with libmandoc.a.

Greetings,
Sören

diff -upr a/Makefile b/Makefile
--- a/Makefile	2021-09-23 20:03:23.000000000 +0200
+++ b/Makefile	2021-09-24 16:42:21.890918029 +0200
@@ -392,7 +392,7 @@ distclean: clean
 	rm -f Makefile.local config.h config.h.old config.log config.log.old
 
 clean:
-	rm -f libmandoc.a $(LIBMANDOC_OBJS) $(ALL_COBJS)
+	rm -f $(LIBMANDOC) $(LIBMANDOC_OBJS) $(ALL_COBJS)
 	rm -f mandoc man $(MAIN_OBJS)
 	rm -f man.cgi $(CGI_OBJS)
 	rm -f mandocd catman catman.o $(MANDOCD_OBJS)
@@ -432,11 +432,11 @@ base-install: mandoc demandoc soelim
 	$(INSTALL_MAN) makewhatis.8 \
 		$(DESTDIR)$(MANDIR)/man8/$(BINM_MAKEWHATIS).8
 
-lib-install: libmandoc.a
+lib-install: $(LIBMANDOC)
 	mkdir -p $(DESTDIR)$(LIBDIR)
 	mkdir -p $(DESTDIR)$(INCLUDEDIR)
 	mkdir -p $(DESTDIR)$(MANDIR)/man3
-	$(INSTALL_LIB) libmandoc.a $(DESTDIR)$(LIBDIR)
+	$(INSTALL_LIB) $(LIBMANDOC) $(DESTDIR)$(LIBDIR)
 	$(INSTALL_LIB) eqn.h man.h mandoc.h mandoc_aux.h mandoc_parse.h \
 		mdoc.h roff.h tbl.h $(DESTDIR)$(INCLUDEDIR)
 	$(INSTALL_MAN) mandoc.3 mandoc_escape.3 mandoc_malloc.3 \
@@ -514,24 +514,26 @@ Makefile.local config.h: configure $(TES
 
 libmandoc.a: $(MANDOC_COBJS) $(LIBMANDOC_OBJS)
 	$(AR) rs $@ $(MANDOC_COBJS) $(LIBMANDOC_OBJS)
+libmandoc.so: $(MANDOC_COBJS) $(LIBMANDOC_OBJS)
+	$(CC) $(LDFLAGS) -shared -o $@ $(MANDOC_COBJS) $(LIBMANDOC_OBJS) $(LDADD)
 
-mandoc: $(MAIN_OBJS) libmandoc.a
-	$(CC) -o $@ $(LDFLAGS) $(MAIN_OBJS) libmandoc.a $(LDADD)
+mandoc: $(MAIN_OBJS) $(LIBMANDOC)
+	$(CC) -o $@ $(LDFLAGS) $(MAIN_OBJS) $(LIBMANDOC) $(LDADD)
 
 man: mandoc
 	$(LN) mandoc man
 
-man.cgi: $(CGI_OBJS) libmandoc.a
-	$(CC) $(STATIC) -o $@ $(LDFLAGS) $(CGI_OBJS) libmandoc.a $(LDADD)
+man.cgi: $(CGI_OBJS) $(LIBMANDOC)
+	$(CC) $(STATIC) -o $@ $(LDFLAGS) $(CGI_OBJS) $(LIBMANDOC) $(LDADD)
 
-mandocd: $(MANDOCD_OBJS) libmandoc.a
-	$(CC) -o $@ $(LDFLAGS) $(MANDOCD_OBJS) libmandoc.a $(LDADD)
+mandocd: $(MANDOCD_OBJS) $(LIBMANDOC)
+	$(CC) -o $@ $(LDFLAGS) $(MANDOCD_OBJS) $(LIBMANDOC) $(LDADD)
 
-catman: catman.o libmandoc.a
-	$(CC) -o $@ $(LDFLAGS) catman.o libmandoc.a $(LDADD)
+catman: catman.o $(LIBMANDOC)
+	$(CC) -o $@ $(LDFLAGS) catman.o $(LIBMANDOC) $(LDADD)
 
-demandoc: $(DEMANDOC_OBJS) libmandoc.a
-	$(CC) -o $@ $(LDFLAGS) $(DEMANDOC_OBJS) libmandoc.a $(LDADD)
+demandoc: $(DEMANDOC_OBJS) $(LIBMANDOC)
+	$(CC) -o $@ $(LDFLAGS) $(DEMANDOC_OBJS) $(LIBMANDOC) $(LDADD)
 
 soelim: $(SOELIM_COBJS) soelim.o
 	$(CC) -o $@ $(LDFLAGS) $(SOELIM_COBJS) soelim.o
diff -upr a/configure b/configure
--- a/configure	2021-09-23 20:03:23.000000000 +0200
+++ b/configure	2021-09-24 17:29:03.942587555 +0200
@@ -51,10 +51,12 @@ LD_NANOSLEEP=
 LD_OHASH=
 LD_RECVMSG=
 STATIC=
+STATIC_LIB=1
 
 BUILD_CGI=0
 BUILD_CATMAN=0
 INSTALL_LIBMANDOC=0
+LIBMANDOC=
 
 HAVE_ATTRIBUTE=
 HAVE_CMSG=
@@ -276,6 +278,14 @@ elif [ ${HAVE_WFLAG} -eq 0 ]; then
 else
 	CFLAGS="${DEFCFLAGS}"
 fi
+if [ "${STATIC_LIB}" -eq 1 ]; then
+	LIBMANDOC=libmandoc.a
+else
+	CFLAGS="${CFLAGS} -fPIC"
+	INSTALL_LIBMANDOC=1
+	LIBMANDOC=libmandoc.so
+	
+fi
 echo "selected CFLAGS=\"${CFLAGS}\"" 1>&2
 echo "selected CFLAGS=\"${CFLAGS}\"" 1>&3
 echo 1>&3
@@ -649,6 +659,7 @@ SBINDIR		= ${SBINDIR}
 BIN_FROM_SBIN	= ${BIN_FROM_SBIN}
 INCLUDEDIR	= ${INCLUDEDIR}
 LIBDIR		= ${LIBDIR}
+LIBMANDOC	= ${LIBMANDOC}
 MANDIR		= ${MANDIR}
 WWWPREFIX	= ${WWWPREFIX}
 HTDOCDIR	= ${HTDOCDIR}
--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv


                 reply	other threads:[~2021-09-25  8:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2L1YC2WD8JUE5.3QGU3CQ10EGCD@8pit.net \
    --to=soeren@soeren-tempel.net \
    --cc=tech@mandoc.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).