[hackers] [libgrapheme] Enhance build-system to perfectly support OpenBSD and macOS || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Sat, 8 Oct 2022 10:46:42 +0200 (CEST)

commit a6b3a194f0381c5aef9346d39b02eb058111d2a2
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Sat Oct 8 10:40:03 2022 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Sat Oct 8 10:46:31 2022 +0200

    Enhance build-system to perfectly support OpenBSD and macOS
    
    Studying the source material on OpenBSD[0], it is written that
    
            Quite a few ports need tweaks to build shared libraries correctly
            anyways. Remember that building shared libraries should be done with
    
                    $ cc -shared -fpic|-fPIC -o libfoo.so.4.5 obj1 obj2
    
            Trying to rename the library after the fact to adjust the version
            number does not work: ELF libraries use some extra magic to set the
            library internal name, so you must link it with the correct version
            the first time.
    
    Thus, it is necessary to directly compile into $(SONAME), which is
    changed to in this commit.
    
    The magic flags for macOS were taken from [1]. It sets up the linker
    such that it automatically respects semantic versioning and will load
    any library with a smaller compatible version (e.g. same minor-version).
    
    Additionally, both OpenBSD and macOS have smarter linkers than Linux
    and don't need symlinks from varying versions to work right. Thus a
    flag SOSYMLINK was added to enable toggling this from the config.mk.
    
    For convenience, the best-practices for each platform are added to
    the config.mk in a commented-out form, saving everybody some time.
    
    [0]:https://www.openbsd.org/faq/ports/specialtopics.html#SharedLibs
    [1]:https://begriffs.com/posts/2021-07-04-shared-libraries.html#linking
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/Makefile b/Makefile
index 755a4e1..a4ded5b 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -99,7 +99,7 @@ MAN3 =\
 MAN7 =\
         man/libgrapheme\
 
-all: data/LICENSE $(MAN3:=.3) $(MAN7:=.7) libgrapheme.a libgrapheme.so
+all: data/LICENSE $(MAN3:=.3) $(MAN7:=.7) libgrapheme.a $(SONAME)
 
 data/DerivedCoreProperties.txt:
         wget -O $_AT_ https://www.unicode.org/Public/$(UNICODE_VERSION)/ucd/DerivedCoreProperties.txt
_AT_@ -260,7 +260,7 @@ libgrapheme.a: $(SRC:=.o)
         $(AR) -rc $_AT_ $?
         $(RANLIB) $_AT_
 
-libgrapheme.so: $(SRC:=.o)
+$(SONAME): $(SRC:=.o)
         $(CC) -o $_AT_ $(SOFLAGS) $(LDFLAGS) $(SRC:=.o)
 
 $(MAN3:=.3):
_AT_@ -283,10 +283,10 @@ install: all
         cp -f $(MAN3:=.3) "$(DESTDIR)$(MANPREFIX)/man3"
         cp -f $(MAN7:=.7) "$(DESTDIR)$(MANPREFIX)/man7"
         cp -f libgrapheme.a "$(DESTDIR)$(LIBPREFIX)"
- cp -f libgrapheme.so "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION)"
- i=0; while [ "$$i" -le $(VERSION_MINOR) ]; do ln -sf "libgrapheme.so.$(VERSION)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR).$$i"; i=$$((i+1)); done
- ln -sf "libgrapheme.so.$(VERSION)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR)"
- ln -sf "libgrapheme.so.$(VERSION)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so"
+ cp -f $(SONAME) "$(DESTDIR)$(LIBPREFIX)/$(SONAME)"
+ if [ "$(SOSYMLINK)" = "true" ]; then i=0; while [ "$$i" -le $(VERSION_MINOR) ]; do ln -sf "$(SONAME)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR).$$i"; i=$$((i+1)); done; fi
+ if [ "$(SOSYMLINK)" = "true" ]; then ln -sf "$(SONAME)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR)"; fi
+ if [ "$(SOSYMLINK)" = "true" ]; then ln -sf "$(SONAME)" "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so"; fi
         cp -f grapheme.h "$(DESTDIR)$(INCPREFIX)"
         $(LDCONFIG)
 
