update bundled ExtUtils::HasCompiler to 0.013
[gitmo/Class-C3.git] / inc / ExtUtils / HasCompiler.pm
CommitLineData
38787c29 1package ExtUtils::HasCompiler;
42540a11 2$ExtUtils::HasCompiler::VERSION = '0.013';
38787c29 3use strict;
4use warnings;
5
6use base 'Exporter';
7our @EXPORT_OK = qw/can_compile_loadable_object/;
8our %EXPORT_TAGS = (all => \@EXPORT_OK);
9
10use Config;
11use Carp 'carp';
12use File::Basename 'basename';
13use File::Spec::Functions qw/catfile catdir/;
14use File::Temp qw/tempdir tempfile/;
15
16my $tempdir = tempdir(CLEANUP => 1);
17
18my $loadable_object_format = <<'END';
19#define PERL_NO_GET_CONTEXT
20#include "EXTERN.h"
21#include "perl.h"
22#include "XSUB.h"
23
24#ifndef PERL_UNUSED_VAR
25#define PERL_UNUSED_VAR(var)
26#endif
27
28XS(exported) {
29#ifdef dVAR
30 dVAR;
31#endif
32 dXSARGS;
33
34 PERL_UNUSED_VAR(cv); /* -W */
35 PERL_UNUSED_VAR(items); /* -W */
36
37 XSRETURN_IV(42);
38}
39
40#ifndef XS_EXTERNAL
41#define XS_EXTERNAL(foo) XS(foo)
42#endif
43
44/* we don't want to mess with .def files on mingw */
45#if defined(WIN32) && defined(__GNUC__)
46# define EXPORT __declspec(dllexport)
47#else
48# define EXPORT
49#endif
50
51EXPORT XS_EXTERNAL(boot_%s) {
52#ifdef dVAR
53 dVAR;
54#endif
55 dXSARGS;
56
57 PERL_UNUSED_VAR(cv); /* -W */
58 PERL_UNUSED_VAR(items); /* -W */
59
60 newXS("%s::exported", exported, __FILE__);
61}
62
63END
64
65my $counter = 1;
66my %prelinking = map { $_ => 1 } qw/MSWin32 VMS aix/;
67
68sub can_compile_loadable_object {
69 my %args = @_;
70
71 my $config = $args{config} || 'ExtUtils::HasCompiler::Config';
72 return if not $config->get('usedl');
73
74 my ($source_handle, $source_name) = tempfile(DIR => $tempdir, SUFFIX => '.c', UNLINK => 1);
75 my $basename = basename($source_name, '.c');
76
77 my $shortname = '_Loadable' . $counter++;
78 my $package = "ExtUtils::HasCompiler::$shortname";
79 printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return };
80 close $source_handle or do { carp "Couldn't close $source_name: $!"; return };
81
82 my $abs_basename = catfile($tempdir, $basename);
83 my $object_file = $abs_basename . $config->get('_o');
84 my $loadable_object = $abs_basename . '.' . $config->get('dlext');
85 my $incdir = catdir($config->get('archlibexp'), 'CORE');
86
87 my ($cc, $ccflags, $optimize, $cccdlflags, $ld, $ldflags, $lddlflags, $libperl, $perllibs) = map { $config->get($_) } qw/cc ccflags optimize cccdlflags ld ldflags lddlflags libperl perllibs/;
88
89 if ($prelinking{$^O}) {
90 require ExtUtils::Mksymlists;
91 ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {});
92 }
93 my @commands;
94 if ($^O eq 'MSWin32' && $cc =~ /^cl/) {
95 push @commands, qq{$cc $ccflags $cccdlflags $optimize /I "$incdir" /c $source_name /Fo$object_file};
96 push @commands, qq{$ld $object_file $lddlflags $libperl $perllibs /out:$loadable_object /def:$abs_basename.def /pdb:$abs_basename.pdb};
97 }
98 elsif ($^O eq 'VMS') {
99 # Mksymlists is only the beginning of the story.
100 open my $opt_fh, '>>', "$abs_basename.opt" or do { carp "Couldn't append to '$abs_basename.opt'"; return };
101 print $opt_fh "PerlShr/Share\n";
102 close $opt_fh;
103
104 my $incdirs = $ccflags =~ s{ /inc[^=]+ (?:=)+ (?:\()? ( [^\/\)]* ) }{}xi ? "$1,$incdir" : $incdir;
105 push @commands, qq{$cc $ccflags $optimize /include=($incdirs) $cccdlflags $source_name /obj=$object_file};
106 push @commands, qq{$ld $ldflags $lddlflags=$loadable_object $object_file,$abs_basename.opt/OPTIONS,${incdir}perlshr_attr.opt/OPTIONS' $perllibs};
107 }
108 else {
109 my @extra;
110 if ($^O eq 'MSWin32') {
42540a11 111 my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0];
112 push @extra, "$abs_basename.def", $lib, $perllibs;
38787c29 113 }
114 elsif ($^O eq 'cygwin') {
115 push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a');
116 }
117 elsif ($^O eq 'aix') {
118 $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/;
119 $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/;
120 }
42540a11 121 elsif ($^O eq 'android') {
122 push @extra, qq{"-L$incdir"}, '-lperl', $perllibs;
123 }
38787c29 124 push @commands, qq{$cc $ccflags $optimize "-I$incdir" $cccdlflags -c $source_name -o $object_file};
42540a11 125 push @commands, qq{$cc $optimize $object_file -o $loadable_object $lddlflags @extra};
38787c29 126 }
127
128 for my $command (@commands) {
129 print "$command\n" if not $args{quiet};
130 system $command and do { carp "Couldn't execute $command: $!"; return };
131 }
132
133 # Skip loading when cross-compiling
134 return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile');
135
136 require DynaLoader;
137 local @DynaLoader::dl_require_symbols = "boot_$basename";
42540a11 138 my $handle = DynaLoader::dl_load_file(File::Spec->rel2abs($loadable_object), 0);
38787c29 139 if ($handle) {
140 my $symbol = DynaLoader::dl_find_symbol($handle, "boot_$basename") or do { carp "Couldn't find boot symbol for $basename"; return };
141 my $compilet = DynaLoader::dl_install_xsub('__ANON__::__ANON__', $symbol, $source_name);
142 my $ret = eval { $compilet->(); $package->exported } or carp $@;
143 delete $ExtUtils::HasCompiler::{"$shortname\::"};
144 eval { DynaLoader::dl_unload_file($handle) } or carp $@;
145 return defined $ret && $ret == 42;
146 }
147 else {
148 carp "Couldn't load $loadable_object: " . DynaLoader::dl_error();
149 return;
150 }
151}
152
153sub ExtUtils::HasCompiler::Config::get {
154 my (undef, $key) = @_;
155 return $ENV{uc $key} || $Config{$key};
156}
157
1581;
159
160# ABSTRACT: Check for the presence of a compiler
161
162__END__
163
164=pod
165
166=encoding UTF-8
167
168=head1 NAME
169
170ExtUtils::HasCompiler - Check for the presence of a compiler
171
172=head1 VERSION
173
42540a11 174version 0.013
38787c29 175
176=head1 DESCRIPTION
177
178This module tries to check if the current system is capable of compiling, linking and loading an XS module.
179
180B<Notice>: this is an early release, interface stability isn't guaranteed yet.
181
182=head1 FUNCTIONS
183
184=head2 can_compile_loadable_object(%opts)
185
186This checks if the system can compile, link and load a perl loadable object. It may take the following options:
187
188=over 4
189
190=item * quiet
191
192Do not output the executed compilation commands.
193
194=item * config
195
196An L<ExtUtils::Config|ExtUtils::Config> (compatible) object for configuration.
197
198=item * skip_load
199
200This causes can_compile_loadable_object to not try to load the generated object. This defaults to true on a cross-compiling perl.
201
202=back
203
204=head1 AUTHOR
205
206Leon Timmermans <leont@cpan.org>
207
208=head1 COPYRIGHT AND LICENSE
209
210This software is copyright (c) 2014 by Leon Timmermans.
211
212This is free software; you can redistribute it and/or modify it under
213the same terms as the Perl 5 programming language system itself.
214
215=cut