| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ExtUtils::HasCompiler; | 
| 2 |  |  |  |  |  |  | $ExtUtils::HasCompiler::VERSION = '0.020'; # TRIAL | 
| 3 | 1 |  |  | 1 |  | 15585 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 5 | use base 'Exporter'; | 
|  | 1 |  |  |  |  | 16 |  | 
|  | 1 |  |  |  |  | 128 |  | 
| 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 | 1 |  |  | 1 |  | 6 | use Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 11 | 1 |  |  | 1 |  | 5 | use Carp 'carp'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 92 |  | 
| 12 | 1 |  |  | 1 |  | 6 | use File::Basename 'basename'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 66 |  | 
| 13 | 1 |  |  | 1 |  | 485 | use File::Spec::Functions qw/catfile catdir rel2abs/; | 
|  | 1 |  |  |  |  | 725 |  | 
|  | 1 |  |  |  |  | 62 |  | 
| 14 | 1 |  |  | 1 |  | 565 | use File::Temp qw/tempdir tempfile/; | 
|  | 1 |  |  |  |  | 17200 |  | 
|  | 1 |  |  |  |  | 1286 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my $tempdir = tempdir('HASCOMPILERXXXX', CLEANUP => 1, DIR => '.'); | 
| 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 | 2 |  |  | 2 | 1 | 83909 | my %args = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 2 |  | 50 |  |  | 19 | my $output = $args{output} || \*STDOUT; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 2 |  | 100 |  |  | 20 | my $config = $args{config} || 'ExtUtils::HasCompiler::Config'; | 
| 74 | 2 | 50 |  |  |  | 19 | return if not $config->get('usedl'); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 2 |  |  |  |  | 21 | my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1); | 
| 77 | 2 |  |  |  |  | 1120 | my $basename = basename($source_name, '.c'); | 
| 78 | 2 |  |  |  |  | 14 | my $abs_basename = catfile($tempdir, $basename); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 2 |  |  |  |  | 9 | 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/; | 
|  | 24 |  |  |  |  | 57 |  | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 2 |  |  |  |  | 11 | my $incdir = catdir($archlibexp, 'CORE'); | 
| 83 | 2 |  |  |  |  | 8 | my $object_file = $abs_basename.$_o; | 
| 84 | 2 |  |  |  |  | 7 | my $loadable_object = "$abs_basename.$dlext"; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 2 |  |  |  |  | 5 | my @commands; | 
| 87 | 2 | 50 | 33 |  |  | 18 | if ($^O eq 'MSWin32' && $cc =~ /^cl/) { | 
|  |  | 50 |  |  |  |  |  | 
| 88 | 0 |  |  |  |  | 0 | push @commands, qq{$cc $ccflags $cccdlflags $optimize /I "$incdir" /c $source_name /Fo$object_file}; | 
| 89 | 0 |  |  |  |  | 0 | 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 | 0 | 0 |  |  |  | 0 | open my $opt_fh, '>>', "$abs_basename.opt" or do { carp "Couldn't append to '$abs_basename.opt'"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 94 | 0 |  |  |  |  | 0 | print $opt_fh "PerlShr/Share\n"; | 
| 95 | 0 |  |  |  |  | 0 | close $opt_fh; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 | 0 |  |  |  | 0 | my $incdirs = $ccflags =~ s{ /inc[^=]+ (?:=)+ (?:\()? ( [^\/\)]* ) }{}xi ? "$1,$incdir" : $incdir; | 
| 98 | 0 |  |  |  |  | 0 | push @commands, qq{$cc $ccflags $optimize /include=($incdirs) $cccdlflags $source_name /obj=$object_file}; | 
| 99 | 0 |  |  |  |  | 0 | push @commands, qq{$ld $ldflags $lddlflags=$loadable_object $object_file,$abs_basename.opt/OPTIONS,${incdir}perlshr_attr.opt/OPTIONS' $perllibs}; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | else { | 
| 102 | 2 |  |  |  |  | 6 | my @extra; | 
| 103 | 2 | 50 |  |  |  | 21 | if ($^O eq 'MSWin32') { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 104 | 0 |  |  |  |  | 0 | my $lib = '-l' . ($libperl =~ /lib([^.]+)\./)[0]; | 
| 105 | 0 |  |  |  |  | 0 | push @extra, "$abs_basename.def", $lib, $perllibs; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | elsif ($^O eq 'cygwin') { | 
| 108 | 0 | 0 |  |  |  | 0 | push @extra, catfile($incdir, $config->get('useshrplib') ? 'libperl.dll.a' : 'libperl.a'); | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | elsif ($^O eq 'aix') { | 
| 111 | 0 |  |  |  |  | 0 | $lddlflags =~ s/\Q$(BASEEXT)\E/$abs_basename/; | 
| 112 | 0 |  |  |  |  | 0 | $lddlflags =~ s/\Q$(PERL_INC)\E/$incdir/; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | elsif ($^O eq 'android') { | 
| 115 | 0 |  |  |  |  | 0 | push @extra, qq{"-L$incdir"}, '-lperl', $perllibs; | 
| 116 |  |  |  |  |  |  | } | 
| 117 | 2 |  |  |  |  | 15 | push @commands, qq{$cc $ccflags $optimize "-I$incdir" $cccdlflags -c $source_name -o $object_file}; | 
| 118 | 2 |  |  |  |  | 12 | push @commands, qq{$ld $object_file -o $loadable_object $lddlflags @extra}; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 2 | 50 |  |  |  | 9 | if ($prelinking{$^O}) { | 
| 122 | 0 |  |  |  |  | 0 | require ExtUtils::Mksymlists; | 
| 123 | 0 |  |  |  |  | 0 | ExtUtils::Mksymlists::Mksymlists(NAME => $basename, FILE => $abs_basename, IMPORTS => {}); | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 2 |  |  |  |  | 9 | my $shortname = '_Loadable' . $counter++; | 
| 127 | 2 |  |  |  |  | 7 | my $package = "ExtUtils::HasCompiler::$shortname"; | 
| 128 | 2 | 50 |  |  |  | 30 | printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 129 | 2 | 50 |  |  |  | 91 | close $source_handle or do { carp "Couldn't close $source_name: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 2 |  |  |  |  | 9 | for my $command (@commands) { | 
| 132 | 4 | 50 |  |  |  | 88 | print $output "$command\n" if not $args{quiet}; | 
| 133 | 4 | 50 |  |  |  | 408937 | system $command and do { carp "Couldn't execute $command: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | # Skip loading when cross-compiling | 
| 137 | 2 | 50 |  |  |  | 93 | return 1 if exists $args{skip_load} ? $args{skip_load} : $config->get('usecrosscompile'); | 
|  |  | 50 |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 2 |  |  |  |  | 53 | require DynaLoader; | 
| 140 | 2 |  |  |  |  | 29 | local @DynaLoader::dl_require_symbols = "boot_$basename"; | 
| 141 | 2 |  |  |  |  | 43 | my $handle = DynaLoader::dl_load_file(rel2abs($loadable_object), 0); | 
| 142 | 2 | 50 |  |  |  | 644 | if ($handle) { | 
| 143 | 0 | 0 |  |  |  | 0 | my $symbol = DynaLoader::dl_find_symbol($handle, "boot_$basename") or do { carp "Couldn't find boot symbol for $basename"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 144 | 0 |  |  |  |  | 0 | my $compilet = DynaLoader::dl_install_xsub('__ANON__::__ANON__', $symbol, $source_name); | 
| 145 | 0 | 0 |  |  |  | 0 | my $ret = eval { $compilet->(); $package->exported } or carp $@; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 146 | 0 |  |  |  |  | 0 | delete $ExtUtils::HasCompiler::{"$shortname\::"}; | 
| 147 | 0 | 0 |  |  |  | 0 | eval { DynaLoader::dl_unload_file($handle) } or carp $@; | 
|  | 0 |  |  |  |  | 0 |  | 
| 148 | 0 |  | 0 |  |  | 0 | return defined $ret && $ret == 42; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  | else { | 
| 151 | 2 |  |  |  |  | 773 | carp "Couldn't load $loadable_object: " . DynaLoader::dl_error(); | 
| 152 | 2 |  |  |  |  | 229 | return; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | my %static_unsupported_on = map { $_ => 1 } qw/VMS aix MSWin32 cygwin/; | 
| 157 |  |  |  |  |  |  | sub can_compile_static_library { | 
| 158 | 0 |  |  | 0 | 1 | 0 | my %args = @_; | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 0 |  | 0 |  |  | 0 | my $output = $args{output} || \*STDOUT; | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 0 |  | 0 |  |  | 0 | my $config = $args{config} || 'ExtUtils::HasCompiler::Config'; | 
| 163 | 0 | 0 |  |  |  | 0 | return if $config->get('useshrplib') eq 'true'; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 |  |  |  |  | 0 | my ($source_handle, $source_name) = tempfile('TESTXXXX', DIR => $tempdir, SUFFIX => '.c', UNLINK => 1); | 
| 166 | 0 |  |  |  |  | 0 | my $basename = basename($source_name, '.c'); | 
| 167 | 0 |  |  |  |  | 0 | my $abs_basename = catfile($tempdir, $basename); | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 0 |  |  |  |  | 0 | 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/; | 
|  | 0 |  |  |  |  | 0 |  | 
| 170 | 0 |  |  |  |  | 0 | my $incdir = catdir($archlibexp, 'CORE'); | 
| 171 | 0 |  |  |  |  | 0 | my $object_file = "$abs_basename$_o"; | 
| 172 | 0 |  |  |  |  | 0 | my $static_library = $abs_basename.$lib_ext; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 0 |  |  |  |  | 0 | my @commands; | 
| 175 | 0 | 0 |  |  |  | 0 | if ($static_unsupported_on{$^O}) { | 
| 176 | 0 |  |  |  |  | 0 | return; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  | else { | 
| 179 | 0 | 0 |  |  |  | 0 | my $my_ar = length $full_ar ? $full_ar : $ar; | 
| 180 | 0 |  |  |  |  | 0 | push @commands, qq{$cc $ccflags $optimize "-I$incdir" -c $source_name -o $object_file}; | 
| 181 | 0 |  |  |  |  | 0 | push @commands, qq{$my_ar cr $static_library $object_file}; | 
| 182 | 0 | 0 |  |  |  | 0 | push @commands, qq{$ranlib $static_library} if $ranlib ne ':'; | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 0 |  |  |  |  | 0 | my $shortname = '_Loadable' . $counter++; | 
| 186 | 0 |  |  |  |  | 0 | my $package = "ExtUtils::HasCompiler::$shortname"; | 
| 187 | 0 | 0 |  |  |  | 0 | printf $source_handle $loadable_object_format, $basename, $package or do { carp "Couldn't write to $source_name: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 188 | 0 | 0 |  |  |  | 0 | close $source_handle or do { carp "Couldn't close $source_name: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  | 0 | for my $command (@commands) { | 
| 191 | 0 | 0 |  |  |  | 0 | print $output "$command\n" if not $args{quiet}; | 
| 192 | 0 | 0 |  |  |  | 0 | system $command and do { carp "Couldn't execute $command: $!"; return }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 193 |  |  |  |  |  |  | } | 
| 194 | 0 |  |  |  |  | 0 | return 1; | 
| 195 |  |  |  |  |  |  | } | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | sub can_compile_extension { | 
| 198 | 1 |  |  | 1 | 1 | 1171791 | my %args = @_; | 
| 199 | 1 |  | 50 |  |  | 14 | $args{config} ||= 'ExtUtils::HasCompiler::Config'; | 
| 200 | 1 |  | 33 |  |  | 15 | my $linktype = $args{linktype} || ($args{config}->get('usedl') ? 'dynamic' : 'static'); | 
| 201 | 1 | 50 |  |  |  | 14 | return $linktype eq 'static' ? can_compile_static_library(%args) : can_compile_loadable_object(%args); | 
| 202 |  |  |  |  |  |  | } | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | sub ExtUtils::HasCompiler::Config::get { | 
| 205 | 29 |  |  | 29 |  | 86 | my (undef, $key) = @_; | 
| 206 | 29 |  | 66 |  |  | 341 | return $ENV{uc $key} || $Config{$key}; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | 1; | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # ABSTRACT: Check for the presence of a compiler | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | __END__ |