_AT_@ -294,15 +294,15 @@ uninstall:
         for m in $(MAN3:=.3); do rm -f "$(DESTDIR)$(MANPREFIX)/man3/`basename $$m`"; done
         for m in $(MAN7:=.7); do rm -f "$(DESTDIR)$(MANPREFIX)/man7/`basename $$m`"; done
         rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.a"
- rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION)"
- i=0; while [ "$$i" -le $(VERSION_MINOR) ]; do rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR).$$i"; i=$$((i+1)); done
- rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR)"
- rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so"
+ rm -f "$(DESTDIR)$(LIBPREFIX)/$(SONAME)"
+ if [ "$(SOSYMLINK)" = "true" ]; then i=0; while [ "$$i" -le $(VERSION_MINOR) ]; do rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR).$$i"; i=$$((i+1)); done; fi
+ if [ "$(SOSYMLINK)" = "true" ]; then rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so.$(VERSION_MAJOR)"; fi
+ if [ "$(SOSYMLINK)" = "true" ]; then rm -f "$(DESTDIR)$(LIBPREFIX)/libgrapheme.so"; fi
         rm -f "$(DESTDIR)$(INCPREFIX)/grapheme.h"
         $(LDCONFIG)
 
 clean:
- rm -f $(BENCHMARK:=.o) benchmark/util.o $(BENCHMARK) $(GEN:=.h) $(GEN:=.o) gen/util.o $(GEN) $(SRC:=.o) src/util.o $(TEST:=.o) test/util.o $(TEST) libgrapheme.a libgrapheme.so $(MAN3:=.3) $(MAN7:=.7)
+ rm -f $(BENCHMARK:=.o) benchmark/util.o $(BENCHMARK) $(GEN:=.h) $(GEN:=.o) gen/util.o $(GEN) $(SRC:=.o) src/util.o $(TEST:=.o) test/util.o $(TEST) libgrapheme.a $(SONAME) $(MAN3:=.3) $(MAN7:=.7)
 
 clean-data:
         rm -f $(DATA)
diff --git a/config.mk b/config.mk
index cc8a903..32829c3 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -15,8 +15,23 @@ BUILD_CPPFLAGS = $(CPPFLAGS)
 BUILD_CFLAGS = $(CFLAGS)
 BUILD_LDFLAGS = $(LDFLAGS)
 
-SHFLAGS = -fPIC -ffreestanding
-SOFLAGS = -shared -nostdlib -Wl,--soname=libgrapheme.so.$(VERSION_MAJOR).$(VERSION_MINOR)
+SHFLAGS = -fPIC -ffreestanding
+
+SOFLAGS = -shared -nostdlib -Wl,--soname=libgrapheme.so.$(VERSION_MAJOR).$(VERSION_MINOR)
+SONAME = libgrapheme.so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
+SOSYMLINK = true
+
+# -- OpenBSD -- (also unset LDCONFIG)
+# SOFLAGS = -shared -nostdlib
+# SONAME = libgrapheme.so.$(VERSION_MAJOR).$(VERSION_MINOR)
+# SOSYMLINK = false
+
+# -- macOS -- (also unset LDCONFIG)
+# SOFLAGS = -dynamiclib -install_name "libgrapheme.$(VERSION_MAJOR).dylib" \
+# -current_version "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)" \
+# -compatibility_version "$(VERSION_MAJOR).$(VERSION_MINOR).0"
+# SONAME = libgrapheme.$(VERSION_MAJOR).dylib
+# SOSYMLINK = false
 
 # tools
 CC = cc
Received on Sat Oct 08 2022 - 10:46:42 CEST

This archive was generated by hypermail 2.3.0 : Sat Oct 08 2022 - 10:48:37 CEST