Create a dynamic library.
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 31 Mar 2019 13:32:42 +0000 (15:32 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 22 Apr 2019 14:53:32 +0000 (16:53 +0200)
As pointed out by Adam Borowski, static linking makes updates tedious.
This patch switches the package over to create a dynamic library
instead.

We have to specify -fPIC for compiling and --shared for linking. The
shared object is called liblopsub.so.X.Y.Z, where X, Y, and Z are
the digits of the version string as created from the git revision by
the version-gen.sh script. The soname for the linker to put into each
executable which is linked against lopsub is liblopsub.so.$X.

The install target is modified to install liblopsub.so.X.Y.Z instead
of liblopsub.a, and it also creates the usual symbolic links for
shared libraries. It does not run ldconfig, though.

Requested-by: Adam Borowski <kilobyte@angband.pl>
.gitignore
Makefile

index 3dc13ed7005a2c9beb6dc249caa28ba697395ca8..e7965bb360829380a3b1a5227b26c4c357a80f94 100644 (file)
@@ -5,7 +5,7 @@ config_file.c
 lopsubgen.c
 version.c
 lopsub.h
 lopsubgen.c
 version.c
 lopsub.h
-liblopsub.a
+liblopsub.*
 lopsubex
 lopsubgen
 lopsubgen-stage1
 lopsubex
 lopsubgen
 lopsubgen-stage1
index 7e44330b4ee335e75327c4c5318bbd234a5b112f..408e3a5ac123dff22cd744a1487e18de3ed53c64 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -23,9 +23,14 @@ ZCAT := zcat
 
 DATE := $(shell date '+%B %Y')
 GIT_VERSION := $(shell ./version-gen.sh)
 
 DATE := $(shell date '+%B %Y')
 GIT_VERSION := $(shell ./version-gen.sh)
+PLAIN_VERSION := $(firstword $(subst -, , $(GIT_VERSION)))
+MAJOR_VERSION := $(firstword $(subst ., , $(PLAIN_VERSION)))
+SONAME := liblopsub.so.$(MAJOR_VERSION)
+REALNAME := liblopsub.so.$(PLAIN_VERSION)
+LINKERNAME:=liblopsub.so
 
 m4_man_pages := lopsub-suite.5.gz lopsub.7.gz
 
 m4_man_pages := lopsub-suite.5.gz lopsub.7.gz
-all := $(m4_man_pages) liblopsub.a lopsubgen lopsubgen.1.gz \
+all := $(m4_man_pages) $(REALNAME) lopsubgen lopsubgen.1.gz \
        lopsubex lopsubex.1.gz
 all: $(all)
 
        lopsubex lopsubex.1.gz
 all: $(all)
 
@@ -64,25 +69,25 @@ lopsubgen.lsg.c lopsubgen.lsg.h: lopsubgen.suite lopsubgen-stage1 \
 lsg1_objs := lopsubgen.o lsg1.o version.o
 lsg_objs := lopsubgen.o lsg.o lopsubgen.lsg.o lopsub.o version.o
 liblopsub_objs := config_file.o lopsub.o version.o
 lsg1_objs := lopsubgen.o lsg1.o version.o
 lsg_objs := lopsubgen.o lsg.o lopsubgen.lsg.o lopsub.o version.o
 liblopsub_objs := config_file.o lopsub.o version.o
-lopsubex_objs := lopsubex.o lopsubex.lsg.o version.o
+lopsubex_objs := lopsubex.o lopsubex.lsg.o $(liblopsub_objs)
 
 $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c
 lopsubgen.o config_file.o:
 
 $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c
 lopsubgen.o config_file.o:
-       $(CC) -g -c -o $@ ${@:.o=.c}
+       $(CC) -g -c -fPIC -o $@ ${@:.o=.c}
 lsg1.o: lsg.c lsg.h
        $(CC) -g -DSTAGE1 -Wall -g -c $< -o $@
 %.o: %.c
 lsg1.o: lsg.c lsg.h
        $(CC) -g -DSTAGE1 -Wall -g -c $< -o $@
 %.o: %.c
-       $(CC) -Wall -I. -g -c -o $@ $<
+       $(CC) -Wall -I. -g -c -fPIC -o $@ $<
 
 # linking
 lopsubgen-stage1: $(lsg1_objs)
        $(CC) -Wall -g $(lsg1_objs) -o $@
 lopsubgen: $(lsg_objs)
        $(CC) -Wall -g -o $@ $(lsg_objs)
 
 # linking
 lopsubgen-stage1: $(lsg1_objs)
        $(CC) -Wall -g $(lsg1_objs) -o $@
 lopsubgen: $(lsg_objs)
        $(CC) -Wall -g -o $@ $(lsg_objs)
-liblopsub.a: $(liblopsub_objs)
-       $(AR) -rcs $@ $^
-lopsubex: $(lopsubex_objs) liblopsub.a
-       $(CC) -Wall -g -o $@ $^
+$(REALNAME): $(liblopsub_objs)
+       $(CC) --shared -Wl,-soname,liblopsub.so.$(MAJOR_VERSION) -o $@ $^
+lopsubex: $(lopsubex_objs) $(REALNAME)
+       $(CC) -Wall -g -o $@ $(lopsubex_objs)
 
 # web
 html := $(addprefix web/, $(addsuffix .html, \
 
 # web
 html := $(addprefix web/, $(addsuffix .html, \
@@ -104,7 +109,9 @@ install: $(all)
        $(INSTALL) -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include \
                $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/share/man/man5 \
                $(DESTDIR)$(PREFIX)/share/man/man7 $(DESTDIR)$(PREFIX)/bin
        $(INSTALL) -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include \
                $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/share/man/man5 \
                $(DESTDIR)$(PREFIX)/share/man/man7 $(DESTDIR)$(PREFIX)/bin
-       $(INSTALL) -m 644 liblopsub.a $(DESTDIR)$(PREFIX)/lib
+       $(INSTALL) -m 644 $(REALNAME) $(DESTDIR)$(PREFIX)/lib
+       $(LN) -s $(REALNAME) $(DESTDIR)$(PREFIX)/lib/$(SONAME)
+       $(LN) -s $(SONAME) $(DESTDIR)$(PREFIX)/lib/$(LINKERNAME)
        $(INSTALL) -m 755 lopsubgen $(DESTDIR)$(PREFIX)/bin
        $(INSTALL) -m 644 lopsub.h $(DESTDIR)$(PREFIX)/include
        $(INSTALL) -m 644 lopsub-internal.h $(DESTDIR)$(PREFIX)/include
        $(INSTALL) -m 755 lopsubgen $(DESTDIR)$(PREFIX)/bin
        $(INSTALL) -m 644 lopsub.h $(DESTDIR)$(PREFIX)/include
        $(INSTALL) -m 644 lopsub-internal.h $(DESTDIR)$(PREFIX)/include