File Coverage

blib/lib/Alien/Build/Plugin/PkgConfig/MakeStatic.pm
Criterion Covered Total %
statement 47 48 97.9
branch 15 22 68.1
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 73 81 90.1


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::PkgConfig::MakeStatic;
2              
3 2     2   1383 use strict;
  2         4  
  2         62  
4 2     2   10 use warnings;
  2         6  
  2         45  
5 2     2   34 use 5.008004;
  2         7  
6 2     2   444 use Alien::Build::Plugin;
  2         6  
  2         16  
7 2     2   12 use Path::Tiny ();
  2         4  
  2         1436  
8              
9             # ABSTRACT: Convert .pc files into static
10             our $VERSION = '2.46'; # VERSION
11              
12              
13             has path => undef;
14              
15             sub _convert
16             {
17 2     2   8 my($self, $build, $path) = @_;
18              
19 2 50       9 die "unable to read $path" unless -r $path;
20 2 50       59 die "unable to write $path" unless -w $path;
21              
22 2         43 $build->log("converting $path to static");
23              
24             my %h = map {
25 2         19 my($key, $value) = /^(.*?):(.*?)$/;
  6         550  
26 6         28 $value =~ s{^\s+}{};
27 6         17 $value =~ s{\s+$}{};
28 6         27 ($key => $value);
29             } grep /^(?:Libs|Cflags)(?:\.private)?:/, $path->lines;
30              
31 2 100       12 $h{Cflags} = '' unless defined $h{Cflags};
32 2 50       5 $h{Libs} = '' unless defined $h{Libs};
33              
34 2 100       16 $h{Cflags} .= ' ' . $h{"Cflags.private"} if defined $h{"Cflags.private"};
35 2 50       8 $h{Libs} .= ' ' . $h{"Libs.private"} if defined $h{"Libs.private"};
36              
37 2         5 $h{"Cflags.private"} = '';
38 2         4 $h{"Libs.private"} = '';
39              
40             $path->edit_lines(sub {
41              
42 8 100   8   969 if(/^(.*?):/)
43             {
44 6         17 my $key = $1;
45 6 50       17 if(defined $h{$key})
46             {
47 6         34 s/^(.*?):.*$/$1: $h{$key} /;
48 6         20 delete $h{$key};
49             }
50             }
51              
52 2         19 });
53              
54 2         390 $path->append("$_: $h{$_}\n") foreach keys %h;
55             }
56              
57             sub _recurse
58             {
59 3     3   119 my($self, $build, $dir) = @_;
60              
61 3         11 foreach my $child ($dir->children)
62             {
63 4 100       812 if(-d $child)
    50          
64             {
65 2         50 $self->_recurse($build, $child);
66             }
67             elsif($child->basename =~ /\.pc$/)
68             {
69 2         137 $self->_convert($build, $child);
70             }
71             }
72             }
73              
74             sub init
75             {
76 1     1 1 4 my($self, $meta) = @_;
77              
78 1         4 $meta->add_requires('configure' => 'Alien::Build::Plugin::Build::SearchDep' => '0.35');
79              
80             $meta->before_hook(
81             gather_share => sub {
82 1     1   4 my($build) = @_;
83              
84 1 50       5 if($self->path)
85             {
86 0         0 $self->_convert($build, Path::Tiny->new($self->path)->absolute);
87             }
88             else
89             {
90 1         5 $self->_recurse($build, Path::Tiny->new(".")->absolute);
91             }
92              
93             },
94 1         10 );
95             }
96              
97             1;
98              
99             __END__