Skip to content

Commit 1c1a572

Browse files
committed
MSVC: Test whether 32-bit Perl needs -D_USE_32BIT_TIME_T.
Commits 5a5c2fe and b5178c5 introduced support for modern MSVC-built, 32-bit Perl, but they broke use of MinGW-built, 32-bit Perl distributions like Strawberry Perl and modern ActivePerl. Perl has no robust means to report whether it expects a -D_USE_32BIT_TIME_T ABI, so test this. Back-patch to 9.3 (all supported versions). The chief alternative was a heuristic of adding -D_USE_32BIT_TIME_T when $Config{gccversion} is nonempty. That banks on every gcc-built Perl using the same ABI. gcc could change its default ABI the way MSVC once did, and one could build Perl with gcc and the non-default ABI. The GNU make build system could benefit from a similar test, without which it does not support MSVC-built Perl. For now, just add a comment. Most users taking the special step of building Perl with MSVC probably build PostgreSQL with MSVC. Discussion: https://postgr.es/m/20171130041441.GA3161526@rfd.leadboat.com
1 parent 85a83a3 commit 1c1a572

File tree

2 files changed

+158
-42
lines changed

2 files changed

+158
-42
lines changed

config/perl.m4

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,23 @@ AC_DEFUN([PGAC_CHECK_PERL_CONFIGS],
5151

5252
# PGAC_CHECK_PERL_EMBED_CCFLAGS
5353
# -----------------------------
54-
# We selectively extract stuff from $Config{ccflags}. We don't really need
55-
# anything except -D switches, and other sorts of compiler switches can
56-
# actively break things if Perl was compiled with a different compiler.
57-
# Moreover, although Perl likes to put stuff like -D_LARGEFILE_SOURCE and
58-
# -D_FILE_OFFSET_BITS=64 here, it would be fatal to try to compile PL/Perl
59-
# to a different libc ABI than core Postgres uses. The available information
60-
# says that all the symbols that affect Perl's own ABI begin with letters,
61-
# so it should be sufficient to adopt -D switches for symbols not beginning
62-
# with underscore. An exception is that we need to let through
63-
# -D_USE_32BIT_TIME_T if it's present. (We probably could restrict that to
64-
# only get through on Windows, but for the moment we let it through always.)
65-
# For debugging purposes, let's have the configure output report the raw
66-
# ccflags value as well as the set of flags we chose to adopt.
54+
# We selectively extract stuff from $Config{ccflags}. For debugging purposes,
55+
# let's have the configure output report the raw ccflags value as well as the
56+
# set of flags we chose to adopt. We don't really need anything except -D
57+
# switches, and other sorts of compiler switches can actively break things if
58+
# Perl was compiled with a different compiler. Moreover, although Perl likes
59+
# to put stuff like -D_LARGEFILE_SOURCE and -D_FILE_OFFSET_BITS=64 here, it
60+
# would be fatal to try to compile PL/Perl to a different libc ABI than core
61+
# Postgres uses. The available information says that most symbols that affect
62+
# Perl's own ABI begin with letters, so it's almost sufficient to adopt -D
63+
# switches for symbols not beginning with underscore. Some exceptions are the
64+
# Windows-specific -D_USE_32BIT_TIME_T and -D__MINGW_USE_VC2005_COMPAT; see
65+
# Mkvcbuild.pm for details. We absorb the former when Perl reports it. Perl
66+
# never reports the latter, and we don't attempt to deduce when it's needed.
67+
# Consequently, we don't support using MinGW to link to MSVC-built Perl. As
68+
# of 2017, all supported ActivePerl and Strawberry Perl are MinGW-built. If
69+
# that changes or an MSVC-built Perl distribution becomes prominent, we can
70+
# revisit this limitation.
6771
AC_DEFUN([PGAC_CHECK_PERL_EMBED_CCFLAGS],
6872
[AC_REQUIRE([PGAC_PATH_PERL])
6973
AC_MSG_CHECKING([for CFLAGS recommended by Perl])

src/tools/msvc/Mkvcbuild.pm

Lines changed: 141 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ my $libpgport;
2727
my $libpgcommon;
2828
my $postgres;
2929
my $libpq;
30+
my @unlink_on_exit;
3031

3132
# Set of variables for modules in contrib/ and src/test/modules/
3233
my $contrib_defines = { 'refint' => 'REFINT_VERBOSE' };
@@ -508,34 +509,154 @@ sub mkvcbuild
508509
my $plperl =
509510
$solution->AddProject('plperl', 'dll', 'PLs', 'src/pl/plperl');
510511
$plperl->AddIncludeDir($solution->{options}->{perl} . '/lib/CORE');
512+
$plperl->AddReference($postgres);
513+
514+
my $perl_path = $solution->{options}->{perl} . '\lib\CORE\*perl*';
515+
516+
# ActivePerl 5.16 provided perl516.lib; 5.18 provided libperl518.a
517+
my @perl_libs =
518+
grep { /perl\d+\.lib$|libperl\d+\.a$/ } glob($perl_path);
519+
if (@perl_libs == 1)
520+
{
521+
$plperl->AddLibrary($perl_libs[0]);
522+
}
523+
else
524+
{
525+
die
526+
"could not identify perl library version matching pattern $perl_path\n";
527+
}
511528

512529
# Add defines from Perl's ccflags; see PGAC_CHECK_PERL_EMBED_CCFLAGS
513530
my @perl_embed_ccflags;
514531
foreach my $f (split(" ", $Config{ccflags}))
515532
{
516-
if ( $f =~ /^-D[^_]/
517-
|| $f =~ /^-D_USE_32BIT_TIME_T/)
533+
if ($f =~ /^-D[^_]/)
518534
{
519535
$f =~ s/\-D//;
520536
push(@perl_embed_ccflags, $f);
521537
}
522538
}
523539

524-
# Perl versions before 5.13.4 don't provide -D_USE_32BIT_TIME_T
525-
# regardless of how they were built. On 32-bit Windows, assume
526-
# such a version was built with a pre-MSVC-2005 compiler, and
527-
# define the symbol anyway, so that we are compatible if we're
528-
# being built with a later MSVC version.
529-
push(@perl_embed_ccflags, '_USE_32BIT_TIME_T')
530-
if $solution->{platform} eq 'Win32'
531-
&& $Config{PERL_REVISION} == 5
532-
&& ($Config{PERL_VERSION} < 13
533-
|| ( $Config{PERL_VERSION} == 13
534-
&& $Config{PERL_SUBVERSION} < 4));
535-
536-
# Also, a hack to prevent duplicate definitions of uid_t/gid_t
540+
# hack to prevent duplicate definitions of uid_t/gid_t
537541
push(@perl_embed_ccflags, 'PLPERL_HAVE_UID_GID');
538542

543+
# Windows offers several 32-bit ABIs. Perl is sensitive to
544+
# sizeof(time_t), one of the ABI dimensions. To get 32-bit time_t,
545+
# use "cl -D_USE_32BIT_TIME_T" or plain "gcc". For 64-bit time_t, use
546+
# "gcc -D__MINGW_USE_VC2005_COMPAT" or plain "cl". Before MSVC 2005,
547+
# plain "cl" chose 32-bit time_t. PostgreSQL doesn't support building
548+
# with pre-MSVC-2005 compilers, but it does support linking to Perl
549+
# built with such a compiler. MSVC-built Perl 5.13.4 and later report
550+
# -D_USE_32BIT_TIME_T in $Config{ccflags} if applicable, but
551+
# MinGW-built Perl never reports -D_USE_32BIT_TIME_T despite typically
552+
# needing it. Ignore the $Config{ccflags} opinion about
553+
# -D_USE_32BIT_TIME_T, and use a runtime test to deduce the ABI Perl
554+
# expects. Specifically, test use of PL_modglobal, which maps to a
555+
# PerlInterpreter field whose position depends on sizeof(time_t).
556+
if ($solution->{platform} eq 'Win32')
557+
{
558+
my $source_file = 'conftest.c';
559+
my $obj = 'conftest.obj';
560+
my $exe = 'conftest.exe';
561+
my @conftest = ($source_file, $obj, $exe);
562+
push @unlink_on_exit, @conftest;
563+
unlink $source_file;
564+
open my $o, '>', $source_file
565+
|| croak "Could not write to $source_file";
566+
print $o '
567+
/* compare to plperl.h */
568+
#define __inline__ __inline
569+
#define PERL_NO_GET_CONTEXT
570+
#include <EXTERN.h>
571+
#include <perl.h>
572+
573+
int
574+
main(int argc, char **argv)
575+
{
576+
int dummy_argc = 1;
577+
char *dummy_argv[1] = {""};
578+
char *dummy_env[1] = {NULL};
579+
static PerlInterpreter *interp;
580+
581+
PERL_SYS_INIT3(&dummy_argc, (char ***) &dummy_argv,
582+
(char ***) &dummy_env);
583+
interp = perl_alloc();
584+
perl_construct(interp);
585+
{
586+
dTHX;
587+
const char key[] = "dummy";
588+
589+
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
590+
hv_store(PL_modglobal, key, sizeof(key) - 1, newSViv(1), 0);
591+
return hv_fetch(PL_modglobal, key, sizeof(key) - 1, 0) == NULL;
592+
}
593+
}
594+
';
595+
close $o;
596+
597+
# Build $source_file with a given #define, and return a true value
598+
# if a run of the resulting binary exits successfully.
599+
my $try_define = sub {
600+
my $define = shift;
601+
602+
unlink $obj, $exe;
603+
my @cmd = (
604+
'cl',
605+
'-I' . $solution->{options}->{perl} . '/lib/CORE',
606+
(map { "-D$_" } @perl_embed_ccflags, $define || ()),
607+
$source_file,
608+
'/link',
609+
$perl_libs[0]);
610+
my $compile_output = `@cmd 2>&1`;
611+
-f $exe || die "Failed to build Perl test:\n$compile_output";
612+
613+
{
614+
615+
# Some builds exhibit runtime failure through Perl warning
616+
# 'Can't spawn "conftest.exe"'; supress that.
617+
no warnings;
618+
619+
# Disable error dialog boxes like we do in the postmaster.
620+
# Here, we run code that triggers relevant errors.
621+
use Win32API::File qw(SetErrorMode :SEM_);
622+
my $oldmode = SetErrorMode(
623+
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
624+
system(".\\$exe");
625+
SetErrorMode($oldmode);
626+
}
627+
628+
return !($? >> 8);
629+
};
630+
631+
my $define_32bit_time = '_USE_32BIT_TIME_T';
632+
my $ok_now = $try_define->(undef);
633+
my $ok_32bit = $try_define->($define_32bit_time);
634+
unlink @conftest;
635+
if (!$ok_now && !$ok_32bit)
636+
{
637+
638+
# Unsupported configuration. Since we used %Config from the
639+
# Perl running the build scripts, this is expected if
640+
# attempting to link with some other Perl.
641+
die "Perl test fails with or without -D$define_32bit_time";
642+
}
643+
elsif ($ok_now && $ok_32bit)
644+
{
645+
646+
# Resulting build may work, but it's especially important to
647+
# verify with "vcregress plcheck". A refined test may avoid
648+
# this outcome.
649+
warn "Perl test passes with or without -D$define_32bit_time";
650+
}
651+
elsif ($ok_32bit)
652+
{
653+
push(@perl_embed_ccflags, $define_32bit_time);
654+
} # else $ok_now, hence no flag required
655+
}
656+
657+
print "CFLAGS recommended by Perl: $Config{ccflags}\n";
658+
print "CFLAGS to compile embedded Perl: ",
659+
(join ' ', map { "-D$_" } @perl_embed_ccflags), "\n";
539660
foreach my $f (@perl_embed_ccflags)
540661
{
541662
$plperl->AddDefine($f);
@@ -605,20 +726,6 @@ sub mkvcbuild
605726
die 'Failed to create plperl_opmask.h' . "\n";
606727
}
607728
}
608-
$plperl->AddReference($postgres);
609-
my $perl_path = $solution->{options}->{perl} . '\lib\CORE\*perl*';
610-
# ActivePerl 5.16 provided perl516.lib; 5.18 provided libperl518.a
611-
my @perl_libs =
612-
grep { /perl\d+\.lib$|libperl\d+\.a$/ } glob($perl_path);
613-
if (@perl_libs == 1)
614-
{
615-
$plperl->AddLibrary($perl_libs[0]);
616-
}
617-
else
618-
{
619-
die
620-
"could not identify perl library version matching pattern $perl_path\n";
621-
}
622729

623730
# Add transform module dependent on plperl
624731
my $hstore_plperl = AddTransformModule(
@@ -965,4 +1072,9 @@ sub AdjustModule
9651072
}
9661073
}
9671074

1075+
END
1076+
{
1077+
unlink @unlink_on_exit;
1078+
}
1079+
9681080
1;

0 commit comments

Comments
 (0)