File Coverage

blib/lib/PkgConfig/LibPkgConf/Client.pm
Criterion Covered Total %
statement 60 72 83.3
branch 21 28 75.0
condition 4 8 50.0
subroutine 14 15 93.3
pod 7 7 100.0
total 106 130 81.5


line stmt bran cond sub pod time code
1             package PkgConfig::LibPkgConf::Client;
2              
3 5     5   294135 use strict;
  5         19  
  5         138  
4 5     5   24 use warnings;
  5         11  
  5         130  
5 5     5   1488 use PkgConfig::LibPkgConf::XS;
  5         14  
  5         132  
6 5     5   28 use Scalar::Util ();
  5         9  
  5         4457  
7              
8             our $VERSION = '0.10';
9              
10             =head1 NAME
11              
12             PkgConfig::LibPkgConf::Client - Query installed libraries for compiling and linking software
13              
14             =head1 SYNOPSIS
15              
16             use PkgConfig::LibPkgConf::Client;
17            
18             my $client = PkgConfig::LibPkgConf::Client->new;
19             $client->env;
20            
21             my $pkg = $client->find('libarchive');
22             my $cflags = $pkg->cflags;
23             my $libs = $pkg->libs;
24              
25             =head1 DESCRIPTION
26              
27             The L objects store all necessary state
28             for C allowing for multiple instances to run in parallel.
29              
30             =head1 CONSTRUCTOR
31              
32             =head2 new
33              
34             my $client = PkgConfig::LibPkgConf::Client->new(%opts);
35             my $client = PkgConfig::LibPkgConf::Client->new(\%opts);
36              
37             Creates an instance of L. Possible
38             options include:
39              
40             =over 4
41              
42             =item path
43              
44             The search path to look for C<.pc> files. This may be specified
45             either as a string with the appropriate path separator character,
46             or as a list reference. This will override the C compiled
47             in defaults and the environment variables for C or
48             C.
49              
50             =item filter_lib_dirs
51              
52             List of directories to filter for libraries. This overrides the
53             C compiled in default and environment variable for
54             C.
55              
56             =item filter_include_dirs
57              
58             List of directories to filter for include. This overrides the
59             C compiled in default and environment variable for
60             C.
61              
62             =back
63              
64             environment variables honored:
65              
66             =over 4
67              
68             =item PKG_CONFIG_PATH
69              
70             =item PKG_CONFIG_LIBDIR
71              
72             =item PKG_CONFIG_SYSTEM_LIBRARY_PATH
73              
74             =item PKG_CONFIG_SYSTEM_INCLUDE_PATH
75              
76             =item PKG_CONFIG_ALLOW_SYSTEM_CFLAGS
77              
78             =item PKG_CONFIG_ALLOW_SYSTEM_LIBS
79              
80             =back
81              
82             =cut
83              
84             sub new
85             {
86 41     41 1 137123 my $class = shift;
87 41 50       184 my $opts = ref $_[0] eq 'HASH' ? { %{$_[0]} } : { @_ };
  0         0  
88              
89 41         115 my $self = bless {}, $class;
90              
91 41         60 my $eh = do {
92 41         73 my $o = $self;
93 41         134 Scalar::Util::weaken($o);
94 41     2   173 sub { $o->error($_[0]) };
  2         26  
95             };
96              
97             my $path_cvt = sub {
98 25 100   25   95 ref $_[0] ? join(PkgConfig::LibPkgConf::Util::path_sep(), @{$_[0]}) : $_[0];
  21         120  
99 41         158 };
100            
101 41 50 33     161 if($ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} && !defined $opts->{filter_include_dirs})
102             {
103 0         0 $opts->{filter_include_dirs} = [];
104             }
105            
106 41 50 33     132 if($ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} && !defined $opts->{filter_lib_dirs})
107             {
108 0         0 $opts->{filter_lib_dirs} = [];
109             }
110              
111 41 100       322 local $ENV{PKG_CONFIG_SYSTEM_LIBRARY_PATH} = $path_cvt->(delete $opts->{filter_lib_dirs}) if defined $opts->{filter_lib_dirs};
112 41 100       108 local $ENV{PKG_CONFIG_SYSTEM_INCLUDE_PATH} = $path_cvt->(delete $opts->{filter_include_dirs}) if defined $opts->{filter_include_dirs};
113              
114 41   100     1517 _init($self, $eh, delete $opts->{maxdepth} || 2000);
115              
116 41 100       105 if($opts->{path})
117             {
118 11         29 local $ENV{PKG_CONFIG_PATH} = $path_cvt->(delete $opts->{path});
119 11         322 $self->_dir_list_build(1);
120             }
121             else
122             {
123 30         435 $self->_dir_list_build(0);
124             }
125            
126 41 50       139 if(defined $ENV{PKG_CONFIG_TOP_BUILD_DIR})
127             {
128 0         0 $self->buildroot_dir($ENV{PKG_CONFIG_TOP_BUILD_DIR});
129             }
130 41 50       111 if(defined $ENV{PKG_CONFIG_SYSROOT_DIR})
131             {
132 0         0 $self->sysroot_dir($ENV{PKG_CONFIG_SYSROOT_DIR});
133             }
134              
135 41 100       109 if(my $global = delete $opts->{global})
136             {
137 3         20 $self->global($_ => $global->{$_}) for keys %$global;
138             }
139              
140 41         161 foreach my $key (sort keys %$opts)
141             {
142 0         0 require Carp;
143 0         0 Carp::carp("Unused unknown option $key");
144             }
145              
146 41         287 $self;
147             }
148              
149             =head1 ATTRIBUTES
150              
151             =head2 path
152              
153             my @path = $client->path;
154              
155             The search path to look for C<.pc> files.
156              
157             =head2 filter_lib_dirs
158              
159             my @dirs = $client->filter_lib_dirs;
160              
161             List of directories to filter for libraries.
162              
163             =head2 filter_include_dirs
164              
165             my @dirs = $client->filter_include_dirs;
166              
167             List of directories to filter for includes.
168              
169             =head2 sysroot_dir
170              
171             my $dir = $client->sysroot_dir;
172             $client->sysroot_dir($dir);
173              
174             Get or set the sysroot directory.
175              
176             =head2 buildroot_dir
177              
178             my $dir = $client->buildroot_dir;
179             $client->buildroot_dir($dir);
180              
181             Get or set the buildroot directory.
182              
183             =head2 maxdepth
184              
185             my $int = $client->maxdepth;
186             $client->maxdepth($int);
187              
188             Get or set the maximum dependency depth. This is 2000 by default.
189              
190             =head1 METHODS
191              
192             =head2 env
193              
194             my $client->env;
195              
196             This method loads settings for the client object from the environment using
197             the standard C or C environment variables. It honors the
198             following list of environment variables:
199              
200             =over 4
201              
202             =item PKG_CONFIG_LOG
203              
204             =back
205              
206             =cut
207              
208             # PKG_CONFIG_DEBUG_SPEW
209             # PKG_CONFIG_IGNORE_CONFLICTS
210             # PKG_CONFIG_PURE_DEPGRAPH
211             # PKG_CONFIG_DISABLE_UNINSTALLED
212              
213             sub env
214             {
215 3     3 1 7 my($self) = @_;
216 3 50       7 if($ENV{PKG_CONFIG_LOG})
217             {
218 0         0 $self->audit_set_log($ENV{PKG_CONFIG_LOG}, "w");
219             }
220 3         17 $self;
221             }
222              
223             =head2 find
224              
225             my $pkg = $client->find($package_name);
226              
227             Searches the <.pc> file for the package with the given C<$package_name>.
228             If found returns an instance of L. If
229             not found returns C.
230              
231             =cut
232              
233             sub _pkg
234             {
235 17     17   55 my($client, $ptr, @rest) = @_;
236 17         1127 require PkgConfig::LibPkgConf::Package;
237 17         120 bless {
238             client => $client,
239             ptr => $ptr,
240             @rest
241             }, 'PkgConfig::LibPkgConf::Package';
242             }
243              
244             sub find
245             {
246 18     18 1 69 my($self, $name) = @_;
247 18         2243 my $ptr = _find($self, $name);
248 18 100       89 $ptr ? _pkg($self, $ptr, name => $name) : ();
249             }
250              
251             =head2 package_from_file
252              
253             my $pkg = $client->package_from_file($filename);
254              
255             Load the specific <.pc> file.
256              
257             =cut
258              
259             sub package_from_file
260             {
261 1     1 1 7 my($self, $filename) = @_;
262 1         160 my $ptr = _package_from_file($self, $filename);
263 1 50       8 $ptr ? _pkg($self, $ptr, filename => $filename) : ();
264             }
265              
266             =head2 scan_all
267              
268             $client->scan_all(sub {
269             my($client, $package) = @_;
270             ...
271             return $bool;
272             });
273              
274             Iterates through all packages and calls the given subroutine reference
275             for each package. C<$package> isa L.
276             The scan will continue so long as a non true value is returned
277             (as C<$bool>).
278              
279             =cut
280              
281             sub scan_all
282             {
283 1     1 1 20675 my($self, $callback) = @_;
284              
285             my $wrapper = sub {
286 3     3   196 my($ptr) = @_;
287 3         7 my $package = _pkg($self, $ptr);
288 3         11 $callback->($self, $package);
289 1         5 };
290              
291 1         230 $self->_scan_all($wrapper);
292             }
293              
294             =head2 global
295              
296             $client->global($key => $value);
297             my $value = $client->global($key);
298              
299             Define or get the global variable.
300              
301             =cut
302              
303             sub global
304             {
305 7     7 1 24 my($self, $key, $value) = @_;
306 7 100       27 if(defined $value)
307             {
308 4         31 $self->_set_global("$key=$value");
309             }
310 7         40 $self->_get_global($key);
311             }
312              
313             =head2 error
314              
315             my $client->error($message);
316              
317             Called when C comes across a non-fatal error. By default
318             the error is simply displayed as a warning using L. The intention
319             of this method is if you want to override that behavior, you will subclass
320             L and implement your own version of the
321             C method.
322              
323             =cut
324              
325             sub error
326             {
327 0     0 1   my($self, $msg) = @_;
328 0           require Carp;
329 0           Carp::carp($msg);
330 0           1;
331             }
332              
333             1;
334              
335             =head2 audit_set_log
336              
337             $client->audit_set_log($filename, $mode);
338              
339             Opens a file with the C C style C<$mode>, and uses it for the
340             audit log. The file is managed entirely by the client class and will be
341             closed when the object falls out of scope. Examples:
342              
343             $client->audit_set_log("audit.log", "a"); # append to existing file
344             $client->audit_set_log("audit2.log", "w"); # new or replace file
345              
346             =head1 SUPPORT
347              
348             IRC #native on irc.perl.org
349              
350             Project GitHub tracker:
351              
352             L
353              
354             If you want to contribute, please open a pull request on GitHub:
355              
356             L
357              
358             =head1 SEE ALSO
359              
360             For additional related modules, see L
361              
362             =head1 AUTHOR
363              
364             Graham Ollis
365              
366             For additional contributors see L
367              
368             =head1 COPYRIGHT AND LICENSE
369              
370             This software is copyright (c) 2016 Graham Ollis.
371              
372             This is free software; you may redistribute it and/or modify it under
373             the same terms as the Perl 5 programming language system itself.
374              
375             =cut