Debian Codegraph @

inc/ExtUtils/HasCompiler.pm

raw
1 package ExtUtils::HasCompiler;
2 $ExtUtils::HasCompiler::VERSION = '0.025';
3 use strict;
4 use warnings;
5
6 use base 'Exporter';
7 our @EXPORT_OK = qw/can_compile_loadable_object can_compile_static_library can_compile_extension/;
8 our %EXPORT_TAGS = (all => \@EXPORT_OK);
9
10 use Config;
11 use Carp 'carp';
12 use File::Basename 'basename';
13 use File::Spec::Functions qw/catfile catdir rel2abs curdir/;
14 use File::Temp qw/tempdir tempfile/;
15
16 my $tempdir = tempdir('HASCOMPILERXXXX', CLEANUP => 1, DIR => rel2abs(curdir));
17
18 my $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
28 XS(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
51 EXPORT 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
63 END
64
65 my $counter = 1;
66 my %prelinking = map { $_ => 1 } qw/MSWin32 VMS aix/;
67
68 sub can_compile_loadable_object {
69 my %args = @_;
70
71 my $output = $args{output} || \*STDOUT;
72
73 my $config = $args{config} || 'ExtUtils::HasCompiler::Config';
74 return if not $config->get('usedl');
75
76 my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1);
77 my $basename = basename($source_name, '.c');
78 my $abs_basename = catfile($tempdir, $basename);
79
80 my ($cc, $ccflags, $optimize, $cccdlflags, $ld, $ldflags, $lddlflags, $libperl, $perllibs, $archlibexp, $_o, $dlext) = map { $config->get($_) } qw/cc ccflags optimize cccdlflags ld ldflags lddlflags libperl perllibs archlibexp _o dlext/;
81
82 my $incdir = catdir($archlibexp, 'CORE');
83 my $object_file = $abs_basename.$_o;
84 my $loadable_object = "$abs_basename.$dlext";
85
86 my @commands;
87 if ($^O eq 'MSWin32' && $cc =~ /^cl/) {
88 push @commands, qq{$cc $ccflags $cccdlflags $optimize /I "$incdir" /c $source_name /Fo$object_file};
89 push @commands, qq{$ld $object_file $lddlflags $libperl $perllibs /out:$loadable_object /def:$abs_basename.def /pdb:$abs_basename.pdb};
90 }
91 elsif ($^O eq 'VMS') {
92 # Mksymlists is only the beginning of the story.
93 open my $opt_fh, '>>', "$abs_basename.opt" or do { carp "Couldn't append to '$abs_basename.opt'"; return };
94 print $opt_fh "PerlShr/Share\n";
95 close $opt_fh;
96
97 my $incdirs = $ccflags =~ s{ /inc[^=]+ (?:=)+ (?:\()? ( [^\/\)]* ) }{}xi ? "$1,$incdir" : $incdir;
98 push @commands, qq{$cc $ccflags $optimize /include=($incdirs) $cccdlflags $source_name /obj=$object_file};
99 push @commands, qq{$ld $ldflags $lddlflags=$loadable_object $object_file,$abs_basename.opt/OPTIONS,${incdir}perlshr_attr.opt/OPTIONS' $perllibs};
100 }
101 else {
102 my @extra;
103 my $inc = qq{"-I$incdir"};
104 if ($^O eq 'MSWin32') {
105 my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0];
106 push @extra, "$abs_basename.def", $lib, $perllibs;
107 }
108 elsif ($^O =~ /^(cygwin|msys)$/) {
109 push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a');
110 }
111 elsif ($^O eq 'aix') {
112 $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/;
113 $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/;
114 }
115 elsif ($^O eq 'android') {
116 push @extra, qq{"-L$incdir"}, '-lperl', $perllibs;
117 }
118 elsif ($^O eq 'darwin' && $config->get('perlpath') eq '/usr/bin/perl' && ($config->get('osvers') =~ /(\d+)/)[0] >= 18) {
119 $inc = qq{-iwithsysroot "$incdir"};
120 }
121 push @commands, qq{$cc $ccflags $optimize $inc $cccdlflags -c $source_name -o $object_file};
122 push @commands, qq{$ld $object_file -o $loadable_object $lddlflags @extra};
123 }
124
125 if ($prelinking{$^O}) {
126 require ExtUtils::Mksymlists;
127 ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {});
128 }
129
130 my $shortname = '_Loadable' . $counter++;
131 my $package = "ExtUtils::HasCompiler::$shortname";
132 printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return };
133 close $source_handle or do { carp "Couldn't close $source_name: $!"; return };
134
135 for my $command (@commands) {
136 print $output "$command\n" if not $args{quiet};
137 system $command and do { carp "Couldn't execute $command: $!"; return };
138 }
139
140 # Skip loading when cross-compiling
141 return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile');
142
143 require DynaLoader;
144 local @DynaLoader::dl_require_symbols = "boot_$basename";
145 my $handle = DynaLoader::dl_load_file(rel2abs($loadable_object), 0);
146 if ($handle) {
147 my $symbol = DynaLoader::dl_find_symbol($handle, "boot_$basename") or do { carp "Couldn't find boot symbol for $basename"; return };
148 my $compilet = DynaLoader::dl_install_xsub('__ANON__::__ANON__', $symbol, $source_name);
149 my $ret = eval { $compilet->(); $package->exported } or carp $@;
150 delete $ExtUtils::HasCompiler::{"$shortname\::"};
151 eval { DynaLoader::dl_unload_file($handle) } or carp $@;
152 return defined $ret && $ret == 42;
153 }
154 else {
155 carp "Couldn't load $loadable_object: " . DynaLoader::dl_error();
156 return;
157 }
158 }
159
160 my %static_unsupported_on = map { $_ => 1 } qw/VMS aix MSWin32 cygwin/;
161 sub can_compile_static_library {
162 my %args = @_;
163
164 my $output = $args{output} || \*STDOUT;
165
166 my $config = $args{config} || 'ExtUtils::HasCompiler::Config';
167 return if $config->get('useshrplib') eq 'true';
168
169 my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1);
170 my $basename = basename($source_name, '.c');
171 my $abs_basename = catfile($tempdir, $basename);
172
173 my ($cc, $ccflags, $optimize, $ar, $full_ar, $ranlib, $archlibexp, $_o, $lib_ext) = map { $config->get($_) } qw/cc ccflags optimize ar full_ar ranlib archlibexp _o lib_ext/;
174 my $incdir = catdir($archlibexp, 'CORE');
175 my $object_file = "$abs_basename$_o";
176 my $static_library = $abs_basename.$lib_ext;
177
178 my @commands;
179 if ($static_unsupported_on{$^O}) {
180 return;
181 }
182 else {
183 my $my_ar = length $full_ar ? $full_ar : $ar;
184 push @commands, qq{$cc $ccflags $optimize "-I$incdir" -c $source_name -o $object_file};
185 push @commands, qq{$my_ar cr $static_library $object_file};
186 push @commands, qq{$ranlib $static_library} if $ranlib ne ':';
187 }
188
189 my $shortname = '_Loadable' . $counter++;
190 my $package = "ExtUtils::HasCompiler::$shortname";
191 printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return };
192 close $source_handle or do { carp "Couldn't close $source_name: $!"; return };
193
194 for my $command (@commands) {
195 print $output "$command\n" if not $args{quiet};
196 system $command and do { carp "Couldn't execute $command: $!"; return };
197 }
198 return 1;
199 }
200
201 sub can_compile_extension {
202 my %args = @_;
203 $args{config} ||= 'ExtUtils::HasCompiler::Config';
204 my $linktype = $args{linktype} || ($args{config}->get('usedl') ? 'dynamic' : 'static');
205 return $linktype eq 'static' ? can_compile_static_library(%args) : can_compile_loadable_object(%args);
206 }
207
208 sub ExtUtils::HasCompiler::Config::get {
209 my (undef, $key) = @_;
210 return $ENV{uc $key} || $Config{$key};
211 }
212
213 1;
214
215 # ABSTRACT: Check for the presence of a compiler
216
217 __END__
218
219 =pod
220
221 =encoding UTF-8
222
223 =head1 NAME
224
225 ExtUtils::HasCompiler - Check for the presence of a compiler
226
227 =head1 VERSION
228
229 version 0.025
230
231 =head1 SYNOPSIS
232
233 use ExtUtils::HasCompiler 'can_compile_extension';
234 if (can_compile_extension()) {
235 ...
236 }
237 else {
238 ...
239 }
240
241 =head1 DESCRIPTION
242
243 This module tries to check if the current system is capable of compiling, linking and loading an XS module.
244
245 B<Notice>: this is an early release, interface stability isn't guaranteed yet.
246
247 =head1 FUNCTIONS
248
249 =head2 can_compile_loadable_object(%opts)
250
251 This checks if the system can compile, link and load a perl loadable object. It may take the following options:
252
253 =over 4
254
255 =item * quiet
256
257 Do not output the executed compilation commands.
258
259 =item * config
260
261 An L<ExtUtils::Config|ExtUtils::Config> (compatible) object for configuration.
262
263 =item * skip_load
264
265 This causes can_compile_loadable_object to not try to load the generated object. This defaults to true on a cross-compiling perl.
266
267 =back
268
269 =head2 can_compile_static_library(%opts)
270
271 This checks if the system can compile and link a perl static library. It does not check it it can compile a new perl with it. It may take the following options:
272
273 =over 4
274
275 =item * quiet
276
277 Do not output the executed compilation commands.
278
279 =item * config
280
281 An L<ExtUtils::Config|ExtUtils::Config> (compatible) object for configuration.
282
283 =back
284
285 =head2 can_compile_extension(%opts)
286
287 This will call either C<can_compile_loadable_object>, or C<can_compile_static_library>, depending on which is the default on your configuration. In addition to the arguments listed above, it can take one more optional argument:
288
289 =over 4
290
291 =item * linktype
292
293 This will force the linktype to be either static or dynamic. Dynamic compilation on a static perl won't work, but static libraries can be viable on a dynamic perl.
294
295 =back
296
297 =head1 AUTHOR
298
299 Leon Timmermans <[email protected]>
300
301 =head1 COPYRIGHT AND LICENSE
302
303 This software is copyright (c) 2014 by Leon Timmermans.
304
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307
308 =cut