macOS port: .dylib support in Makefile and ffi.ss

ober

2e20093b845f22b237fc3f03dc676ac561e1efc6

diff --git a/Makefile b/Makefile
index a7adc18..040e0c6 100644
--- a/Makefile
+++ b/Makefile
@@ -6,18 +6,28 @@ CC       ?= gcc
 CFLAGS   ?= -O2 -fPIC -Wall
 SCHEME   ?= scheme
 
-export CHEZ_PCRE2_LIB := $(CURDIR)
-export LD_LIBRARY_PATH := $(PCRE2_LIBDIR):$(LD_LIBRARY_PATH)
+UNAME_S := $(shell uname -s)
+ifeq ($(UNAME_S),Darwin)
+  SHLIB_EXT   := dylib
+  SHLIB_FLAGS := -dynamiclib -Wl,-undefined,dynamic_lookup
+  export CHEZ_PCRE2_LIB := $(CURDIR)
+  export DYLD_LIBRARY_PATH := $(PCRE2_LIBDIR):$(CURDIR):$(DYLD_LIBRARY_PATH)
+else
+  SHLIB_EXT   := so
+  SHLIB_FLAGS := -shared
+  export CHEZ_PCRE2_LIB := $(CURDIR)
+  export LD_LIBRARY_PATH := $(PCRE2_LIBDIR):$(LD_LIBRARY_PATH)
+endif
 
 .PHONY: all clean test
 
-all: pcre2_shim.so
+all: pcre2_shim.$(SHLIB_EXT)
 
-pcre2_shim.so: pcre2_shim.c
-	$(CC) $(CFLAGS) $(PCRE2_CFLAGS) -shared -o $@ $< $(PCRE2_LIBS)
+pcre2_shim.$(SHLIB_EXT): pcre2_shim.c
+	$(CC) $(CFLAGS) $(SHLIB_FLAGS) $(PCRE2_CFLAGS) -o $@ $< $(PCRE2_LIBS)
 
-test: pcre2_shim.so
+test: pcre2_shim.$(SHLIB_EXT)
 	$(SCHEME) --libdirs . --script pcre2-test.ss
 
 clean:
-	rm -f pcre2_shim.so
+	rm -f pcre2_shim.so pcre2_shim.dylib
diff --git a/chez-pcre2/ffi.ss b/chez-pcre2/ffi.ss
index f5b3bda..7ac1c3e 100644
--- a/chez-pcre2/ffi.ss
+++ b/chez-pcre2/ffi.ss
@@ -58,15 +58,23 @@
 
   (import (chezscheme))
 
+  ;; Detect macOS by checking machine-type suffix ("osx").
+  (define shlib-ext
+    (let ([mt (symbol->string (machine-type))])
+      (if (and (>= (string-length mt) 3)
+               (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx"))
+          "dylib"
+          "so")))
+
   ;; Load the C shim shared library.
-  ;; Set CHEZ_PCRE2_LIB to the directory containing pcre2_shim.so,
+  ;; Set CHEZ_PCRE2_LIB to the directory containing pcre2_shim.so/.dylib,
   ;; or place it in the current directory or a system library path.
   (define shim-loaded
     (load-shared-object
       (let ([env (getenv "CHEZ_PCRE2_LIB")])
         (if env
-            (format "~a/pcre2_shim.so" env)
-            "pcre2_shim.so"))))
+            (string-append env "/pcre2_shim." shlib-ext)
+            (string-append "pcre2_shim." shlib-ext)))))
 
   ;; -----------------------------------------------------------------------
   ;; Constants — fetched once from C at load time