| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
#---------------------------------------------------------------------- |
| 4 |
# |
| 5 |
# mark_pgdllimport.pl |
| 6 |
# Perl script that tries to add PGDLLIMPORT markings to PostgreSQL |
| 7 |
# header files. |
| 8 |
# |
| 9 |
# This relies on a few idiosyncrasies of the PostgreSQL coding style, |
| 10 |
# such as the fact that we always use "extern" in function |
| 11 |
# declarations, and that we don't use // comments. It's not very |
| 12 |
# smart and may not catch all cases. |
| 13 |
# |
| 14 |
# It's probably a good idea to run pgindent on any files that this |
| 15 |
# script modifies before committing. This script uses as arguments |
| 16 |
# a list of the header files to scan for the markings. |
| 17 |
# |
| 18 |
# Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 19 |
# Portions Copyright (c) 1994, Regents of the University of California |
| 20 |
# |
| 21 |
# src/tools/mark_pgdllimport.pl |
| 22 |
# |
| 23 |
#---------------------------------------------------------------------- |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings FATAL => 'all'; |
| 27 |
|
| 28 |
for my $include_file (@ARGV) |
| 29 |
{ |
| 30 |
open(my $rfh, '<', $include_file) || die "$include_file: $!"; |
| 31 |
my $buffer = ''; |
| 32 |
my $num_pgdllimport_added = 0; |
| 33 |
|
| 34 |
while (my $raw_line = <$rfh>) |
| 35 |
{ |
| 36 |
my $needs_pgdllimport = 1; |
| 37 |
|
| 38 |
# By convention we declare global variables explicitly extern. We're |
| 39 |
# looking for those not already marked with PGDLLIMPORT. |
| 40 |
$needs_pgdllimport = 0 |
| 41 |
if $raw_line !~ /^extern\s+/ |
| 42 |
|| $raw_line =~ /PGDLLIMPORT/; |
| 43 |
|
| 44 |
# Make a copy of the line and perform a simple-minded comment strip. |
| 45 |
# Also strip trailing whitespace. |
| 46 |
my $stripped_line = $raw_line; |
| 47 |
$stripped_line =~ s/\/\*.*\*\///g; |
| 48 |
$stripped_line =~ s/\s+$//; |
| 49 |
|
| 50 |
# Variable declarations should end in a semicolon. If we see an |
| 51 |
# opening parenthesis, it's probably a function declaration. |
| 52 |
$needs_pgdllimport = 0 |
| 53 |
if $stripped_line !~ /;$/ |
| 54 |
|| $stripped_line =~ /\(/; |
| 55 |
|
| 56 |
# Add PGDLLIMPORT marker, if required. |
| 57 |
if ($needs_pgdllimport) |
| 58 |
{ |
| 59 |
$raw_line =~ s/^extern/extern PGDLLIMPORT/; |
| 60 |
++$num_pgdllimport_added; |
| 61 |
} |
| 62 |
|
| 63 |
# Add line to buffer. |
| 64 |
$buffer .= $raw_line; |
| 65 |
} |
| 66 |
|
| 67 |
close($rfh); |
| 68 |
|
| 69 |
# If we added any PGDLLIMPORT markers, rewrite the file. |
| 70 |
if ($num_pgdllimport_added > 0) |
| 71 |
{ |
| 72 |
printf "%s: adding %d PGDLLIMPORT markers\n", |
| 73 |
$include_file, $num_pgdllimport_added; |
| 74 |
open(my $wfh, '>', $include_file) || die "$include_file: $!"; |
| 75 |
print $wfh $buffer; |
| 76 |
close($wfh); |
| 77 |
} |
| 78 |
} |