update EUHC
[gitmo/Class-C3.git] / inc / ExtUtils / HasCompiler.pm
CommitLineData
38787c29 1package ExtUtils::HasCompiler;
9f7312b9 2$ExtUtils::HasCompiler::VERSION = '0.016';
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';
9f7312b9 13use File::Spec::Functions qw/catfile catdir rel2abs/;
38787c29 14use File::Temp qw/tempdir tempfile/;
15
9f7312b9 16my $tempdir = tempdir('HASCOMPILERXXXX', CLEANUP => 1, DIR => '.');
38787c29 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
9f7312b9 71 my $output = $args{output} || \*STDOUT;
72
38787c29 73 my $config = $args{config} || 'ExtUtils::HasCompiler::Config';
74 return if not $config->get('usedl');
75
9f7312b9 76 my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1);
38787c29 77 my $basename = basename($source_name, '.c');
78
79 my $shortname = '_Loadable' . $counter++;
80 my $package = "ExtUtils::HasCompiler::$shortname";
81 printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return };
82 close $source_handle or do { carp "Couldn't close $source_name: $!"; return };
83
84 my $abs_basename = catfile($tempdir, $basename);
85 my $object_file = $abs_basename . $config->get('_o');
86 my $loadable_object = $abs_basename . '.' . $config->get('dlext');
87 my $incdir = catdir($config->get('archlibexp'), 'CORE');
88
89 my ($cc, $ccflags, $optimize, $cccdlflags, $ld, $ldflags, $lddlflags, $libperl, $perllibs) = map { $config->get($_) } qw/cc ccflags optimize cccdlflags ld ldflags lddlflags libperl perllibs/;
90
91 if ($prelinking{$^O}) {
92 require ExtUtils::Mksymlists;
93 ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {});
94 }
95 my @commands;
96 if ($^O eq 'MSWin32' && $cc =~ /^cl/) {
97 push @commands, qq{$cc $ccflags $cccdlflags $optimize /I "$incdir" /c $source_name /Fo$object_file};
98 push @commands, qq{$ld $object_file $lddlflags $libperl $perllibs /out:$loadable_object /def:$abs_basename.def /pdb:$abs_basename.pdb};
99 }
100 elsif ($^O eq 'VMS') {
101 # Mksymlists is only the beginning of the story.
102 open my $opt_fh, '>>', "$abs_basename.opt" or do { carp "Couldn't append to '$abs_basename.opt'"; return };
103 print $opt_fh "PerlShr/Share\n";
104 close $opt_fh;
105
106 my $incdirs = $ccflags =~ s{ /inc[^=]+ (?:=)+ (?:\()? ( [^\/\)]* ) }{}xi ? "$1,$incdir" : $incdir;
107 push @commands, qq{$cc $ccflags $optimize /include=($incdirs) $cccdlflags $source_name /obj=$object_file};
108 push @commands, qq{$ld $ldflags $lddlflags=$loadable_object $object_file,$abs_basename.opt/OPTIONS,${incdir}perlshr_attr.opt/OPTIONS' $perllibs};
109 }
110 else {
111 my @extra;
112 if ($^O eq 'MSWin32') {
42540a11 113 my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0];
114 push @extra, "$abs_basename.def", $lib, $perllibs;
38787c29 115 }
116 elsif ($^O eq 'cygwin') {
117 push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a');
118 }
119 elsif ($^O eq 'aix') {
120 $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/;
121 $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/;
122 }
42540a11 123 elsif ($^O eq 'android') {
124 push @extra, qq{"-L$incdir"}, '-lperl', $perllibs;
125 }
38787c29 126 push @commands, qq{$cc $ccflags $optimize "-I$incdir" $cccdlflags -c $source_name -o $object_file};
9f7312b9 127 push @commands, qq{$ld $optimize $object_file -o $loadable_object $lddlflags @extra};
38787c29 128 }
129
130 for my $command (@commands) {
9f7312b9 131 print $output "$command\n" if not $args{quiet};
38787c29 132 system $command and do { carp "Couldn't execute $command: $!"; return };
133 }
134
135 # Skip loading when cross-compiling
136 return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile');
137
138 require DynaLoader;
139 local @DynaLoader::dl_require_symbols = "boot_$basename";
9f7312b9 140 my $handle = DynaLoader::dl_load_file(rel2abs($loadable_object), 0);
38787c29 141 if ($handle) {
142 my $symbol = DynaLoader::dl_find_symbol($handle, "boot_$basename") or do { carp "Couldn't find boot symbol for $basename"; return };
143 my $compilet = DynaLoader::dl_install_xsub('__ANON__::__ANON__', $symbol, $source_name);
144 my $ret = eval { $compilet->(); $package->exported } or carp $@;
145 delete $ExtUtils::HasCompiler::{"$shortname\::"};
146 eval { DynaLoader::dl_unload_file($handle) } or carp $@;
147 return defined $ret && $ret == 42;
148 }
149 else {
150 carp "Couldn't load $loadable_object: " . DynaLoader::dl_error();
151 return;
152 }
153}
154
155sub ExtUtils::HasCompiler::Config::get {
156 my (undef, $key) = @_;
157 return $ENV{uc $key} || $Config{$key};
158}
159
1601;
161
162# ABSTRACT: Check for the presence of a compiler
163
164__END__
165
166=pod
167
168=encoding UTF-8
169
170=head1 NAME
171
172ExtUtils::HasCompiler - Check for the presence of a compiler
173
174=head1 VERSION
175
9f7312b9 176version 0.016
38787c29 177
178=head1 DESCRIPTION
179
180This module tries to check if the current system is capable of compiling, linking and loading an XS module.
181
182B<Notice>: this is an early release, interface stability isn't guaranteed yet.
183
184=head1 FUNCTIONS
185
186=head2 can_compile_loadable_object(%opts)
187
188This checks if the system can compile, link and load a perl loadable object. It may take the following options:
189
190=over 4
191
192=item * quiet
193
194Do not output the executed compilation commands.
195
196=item * config
197
198An L<ExtUtils::Config|ExtUtils::Config> (compatible) object for configuration.
199
200=item * skip_load
201
202This causes can_compile_loadable_object to not try to load the generated object. This defaults to true on a cross-compiling perl.
203
204=back
205
206=head1 AUTHOR
207
208Leon Timmermans <leont@cpan.org>
209
210=head1 COPYRIGHT AND LICENSE
211
212This software is copyright (c) 2014 by Leon Timmermans.
213
214This is free software; you can redistribute it and/or modify it under
215the same terms as the Perl 5 programming language system itself.
216
217=cut