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

Commit 88f2977

Browse files
nmischvbwagner
authored andcommitted
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 4cf390c commit 88f2977

File tree

2 files changed

+161
-28
lines changed

2 files changed

+161
-28
lines changed

config/perl.m4

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

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

src/tools/msvc/Mkvcbuild.pm

Lines changed: 144 additions & 15 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' };
@@ -547,34 +548,154 @@ sub mkvcbuild
547548
my $plperl =
548549
$solution->AddProject('plperl', 'dll', 'PLs', 'src/pl/plperl');
549550
$plperl->AddIncludeDir($solution->{options}->{perl} . '/lib/CORE');
551+
$plperl->AddReference($postgres);
552+
553+
my $perl_path = $solution->{options}->{perl} . '\lib\CORE\*perl*';
554+
555+
# ActivePerl 5.16 provided perl516.lib; 5.18 provided libperl518.a
556+
my @perl_libs =
557+
grep { /perl\d+\.lib$|libperl\d+\.a$/ } glob($perl_path);
558+
if (@perl_libs == 1)
559+
{
560+
$plperl->AddLibrary($perl_libs[0]);
561+
}
562+
else
563+
{
564+
die
565+
"could not identify perl library version matching pattern $perl_path\n";
566+
}
550567

551568
# Add defines from Perl's ccflags; see PGAC_CHECK_PERL_EMBED_CCFLAGS
552569
my @perl_embed_ccflags;
553570
foreach my $f (split(" ", $Config{ccflags}))
554571
{
555-
if ( $f =~ /^-D[^_]/
556-
|| $f =~ /^-D_USE_32BIT_TIME_T/)
572+
if ($f =~ /^-D[^_]/)
557573
{
558574
$f =~ s/\-D//;
559575
push(@perl_embed_ccflags, $f);
560576
}
561577
}
562578

563-
# Perl versions before 5.13.4 don't provide -D_USE_32BIT_TIME_T
564-
# regardless of how they were built. On 32-bit Windows, assume
565-
# such a version was built with a pre-MSVC-2005 compiler, and
566-
# define the symbol anyway, so that we are compatible if we're
567-
# being built with a later MSVC version.
568-
push(@perl_embed_ccflags, '_USE_32BIT_TIME_T')
569-
if $solution->{platform} eq 'Win32'
570-
&& $Config{PERL_REVISION} == 5
571-
&& ($Config{PERL_VERSION} < 13
572-
|| ( $Config{PERL_VERSION} == 13
573-
&& $Config{PERL_SUBVERSION} < 4));
574-
575-
# Also, a hack to prevent duplicate definitions of uid_t/gid_t
579+
# hack to prevent duplicate definitions of uid_t/gid_t
576580
push(@perl_embed_ccflags, 'PLPERL_HAVE_UID_GID');
577581

