1 package ExtUtils::HasCompiler;
2 $ExtUtils::HasCompiler::VERSION = '0.013';
7 our @EXPORT_OK = qw/can_compile_loadable_object/;
8 our %EXPORT_TAGS = (all => \@EXPORT_OK);
12 use File::Basename 'basename';
13 use File::Spec::Functions qw/catfile catdir/;
14 use File::Temp qw/tempdir tempfile/;
16 my $tempdir = tempdir(CLEANUP => 1);
18 my $loadable_object_format = <<'END';
19 #define PERL_NO_GET_CONTEXT
24 #ifndef PERL_UNUSED_VAR
25 #define PERL_UNUSED_VAR(var)
34 PERL_UNUSED_VAR(cv); /* -W */
35 PERL_UNUSED_VAR(items); /* -W */
41 #define XS_EXTERNAL(foo) XS(foo)
44 /* we don't want to mess with .def files on mingw */
45 #if defined(WIN32) && defined(__GNUC__)
46 # define EXPORT __declspec(dllexport)
51 EXPORT XS_EXTERNAL(boot_%s) {
57 PERL_UNUSED_VAR(cv); /* -W */
58 PERL_UNUSED_VAR(items); /* -W */
60 newXS("%s::exported", exported, __FILE__);
66 my %prelinking = map { $_ => 1 } qw/MSWin32 VMS aix/;
68 sub can_compile_loadable_object {
71 my $config = $args{config} || 'ExtUtils::HasCompiler::Config';
72 return if not $config->get('usedl');
74 my ($source_handle, $source_name) = tempfile(DIR => $tempdir, SUFFIX => '.c', UNLINK => 1);
75 my $basename = basename($source_name, '.c');
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 };
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');
87 my ($cc, $ccflags, $optimize, $cccdlflags, $ld, $ldflags, $lddlflags, $libperl, $perllibs) = map { $config->get($_) } qw/cc ccflags optimize cccdlflags ld ldflags lddlflags libperl perllibs/;
89 if ($prelinking{$^O}) {
90 require ExtUtils::Mksymlists;
91 ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {});
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};
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";
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};
110 if ($^O eq 'MSWin32') {
111 my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0];
112 push @extra, "$abs_basename.def", $lib, $perllibs;
114 elsif ($^O eq 'cygwin') {
115 push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a');
117 elsif ($^O eq 'aix') {
118 $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/;
119 $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/;
121 elsif ($^O eq 'android') {
122 push @extra, qq{"-L$incdir"}, '-lperl', $perllibs;
124 push @commands, qq{$cc $ccflags $optimize "-I$incdir" $cccdlflags -c $source_name -o $object_file};
125 push @commands, qq{$cc $optimize $object_file -o $loadable_object $lddlflags @extra};
128 for my $command (@commands) {
129 print "$command\n" if not $args{quiet};
130 system $command and do { carp "Couldn't execute $command: $!"; return };
133 # Skip loading when cross-compiling
134 return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile');
137 local @DynaLoader::dl_require_symbols = "boot_$basename";
138 my $handle = DynaLoader::dl_load_file(File::Spec->rel2abs($loadable_object), 0);
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;
148 carp "Couldn't load $loadable_object: " . DynaLoader::dl_error();
153 sub ExtUtils::HasCompiler::Config::get {
154 my (undef, $key) = @_;
155 return $ENV{uc $key} || $Config{$key};
160 # ABSTRACT: Check for the presence of a compiler
170 ExtUtils::HasCompiler - Check for the presence of a compiler
178 This module tries to check if the current system is capable of compiling, linking and loading an XS module.
180 B<Notice>: this is an early release, interface stability isn't guaranteed yet.
184 =head2 can_compile_loadable_object(%opts)
186 This checks if the system can compile, link and load a perl loadable object. It may take the following options:
192 Do not output the executed compilation commands.
196 An L<ExtUtils::Config|ExtUtils::Config> (compatible) object for configuration.
200 This causes can_compile_loadable_object to not try to load the generated object. This defaults to true on a cross-compiling perl.
206 Leon Timmermans <leont@cpan.org>
208 =head1 COPYRIGHT AND LICENSE
210 This software is copyright (c) 2014 by Leon Timmermans.
212 This is free software; you can redistribute it and/or modify it under
213 the same terms as the Perl 5 programming language system itself.