Add support for building macOS dylibs.

Build whiptcl and python modules as bundles, not dylibs.

Remove PIFLAGS and PLFLAGS variables because their contents was already part of
PCFLAGS and PLDFLAGS respectively.

Try to get LDFLAGS from python-config using --embed first, as required by Python
3.8 and later. Fall back to not using --embed for Python 3.7 and earlier.

Don't silently ignore build/install errors.

Create install-py and install-tcl targets to install just those parts.

Install python modules to the correct platlib and purelib directories.

TODO: PR URLs
--- Makefile.in.orig	2023-10-25 05:21:25.000000000 -0500
+++ Makefile.in	2024-04-25 22:01:35.000000000 -0500
@@ -7,16 +7,22 @@
 LDFLAGS = @LDFLAGS@
 CPPFLAGS = -D_GNU_SOURCE @CPPFLAGS@
 GNU_LD = @GNU_LD@
+UNAME_S := $(shell uname -s)
 
 VERSION = @VERSION@
 TAG = r$(subst .,-,$(VERSION))
 SONAME = @SONAME@
+ifeq ($(UNAME_S),Darwin)
+SOEXT = dylib
+else
 SOEXT = so
+endif
+BUNDLEEXT = so
 
 PYTHONVERS = @PYTHONVERS@
 WHIPTCLLIB = @WHIPTCLLIB@
 ifneq ($(WHIPTCLLIB),)
-WHIPTCLSO = $(WHIPTCLLIB).$(SOEXT)
+WHIPTCLSO = $(WHIPTCLLIB).$(BUNDLEEXT)
 else
 WHIPTCLSO =
 endif
@@ -26,17 +32,30 @@
 NDIALOGOBJS = whiptail.o dialogboxes.o
 WHIPTCLOBJS = shared/whiptcl.o shared/dialogboxes.o
 LIBNEWT = libnewt.a
+ifeq ($(UNAME_S),Darwin)
+SHARED = -dynamiclib
+BUNDLE = -bundle
+LIBNEWTSH = libnewt.$(VERSION).$(SOEXT)
+LIBNEWTSONAME = libnewt.$(SONAME).$(SOEXT)
+else
+SHARED = -shared
+BUNDLE = -shared
 LIBNEWTSH = libnewt.$(SOEXT).$(VERSION)
 LIBNEWTSONAME = libnewt.$(SOEXT).$(SONAME)
+endif
 LIBOBJS = newt.o button.o form.o checkbox.o entry.o label.o listbox.o \
           scrollbar.o textbox.o scale.o grid.o windows.o buttonbar.o \
 	  checkboxtree.o
 
+ifeq ($(UNAME_S),Darwin)
+SHLIBFLAGS= -install_name $(libdir)/$(LIBNEWTSONAME) -current_version $(VERSION) -compatibility_version $(SONAME)
+else
 ifneq ($(GNU_LD),)
 SHLIBFLAGS= -Wl,--version-script,newt.0.52.ver -Wl,-soname,$(LIBNEWTSONAME)
 else
 SHLIBFLAGS=
 endif
+endif
 
 SHCFLAGS = -fPIC
 
@@ -65,7 +84,7 @@
 TARGET=depend $(PROGS)
 endif
 
-all:	$(TARGET) _snack.$(SOEXT)
+all:	$(TARGET) _snack.$(BUNDLEEXT)
 
 test:	test.o $(LIBNEWT)
 	$(CC) -g -o test test.o $(LIBNEWT) $(LDFLAGS) $(LIBS)
@@ -82,8 +101,9 @@
 showkey:	showkey.o $(LIBNEWT)
 	$(CC) -g -o showkey showkey.o $(LIBNEWT) $(LDFLAGS) $(LIBS)
 
