From 1ee3aa818daf20ba1bd2fd7edc1a2c1811035a57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lawrence=20Vel=C3=A1zquez?= <vq@larryv.me>
Date: Thu, 3 Jun 2021 14:37:57 -0400
Subject: [PATCH 3/6] Index common symbols in static archives in Mac build

On macOS, ar(1) does not include common symbols in static archives'
tables of contents, causing this build to fail at link time. Update the
tables with ranlib(1), as described in the libtool(1) man page
(excerpted from macOS 10.14.6):

    The following options pertain to the table of contents for an
    archive library, and apply to both `libtool -static` and `ranlib`:

    [...]

    -c  Include common symbols as definitions with respect to the table
        of contents. This is seldom the intended behavior for linking
        from a library, as it forces the linking of a library member
        just because it uses an uninitialized global that is undefined
        at that point in the linking. This option is included only
        because this was the original behavior of `ranlib`. This option
        is not the default.

(Forcibly loading all members using `ld -all_load` also works but seems
excessive.)
---
 icmake/bootstrap/functions | 6 ++++++
 icmake/icm_bootstrap       | 3 +++
 icmake/rss/icm_bootstrap   | 5 +++++
 3 files changed, 14 insertions(+)

diff --git a/icmake/bootstrap/functions b/icmake/bootstrap/functions
index f6bc90f..3b1e7d7 100644
--- a/icmake/bootstrap/functions
+++ b/icmake/bootstrap/functions
@@ -37,6 +37,12 @@ build()
 
     try ar crs ../tmp/lib${program}.a */*.o
 
+    if [ "$UNAME" = Darwin ] ; then
+        # ensure common symbols are in table of contents, or linking fails
+        echo -n .
+        try ranlib -c ../tmp/lib${program}.a
+    fi
+
     echo -n .
 
     try ${CC} -o ../tmp/${LIBDIR}/${program} *.o -l${program} -licrss \
diff --git a/icmake/icm_bootstrap b/icmake/icm_bootstrap
index 9a80252..3cc1b8b 100755
--- a/icmake/icm_bootstrap
+++ b/icmake/icm_bootstrap
@@ -20,6 +20,9 @@ fi
 . bootstrap/functions
 . tmp/INSTALL.sh
 
+# save OS name to check for Mac (Darwin) later
+UNAME=`uname`
+export UNAME
 
 # build icmake and its support programs
 for target in rss icmake un pp comp exec dep icmbuild
diff --git a/icmake/rss/icm_bootstrap b/icmake/rss/icm_bootstrap
index a238c34..ab8455d 100755
--- a/icmake/rss/icm_bootstrap
+++ b/icmake/rss/icm_bootstrap
@@ -9,6 +9,11 @@ echo -n .
 try ${CC} -c ${CFLAGS} *.c
 echo -n .
 try ar rs ../tmp/libicrss.a *.o
+if [ "$UNAME" = Darwin ] ; then
+    # ensure common symbols are in table of contents, or linking fails
+    echo -n .
+    try ranlib -c ../tmp/libicrss.a
+fi
 echo -n .
 rm *.o
 echo .
-- 
2.31.1

