From e405598656f0b96302db1073642671557d4be64d Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 25 Sep 2022 11:06:20 -0700
Subject: [PATCH v17 15/23] windows: Set UMDF_USING_NTSTATUS globally, include
 ntstatus.h

We'd like to use precompiled headers on windows to reduce compile times. Right
now we rely on defining WIN32_NO_STATUS before including postgres.h in a few
select places - which doesn't work with precompiled headers.  Instead define
it globally.

When UMDF_USING_NTSTATUS is defined we need to explicitly include ntstatus.h,
winternl.h to get a comparable set of symbols. Right now these includes would
be required in a number of non-platform-specific .c files - to avoid that,
include them in win32_port.h. Based on my measurements that doesn't increase
compile times measurably.
---
 src/include/port/win32_port.h | 9 +++++++++
 src/include/port/win32ntdll.h | 6 ------
 src/port/open.c               | 2 --
 src/port/win32fdatasync.c     | 2 --
 src/port/win32ntdll.c         | 2 --
 src/port/win32stat.c          | 2 --
 6 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index 707f8760cab..67382245504 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -48,12 +48,21 @@
  * significantly.  WIN32_LEAN_AND_MEAN reduces that a bit. It'd be better to
  * remove the include of windows.h (as well as indirect inclusions of it) from
  * such a central place, but until then...
+ *
+ * To be able to include ntstatus.h tell windows.h to not declare NTSTATUS by
+ * temporarily defining UMDF_USING_NTSTATUS, otherwise we'll get warning about
+ * macro redefinitions, as windows.h also defines NTSTATUS (yuck). That in
+ * turn requires including ntstatus.h, winternl.h to get common symbols.
  */
 #define WIN32_LEAN_AND_MEAN
+#define UMDF_USING_NTSTATUS
 
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <windows.h>
+#include <ntstatus.h>
+#include <winternl.h>
+
 #undef small
 #include <process.h>
 #include <signal.h>
diff --git a/src/include/port/win32ntdll.h b/src/include/port/win32ntdll.h
index 34cebddd542..6d13a18b395 100644
--- a/src/include/port/win32ntdll.h
+++ b/src/include/port/win32ntdll.h
@@ -14,12 +14,6 @@
 #ifndef WIN32NTDLL_H
 #define WIN32NTDLL_H
 
-/*
- * Because this includes NT headers that normally conflict with Win32 headers,
- * any translation unit that includes it should #define UMDF_USING_NTSTATUS
- * before including <windows.h>.
- */
-
 #include <ntstatus.h>
 #include <winternl.h>
 
diff --git a/src/port/open.c b/src/port/open.c
index 8c09c7c1f77..fd4faf604ea 100644
--- a/src/port/open.c
+++ b/src/port/open.c
@@ -13,8 +13,6 @@
 
 #ifdef WIN32
 
-#define UMDF_USING_NTSTATUS
-
 #ifndef FRONTEND
 #include "postgres.h"
 #else
diff --git a/src/port/win32fdatasync.c b/src/port/win32fdatasync.c
index 31d631c6962..a5431615ab2 100644
--- a/src/port/win32fdatasync.c
+++ b/src/port/win32fdatasync.c
@@ -11,8 +11,6 @@
  *-------------------------------------------------------------------------
  */
 
-#define UMDF_USING_NTSTATUS
-
 #ifdef FRONTEND
 #include "postgres_fe.h"
 #else
diff --git a/src/port/win32ntdll.c b/src/port/win32ntdll.c
index eb614077543..db3e5fa251f 100644
--- a/src/port/win32ntdll.c
+++ b/src/port/win32ntdll.c
@@ -13,8 +13,6 @@
  *-------------------------------------------------------------------------
  */
 
-#define UMDF_USING_NTSTATUS
-
 #include "c.h"
 
 #include "port/win32ntdll.h"
diff --git a/src/port/win32stat.c b/src/port/win32stat.c
index 26443293d78..5f3d0d22ff4 100644
--- a/src/port/win32stat.c
+++ b/src/port/win32stat.c
@@ -15,8 +15,6 @@
 
 #ifdef WIN32
 
-#define UMDF_USING_NTSTATUS
-
 #include "c.h"
 #include "port/win32ntdll.h"
 
-- 
2.37.3.542.gdd3f6c4cae

