[PATCH] Fix PostgreSQL server build and install problems under MSYS2

Started by Гурам Дукаabout 6 years ago4 messages
#1Гурам Дука
guram.duka@gmail.com
1 attachment(s)

Hi hackers.

I made a patch fixing build and install problems under MSYS2, including
llvmjit.

I have tested this in my environment and it works, of course need more
extensive testing.
Attached is a patch that fixes it. Tag REL_11_5. Easy to adapt for other
versions.

--
Best regards.
Guram Duka.

Attachments:

postgresql-11.5-msys2-v1.patchapplication/octet-stream; name=postgresql-11.5-msys2-v1.patchDownload
--- a/configure.in	2019-08-06 00:14:59.000000000 +0300
+++ b/configure.in	2019-11-11 10:02:25.963408700 +0300
@@ -1183,7 +1183,7 @@ else
 fi
 
 if test "$with_gssapi" = yes ; then
-  if test "$PORTNAME" != "win32"; then
+  if test "$PORTNAME" != "win32" || which gss 2>&1 >/dev/null; then
     AC_SEARCH_LIBS(gss_init_sec_context, [gssapi_krb5 gss 'gssapi -lkrb5 -lcrypto'], [],
                    [AC_MSG_ERROR([could not find function 'gss_init_sec_context' required for GSSAPI])])
   else
@@ -1355,7 +1355,8 @@ fi
 
 if test "$with_gssapi" = yes ; then
   AC_CHECK_HEADERS(gssapi/gssapi.h, [],
-	[AC_CHECK_HEADERS(gssapi.h, [], [AC_MSG_ERROR([gssapi.h header file is required for GSSAPI])])])
+	[AC_CHECK_HEADERS(gssapi.h, [],
+		[AC_CHECK_HEADERS(gss/api.h, [], [AC_MSG_ERROR([gssapi.h header file is required for GSSAPI])])])])
 fi
 
 if test "$with_openssl" = yes ; then
--- a/src/include/libpq/libpq-be.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/libpq/libpq-be.h	2019-11-06 08:39:24.171190800 +0300
@@ -28,7 +28,9 @@
 #endif
 
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
--- a/src/include/pg_config.h.in	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/pg_config.h.in	2019-11-06 09:43:52.905854600 +0300
@@ -291,6 +291,9 @@
 /* Define to 1 if you have the <gssapi.h> header file. */
 #undef HAVE_GSSAPI_H
 
+/* Define to 1 if you have the <gss/api.h> header file. */
+#undef HAVE_GSS_API_H
+
 /* Define to 1 if you have the <history.h> header file. */
 #undef HAVE_HISTORY_H
 
--- a/src/interfaces/libpq/libpq-int.h	2019-11-06 10:19:05.434167800 +0300
+++ a/src/interfaces/libpq/libpq-int.h	2019-11-06 10:19:14.805284100 +0300
@@ -44,7 +44,9 @@
 #include "pqexpbuffer.h"
 
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
--- a/src/Makefile.shlib	2019-08-06 00:14:59.000000000 +0300
+++ b/src/Makefile.shlib	2019-11-06 11:49:07.937781400 +0300
@@ -389,7 +389,7 @@ else
 DLL_DEFFILE = lib$(NAME)dll.def
 
 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
+	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
 endif
 
 endif # PORTNAME == cygwin
--- a/src/interfaces/libpq/fe-auth.c	2019-08-06 00:14:59.000000000 +0300
+++ a/src/interfaces/libpq/fe-auth.c	2019-11-06 12:36:11.588544700 +0300
@@ -50,6 +50,11 @@
  */
 
 #if defined(WIN32) && !defined(_MSC_VER)