582+
# Windows offers several 32-bit ABIs. Perl is sensitive to
583+
# sizeof(time_t), one of the ABI dimensions. To get 32-bit time_t,
584+
# use "cl -D_USE_32BIT_TIME_T" or plain "gcc". For 64-bit time_t, use
585+
# "gcc -D__MINGW_USE_VC2005_COMPAT" or plain "cl". Before MSVC 2005,
586+
# plain "cl" chose 32-bit time_t. PostgreSQL doesn't support building
587+
# with pre-MSVC-2005 compilers, but it does support linking to Perl
588+
# built with such a compiler. MSVC-built Perl 5.13.4 and later report
589+
# -D_USE_32BIT_TIME_T in $Config{ccflags} if applicable, but
590+
# MinGW-built Perl never reports -D_USE_32BIT_TIME_T despite typically
591+
# needing it. Ignore the $Config{ccflags} opinion about
592+
# -D_USE_32BIT_TIME_T, and use a runtime test to deduce the ABI Perl
593+
# expects. Specifically, test use of PL_modglobal, which maps to a
594+
# PerlInterpreter field whose position depends on sizeof(time_t).
595+
if ($solution->{platform} eq 'Win32')
596+
{
597+
my $source_file = 'conftest.c';
598+
my $obj = 'conftest.obj';
599+
my $exe = 'conftest.exe';
600+
my @conftest = ($source_file, $obj, $exe);
601+
push @unlink_on_exit, @conftest;
602+
unlink $source_file;
603+
open my $o, '>', $source_file
604+
|| croak "Could not write to $source_file";
605+
print $o '
606+
/* compare to plperl.h */
607+
#define __inline__ __inline
608+
#define PERL_NO_GET_CONTEXT
609+
#include <EXTERN.h>
610+
#include <perl.h>
611+
612+
int
613+
main(int argc, char **argv)
614+
{
615+
int dummy_argc = 1;
616+
char *dummy_argv[1] = {""};
617+
char *dummy_env[1] = {NULL};
618+
static PerlInterpreter *interp;
619+
620+
PERL_SYS_INIT3(&dummy_argc, (char ***) &dummy_argv,
621+
(char ***) &dummy_env);
622+
interp = perl_alloc();
623+
perl_construct(interp);
624+
{
625+
dTHX;
626+
const char key[] = "dummy";
627+
628+
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
629+
hv_store(PL_modglobal, key, sizeof(key) - 1, newSViv(1), 0);
630+
return hv_fetch(PL_modglobal, key, sizeof(key) - 1, 0) == NULL;
631+
}
632+
}
633+
';
634+
close $o;
635+
636+
# Build $source_file with a given #define, and return a true value
637+
# if a run of the resulting binary exits successfully.
638+
my $try_define = sub {
639+
my $define = shift;
640+
641+
unlink $obj, $exe;
642+
my @cmd = (
643+
'cl',
644+
'-I' . $solution->{options}->{perl} . '/lib/CORE',
645+
(map { "-D$_" } @perl_embed_ccflags, $define || ()),
646+
$source_file,
647+
'/link',
648+
$perl_libs[0]);
649+
my $compile_output = `@cmd 2>&1`;
650+
-f $exe || die "Failed to build Perl test:\n$compile_output";
651+
652+
{
653+
654+
# Some builds exhibit runtime failure through Perl warning
655+
# 'Can't spawn "conftest.exe"'; supress that.
656+
no warnings;
657+
658+
# Disable error dialog boxes like we do in the postmaster.
659+
# Here, we run code that triggers relevant errors.
660+
use Win32API::File qw(SetErrorMode :SEM_);
661+
my $oldmode = SetErrorMode(
662+
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
663+
system(".\\$exe");
664+
SetErrorMode($oldmode);
665+
}
666+
667+
return !($? >> 8);
668+
};
669+
670+
my $define_32bit_time = '_USE_32BIT_TIME_T';
671+
my $ok_now = $try_define->(undef);
672+
my $ok_32bit = $try_define->($define_32bit_time);
673+
unlink @conftest;
674+
if (!$ok_now && !$ok_32bit)
675+
{
676+
677+
# Unsupported configuration. Since we used %Config from the
678+
# Perl running the build scripts, this is expected if
679+
# attempting to link with some other Perl.
680+
die "Perl test fails with or without -D$define_32bit_time";
681+
}
682+
elsif ($ok_now && $ok_32bit)
683+
{
684+
685+
# Resulting build may work, but it's especially important to
686+
# verify with "vcregress plcheck". A refined test may avoid
687+
# this outcome.
688+
warn "Perl test passes with or without -D$define_32bit_time";
689+
}
690+
elsif ($ok_32bit)
691+
{
692+
push(@perl_embed_ccflags, $define_32bit_time);
693+
} # else $ok_now, hence no flag required
694+
}
695+
696+
print "CFLAGS recommended by Perl: $Config{ccflags}\n";
697+
print "CFLAGS to compile embedded Perl: ",
698+
(join ' ', map { "-D$_" } @perl_embed_ccflags), "\n";
578699
foreach my $f (@perl_embed_ccflags)
579700
{
580701
$plperl->AddDefine($f);
@@ -644,6 +765,7 @@ sub mkvcbuild
644765
die 'Failed to create plperl_opmask.h' . "\n";
645766
}
646767
}
768+
<<<<<<< HEAD
647769
$plperl->AddReference($postgres);
648770
my $perl_path = $solution->{options}->{perl} . '\lib\CORE\*.*';
649771
my @perl_libs =
@@ -658,6 +780,8 @@ sub mkvcbuild
658780
die
659781
"could not identify perl library version matching pattern $perl_path\n";
660782
}
783+
=======
784+
>>>>>>> 9b5c99790e... MSVC: Test whether 32-bit Perl needs -D_USE_32BIT_TIME_T.
661785
662786
# Add transform module dependent on plperl
663787
my $hstore_plperl = AddTransformModule(
@@ -1032,4 +1156,9 @@ sub AdjustModule
10321156
}
10331157
}
10341158
1159+
END
1160+
{
1161+
unlink @unlink_on_exit;
1162+
}
1163+
10351164
1;

0 commit comments

Comments
 (0)