From 2edec90c5a1b250e0228275cb4572c80b3059f5a Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Tue, 15 Feb 2022 08:44:28 -0500
Subject: [PATCH v2 1/2] Dumb script to apply PGDLLIMPORT markings.

---
 src/tools/mark_pgdllimport.pl | 54 +++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100755 src/tools/mark_pgdllimport.pl

diff --git a/src/tools/mark_pgdllimport.pl b/src/tools/mark_pgdllimport.pl
new file mode 100755
index 0000000000..570cd04fd6
--- /dev/null
+++ b/src/tools/mark_pgdllimport.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+for my $include_file (@ARGV)
+{
+	open(my $rfh, '<', $include_file) || die "$include_file: $!";
+	my $buffer = '';
+	my $num_pgdllimport_added = 0;
+
+	while (my $raw_line = <$rfh>)
+	{
+		my	$needs_pgdllimport = 1;
+
+		# By convention we declare global variables explicitly extern. We're 
+		# looking for those not already marked with PGDLLIMPORT.
+		$needs_pgdllimport = 0 if $raw_line !~ /^extern\s+/
+			|| $raw_line =~ /PGDLLIMPORT/;
+
+		# Make a copy of the line and perform a simple-minded comment strip.
+		# Also strip trailing whitespace.
+		my $stripped_line = $raw_line;
+		$stripped_line =~ s/\/\*.*\*\///g;
+		$stripped_line =~ s/\s+$//;
+
+		# Variable declarations should end in a semicolon. If we see an
+		# opening parenthesis, it's probably a function declaration.
+		$needs_pgdllimport = 0 if $stripped_line !~ /;$/
+			|| $stripped_line =~ /\(/;
+
+		# Add PGDLLIMPORT marker, if required.
+		if ($needs_pgdllimport)
+		{
+			$raw_line =~ s/^extern/extern PGDLLIMPORT/;
+			++$num_pgdllimport_added;
+		}
+
+		# Add line to buffer.
+		$buffer .= $raw_line;
+	}
+
+	close($rfh);
+
+	# If we added any PGDLLIMPORT markers, rewrite the file.
+	if ($num_pgdllimport_added > 0)
+	{
+		printf "%s: adding %d PGDLLIMPORT markers\n",
+			$include_file, $num_pgdllimport_added;
+		open(my $wfh, '>', $include_file) || die "$include_file: $!";
+		print $wfh $buffer;
+		close($wfh);
+	}
+}
-- 
2.24.3 (Apple Git-128)