+#if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP)
+static gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_desc =
+{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
+gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
+#else
 /*
  * MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
  * that contain the OIDs required. Redefine here, values copied
@@ -59,6 +64,7 @@ static const gss_OID_desc GSS_C_NT_HOSTB
 {10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
 static GSS_DLLIMP gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
 #endif
+#endif
 
 /*
  * Fetch all errors of a specific type and append to "str".
--- a/src/backend/libpq/auth.c	2019-08-06 00:14:59.000000000 +0300
+++ a/src/backend/libpq/auth.c	2019-11-06 12:37:03.241571900 +0300
@@ -173,7 +173,9 @@ bool		pg_krb_caseins_users;
  *----------------------------------------------------------------
  */
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
@@ -1026,6 +1028,11 @@ CheckSCRAMAuth(Port *port, char *shadow_
 #ifdef ENABLE_GSS
 
 #if defined(WIN32) && !defined(_MSC_VER)
+#if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP)
+static gss_OID_desc GSS_C_NT_USER_NAME_desc =
+{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
+gss_OID GSS_C_NT_USER_NAME = &GSS_C_NT_USER_NAME_desc;
+#else
 /*
  * MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
  * that contain the OIDs required. Redefine here, values copied
@@ -1035,6 +1042,7 @@ static const gss_OID_desc GSS_C_NT_USER_
 {10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
 static GSS_DLLIMP gss_OID GSS_C_NT_USER_NAME = &GSS_C_NT_USER_NAME_desc;
 #endif
+#endif
 
 
 /*
--- a/src/include/c.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/c.h	2019-11-07 16:06:08.452318100 +0300
@@ -786,7 +786,7 @@ extern void ExceptionalCondition(const c
  * helpful error message, but it beats not getting an error at all.
  */
 #ifndef __cplusplus
-#ifdef HAVE__STATIC_ASSERT
+#if defined(HAVE__STATIC_ASSERT) && !defined(__clang__)
 #define StaticAssertStmt(condition, errmessage) \
 	do { _Static_assert(condition, errmessage); } while(0)
 #define StaticAssertExpr(condition, errmessage) \
@@ -1195,7 +1195,7 @@ extern unsigned long long strtoull(const
  * setjmp. Incidentally, nothing provides setjmp's functionality in
  * that case.  We now support the case only on Windows.
  */
-#ifdef WIN32
+#if defined(WIN32) && !defined(__cplusplus)
 #define sigjmp_buf jmp_buf
 #define sigsetjmp(x,y) setjmp(x)
 #define siglongjmp longjmp
--- a/src/include/port/win32_port.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/port/win32_port.h	2019-11-07 16:53:13.547209400 +0300
@@ -263,8 +263,10 @@ typedef int pid_t;
  */
 #ifndef UNSAFE_STAT_OK
 extern int	pgwin32_safestat(const char *path, struct stat *buf);
+#if defined(WIN32) && !defined(__cplusplus) && !defined(BUILDING_DLL)
 #define stat(a,b) pgwin32_safestat(a,b)
 #endif
+#endif
 
 /* These macros are not provided by older MinGW, nor by MSVC */
 #ifndef S_IRUSR
@@ -440,7 +442,9 @@ extern int	pgkill(int pid, int sig);
 /* In backend/port/win32/socket.c */
 #ifndef FRONTEND
 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
+#if defined(WIN32) && !defined(__cplusplus)
 #define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)
+#endif
 #define listen(s, backlog) pgwin32_listen(s, backlog)
 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
--- a/src/include/utils/elog.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/utils/elog.h	2019-11-07 16:21:45.966009500 +0300
@@ -315,8 +315,9 @@ extern PGDLLIMPORT ErrorContextCallback
 	(pg_re_throw(), pg_unreachable())
 #endif
 
+#if defined(WIN32) && !defined(__cplusplus)
 extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
-
+#endif
 
 /* Stuff that error handlers might want to use */
 
--- a/src/Makefile.shlib	2019-11-08 09:53:47.092372100 +0300
+++ b/src/Makefile.shlib	2019-11-08 10:17:30.411522400 +0300
@@ -384,12 +384,12 @@ $(stlib): $(shlib)
 # Else we just use --export-all-symbols.
 ifeq (,$(SHLIB_EXPORTS))
 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
+	$(COMPILER)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
 else
 DLL_DEFFILE = lib$(NAME)dll.def
 
 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
+	$(COMPILER)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
 endif
 
 endif # PORTNAME == cygwin
--- a/src/backend/jit/llvm/Makefile	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/Makefile	2019-11-07 16:48:42.580263400 +0300
@@ -31,7 +31,7 @@ SHLIB_LINK += $(LLVM_LIBS)
 # Because this module includes C++ files, we need to use a C++
 # compiler for linking. Makefile.shlib uses $(COMPILER) to build
 # loadable modules.
-override COMPILER = $(CXX) $(CFLAGS)
+override COMPILER = $(CXX) $(CXXFLAGS) -static-libstdc++ 
 
 OBJS=$(WIN32RES)
 
--- a/src/backend/utils/init/globals.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/utils/init/globals.c	2019-11-08 11:11:49.364880400 +0300
@@ -68,8 +68,8 @@ int			data_directory_mode = PG_DIR_MODE_
 
 char		OutputFileName[MAXPGPATH];	/* debugging output file */
 
-char		my_exec_path[MAXPGPATH];	/* full path to my executable */
-char		pkglib_path[MAXPGPATH]; /* full path to lib directory */
+PGDLLIMPORT char		my_exec_path[MAXPGPATH];	/* full path to my executable */
+PGDLLIMPORT char		pkglib_path[MAXPGPATH]; /* full path to lib directory */
 
 #ifdef EXEC_BACKEND
 char		postgres_exec_path[MAXPGPATH];	/* full path to backend */
--- a/src/include/miscadmin.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/miscadmin.h	2019-11-08 11:10:46.294686700 +0300
@@ -170,7 +170,7 @@ extern int	MyPMChildSlot;
 
 extern char OutputFileName[];
 extern PGDLLIMPORT char my_exec_path[];
-extern char pkglib_path[];
+extern PGDLLIMPORT char pkglib_path[];
 
 #ifdef EXEC_BACKEND
 extern char postgres_exec_path[];
--- a/src/include/jit/jit.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/jit/jit.h	2019-11-08 11:41:40.034349100 +0300
@@ -81,10 +81,10 @@ struct JitProviderCallbacks
 /* GUCs */
 extern bool jit_enabled;
 extern char *jit_provider;
-extern bool jit_debugging_support;
-extern bool jit_dump_bitcode;
+extern PGDLLIMPORT bool jit_debugging_support;
+extern PGDLLIMPORT bool jit_dump_bitcode;
 extern bool jit_expressions;
-extern bool jit_profiling_support;
+extern PGDLLIMPORT bool jit_profiling_support;
 extern bool jit_tuple_deforming;
 extern double jit_above_cost;
 extern double jit_inline_above_cost;
--- a/src/backend/jit/jit.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/jit.c	2019-11-08 11:51:53.493700200 +0300
@@ -34,10 +34,10 @@
 /* GUCs */
 bool		jit_enabled = false;
 char	   *jit_provider = NULL;
-bool		jit_debugging_support = false;
-bool		jit_dump_bitcode = false;
+PGDLLIMPORT bool		jit_debugging_support = false;
+PGDLLIMPORT bool		jit_dump_bitcode = false;
 bool		jit_expressions = true;
-bool		jit_profiling_support = false;
+PGDLLIMPORT bool		jit_profiling_support = false;
 bool		jit_tuple_deforming = true;
 double		jit_above_cost = 100000;
 double		jit_inline_above_cost = 500000;
--- a/src/backend/jit/llvm/llvmjit.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/llvmjit.c	2019-11-08 12:21:48.556440900 +0300
@@ -11,6 +11,7 @@
  *-------------------------------------------------------------------------
  */
 
+#define FORCE_PGDLLIMPORT 1
 #include "postgres.h"
 
 #include "jit/llvmjit.h"
@@ -23,7 +24,6 @@
 #include "portability/instr_time.h"
 #include "storage/ipc.h"
 
-
 #include <llvm-c/Analysis.h>
 #include <llvm-c/BitReader.h>
 #include <llvm-c/BitWriter.h>
@@ -855,7 +855,7 @@ llvm_split_symbol_name(const char *name,
 		 * Symbol names cannot contain a ., therefore we can split based on
 		 * first and last occurance of one.
 		 */
-		*funcname = rindex(name, '.');
+		*funcname = strrchr(name, '.');
 		(*funcname)++;			/* jump over . */
 
 		*modname = pnstrdup(name + strlen("pgextern."),
--- a/src/backend/jit/llvm/llvmjit_inline.cpp	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/llvmjit_inline.cpp	2019-11-08 12:21:32.678703500 +0300
@@ -21,6 +21,8 @@
 
 extern "C"
 {
+#define FORCE_PGDLLIMPORT 1
+#include "pg_config_os.h"
 #include "postgres.h"
 }
 
@@ -29,7 +31,9 @@ extern "C"
 extern "C"
 {
 #include <fcntl.h>
+#ifndef WIN32
 #include <sys/mman.h>
+#endif
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
--- a/src/include/port/win32.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/port/win32.h	2019-11-08 12:23:29.827483400 +0300
@@ -45,7 +45,7 @@
  * defines for dynamic linking on Win32 platform
  */
 
-#ifdef BUILDING_DLL
+#if defined(BUILDING_DLL) && !defined(FORCE_PGDLLIMPORT)
 #define PGDLLIMPORT __declspec (dllexport)
 #else
 #define PGDLLIMPORT __declspec (dllimport)
--- a/src/makefiles/pgxs.mk	2019-08-06 00:14:59.000000000 +0300
+++ b/src/makefiles/pgxs.mk	2019-11-08 15:20:32.058904500 +0300
@@ -211,7 +211,7 @@ endef
 all: $(PROGRAM) $(DATA_built) $(HEADER_allbuilt) $(SCRIPTS_built) $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .control, $(EXTENSION))
 
 ifeq ($(with_llvm), yes)
-all: $(addsuffix .bc, $(MODULES)) $(patsubst %.o,%.bc, $(OBJS))
+all: $(addsuffix .bc, $(MODULES)) $(patsubst win32ver.bc,win32ver.o, $(patsubst %.o,%.bc, $(OBJS)))
 endif
 
 ifdef MODULE_big
@@ -259,7 +259,7 @@ ifneq (,$(strip $(HEADER_dirs)))
 endif # HEADERS
 ifdef MODULE_big
 ifeq ($(with_llvm), yes)
-	$(call install_llvm_module,$(MODULE_big),$(OBJS))
+	$(call install_llvm_module,$(MODULE_big),$(filter-out win32ver.o, $(OBJS)))
 endif # with_llvm
 
 install: install-lib
--- a/config/llvm.m4	2019-08-06 00:14:59.000000000 +0300
+++ b/config/llvm.m4	2019-11-11 09:47:45.546676500 +0300
@@ -89,7 +89,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
     esac
   done
 
-  LLVM_BINPATH=`$LLVM_CONFIG --bindir`
+  LLVM_BINPATH=`$LLVM_CONFIG --bindir | sed -E -e "s/\x5C\x5C/\//g"`
 
   # LLVM_CONFIG, CLANG are already output via AC_ARG_VAR
   AC_SUBST(LLVM_LIBS)
--- a/src/pl/plperl/GNUmakefile 2019-10-29 14:46:46.000000000 +0300
+++ b/src/pl/plperl/GNUmakefile	2019-11-06 13:02:18.910992500 +0300
@@ -48,7 +48,7 @@ lib$(perlwithver).a: $(perlwithver).def
 	dlltool --dllname $(perlwithver).dll --def $(perlwithver).def --output-lib lib$(perlwithver).a
 
 $(perlwithver).def: $(PERLDLL)
-	pexports $^ > $@
+	gendef - $^ > $@ || pexports $^ > $@
 
 endif # win32
 
--- a/src/pl/plperl/plperl.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/pl/plperl/plperl.c	2019-11-06 14:34:35.252510900 +0300
@@ -4163,7 +4163,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newctype = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_ctype(newctype);
+#endif
 		}
 #endif							/* USE_LOCALE_CTYPE */
 #ifdef USE_LOCALE_COLLATE
@@ -4181,7 +4183,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newcoll = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_collate(newcoll);
+#endif
 		}
 #endif							/* USE_LOCALE_COLLATE */
 
@@ -4200,7 +4204,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newnum = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_numeric(newnum);
+#endif
 		}
 #endif							/* USE_LOCALE_NUMERIC */
 	}
--- a/src/pl/plpython/Makefile	2019-08-06 00:14:59.000000000 +0300
+++ b/src/pl/plpython/Makefile	2019-11-06 19:00:59.513600600 +0300
@@ -4,13 +4,6 @@ subdir = src/pl/plpython
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-
-# On Windows we have to remove -lpython from the link since we are
-# building our own
-ifeq ($(PORTNAME), win32)
-override python_libspec =
-endif
-
 override CPPFLAGS := -I. -I$(srcdir) $(python_includespec) $(CPPFLAGS)
 
 rpathdir = $(python_libdir)
@@ -61,15 +54,20 @@ INCS = 	plpython.h \
 ifeq ($(PORTNAME), win32)
 
 pytverstr=$(subst .,,${python_version})
-PYTHONDLL=$(subst \,/,$(WINDIR))/system32/python${pytverstr}.dll
+PYTHONDLL=$(shell echo $(python_libspec).dll | sed -E -e "s/-L//" -e "s/\s+-l/\//")
+ifneq ("$(wildcard $(PYTHONDLL))","")
+    @echo $(PYTHONDLL) FILE_EXISTS
+else
+	PYTHONDLL=$(shell echo $(python_libspec).dll | sed -E -e "s/-L//" -e "s/\s+-l/\/lib/")
+endif
 
 OBJS += libpython${pytverstr}.a
 
 libpython${pytverstr}.a: python${pytverstr}.def
-	dlltool --dllname python${pytverstr}.dll --def python${pytverstr}.def --output-lib libpython${pytverstr}.a
+	dlltool --dllname $(shell basename $(PYTHONDLL)) --def python${pytverstr}.def --output-lib libpython${pytverstr}.a
 
 python${pytverstr}.def:
-	pexports $(PYTHONDLL) > $@
+	gendef - $(PYTHONDLL) > $@ || pexports $(PYTHONDLL) > $@
 
 endif # win32
 
--- a/config/python.m4	2019-08-06 00:14:59.000000000 +0300
+++ b/config/python.m4	2019-11-11 09:51:33.294301200 +0300
@@ -64,6 +64,7 @@ else:
     print(a + ' ' + b)"`
 if test "$PORTNAME" = win32 ; then
     python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'`
+	python_bindir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('BINDIR'))))"`
 fi
 AC_MSG_RESULT([$python_includespec])
 
