From 016327ca53842e76a7105f02220b987411f44d6d Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 21 Aug 2023 08:19:28 +0200
Subject: [PATCH 100/N] Build and install libpython3.dll

This provides the limited ABI as a separate DLL, forwarding to the
real one. This makes linking with "-lpython3" work.

Fixes #147
---
 Makefile.pre.in | 15 +++++++++++++--
 PC/python3dll.c |  8 ++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 2d2a64f..e9ded19 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -273,6 +273,8 @@ LIBRARY_DEPS=	@LIBRARY_DEPS@
 LINK_PYTHON_DEPS=@LINK_PYTHON_DEPS@
 PY_ENABLE_SHARED=	@PY_ENABLE_SHARED@
 STATIC_LIBPYTHON=	@STATIC_LIBPYTHON@
+ABI3DLLLIBRARY= libpython3.dll
+ABI3LDLIBRARY=  libpython3.dll.a
 
 
 LIBS=		@LIBS@
@@ -633,7 +635,7 @@ all:		@DEF_MAKE_ALL_RULE@
 
 .PHONY: build_all
 build_all:	check-clean-src $(BUILDPYTHON) $(BUILDPYTHONW) $(BUILDVENVLAUNCHER) $(BUILDVENVWLAUNCHER) platform sharedmods \
-		gdbhooks Programs/_testembed scripts checksharedmods rundsymutil
+		gdbhooks Programs/_testembed scripts checksharedmods rundsymutil $(ABI3DLLLIBRARY) $(ABI3LDLIBRARY)
 
 .PHONY: build_wasm
 build_wasm: check-clean-src $(BUILDPYTHON) platform sharedmods \
@@ -808,6 +810,9 @@ pythonw_exe.o: $(srcdir)/PC/pythonw_exe.rc
 python_nt.o: $(srcdir)/PC/python_nt.rc
 	$(WINDRES) $(RCFLAGS) -DORIGINAL_FILENAME=\\\"$(DLLLIBRARY)\\\" -I$(srcdir)/Include -I$(srcdir)/PC -I. $(srcdir)/PC/python_nt.rc $@
 
+python3dll_nt.o: $(srcdir)/PC/python_nt.rc
+	$(WINDRES) $(RCFLAGS) -DORIGINAL_FILENAME=\\\"$(ABI3DLLLIBRARY)\\\" -I$(srcdir)/Include -I$(srcdir)/PC -I. $(srcdir)/PC/python_nt.rc $@
+
 venvlauncher.o: $(srcdir)/PC/pylauncher.rc
 	$(WINDRES) $(RCFLAGS) -DPY_ICON -I$(srcdir)/Include -I$(srcdir)/PC -I. $(srcdir)/PC/pylauncher.rc $@
 
@@ -921,6 +926,10 @@ $(DLLLIBRARY) libpython$(LDVERSION).dll.a: $(LIBRARY_OBJS) python_nt.o
 	else true; \
 	fi
 
+$(ABI3DLLLIBRARY) $(ABI3LDLIBRARY): python3dll_nt.o $(srcdir)/PC/launcher.c
+	$(LDSHARED) -DPYTHON_DLL_NAME=\"$(DLLLIBRARY)\" $(srcdir)/PC/python3dll.c -Wl,--out-implib=$(ABI3LDLIBRARY) -o $(ABI3DLLLIBRARY) python3dll_nt.o \
+		$(LDFLAGS_NODIST);
+
 # wasm32-emscripten browser build
 # wasm assets directory is relative to current build dir, e.g. "./usr/local".
 # --preload-file turns a relative asset path into an absolute path.
@@ -2029,6 +2038,7 @@ altbininstall: $(BUILDPYTHON) @FRAMEWORKPYTHONW@
 		if test -f $(LDLIBRARY) && test "$(PYTHONFRAMEWORKDIR)" = "no-framework" ; then \
 			if test -n "$(DLLLIBRARY)" ; then \
 				$(INSTALL_SHARED) $(DLLLIBRARY) $(DESTDIR)$(BINDIR); \
+				$(INSTALL_SHARED) $(ABI3DLLLIBRARY) $(DESTDIR)$(BINDIR); \
 			else \
 				$(INSTALL_SHARED) $(LDLIBRARY) $(DESTDIR)$(LIBDIR)/$(INSTSONAME); \
 				if test $(LDLIBRARY) != $(INSTSONAME); then \
@@ -2506,6 +2516,7 @@ libainstall: all scripts
 			if test "$(PYTHONFRAMEWORKDIR)" = no-framework; then \
 				if test "$(SHLIB_SUFFIX)" = .dll -o "$(SHLIB_SUFFIX)" = .pyd; then \
 					$(INSTALL_DATA) $(LDLIBRARY) $(DESTDIR)$(LIBDIR) ; \
+					$(INSTALL_DATA) $(ABI3LDLIBRARY) $(DESTDIR)$(LIBDIR) ; \
 				else \
 					$(INSTALL_DATA) $(LIBRARY) $(DESTDIR)$(LIBPL)/$(LIBRARY) ; \
 				fi; \
@@ -2759,7 +2770,7 @@ clean: clean-retain-profile clean-bolt
 
 .PHONY: clobber
 clobber: clean
-	-rm -f $(BUILDPYTHON) $(LIBRARY) $(LDLIBRARY) $(DLLLIBRARY) \
+	-rm -f $(BUILDPYTHON) $(LIBRARY) $(LDLIBRARY) $(DLLLIBRARY)$(ABI3LDLIBRARY) $(ABI3DLLLIBRARY) \
 		tags TAGS \
 		config.cache config.log pyconfig.h Modules/config.c
 	-rm -rf build platform
diff --git a/PC/python3dll.c b/PC/python3dll.c
index 505feef..ee9380c 100755
--- a/PC/python3dll.c
+++ b/PC/python3dll.c
@@ -2,6 +2,7 @@
 
 /* Generated by Tools/build/stable_abi.py */
 
+#ifdef _MSC_VER
 #ifdef _M_IX86
 #define DECORATE "_"
 #else
@@ -12,6 +13,13 @@
     __pragma(comment(linker, "/EXPORT:" DECORATE #name "=" PYTHON_DLL_NAME "." #name))
 #define EXPORT_DATA(name) \
     __pragma(comment(linker, "/EXPORT:" DECORATE #name "=" PYTHON_DLL_NAME "." #name ",DATA"))
+#else
+// XXX: Why do we need the .dll extension and no DECORATE compared to the MSVC case?
+#define EXPORT_FUNC(name) \
+    asm(".section .drectve\n\t.ascii \" -export:" #name "=\\\"" PYTHON_DLL_NAME "." #name "\\\" \"");
+#define EXPORT_DATA(name) \
+    asm(".section .drectve\n\t.ascii \" -export:" #name "=\\\"" PYTHON_DLL_NAME "." #name "\\\",DATA \"");
+#endif
 
 EXPORT_FUNC(_Py_BuildValue_SizeT)
 EXPORT_FUNC(_Py_CheckRecursiveCall)
