File Coverage

blib/lib/Alien/Build/Plugin/PkgConfig/PP.pm
Criterion Covered Total %
statement 97 106 91.5
branch 29 38 76.3
condition 6 8 75.0
subroutine 13 13 100.0
pod 2 2 100.0
total 147 167 88.0


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::PkgConfig::PP;
2              
3 7     7   1071 use strict;
  7         14  
  7         201  
4 7     7   41 use warnings;
  7         14  
  7         159  
5 7     7   112 use 5.008004;
  7         25  
6 7     7   402 use Alien::Build::Plugin;
  7         22  
  7         44  
7 7     7   43 use Carp ();
  7         14  
  7         140  
8 7     7   1361 use File::Which ();
  7         2693  
  7         163  
9 7     7   2219 use Env qw( @PKG_CONFIG_PATH );
  7         12772  
  7         48  
10              
11             # ABSTRACT: Probe system and determine library or tool properties using PkgConfig.pm
12             our $VERSION = '2.45'; # VERSION
13              
14              
15             has '+pkg_name' => sub {
16             Carp::croak "pkg_name is a required property";
17             };
18              
19              
20             has atleast_version => undef;
21              
22              
23             has exact_version => undef;
24              
25              
26             has max_version => undef;
27              
28              
29             has minimum_version => undef;
30              
31              
32 7     7   1724 use constant _min_version => '0.14026';
  7         15  
  7         7506  