@@ -121,7 +122,7 @@ else
 	fi
 	# Search for a likely-looking file.
 	found_shlib=0
-	for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
+	for d in "${python_libdir}" "${python_configdir}" "${python_bindir}" /usr/lib64 /usr/lib
 	do
 		# We don't know the platform DLSUFFIX here, so check 'em all.
 		for e in .so .dll .dylib .sl; do
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Гурам Дука (#1)
Re: [PATCH] Fix PostgreSQL server build and install problems under MSYS2

=?UTF-8?B?0JPRg9GA0LDQvCDQlNGD0LrQsA==?= <guram.duka@gmail.com> writes:

I made a patch fixing build and install problems under MSYS2, including
llvmjit.

This seems like it probably breaks a lot of other cases along the way.
Why have you made all these #if tests dependent on defined(__cplusplus)?
That's surely not specific to MSYS2. (I'm a bit bemused by the idea
that our code compiles at all on a C++ compiler; we have not tried
to make the .c files C++-clean. But if it does work, this probably
breaks it for non-Windows cases.)

The GSSAPI changes seem like they might be better considered
separately from the basic problem of getting a working MSYS2 build.

In any case, you need to explain these changes individually,
not expect that we're just going to adopt them without questions.

regards, tom lane

#3Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#2)
Re: [PATCH] Fix PostgreSQL server build and install problems under MSYS2