-_snack.$(SOEXT):   snack.c $(LIBNEWTSH)
-	@[ -n "$(PYTHONVERS)" ] && for ver in $(PYTHONVERS); do \
+_snack.$(BUNDLEEXT): snack.c $(LIBNEWTSH)
+ifneq ($(PYTHONVERS),)
+	@for ver in $(PYTHONVERS); do \
 		pyconfig=$$ver-config; \
 		if ! $$pyconfig --cflags > /dev/null 2>&1 && \
 				python-config --cflags > /dev/null 2>&1; then \
@@ -92,21 +112,20 @@
 		fi; \
 		mkdir -p $$ver; \
 		PCFLAGS=`$$pyconfig --cflags`; \
-		PIFLAGS=`$$pyconfig --includes`; \
-		PLDFLAGS=`$$pyconfig --ldflags`; \
-		PLFLAGS=`$$pyconfig --libs`; \
-		echo $(CC) $(SHCFLAGS) $(CFLAGS) $(CPPFLAGS) $$PIFLAGS $$PCFLAGS -c -o $$ver/snack.o snack.c; \
-		$(CC) $(SHCFLAGS) $(CFLAGS) $(CPPFLAGS) $$PIFLAGS $$PCFLAGS -c -o $$ver/snack.o snack.c; \
-		echo $(CC) --shared $(LDFLAGS) $$PLDFLAGS $$PLFLAGS -o $$ver/_snack.$(SOEXT) $$ver/snack.o -L.  -lnewt $(LIBS); \
-		$(CC) --shared $(LDFLAGS) $$PLDFLAGS $$PLFLAGS -o $$ver/_snack.$(SOEXT) $$ver/snack.o -L.  -lnewt $(LIBS); \
-	done || :
+		PLDFLAGS=`$$pyconfig --ldflags --embed || $$pyconfig --ldflags`; \
+		echo $(CC) $(SHCFLAGS) $(CFLAGS) $(CPPFLAGS) $$PCFLAGS -c -o $$ver/snack.o snack.c; \
+		$(CC) $(SHCFLAGS) $(CFLAGS) $(CPPFLAGS) $$PCFLAGS -c -o $$ver/snack.o snack.c; \
+		echo $(CC) $(BUNDLE) $(LDFLAGS) $$PLDFLAGS -o $$ver/_snack.$(BUNDLEEXT) $$ver/snack.o -L. -lnewt $(LIBS); \
+		$(CC) $(BUNDLE) $(LDFLAGS) $$PLDFLAGS -o $$ver/_snack.$(BUNDLEEXT) $$ver/snack.o -L. -lnewt $(LIBS); \
+	done
+endif
 	touch $@
 
 whiptail: $(NDIALOGOBJS) $(LIBNEWTSH)
 	$(CC) -g -o whiptail $(NDIALOGOBJS) -L. $(LDFLAGS) -lnewt $(LIBS) -lpopt
 
-whiptcl.$(SOEXT): $(WHIPTCLOBJS) $(LIBNEWTSH)
-	$(CC) -shared $(SHCFLAGS) $(LDFLAGS) -o whiptcl.$(SOEXT) $(WHIPTCLOBJS) -L. -lnewt  $(LIBTCL) -lpopt $(LIBS)
+$(WHIPTCLSO): $(WHIPTCLOBJS) $(LIBNEWTSH)
+	$(CC) $(BUNDLE) $(SHCFLAGS) $(LDFLAGS) -o $(WHIPTCLSO) $(WHIPTCLOBJS) -L. -lnewt $(LIBTCL) -lpopt $(LIBS)
 
 $(LIBNEWT): $(LIBOBJS)
 	ar rv $@ $^
@@ -118,7 +137,7 @@
 
 clean:
 	rm -f $(PROGS) *.o $(LIBNEWT) core $(LIBNEWTSH)  \