33              
34             # private for now, used by negotiator
35             has register_prereqs => 1;
36              
37             sub available
38             {
39 19     19 1 118167 !!eval { require PkgConfig; PkgConfig->VERSION(_min_version) };
  19         2018  
  19         346458  
40             }
41              
42             sub _cleanup
43             {
44 16     16   2249 my($value) = @_;
45 16         100 $value =~ s{\s*$}{ };
46 16         47 $value;
47             }
48              
49             sub init
50             {
51 28     28 1 119 my($self, $meta) = @_;
52              
53 28 50       84 unless(defined $meta->prop->{env}->{PKG_CONFIG})
54             {
55             # TODO: Better would be to to "execute" lib/PkgConfig.pm
56             # as that should always be available, and will match the
57             # exact version of PkgConfig.pm that we are using here.
58             # there are a few corner cases to deal with before we
59             # can do this. What is here should handle most use cases.
60 28 0       116 my $command_line =
    0          
    50          
61             File::Which::which('ppkg-config')
62             ? 'ppkg-config'
63             : File::Which::which('pkg-config.pl')
64             ? 'pkg-config.pl'
65             : File::Which::which('pkg-config')
66             ? 'pkg-config'
67             : undef;
68 28 50       3817 $meta->prop->{env}->{PKG_CONFIG} = $command_line
69             if defined $command_line;
70             }
71              
72 28 100       123 if($self->register_prereqs)
73             {
74 21         58 $meta->add_requires('configure' => 'PkgConfig' => _min_version);
75             }
76              
77 28 100       88 my($pkg_name, @alt_names) = (ref $self->pkg_name) ? (@{ $self->pkg_name }) : ($self->pkg_name);
  2         6  
78              
79             $meta->register_hook(
80             probe => sub {
81 20     20   45 my($build) = @_;
82              
83 20   33     55 $build->runtime_prop->{legacy}->{name} ||= $pkg_name;
84 20         115 $build->hook_prop->{probe_class} = __PACKAGE__;
85 20         92 $build->hook_prop->{probe_instance_id} = $self->instance_id;
86              
87 20         122 require PkgConfig;
88 20         136 my $pkg = PkgConfig->find($pkg_name);
89 20 100       68443 die "package @{[ $pkg_name ]} not found" if $pkg->errmsg;
  3         72  
90              
91 17         289 $build->hook_prop->{version} = $pkg->pkg_version;
92 17         208 my $version = PkgConfig::Version->new($pkg->pkg_version);
93              
94 17         314 my $atleast_version = $self->atleast_version;
95 17 100       50 $atleast_version = $self->minimum_version unless defined $atleast_version;
96 17 100       46 if(defined $atleast_version)
97             {
98 7         20 my $need = PkgConfig::Version->new($atleast_version);
99 7 100       81 if($version < $need)
100             {
101 2         73 die "package @{[ $pkg_name ]} is @{[ $pkg->pkg_version ]}, but at least $atleast_version is required.";
  2         10  
  2         34  
102             }
103             }
104              
105 15 100       182 if(defined $self->exact_version)
106             {
107 4         11 my $need = PkgConfig::Version->new($self->exact_version);
108 4 100       30 if($version != $need)
109             {
110 3         71 die "package @{[ $pkg_name ]} is @{[ $pkg->pkg_version ]}, but exactly @{[ $self->exact_version ]} is required.";
  3         12  
  3         49  
  3         29  
111             }
112             }
113              
114 12 100       54 if(defined $self->max_version)
115             {
116 5         22 my $need = PkgConfig::Version->new($self->max_version);
117 5 100       35 if($version > $need)
118             {
119 2         41 die "package @{[ $pkg_name ]} is @{[ $pkg->pkg_version ]}, but max of @{[ $self->max_version ]} is required.";
  2         7  
  2         31  
  2         18  
120             }
121             }
122              
123 10         77 foreach my $alt (@alt_names)
124             {
125 1         5 my $pkg = PkgConfig->find($alt);
126 1 50       3506 die "package $alt not found" if $pkg->errmsg;
127             }
128              
129 10         85 'system';
130             },
131 28         291 );
132              
133             $meta->register_hook(
134             $_ => sub {
135 4     4   11 my($build) = @_;
136              
137             return if $build->hook_prop->{name} eq 'gather_system'
138 4 100 100     13 && ($build->install_prop->{system_probe_instance_id} || '') ne $self->instance_id;
      100        
139              
140 3         20 require PkgConfig;
141              
142 3         10 foreach my $name ($pkg_name, @alt_names)
143             {
144 4         15 require PkgConfig;
145 4         27 my $pkg = PkgConfig->find($name, search_path => [@PKG_CONFIG_PATH]);
146 4 50       13846 if($pkg->errmsg)
147             {
148 0         0 $build->log("Trying to load the pkg-config information from the source code build");
149 0         0 $build->log("of your package failed");
150 0         0 $build->log("You are currently using the pure-perl implementation of pkg-config");
151 0         0 $build->log("(AB Plugin is named PkgConfig::PP, which uses PkgConfig.pm");
152 0         0 $build->log("It may work better with the real pkg-config.");
153 0         0 $build->log("Try installing your OS' version of pkg-config or unset ALIEN_BUILD_PKG_CONFIG");
154 0         0 die "second load of PkgConfig.pm @{[ $name ]} failed: @{[ $pkg->errmsg ]}"
  0         0  
  0         0  
155             }
156 4         39 my %prop;
157 4         14 $prop{cflags} = _cleanup scalar $pkg->get_cflags;
158 4         14 $prop{libs} = _cleanup scalar $pkg->get_ldflags;
159 4         56 $prop{version} = $pkg->pkg_version;
160 4         37 $pkg = PkgConfig->find($name, static => 1, search_path => [@PKG_CONFIG_PATH]);
161 4         13999 $prop{cflags_static} = _cleanup scalar $pkg->get_cflags;
162 4         13 $prop{libs_static} = _cleanup scalar $pkg->get_ldflags;
163 4         15 $build->runtime_prop->{alt}->{$name} = \%prop;
164             }
165 3         8 foreach my $key (keys %{ $build->runtime_prop->{alt}->{$pkg_name} })
  3         8  
166             {
167 15         27 $build->runtime_prop->{$key} = $build->runtime_prop->{alt}->{$pkg_name}->{$key};
168             }
169 3 100       8 if(keys %{ $build->runtime_prop->{alt} } == 1)
  3         7  
170             {
171 2         5 delete $build->runtime_prop->{alt};
172             }
173             }
174 28         230 ) for qw( gather_system gather_share );
175              
176 28         84 $self;
177             }
178              
179             1;
180              
181             __END__