On Mon, Nov 11, 2019 at 10:56:51AM -0500, Tom Lane wrote:

This seems like it probably breaks a lot of other cases along the way.
Why have you made all these #if tests dependent on defined(__cplusplus)?
That's surely not specific to MSYS2. (I'm a bit bemused by the idea
that our code compiles at all on a C++ compiler; we have not tried
to make the .c files C++-clean. But if it does work, this probably
breaks it for non-Windows cases.)

The GSSAPI changes seem like they might be better considered
separately from the basic problem of getting a working MSYS2 build.

Yeah. We have fairywen in the buildfarm but it does not compile with
GSSAPI and LLVM. If we were to do something, it would be better to
separate those changes into minimum, separate patches, with one each
for each library dependency you are trying to fix so as they can be
evaluated separately. We should also have a buildfarm member on that
if some of those changes actually make sense and are
platform-dependent.

In any case, you need to explain these changes individually,
not expect that we're just going to adopt them without questions.

I am doubtful that the changes in c.h, elog.h, win32_port.h,
miscadmin.h and src/backend/jit/llvm/Makefile are actually needed
thanks to fairywen.

+#if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP)
+static gss_OID_desc GSS_C_NT_USER_NAME_desc =
+{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
This also deserves an explanation.
--
Michael
#4Guram Duka
guram.duka@gmail.com
In reply to: Michael Paquier (#3)
1 attachment(s)
Re: [PATCH] Fix PostgreSQL server build and install problems under MSYS2

Thank you for your comments.

1. The #if ... defined(__ cplusplus) is necessary for the successful
compilation of C++ llvmjit code that uses C headers.
2. #if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP) is necessary for the
successful link libgss.a provided by MSYS2.
3. Remember that you need to run autoreconf before running configure.
4. Found and fixed a small bug in the patch. Build in environment CentOS
7.7.1908 works. Build in environment MSYS2 gcc 9.2.0 works. Build in
environment Visual Studio Community 2019 works.

The second version of the patch attached.
Waiting for your comments.

--
Best regards.
Guram Duka.

Attachments:

postgresql-11.5-msys2-v2.patchapplication/octet-stream; name=postgresql-11.5-msys2-v2.patchDownload
--- a/configure.in	2019-08-06 00:14:59.000000000 +0300
+++ b/configure.in	2019-11-11 10:02:25.963408700 +0300
@@ -1183,7 +1183,7 @@ else
 fi
 
 if test "$with_gssapi" = yes ; then
-  if test "$PORTNAME" != "win32"; then
+  if test "$PORTNAME" != "win32" || which gss 2>&1 >/dev/null; then
     AC_SEARCH_LIBS(gss_init_sec_context, [gssapi_krb5 gss 'gssapi -lkrb5 -lcrypto'], [],
                    [AC_MSG_ERROR([could not find function 'gss_init_sec_context' required for GSSAPI])])
   else
@@ -1355,7 +1355,8 @@ fi
 
 if test "$with_gssapi" = yes ; then
   AC_CHECK_HEADERS(gssapi/gssapi.h, [],
-	[AC_CHECK_HEADERS(gssapi.h, [], [AC_MSG_ERROR([gssapi.h header file is required for GSSAPI])])])
+	[AC_CHECK_HEADERS(gssapi.h, [],
+		[AC_CHECK_HEADERS(gss/api.h, [], [AC_MSG_ERROR([gssapi.h header file is required for GSSAPI])])])])
 fi
 
 if test "$with_openssl" = yes ; then
--- a/src/include/libpq/libpq-be.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/libpq/libpq-be.h	2019-11-06 08:39:24.171190800 +0300
@@ -28,7 +28,9 @@
 #endif
 
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
--- a/src/include/pg_config.h.in	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/pg_config.h.in	2019-11-06 09:43:52.905854600 +0300
@@ -291,6 +291,9 @@
 /* Define to 1 if you have the <gssapi.h> header file. */
 #undef HAVE_GSSAPI_H
 
+/* Define to 1 if you have the <gss/api.h> header file. */
+#undef HAVE_GSS_API_H
+
 /* Define to 1 if you have the <history.h> header file. */
 #undef HAVE_HISTORY_H
 
--- a/src/interfaces/libpq/libpq-int.h	2019-11-06 10:19:05.434167800 +0300
+++ a/src/interfaces/libpq/libpq-int.h	2019-11-06 10:19:14.805284100 +0300
@@ -44,7 +44,9 @@
 #include "pqexpbuffer.h"
 
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
--- a/src/Makefile.shlib	2019-08-06 00:14:59.000000000 +0300
+++ b/src/Makefile.shlib	2019-11-06 11:49:07.937781400 +0300
@@ -389,7 +389,7 @@ else
 DLL_DEFFILE = lib$(NAME)dll.def
 
 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
+	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
 endif
 
 endif # PORTNAME == cygwin
--- a/src/interfaces/libpq/fe-auth.c	2019-08-06 00:14:59.000000000 +0300
+++ a/src/interfaces/libpq/fe-auth.c	2019-11-06 12:36:11.588544700 +0300
@@ -50,6 +50,11 @@
  */
 
 #if defined(WIN32) && !defined(_MSC_VER)
+#if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP)
+static gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_desc =
+{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
+gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
+#else
 /*
  * MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
  * that contain the OIDs required. Redefine here, values copied
@@ -59,6 +64,7 @@ static const gss_OID_desc GSS_C_NT_HOSTB
 {10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
 static GSS_DLLIMP gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
 #endif
+#endif
 
 /*
  * Fetch all errors of a specific type and append to "str".
--- a/src/backend/libpq/auth.c	2019-08-06 00:14:59.000000000 +0300
+++ a/src/backend/libpq/auth.c	2019-11-06 12:37:03.241571900 +0300
@@ -173,7 +173,9 @@ bool		pg_krb_caseins_users;
  *----------------------------------------------------------------
  */
 #ifdef ENABLE_GSS
-#if defined(HAVE_GSSAPI_H)
+#if defined(HAVE_GSS_API_H)
+#include <gss/api.h>
+#elif defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
 #else
 #include <gssapi/gssapi.h>
@@ -1026,6 +1028,11 @@ CheckSCRAMAuth(Port *port, char *shadow_
 #ifdef ENABLE_GSS
 
 #if defined(WIN32) && !defined(_MSC_VER)
+#if defined(HAVE_GSS_API_H) && !defined(GSS_DLLIMP)
+static gss_OID_desc GSS_C_NT_USER_NAME_desc =
+{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
+gss_OID GSS_C_NT_USER_NAME = &GSS_C_NT_USER_NAME_desc;
+#else
 /*
  * MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
  * that contain the OIDs required. Redefine here, values copied
@@ -1035,6 +1042,7 @@ static const gss_OID_desc GSS_C_NT_USER_
 {10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
 static GSS_DLLIMP gss_OID GSS_C_NT_USER_NAME = &GSS_C_NT_USER_NAME_desc;
 #endif
+#endif
 
 
 /*
--- a/src/include/c.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/c.h	2019-11-07 16:06:08.452318100 +0300
@@ -786,7 +786,7 @@ extern void ExceptionalCondition(const c
  * helpful error message, but it beats not getting an error at all.
  */
 #ifndef __cplusplus
-#ifdef HAVE__STATIC_ASSERT
+#if defined(HAVE__STATIC_ASSERT) && !defined(__clang__)
 #define StaticAssertStmt(condition, errmessage) \
 	do { _Static_assert(condition, errmessage); } while(0)
 #define StaticAssertExpr(condition, errmessage) \
@@ -1195,7 +1195,7 @@ extern unsigned long long strtoull(const
  * setjmp. Incidentally, nothing provides setjmp's functionality in
  * that case.  We now support the case only on Windows.
  */
-#ifdef WIN32
+#if defined(WIN32) && !defined(__cplusplus)
 #define sigjmp_buf jmp_buf
 #define sigsetjmp(x,y) setjmp(x)
 #define siglongjmp longjmp
--- a/src/include/port/win32_port.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/port/win32_port.h	2019-11-07 16:53:13.547209400 +0300
@@ -263,8 +263,10 @@ typedef int pid_t;
  */
 #ifndef UNSAFE_STAT_OK
 extern int	pgwin32_safestat(const char *path, struct stat *buf);
+#if defined(WIN32) && !defined(__cplusplus) && !defined(BUILDING_DLL)
 #define stat(a,b) pgwin32_safestat(a,b)
 #endif
+#endif
 
 /* These macros are not provided by older MinGW, nor by MSVC */
 #ifndef S_IRUSR
@@ -440,7 +442,9 @@ extern int	pgkill(int pid, int sig);
 /* In backend/port/win32/socket.c */
 #ifndef FRONTEND
 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
+#if defined(WIN32) && !defined(__cplusplus)
 #define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)
+#endif
 #define listen(s, backlog) pgwin32_listen(s, backlog)
 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
--- a/src/include/utils/elog.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/utils/elog.h	2019-11-07 16:21:45.966009500 +0300
@@ -315,8 +315,9 @@ extern PGDLLIMPORT ErrorContextCallback
 	(pg_re_throw(), pg_unreachable())
 #endif
 
+#if !defined(WIN32) || !defined(__cplusplus)
 extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
-
+#endif
 
 /* Stuff that error handlers might want to use */
 
--- a/src/Makefile.shlib	2019-11-08 09:53:47.092372100 +0300
+++ b/src/Makefile.shlib	2019-11-08 10:17:30.411522400 +0300
@@ -384,12 +384,12 @@ $(stlib): $(shlib)
 # Else we just use --export-all-symbols.
 ifeq (,$(SHLIB_EXPORTS))
 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
+	$(COMPILER)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
 else
 DLL_DEFFILE = lib$(NAME)dll.def
 
 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
+	$(COMPILER)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--output-def,$(DLL_DEFFILE),--out-implib=$(stlib)
 endif
 
 endif # PORTNAME == cygwin
--- a/src/backend/jit/llvm/Makefile	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/Makefile	2019-11-07 16:48:42.580263400 +0300
@@ -31,7 +31,7 @@ SHLIB_LINK += $(LLVM_LIBS)
 # Because this module includes C++ files, we need to use a C++
 # compiler for linking. Makefile.shlib uses $(COMPILER) to build
 # loadable modules.
-override COMPILER = $(CXX) $(CFLAGS)
+override COMPILER = $(CXX) $(CXXFLAGS) -static-libstdc++ 
 
 OBJS=$(WIN32RES)
 
--- a/src/backend/utils/init/globals.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/utils/init/globals.c	2019-11-08 11:11:49.364880400 +0300
@@ -68,8 +68,8 @@ int			data_directory_mode = PG_DIR_MODE_
 
 char		OutputFileName[MAXPGPATH];	/* debugging output file */
 
-char		my_exec_path[MAXPGPATH];	/* full path to my executable */
-char		pkglib_path[MAXPGPATH]; /* full path to lib directory */
+PGDLLIMPORT char		my_exec_path[MAXPGPATH];	/* full path to my executable */
+PGDLLIMPORT char		pkglib_path[MAXPGPATH]; /* full path to lib directory */
 
 #ifdef EXEC_BACKEND
 char		postgres_exec_path[MAXPGPATH];	/* full path to backend */
--- a/src/include/miscadmin.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/miscadmin.h	2019-11-08 11:10:46.294686700 +0300
@@ -170,7 +170,7 @@ extern int	MyPMChildSlot;
 
 extern char OutputFileName[];
 extern PGDLLIMPORT char my_exec_path[];
-extern char pkglib_path[];
+extern PGDLLIMPORT char pkglib_path[];
 
 #ifdef EXEC_BACKEND
 extern char postgres_exec_path[];
--- a/src/include/jit/jit.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/jit/jit.h	2019-11-08 11:41:40.034349100 +0300
@@ -81,10 +81,10 @@ struct JitProviderCallbacks
 /* GUCs */
 extern bool jit_enabled;
 extern char *jit_provider;
-extern bool jit_debugging_support;
-extern bool jit_dump_bitcode;
+extern PGDLLIMPORT bool jit_debugging_support;
+extern PGDLLIMPORT bool jit_dump_bitcode;
 extern bool jit_expressions;
-extern bool jit_profiling_support;
+extern PGDLLIMPORT bool jit_profiling_support;
 extern bool jit_tuple_deforming;
 extern double jit_above_cost;
 extern double jit_inline_above_cost;
--- a/src/backend/jit/jit.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/jit.c	2019-11-08 11:51:53.493700200 +0300
@@ -34,10 +34,10 @@
 /* GUCs */
 bool		jit_enabled = false;
 char	   *jit_provider = NULL;
-bool		jit_debugging_support = false;
-bool		jit_dump_bitcode = false;
+PGDLLIMPORT bool		jit_debugging_support = false;
+PGDLLIMPORT bool		jit_dump_bitcode = false;
 bool		jit_expressions = true;
-bool		jit_profiling_support = false;
+PGDLLIMPORT bool		jit_profiling_support = false;
 bool		jit_tuple_deforming = true;
 double		jit_above_cost = 100000;
 double		jit_inline_above_cost = 500000;
--- a/src/backend/jit/llvm/llvmjit.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/llvmjit.c	2019-11-08 12:21:48.556440900 +0300
@@ -11,6 +11,7 @@
  *-------------------------------------------------------------------------
  */
 
+#define FORCE_PGDLLIMPORT 1
 #include "postgres.h"
 
 #include "jit/llvmjit.h"
@@ -23,7 +24,6 @@
 #include "portability/instr_time.h"
 #include "storage/ipc.h"
 
-
 #include <llvm-c/Analysis.h>
 #include <llvm-c/BitReader.h>
 #include <llvm-c/BitWriter.h>
@@ -855,7 +855,7 @@ llvm_split_symbol_name(const char *name,
 		 * Symbol names cannot contain a ., therefore we can split based on
 		 * first and last occurance of one.
 		 */
-		*funcname = rindex(name, '.');
+		*funcname = strrchr(name, '.');
 		(*funcname)++;			/* jump over . */
 
 		*modname = pnstrdup(name + strlen("pgextern."),
--- a/src/backend/jit/llvm/llvmjit_inline.cpp	2019-08-06 00:14:59.000000000 +0300
+++ b/src/backend/jit/llvm/llvmjit_inline.cpp	2019-11-08 12:21:32.678703500 +0300
@@ -21,6 +21,8 @@
 
 extern "C"
 {
+#define FORCE_PGDLLIMPORT 1
+#include "pg_config_os.h"
 #include "postgres.h"
 }
 
@@ -29,7 +31,9 @@ extern "C"
 extern "C"
 {
 #include <fcntl.h>
+#ifndef WIN32
 #include <sys/mman.h>
+#endif
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
--- a/src/include/port/win32.h	2019-08-06 00:14:59.000000000 +0300
+++ b/src/include/port/win32.h	2019-11-08 12:23:29.827483400 +0300
@@ -45,7 +45,7 @@
  * defines for dynamic linking on Win32 platform
  */
 
-#ifdef BUILDING_DLL
+#if defined(BUILDING_DLL) && !defined(FORCE_PGDLLIMPORT)
 #define PGDLLIMPORT __declspec (dllexport)
 #else
 #define PGDLLIMPORT __declspec (dllimport)
--- a/src/makefiles/pgxs.mk	2019-08-06 00:14:59.000000000 +0300
+++ b/src/makefiles/pgxs.mk	2019-11-08 15:20:32.058904500 +0300
@@ -211,7 +211,7 @@ endef
 all: $(PROGRAM) $(DATA_built) $(HEADER_allbuilt) $(SCRIPTS_built) $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .control, $(EXTENSION))
 
 ifeq ($(with_llvm), yes)
-all: $(addsuffix .bc, $(MODULES)) $(patsubst %.o,%.bc, $(OBJS))
+all: $(addsuffix .bc, $(MODULES)) $(patsubst win32ver.bc,win32ver.o, $(patsubst %.o,%.bc, $(OBJS)))
 endif
 
 ifdef MODULE_big
@@ -259,7 +259,7 @@ ifneq (,$(strip $(HEADER_dirs)))
 endif # HEADERS
 ifdef MODULE_big
 ifeq ($(with_llvm), yes)
-	$(call install_llvm_module,$(MODULE_big),$(OBJS))
+	$(call install_llvm_module,$(MODULE_big),$(filter-out win32ver.o, $(OBJS)))
 endif # with_llvm
 
 install: install-lib
--- a/config/llvm.m4	2019-08-06 00:14:59.000000000 +0300
+++ b/config/llvm.m4	2019-11-11 09:47:45.546676500 +0300
@@ -89,7 +89,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
     esac
   done
 
-  LLVM_BINPATH=`$LLVM_CONFIG --bindir`
+  LLVM_BINPATH=`$LLVM_CONFIG --bindir | sed -E -e "s/\x5C\x5C/\//g"`
 
   # LLVM_CONFIG, CLANG are already output via AC_ARG_VAR
   AC_SUBST(LLVM_LIBS)
--- a/src/pl/plperl/GNUmakefile 2019-10-29 14:46:46.000000000 +0300
+++ b/src/pl/plperl/GNUmakefile	2019-11-06 13:02:18.910992500 +0300
@@ -48,7 +48,7 @@ lib$(perlwithver).a: $(perlwithver).def
 	dlltool --dllname $(perlwithver).dll --def $(perlwithver).def --output-lib lib$(perlwithver).a
 
 $(perlwithver).def: $(PERLDLL)
-	pexports $^ > $@
+	gendef - $^ > $@ || pexports $^ > $@
 
 endif # win32
 
--- a/src/pl/plperl/plperl.c	2019-08-06 00:14:59.000000000 +0300
+++ b/src/pl/plperl/plperl.c	2019-11-06 14:34:35.252510900 +0300
@@ -4163,7 +4163,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newctype = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_ctype(newctype);
+#endif
 		}
 #endif							/* USE_LOCALE_CTYPE */
 #ifdef USE_LOCALE_COLLATE
@@ -4181,7 +4183,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newcoll = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_collate(newcoll);
+#endif
 		}
 #endif							/* USE_LOCALE_COLLATE */
 
@@ -4200,7 +4204,9 @@ setlocale_perl(int category, char *local
 			else
 #endif
 				newnum = RETVAL;
+#if PERL_REVISION <= 5 && PERL_VERSION < 28
 			new_numeric(newnum);
+#endif
 		}
 #endif							/* USE_LOCALE_NUMERIC */
 	}
--- a/src/pl/plpython/Makefile	2019-08-06 00:14:59.000000000 +0300
+++ b/src/pl/plpython/Makefile	2019-11-06 19:00:59.513600600 +0300
@@ -4,13 +4,6 @@ subdir = src/pl/plpython
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-
-# On Windows we have to remove -lpython from the link since we are
-# building our own
-ifeq ($(PORTNAME), win32)
-override python_libspec =
-endif
-
 override CPPFLAGS := -I. -I$(srcdir) $(python_includespec) $(CPPFLAGS)
 
 rpathdir = $(python_libdir)
@@ -61,15 +54,20 @@ INCS = 	plpython.h \
 ifeq ($(PORTNAME), win32)
 
 pytverstr=$(subst .,,${python_version})
-PYTHONDLL=$(subst \,/,$(WINDIR))/system32/python${pytverstr}.dll
+PYTHONDLL=$(shell echo $(python_libspec).dll | sed -E -e "s/-L//" -e "s/\s+-l/\//")
+ifneq ("$(wildcard $(PYTHONDLL))","")
+    @echo $(PYTHONDLL) FILE_EXISTS
+else
+	PYTHONDLL=$(shell echo $(python_libspec).dll | sed -E -e "s/-L//" -e "s/\s+-l/\/lib/")
+endif
 
 OBJS += libpython${pytverstr}.a
 
 libpython${pytverstr}.a: python${pytverstr}.def
-	dlltool --dllname python${pytverstr}.dll --def python${pytverstr}.def --output-lib libpython${pytverstr}.a
+	dlltool --dllname $(shell basename $(PYTHONDLL)) --def python${pytverstr}.def --output-lib libpython${pytverstr}.a
 
 python${pytverstr}.def:
-	pexports $(PYTHONDLL) > $@
+	gendef - $(PYTHONDLL) > $@ || pexports $(PYTHONDLL) > $@
 
 endif # win32
 
--- a/config/python.m4	2019-08-06 00:14:59.000000000 +0300
+++ b/config/python.m4	2019-11-11 09:51:33.294301200 +0300
@@ -64,6 +64,7 @@ else:
     print(a + ' ' + b)"`
 if test "$PORTNAME" = win32 ; then
     python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'`
+	python_bindir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('BINDIR'))))"`
 fi
 AC_MSG_RESULT([$python_includespec])
 
@@ -121,7 +122,7 @@ else
 	fi
 	# Search for a likely-looking file.
 	found_shlib=0
-	for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
+	for d in "${python_libdir}" "${python_configdir}" "${python_bindir}" /usr/lib64 /usr/lib
 	do
 		# We don't know the platform DLSUFFIX here, so check 'em all.
 		for e in .so .dll .dylib .sl; do