-		$(SHAREDDIR)/*.o *.$(SOEXT)*
+		$(SHAREDDIR)/*.o *.$(SOEXT)* *.$(BUNDLEEXT)
 
 depend:
 	$(CC) $(CFLAGS) $(CPPFLAGS) -M $(SOURCES) > .depend
@@ -126,7 +145,7 @@
 sharedlib: $(LIBNEWTSH)
 
 $(LIBNEWTSH): $(SHAREDOBJS)
-	$(CC) -shared -o $(LIBNEWTSH) $(SHLIBFLAGS) $(SHAREDOBJS) $(LDFLAGS) $(LIBS)
+	$(CC) $(SHARED) -o $(LIBNEWTSH) $(SHLIBFLAGS) $(SHAREDOBJS) $(LDFLAGS) $(LIBS)
 	ln -fs $(LIBNEWTSONAME) libnewt.$(SOEXT)
 	ln -fs $(LIBNEWTSH) $(LIBNEWTSONAME)
 
@@ -143,7 +162,7 @@
 	install -m 644 whiptail.1 $(instroot)/$(man1dir)
 	make -C po datadir=$(instroot)/$(datadir) install
 
-install-sh: sharedlib $(WHIPTCLSO) _snack.$(SOEXT)
+install-sh: sharedlib install-tcl install-py
 	[ -d $(instroot)/$(libdir) ] || install -m 755 -d $(instroot)/$(libdir)
 	[ -d $(instroot)/$(includedir) ] || install -m 755 -d $(instroot)/$(includedir)
 	[ -d $(instroot)/$(pkgconfigdir) ] || install -m 755 -d $(instroot)/$(pkgconfigdir)
@@ -151,14 +170,28 @@
 	install -m 755 $(LIBNEWTSH) $(instroot)/$(libdir)
 	ln -sf $(LIBNEWTSONAME) $(instroot)/$(libdir)/libnewt.$(SOEXT)
 	ln -sf $(LIBNEWTSH) $(instroot)/$(libdir)/$(LIBNEWTSONAME)
-	[ -n "$(WHIPTCLSO)" ] && install -m 755 whiptcl.$(SOEXT) $(instroot)/$(libdir) || :
-	[ -n "$(PYTHONVERS)" ] && for ver in $(PYTHONVERS) ; do \
-	   [ -d $(instroot)/$(libdir)/$$ver/site-packages ] || install -m 755 -d $(instroot)/$(libdir)/$$ver/site-packages ;\
-	   install -m 755 $$ver/_snack.$(SOEXT) $(instroot)/$(libdir)/$$ver/site-packages ;\
-	   install -m 644 snack.py $(instroot)/$(libdir)/$$ver/site-packages ;\
-	done || :
 	install -m 644 libnewt.pc $(instroot)/$(pkgconfigdir)
 
+install-tcl: $(WHIPTCLSO)
+ifneq ($(WHIPTCLSO),)
+	[ -d $(instroot)/$(libdir) ] || install -m 755 -d $(instroot)/$(libdir)
+	install -m 755 $(WHIPTCLSO) $(instroot)/$(libdir)
+endif
+
+install-py: _snack.$(BUNDLEEXT)
+ifneq ($(PYTHONVERS),)
+	@for ver in $(PYTHONVERS); do \
+		PLATLIB=`$$ver -c "import sysconfig; print(sysconfig.get_path('platlib'))"`; \
+		PURELIB=`$$ver -c "import sysconfig; print(sysconfig.get_path('purelib'))"`; \
+		[ -d $(instroot)/$$PLATLIB ] || install -m 755 -d $(instroot)/$$PLATLIB ;\
+		[ -d $(instroot)/$$PURELIB ] || install -m 755 -d $(instroot)/$$PURELIB ;\
+		echo install -m 755 $$ver/_snack.$(BUNDLEEXT) $(instroot)/$$PLATLIB;\
+		install -m 755 $$ver/_snack.$(BUNDLEEXT) $(instroot)/$$PLATLIB;\
+		echo install -m 644 snack.py $(instroot)/$$PURELIB;\
+		install -m 644 snack.py $(instroot)/$$PURELIB;\
+	done
+endif
+
 Makefile: configure.ac
 	@echo "You need to rerun ./autogen.sh and ./configure before continuing"
 	@exit 1
