blib/lib/Module/Build/Using/PkgConfig.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019 -- leonerd@leonerd.org.uk
5  
6 package Module::Build::Using::PkgConfig;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.010; # //
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base qw( Module::Build );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶use ExtUtils::PkgConfig;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 our $VERSION = '0.03';
16  
17 =head1 NAME
18  
19 C<Module::Build::Using::PkgConfig> - extend C<Module::Build> to more easily use platform libraries provided by F<pkg-config>
20  
21 =head1 SYNOPSIS
22  
23 In F<Build.PL>:
24  
25 use Module::Build::Using::PkgConfig;
26  
27 my $build = Module::Build::Using::PkgConfig->new(
28 module_name => "Module::Here",
29 ... # other arguments as per Module::Build
30 );
31  
32 # A platform library provided by pkg-config
33 $build->use_pkgconfig( "libfoo" );
34  
35 # We need at least a given version
36 $build->use_pkgconfig( "libbar",
37 atleast_version => "0.5",
38 );
39  
40 # A platform librariy that's also wrapped as an Alien module
41 $build->use_pkgconfig( "libsplot",
42 atleast_version => "1.0",
43 alien => "Alien::libsplot",
44 alien_version => "0.05", # Alien::libsplot 0.05 provides libsplot v1.0
45 );
46  
47 $build->create_build_script;
48  
49 =head1 DESCRIPTION
50  
51 This subclass of L<Module::Build> provides some handy methods to assist the
52 F<Build.PL> script of XS-based module distributions that make use of platform
53 libraries managed by F<pkg-config>.
54  
55 As well as supporting libraries installed on a platform-wide basis and thus
56 visible to F<pkg-config> itself, this subclass also assists with
57 C<Alien::>-based wrappers of these system libraries, allowing them to be
58 dynamically installed at build time if the platform does not provide them.
59  
60 =head2 RPATH generation
61  
62 This module also provides some helper code to generate the required C<RPATH>
63 arguments needed to link against the libraries found by inspecting the
64 C<extra_linker_flags>. This attempts to duplicate the same logic performed
65 by F<libtool> when it would link a C program or library, as we don't get to
66 use its code when linking dynamic libraries for Perl.
67  
68 =head1 PROPERTIES
69  
70 =over 4
71  
72 =item no_late_aliens => BOOL
73  
74 If true, applies the C<no_late_alien> option to every use of C<use_pkgconfig>
75 that specifies an Alien module.
76  
77 =back
78  
79 =cut
80  
81 =head1 METHODS
82  
83 =cut
84  
85 __PACKAGE__->add_property( 'no_late_aliens' );
86  
87 sub new
88 {
89 my $class = shift;
90 my $self = $class->SUPER::new( @_ );
91  
92 $self->configure_requires->{ +__PACKAGE__ } //= 0;
93 $self->configure_requires->{ $_ } //= 0 for our @ISA;
94  
95 if( $self->notes( 'late_aliens' ) ) {
96 $self->notes( 'late_aliens' => undef );
97 }
98  
99 return $self;
100 }
101  
102 =head2 use_pkgconfig
103  
104 $build->use_pkgconfig( $modname, ... )
105  
106 Requires the given F<pkg-config> module of the given version, and extends
107 the compiler and linker arguments sufficient to build from it.
108  
109 Takes the following named options:
110  
111 =over 4
112  
113 =item atleast_version => $modver
114  
115 If given, the F<pkg-config> module is required to be at least the given
116 version. If unspecified, then any version is considered sufficient.
117  
118 =item alien => $alien
119  
120 If given and the F<pkg-config> module does not exist, try to use the given
121 C<Alien::> module to provide it instead.
122  
123 If this module is not yet available and the C<no_late_alien> option is not
124 true, the Alien module is added to the C<requires> dynamic dependencies and
125 checked again at C<build> action time.
126  
127 =item no_late_alien => BOOL
128  
129 If true, suppresses the dynamic C<requires> feature of Alien modules
130 described above.
131  
132 =item alien_version => $version
133  
134 If the C<Alien::> module is not available, gives the module version of it that
135 will be required to provide the F<pkg-config> module of the required version.
136 This gets added to C<requires>.
137  
138 =back
139  
140 If neither the F<pkg-config> module and no C<Alien::> module was requested (or
141 none was found and C<no_late_alien> was set), this method dies with an
142 C<OS unsupported> message, which is usually what is required for a F<Build.PL>
143 script.
144  
145 =head2 try_pkconfig
146  
147 $ok = $build->try_pkconfig( $modname, ... )
148  
149 Boolean-returning version of L</use_pkgconfig>. If successful, returns true.
150 If it fails it returns false rather than dying, allowing the F<Build.PL>
151 script to take alternative action.
152  
153 =cut
154  
155 sub try_pkgconfig
156 {
157 my $self = shift;
158 my ( $modname, %args ) = @_;
159  
160 my $atleast_version = $args{atleast_version} // 0;
161  
162 if( $self->pkgconfig_atleast_version( $modname, $atleast_version ) ) {
163 print "Using $modname from pkg-config\n";
164 $self->add_cflags_libs_from_pkgconfig( $modname );
165 return 1;
166 }
167  
168 if( defined( my $alien = $args{alien} ) ) {
169 if( $self->alien_atleast_version( $alien, $atleast_version ) ) {
170 print "Using $modname from $alien\n";
171 $self->add_cflags_libs_from_alien( $alien );
172 return 1;
173 }
174  
175 return 0 if $args{no_late_alien} or $self->no_late_aliens;
176  
177 return $self->use_late_alien( $alien, %args );
178 }
179  
180 return 0;
181 }
182  
183 sub use_pkgconfig
184 {
185 my $self = shift;
186 my ( $modname ) = @_;
187  
188 $self->try_pkgconfig( @_ ) or
189 die "OS unsupported - $modname unavailable in pkg-config\n";
190 }
191  
192 =head2 pkgconfig_atleast_version
193  
194 $ok = $build->pkgconfig_atleast_version( $modname, $modver )
195  
196 Returns true if the F<pkg-config> module name exists and has at least the
197 given version.
198  
199 =cut
200  
201 sub pkgconfig_atleast_version
202 {
203 my $self = shift;
204 my ( $modname, $modver ) = @_;
205  
206 # Silence its scary errors
207 local *STDERR;
208 open STDERR, ">>", "/dev/null" or die "Cannot reopen STDERR - $!";
209  
210 print "Checking pkg-config $modname ",
211 ( $modver ? "--atleast_version $modver" : "" ),
212 "... ";
213  
214 my $ret = eval { ExtUtils::PkgConfig->atleast_version( $modname, $modver ) };
215  
216 print $ret ? "yes\n" : "no\n";
217  
218 return $ret;
219 }
220  
221 =head2 add_cflags_libs_from_pkgconfig
222  
223 $build->add_cflags_libs_from_pkgconfig( $modname )
224  
225 Extend the C<extra_compiler_flags> and C<extra_linker_flags> arguments from
226 the C<--cflags> and C<--libs> from the given F<pkg-config> module name.
227  
228 =cut
229  
230 sub add_cflags_libs_from_pkgconfig
231 {
232 my $self = shift;
233 my ( $modname ) = @_;
234  
235 my $cflags = ExtUtils::PkgConfig->cflags( $modname );
236 my $libs = ExtUtils::PkgConfig->libs( $modname );
237  
238 $self->push_extra_compiler_flags( split( m/ +/, $cflags // '' ) );
239  
240 $self->push_extra_linker_flags( split( m/ +/, $libs // '' ) );
241 }
242  
243 =head2 alien_atleast_version
244  
245 $ok = $build->alien_atleast_version( $alien, $modver )
246  
247 Returns true if the given C<Alien::> module provides a F<pkg-config> module
248 version at least the given version.
249  
250 =cut
251  
252 sub alien_atleast_version
253 {
254 my $self = shift;
255 my ( $alien, $modver ) = @_;
256  
257 print "Checking $alien ",
258 ( $modver ? "atleast-version $modver" : "" ),
259 "... ";
260  
261 ( my $file = "$alien.pm" ) =~ s{::}{/}g;
262  
263 my $ret = eval { require $file and $alien->atleast_version( $modver ) };
264  
265 print $ret ? "yes\n" : "no\n";
266  
267 return $ret;
268 }
269  
270 =head2 add_cflags_libs_from_alien
271  
272 $build->add_cflags_libs_from_alien( $alien )
273  
274 Extend the C<extra_compiler_flags> and C<extra_linker_flags> arguments from
275 the C<--cflags> and C<--libs> from the given C<Alien::> module name.
276  
277 =cut
278  
279 sub add_cflags_libs_from_alien
280 {
281 my $self = shift;
282 my ( $alien ) = @_;
283  
284 $self->push_extra_compiler_flags( split( m/ +/, $alien->cflags // '' ) );
285  
286 $self->push_extra_linker_flags( split( m/ +/, $alien->libs // '' ) );
287 }
288  
289 =head2 use_late_alien
290  
291 $ok = $build->use_late_alien( $alien, ... )
292  
293 Adds an Alien module directly to the C<requires> hash, and makes a note to use
294 its cflags and libraries later at build time.
295  
296 Normally this method would not be necessary as it is automatically called from
297 L<use_pkgconfig> if required, but one use-case may be to provide a final
298 last-ditch attempt after trying some other possible attempts, after an earlier
299 call to C<use_pkgconfig> with C<no_late_alien> set.
300  
301 =cut
302  
303 sub use_late_alien
304 {
305 my $self = shift;
306 my ( $alien, %args ) = @_;
307  
308 # No Alien module yet
309 print "Adding $alien to build_depends\n";
310  
311 # Order might matter
312 my $late_aliens = $self->notes( 'late_aliens' ) // [];
313  
314 push @$late_aliens, { alien => $alien, atleast_version => $args{atleast_version} };
315 $self->requires->{$alien} = $args{alien_version} // 0;
316  
317 $self->notes( 'late_aliens' => $late_aliens );
318  
319 return 1;
320 }
321  
322 =head2 push_extra_compiler_flags
323  
324 $build->push_extra_compiler_flags( @flags )
325  
326 Appends more values onto the C<extra_compiler_flags>.
327  
328 =cut
329  
330 sub push_extra_compiler_flags
331 {
332 my $self = shift;
333  
334 # Eugh; M::B's API here takes lists to set, but returns ARRAYrefs
335 $self->extra_compiler_flags( @{ $self->extra_compiler_flags }, @_ );
336 }
337  
338 =head2 push_extra_linker_flags
339  
340 $build->push_extra_linker_flags( @flags )
341  
342 Appends more values onto the C<extra_linker_flags>.
343  
344 =cut
345  
346 sub push_extra_linker_flags
347 {
348 my $self = shift;
349  
350 # Eugh; M::B's API here takes lists to set, but returns ARRAYrefs
351 $self->extra_linker_flags( @{ $self->extra_linker_flags }, @_ );
352 }
353  
354 sub ACTION_build
355 {
356 my $self = shift;
357 my @args = @_;
358  
359 if( my $late_aliens = $self->notes( 'late_aliens' ) ) {
360 foreach ( @$late_aliens ) {
361 my ( $alien, $modver ) = delete @{$_}{qw( alien atleast_version )};
362  
363 $self->alien_atleast_version( $alien, $modver ) or
364 die "Unable to find $alien providing pkg-config version $modver\n";
365  
366 $self->add_cflags_libs_from_alien( $alien );
367 }
368 }
369  
370 if( @{ $self->extra_linker_flags } ) {
371 $self->_generate_rpaths;
372 }
373  
374 $self->SUPER::ACTION_build( @args );
375 }
376  
377 sub _generate_rpaths
378 {
379 my $self = shift;
380  
381 # This is all a bit of a mess.
382 # In general, if we're being asked to link against libraries that aren't on
383 # the system search path, we'll have to invent extra -rpath arguments to
384 # pass to the linker to set RPATH in the resultant library.
385 #
386 # To work out what we need, we'll have to recreate the linker's search
387 # strategy, looking for actual libFOO.so.NUM files, and accumulate what
388 # paths they live in. Subtract from that any standard system locations, and
389 # whatever we've got left gets added as RUNPATH
390 #
391 # Also, none of this is specific to using pkg-config, and could apply
392 # equally to any C-library-using code. This probably wants moving to a more
393 # generic "Module::Build::Using::C" or somesuch if one ever gets built.
394 #
395 # Further, this is very GNU/ELF-specific. Likely different logic would
396 # apply on Mac OSX, Windows or other platforms.
397  
398 # TODO: Hunt /etc/ld.so.conf for more of these
399 my @system_libdirs = qw(
400 /lib
401 /usr/lib
402 );
403  
404 my @libdirs = @system_libdirs;
405 my @rpaths;
406  
407 my @ldflags = @{ $self->extra_linker_flags };
408 my @extra_linker_flags;
409  
410 FLAG: while( @ldflags ) {
411 my $flag = shift @ldflags;
412  
413 if( $flag =~ m/^-L(.*)$/ ) {
414 # Another -LDIR to search in
415 push @extra_linker_flags, $flag;
416  
417 push @libdirs, $1;
418 next FLAG;
419 }
420  
421 if( $flag =~ m/^-l(.*)$/ ) {
422 # Another libFOO.so to hunt for
423 push @extra_linker_flags, $flag;
424  
425 my $soname = "lib$1.so";
426 my $libdir;
427 -f "$_/$soname" and $libdir = $_ and last for @libdirs;
428 defined $libdir or next FLAG;
429  
430 # Skip if it's a system one, or already known
431 $_ eq $libdir and next FLAG for @system_libdirs, @rpaths;
432  
433 push @rpaths, $libdir;
434 next FLAG;
435 }
436  
437 if( $flag =~ m/^-Wl,-R(.+)$/ or
438 $flag =~ m/^-Wl,-rpath[=,](.*)$/ or
439 $flag =~ m/^-Wl,-rpath$/ && $ldflags[0] =~ m/^-Wl,(.*)$/ && shift @ldflags ) {
440 # Another RPATH is specified. Don't necessarily add it to
441 # @extra_linker_flags yet in case it's a duplicate
442 my $libdir = $1;
443  
444 # Don't try to validate if it's needed though; presume the linker
445 # flags generally know best.
446  
447 # Skip if it's a system one, or already known
448 $_ eq $libdir and next FLAG for @system_libdirs, @rpaths;
449  
450 push @rpaths, $libdir;
451 next FLAG;
452 }
453  
454 push @extra_linker_flags, $flag;
455 }
456  
457 foreach my $rpath ( @rpaths ) {
458 push @extra_linker_flags, "-Wl,-rpath=$rpath";
459 }
460  
461 # Reset the flags to remove any duplicates
462 $self->extra_linker_flags( @extra_linker_flags );
463 }
464  
465 =head1 TODO
466  
467 =over 4
468  
469 =item *
470  
471 Consider a C<quiet> option to suppress verbose printing
472  
473 =item *
474  
475 Consider defining a constructor argument, perhaps C<build_requires_pkgconfig>,
476 to neaten the common case of simple requirements.
477  
478 =item *
479  
480 Consider further stealing the various helper methods from L<ExtUtils::CChecker>
481 and possibly splitting this class into a lower "C-using XS modules" and
482 higher-level F<pkg-config>+Alien layer.
483  
484 =back
485  
486 =cut
487  
488 =head1 AUTHOR
489  
490 Paul Evans <leonerd@leonerd.org.uk>
491  
492 =cut
493  
494 0x55AA;
↑ Summary