Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 140fa2f

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 a59b342 commit 140fa2f

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
@@ -28,6 +28,7 @@ my $libpgcommon;
2828
my $libpgfeutils;
2929
my $postgres;
3030
my $libpq;
31+
my @unlink_on_exit;
3132

3233
# Set of variables for modules in contrib/ and src/test/modules/
3334
my $contrib_defines = { 'refint' => 'REFINT_VERBOSE' };
@@ -516,34 +517,154 @@ sub mkvcbuild
516517
my $plperl =
517518
$solution->AddProject('plperl', 'dll', 'PLs', 'src/pl/plperl');
518519
$plperl->AddIncludeDir($solution->{options}->{perl} . '/lib/CORE');
520+
$plperl->AddReference($postgres);
521+
522+
my $perl_path = $solution->{options}->{perl} . '\lib\CORE\*perl*';
523+
524+
# ActivePerl 5.16 provided perl516.lib; 5.18 provided libperl518.a
525+
my @perl_libs =
526+
grep { /perl\d+\.lib$|libperl\d+\.a$/ } glob($perl_path);
527+
if (@perl_libs == 1)
528+
{
529+
$plperl->AddLibrary($perl_libs[0]);
530+
}
531+
else
532+
{
533+
die
534+
"could not identify perl library version matching pattern $perl_path\n";
535+
}
519536

520537
# Add defines from Perl's ccflags; see PGAC_CHECK_PERL_EMBED_CCFLAGS
521538
my @perl_embed_ccflags;
522539
foreach my $f (split(" ", $Config{ccflags}))
523540
{
524-
if ( $f =~ /^-D[^_]/
525-
|| $f =~ /^-D_USE_32BIT_TIME_T/)
541+
if ($f =~ /^-D[^_]/)
526542
{
527543
$f =~ s/\-D//;
528544
push(@perl_embed_ccflags, $f);
529545
}
530546
}
531547

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

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

631738
# Add transform module dependent on plperl
632739
my $hstore_plperl = AddTransformModule(
@@ -955,4 +1062,9 @@ sub AdjustModule
9551062
}
9561063
}
9571064

1065+
END
1066+
{
1067+
unlink @unlink_on_exit;
1068+
}
1069+
9581070
1;

0 commit comments

Comments
 (0)