From: Peter Rabbitson Date: Sun, 10 Apr 2016 03:04:16 +0000 (+0200) Subject: Switch to EU::HC (with an inc::latest-like approach) X-Git-Tag: 0.27~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=58cd9d45;p=p5sagit%2Fnamespace-clean.git Switch to EU::HC (with an inc::latest-like approach) Implement semi-elaborate Makefile.PL boilerplate for EU::HC use. While at this point EU::HC seems rather stable, it still hasn't settled enough for complete comfort. This allow loading of "more-recent-than INC", in order to decrease amount of needed release churn to work around newly-found problems. --- diff --git a/Makefile.PL b/Makefile.PL index c7a36f7..04f20c3 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -19,7 +19,6 @@ my %META = ( configure => { requires => { 'ExtUtils::MakeMaker' => 0, - 'ExtUtils::CBuilder' => 0.27, } }, runtime => { @@ -59,13 +58,87 @@ my %MM_ARGS = ( ( and namespace::clean::_Util::_namer_load_error() and - can_xs() + usable_compiler_present() ) # when changing version, also change $sn_ver in namespace/clean/_Util.pm ? ( PREREQ_PM => { 'Sub::Name' => '0.04' } ) : () ); +## XS-checking BOILERPLATE ################################################### +sub usable_compiler_present { + return 0 if parse_args()->{PUREPERL_ONLY}; + + my $ucp; + + local $@; + eval { + # poor man's inc::latest + my $euhc_fn = 'ExtUtils/HasCompiler.pm'; + my $euhc_found_in_INC_dir; + + ( ($euhc_found_in_INC_dir) = grep { + not length ref $_ + and + -f "$_/$euhc_fn" + and + -r "$_/$euhc_fn" + } @INC ) + and + ( + MM->parse_version("$euhc_found_in_INC_dir/$euhc_fn") + > + MM->parse_version("inc/$euhc_fn") + ) + and + eval { require ExtUtils::HasCompiler }; + + unless ( $INC{'ExtUtils/HasCompiler.pm'} ) { + local @INC = ( "inc", @INC ); + require ExtUtils::HasCompiler; + } + + $ucp = ExtUtils::HasCompiler::can_compile_loadable_object(quiet => 1) + ? 1 + : 0 + ; + + 1; + }; + + if( my $used_fn = $INC{'ExtUtils/HasCompiler.pm'} ) { + printf + "ExtUtils::HasCompiler::can_compile_loadable_object() v%s (loaded from %s) returned: %s\n", + ExtUtils::HasCompiler->VERSION, + $used_fn, + ( defined($ucp) ? $ucp : "UNKNOWN" ), + ; + } + else { + print "Something went wrong when trying to load/use ExtUtils::HasCompiler:\n$@\n\n"; + } + + $ucp; +} + + +# FIXME - this has been cargo-culted from +# https://metacpan.org/source/HAARG/strictures-2.000002/Makefile.PL +# There likely will be better ways to handle %ENV and @ARGV directly within +# EU::HC in the future +sub parse_args { + # copied from EUMM + require ExtUtils::MakeMaker; + require Text::ParseWords; + ExtUtils::MakeMaker::parse_args( + my $tmp = {}, + Text::ParseWords::shellwords($ENV{PERL_MM_OPT} || ''), + @ARGV, + ); + return $tmp->{ARGS} || {}; +} +## END XS-checking BOILERPLATE ############################################### + ## BOILERPLATE ############################################################### require ExtUtils::MakeMaker; @@ -108,106 +181,3 @@ delete $MM_ARGS{CONFIGURE_REQUIRES} ExtUtils::MakeMaker::WriteMakefile(%MM_ARGS); ## END BOILERPLATE ########################################################### - - -# FIXME -# Need to replace with EU::HC, but too many changes for this release already -########################################### -# can we locate a (the) C compiler -sub can_cc { - my @chunks = split(/ /, $Config::Config{cc}) or return; - - # $Config{cc} may contain args; try to find out the program part - while (@chunks) { - return can_run("@chunks") || (pop(@chunks), next); - } - - return; -} - -# check if we can run some command -sub can_run { - my ($cmd) = @_; - - return $cmd if -x $cmd; - if (my $found_cmd = MM->maybe_command($cmd)) { - return $found_cmd; - } - - require File::Spec; - for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') { - next if $dir eq ''; - my $abs = File::Spec->catfile($dir, $cmd); - return $abs if (-x $abs or $abs = MM->maybe_command($abs)); - } - - return; -} - -# Can our C compiler environment build XS files -sub can_xs { - # Do we have the configure_requires checker? - local $@; - eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)"; - if ( $@ ) { - # They don't obey configure_requires, so it is - # someone old and delicate. Try to avoid hurting - # them by falling back to an older simpler test. - return can_cc(); - } - - # Do we have a working C compiler - my $builder = ExtUtils::CBuilder->new( - quiet => 1, - ); - unless ( $builder->have_compiler ) { - # No working C compiler - return 0; - } - - # Write a C file representative of what XS becomes - require File::Temp; - my ( $FH, $tmpfile ) = File::Temp::tempfile( - "compilexs-XXXXX", - SUFFIX => '.c', - ); - binmode $FH; - print $FH <<'END_C'; -#include "EXTERN.h" -#include "perl.h" -#include "XSUB.h" - -int main(int argc, char **argv) { - return 0; -} - -int boot_sanexs() { - return 1; -} - -END_C - close $FH; - - # Can the C compiler access the same headers XS does - my @libs = (); - my $object = undef; - eval { - local $^W = 0; - $object = $builder->compile( - source => $tmpfile, - ); - @libs = $builder->link( - objects => $object, - module_name => 'sanexs', - ); - }; - my $result = $@ ? 0 : 1; - - # Clean up all the build files - foreach ( $tmpfile, $object, @libs ) { - next unless defined $_; - 1 while unlink; - } - - return $result; -} diff --git a/inc/ExtUtils/HasCompiler.pm b/inc/ExtUtils/HasCompiler.pm new file mode 100644 index 0000000..6991ece --- /dev/null +++ b/inc/ExtUtils/HasCompiler.pm @@ -0,0 +1,160 @@ +package ExtUtils::HasCompiler; +$ExtUtils::HasCompiler::VERSION = '0.014'; +use strict; +use warnings; + +use base 'Exporter'; +our @EXPORT_OK = qw/can_compile_loadable_object/; +our %EXPORT_TAGS = (all => \@EXPORT_OK); + +use Config; +use Carp 'carp'; +use File::Basename 'basename'; +use File::Spec::Functions qw/catfile catdir rel2abs/; +use File::Temp qw/tempdir tempfile/; + +my $tempdir = tempdir('HASCOMPILERXXXX', CLEANUP => 1, DIR => '.'); + +my $loadable_object_format = <<'END'; +#define PERL_NO_GET_CONTEXT +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +#ifndef PERL_UNUSED_VAR +#define PERL_UNUSED_VAR(var) +#endif + +XS(exported) { +#ifdef dVAR + dVAR; +#endif + dXSARGS; + + PERL_UNUSED_VAR(cv); /* -W */ + PERL_UNUSED_VAR(items); /* -W */ + + XSRETURN_IV(42); +} + +#ifndef XS_EXTERNAL +#define XS_EXTERNAL(foo) XS(foo) +#endif + +/* we don't want to mess with .def files on mingw */ +#if defined(WIN32) && defined(__GNUC__) +# define EXPORT __declspec(dllexport) +#else +# define EXPORT +#endif + +EXPORT XS_EXTERNAL(boot_%s) { +#ifdef dVAR + dVAR; +#endif + dXSARGS; + + PERL_UNUSED_VAR(cv); /* -W */ + PERL_UNUSED_VAR(items); /* -W */ + + newXS("%s::exported", exported, __FILE__); +} + +END + +my $counter = 1; +my %prelinking = map { $_ => 1 } qw/MSWin32 VMS aix/; + +sub can_compile_loadable_object { + my %args = @_; + + my $output = $args{output} || \*STDOUT; + + my $config = $args{config} || 'ExtUtils::HasCompiler::Config'; + return if not $config->get('usedl'); + + my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1); + my $basename = basename($source_name, '.c'); + + my $shortname = '_Loadable' . $counter++; + my $package = "ExtUtils::HasCompiler::$shortname"; + printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return }; + close $source_handle or do { carp "Couldn't close $source_name: $!"; return }; + + my $abs_basename = catfile($tempdir, $basename); + my $object_file = $abs_basename . $config->get('_o'); + my $loadable_object = $abs_basename . '.' . $config->get('dlext'); + my $incdir = catdir($config->get('archlibexp'), 'CORE'); + + my ($cc, $ccflags, $optimize, $cccdlflags, $ld, $ldflags, $lddlflags, $libperl, $perllibs) = map { $config->get($_) } qw/cc ccflags optimize cccdlflags ld ldflags lddlflags libperl perllibs/; + + if ($prelinking{$^O}) { + require ExtUtils::Mksymlists; + ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {}); + } + my @commands; + if ($^O eq 'MSWin32' && $cc =~ /^cl/) { + push @commands, qq{$cc $ccflags $cccdlflags $optimize /I "$incdir" /c $source_name /Fo$object_file}; + push @commands, qq{$ld $object_file $lddlflags $libperl $perllibs /out:$loadable_object /def:$abs_basename.def /pdb:$abs_basename.pdb}; + } + elsif ($^O eq 'VMS') { + # Mksymlists is only the beginning of the story. + open my $opt_fh, '>>', "$abs_basename.opt" or do { carp "Couldn't append to '$abs_basename.opt'"; return }; + print $opt_fh "PerlShr/Share\n"; + close $opt_fh; + + my $incdirs = $ccflags =~ s{ /inc[^=]+ (?:=)+ (?:\()? ( [^\/\)]* ) }{}xi ? "$1,$incdir" : $incdir; + push @commands, qq{$cc $ccflags $optimize /include=($incdirs) $cccdlflags $source_name /obj=$object_file}; + push @commands, qq{$ld $ldflags $lddlflags=$loadable_object $object_file,$abs_basename.opt/OPTIONS,${incdir}perlshr_attr.opt/OPTIONS' $perllibs}; + } + else { + my @extra; + if ($^O eq 'MSWin32') { + my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0]; + push @extra, "$abs_basename.def", $lib, $perllibs; + } + elsif ($^O eq 'cygwin') { + push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a'); + } + elsif ($^O eq 'aix') { + $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/; + $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/; + } + elsif ($^O eq 'android') { + push @extra, qq{"-L$incdir"}, '-lperl', $perllibs; + } + push @commands, qq{$cc $ccflags $optimize "-I$incdir" $cccdlflags -c $source_name -o $object_file}; + push @commands, qq{$cc $optimize $object_file -o $loadable_object $lddlflags @extra}; + } + + for my $command (@commands) { + print $output "$command\n" if not $args{quiet}; + system $command and do { carp "Couldn't execute $command: $!"; return }; + } + + # Skip loading when cross-compiling + return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile'); + + require DynaLoader; + local @DynaLoader::dl_require_symbols = "boot_$basename"; + my $handle = DynaLoader::dl_load_file(rel2abs($loadable_object), 0); + if ($handle) { + my $symbol = DynaLoader::dl_find_symbol($handle, "boot_$basename") or do { carp "Couldn't find boot symbol for $basename"; return }; + my $compilet = DynaLoader::dl_install_xsub('__ANON__::__ANON__', $symbol, $source_name); + my $ret = eval { $compilet->(); $package->exported } or carp $@; + delete $ExtUtils::HasCompiler::{"$shortname\::"}; + eval { DynaLoader::dl_unload_file($handle) } or carp $@; + return defined $ret && $ret == 42; + } + else { + carp "Couldn't load $loadable_object: " . DynaLoader::dl_error(); + return; + } +} + +sub ExtUtils::HasCompiler::Config::get { + my (undef, $key) = @_; + return $ENV{uc $key} || $Config{$key}; +} + +1;