From f1dbfedf76ad191212b664f558cbadbfd9805763 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 28 Sep 2022 10:19:00 -0700
Subject: [PATCH v1 6/6] meson: mingw: Add -Wl,--disable-auto-import, enable
 when linking with readline

I hadn't ported -Wl,--disable-auto-import over from the win32 template as I
had focused on msvc for windows. The flag is desirable as it makes it easier
to find problems one would have with msvc, particularly useful during cross
compilation.

This turned out to be a somewhat happy accident, as it allowed me to realize
that readline actually works on windows these days, as long as auto imports to
enable. Therefore enable auto-import again as part of linking to readline.

We perhaps can come up with a better solution for the readline issue, but this
seems good enough for now.

Discussion: http://postgr.es/m/20220928022724.erzuk5v4ai4b53do@awork3.anarazel.de
---
 meson.build | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/meson.build b/meson.build
index 48b0b4cbed8..b3f543761f1 100644
--- a/meson.build
+++ b/meson.build
@@ -279,6 +279,7 @@ elif host_system == 'windows'
     ldflags += '-Wl,--stack,@0@'.format(cdata.get('WIN32_STACK_RLIMIT'))
     # Need to allow multiple definitions, we e.g. want to override getopt.
     ldflags += '-Wl,--allow-multiple-definition'
+    ldflags += '-Wl,--disable-auto-import'
   endif
 
   os_deps += cc.find_library('ws2_32', required: true)
@@ -1080,6 +1081,13 @@ Use -Dreadline=false to disable readline support.'''.format(readline_dep))
       readline = declare_dependency(dependencies: readline,
         include_directories: postgres_inc)
     endif
+
+    # On windows with mingw readline requires auto-import to successfully
+    # link, as the headers don't use declspec(dllimport)
+    if host_system == 'windows' and cc.get_id() != 'msvc'
+      readline = declare_dependency(dependencies: readline,
+        link_args: '-Wl,--enable-auto-import')
+    endif
   endif
 
   # XXX: Figure out whether to implement mingw warning equivalent
-- 
2.37.3.542.gdd3f6c4cae

