| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Alien::Build::Plugin::Probe::CBuilder; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 6377 | use strict; | 
|  | 4 |  |  |  |  | 18 |  | 
|  | 4 |  |  |  |  | 106 |  | 
| 4 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 81 |  | 
| 5 | 4 |  |  | 4 |  | 58 | use 5.008004; | 
|  | 4 |  |  |  |  | 14 |  | 
| 6 | 4 |  |  | 4 |  | 1126 | use Alien::Build::Plugin; | 
|  | 4 |  |  |  |  | 16 |  | 
|  | 4 |  |  |  |  | 25 |  | 
| 7 | 4 |  |  | 4 |  | 770 | use File::chdir; | 
|  | 4 |  |  |  |  | 5277 |  | 
|  | 4 |  |  |  |  | 397 |  | 
| 8 | 4 |  |  | 4 |  | 26 | use File::Temp (); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 91 |  | 
| 9 | 4 |  |  | 4 |  | 19 | use Capture::Tiny qw( capture_merged capture ); | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 3539 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # ABSTRACT: Probe for system libraries by guessing with ExtUtils::CBuilder | 
| 12 |  |  |  |  |  |  | our $VERSION = '2.47'; # VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has options => sub { {} }; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has cflags  => ''; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | has libs    => ''; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has program => 'int main(int argc, char *argv[]) { return 0; }'; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has version => undef; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | has aliens => []; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has lang => 'C'; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub init | 
| 36 |  |  |  |  |  |  | { | 
| 37 | 5 |  |  | 5 | 1 | 14 | my($self, $meta) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 5 |  |  |  |  | 19 | $meta->add_requires('configure' => 'ExtUtils::CBuilder' => 0 ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 5 | 50 |  |  |  | 7 | if(@{ $self->aliens }) | 
|  | 5 |  |  |  |  | 15 |  | 
| 42 |  |  |  |  |  |  | { | 
| 43 | 0 | 0 | 0 |  |  | 0 | die "You can't specify both 'aliens' and either 'cflags' or 'libs' for the Probe::CBuilder plugin" if $self->cflags || $self->libs; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  | 0 | $meta->add_requires('configure' => $_ => 0 ) for @{ $self->aliens }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 46 | 0 |  |  |  |  | 0 | $meta->add_requires('Alien::Build::Plugin::Probe::CBuilder' => '0.53'); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  | 0 | my $cflags = ''; | 
| 49 | 0 |  |  |  |  | 0 | my $libs   = ''; | 
| 50 | 0 |  |  |  |  | 0 | foreach my $alien (@{ $self->aliens }) | 
|  | 0 |  |  |  |  | 0 |  | 
| 51 |  |  |  |  |  |  | { | 
| 52 | 0 |  |  |  |  | 0 | my $pm = "$alien.pm"; | 
| 53 | 0 |  |  |  |  | 0 | $pm =~ s/::/\//g; | 
| 54 | 0 |  |  |  |  | 0 | require $pm; | 
| 55 | 0 |  |  |  |  | 0 | $cflags .= $alien->cflags . ' '; | 
| 56 | 0 |  |  |  |  | 0 | $libs   .= $alien->libs   . ' '; | 
| 57 |  |  |  |  |  |  | } | 
| 58 | 0 |  |  |  |  | 0 | $self->cflags($cflags); | 
| 59 | 0 |  |  |  |  | 0 | $self->libs($libs); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 5 |  |  |  |  | 9 | my @cpp; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 5 | 50 |  |  |  | 16 | if($self->lang ne 'C') | 
| 65 |  |  |  |  |  |  | { | 
| 66 | 0 |  |  |  |  | 0 | $meta->add_requires('Alien::Build::Plugin::Probe::CBuilder' => '0.53'); | 
| 67 | 0 | 0 |  |  |  | 0 | @cpp = ('C++' => 1) if $self->lang eq 'C++'; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | $meta->register_hook( | 
| 71 |  |  |  |  |  |  | probe => sub { | 
| 72 | 4 |  |  | 4 |  | 13 | my($build) = @_; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 4 |  |  |  |  | 23 | $build->hook_prop->{probe_class} = __PACKAGE__; | 
| 75 | 4 |  |  |  |  | 21 | $build->hook_prop->{probe_instance_id} = $self->instance_id; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 4 |  |  |  |  | 16 | local $CWD = File::Temp::tempdir( CLEANUP => 1, DIR => $CWD ); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 4 |  |  |  |  | 1683 | open my $fh, '>', 'mytest.c'; | 
| 80 | 4 |  |  |  |  | 31 | print $fh $self->program; | 
| 81 | 4 |  |  |  |  | 121 | close $fh; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 4 |  |  |  |  | 14 | $build->log("trying: cflags=@{[ $self->cflags ]} libs=@{[ $self->libs ]}"); | 
|  | 4 |  |  |  |  | 18 |  | 
|  | 4 |  |  |  |  | 19 |  | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 4 |  |  |  |  | 16 | my $cb = ExtUtils::CBuilder->new(%{ $self->options }); | 
|  | 4 |  |  |  |  | 20 |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 4 |  |  |  |  | 3444 | my($out1, $obj) = capture_merged { eval { | 
| 88 | 4 |  |  |  |  | 16 | $cb->compile( | 
| 89 |  |  |  |  |  |  | source               => 'mytest.c', | 
| 90 |  |  |  |  |  |  | extra_compiler_flags => $self->cflags, | 
| 91 |  |  |  |  |  |  | @cpp, | 
| 92 |  |  |  |  |  |  | ); | 
| 93 | 4 |  |  |  |  | 121 | } }; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 4 | 100 |  |  |  | 2848 | if(my $error = $@) | 
| 96 |  |  |  |  |  |  | { | 
| 97 | 1 |  |  |  |  | 8 | $build->log("compile failed: $error"); | 
| 98 | 1 |  |  |  |  | 7 | $build->log("compile failed: $out1"); | 
| 99 | 1 |  |  |  |  | 12 | die $error; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 3 |  |  |  |  | 2459 | my($out2, $exe) = capture_merged { eval { | 
| 103 | 3 |  |  |  |  | 13 | $cb->link_executable( | 
| 104 |  |  |  |  |  |  | objects              => [$obj], | 
| 105 |  |  |  |  |  |  | extra_linker_flags   => $self->libs, | 
| 106 |  |  |  |  |  |  | ); | 
| 107 | 3 |  |  |  |  | 89 | } }; | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 3 | 50 |  |  |  | 1889 | if(my $error = $@) | 
| 110 |  |  |  |  |  |  | { | 
| 111 | 0 |  |  |  |  | 0 | $build->log("link failed: $error"); | 
| 112 | 0 |  |  |  |  | 0 | $build->log("link failed: $out2"); | 
| 113 | 0 |  |  |  |  | 0 | die $error; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 3 | 50 |  |  |  | 82 | my($out, $err, $ret) = capture { system($^O eq 'MSWin32' ? $exe : "./$exe") }; | 
|  | 3 |  |  |  |  | 2563 |  | 
| 117 | 3 | 50 |  |  |  | 2261 | die "execute failed" if $ret; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 3 |  |  |  |  | 10 | my $cflags = $self->cflags; | 
| 120 | 3 |  |  |  |  | 9 | my $libs   = $self->libs; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 3 |  |  |  |  | 25 | $cflags =~ s{\s*$}{ }; | 
| 123 | 3 |  |  |  |  | 17 | $libs =~ s{\s*$}{ }; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 3 |  |  |  |  | 17 | $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id} = { | 
| 126 |  |  |  |  |  |  | cflags  => $cflags, | 
| 127 |  |  |  |  |  |  | libs    => $libs, | 
| 128 |  |  |  |  |  |  | }; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 3 | 100 |  |  |  | 10 | if(defined $self->version) | 
| 131 |  |  |  |  |  |  | { | 
| 132 | 1 |  |  |  |  | 4 | my($version) = $out =~ $self->version; | 
| 133 | 1 |  |  |  |  | 5 | $build->hook_prop->{version} = $version; | 
| 134 | 1 |  |  |  |  | 3 | $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id}->{version} = $version; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 3 |  |  |  |  | 29 | 'system'; | 
| 138 |  |  |  |  |  |  | } | 
| 139 | 5 |  |  |  |  | 51 | ); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | $meta->register_hook( | 
| 142 |  |  |  |  |  |  | gather_system => sub { | 
| 143 | 2 |  |  | 2 |  | 5 | my($build) = @_; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | return if $build->hook_prop->{name} eq 'gather_system' | 
| 146 | 2 | 50 | 50 |  |  | 7 | &&        ($build->install_prop->{system_probe_instance_id} || '') ne $self->instance_id; | 
|  |  |  | 33 |  |  |  |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 2 | 50 |  |  |  | 6 | if(my $p = $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id}) | 
| 149 |  |  |  |  |  |  | { | 
| 150 | 2 |  |  |  |  | 10 | $build->runtime_prop->{$_} = $p->{$_} for keys %$p; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  | }, | 
| 153 | 5 |  |  |  |  | 26 | ); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | 1; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | __END__ |