File Coverage

script/mkpkgconfig
Criterion Covered Total %
statement 57 65 87.6
branch 24 30 80.0
condition 3 4 75.0
subroutine 10 10 100.0
pod n/a
total 94 109 86.2


line stmt bran cond sub pod time code
1             #! /usr/bin/env perl
2              
3             # ABSTRACT: create pkg-config metadata files
4             # PODNAME: mkpkgconfig
5              
6 1     1   565 use strict;
  1         2  
  1         31  
7 1     1   6 use warnings;
  1         2  
  1         27  
8              
9 1     1   676 use Getopt::Long::Descriptive;
  1         37186  
  1         8  
10 1     1   341 use File::Basename;
  1         2  
  1         75  
11 1     1   592 use File::Spec::Functions;
  1         865  
  1         73  
12 1     1   8 use Carp;
  1         3  
  1         70  
13              
14             our $VERSION = 'v2.0.1';
15              
16             use constant {
17 1         1148 ALL_VARS => 1,
18             REQUESTED_VARS => 2,
19             NEEDED_VARS => 3,
20 1     1   7 };
  1         2  
21              
22             our %VarsAuto = (
23             exec_prefix => '${prefix}',
24              
25             bindir => '${exec_prefix}/bin',
26             sbindir => '${exec_prefix}/sbin',
27              
28             libdir => '${exec_prefix}/lib',
29             pkg_libdir => '${libdir}/${package}',
30              
31             libexecdir => '${exec_prefix}/libexec',
32             pkg_libexecdir => '${libexecdir}/${package}',
33              
34             datarootdir => '${prefix}/share',
35              
36             datadir => '${datarootdir}',
37             pkg_datadir => '${datadir}/${package}',
38              
39             sysconfdir => '${prefix}/etc',
40             pkg_sysconfdir => '${sysconfdir}/${package}',
41              
42             localstatedir => '${prefix}/var',
43             pkg_localstatedir => '${localstatedir}/${package}',
44              
45             includedir => '${prefix}/include',
46             pkg_includedir => '${includedir}/${package}',
47              
48             docdir => '${datarootdir}/doc/${package}',
49             infodir => '${datarootdir}/info',
50             mandir => '${datarootdir}/man',
51             );
52              
53             my %KeywordOptions = (
54             Name => { required => 1 },
55             Description => { required => 1 },
56             Requires => { required => 0 },
57             Libs => { required => 0 },
58             Conflicts => { required => 0 },
59             Cflags => { required => 0 },
60             URL => { required => 0 },
61             );
62              
63              
64              
65              
66             main( @ARGV ) unless caller;
67              
68             sub main {
69              
70 6     6   28361 local @ARGV = @_;
71              
72             # save original options for output;
73 6         25 my @sARGV = @ARGV;
74 6         20 my $opt = parse_opts();
75              
76 6         72 require App::mkpkgconfig::PkgConfig;
77              
78 6         199 my $conf = App::mkpkgconfig::PkgConfig->new;
79              
80             # add standard variables
81 6 100       25 $conf->add_variable( prefix => $opt->prefix ) if defined $opt->prefix;
82 6 100       36 $conf->add_variable( package => $opt->package ) if defined $opt->package;
83 6         32 $conf->add_variable( version => $opt->modversion );
84              
85             # add auto generated variables
86 6 100       22 $conf->add_variables( \%VarsAuto )
87             if defined $opt->auto;
88              
89             # override from user
90 6         41 $conf->add_variables( $opt->var );
91              
92             # add standard keywords
93 6         38 $conf->add_keyword( Name => $opt->name );
94 6         27 $conf->add_keyword( Description => $opt->description );
95 6         25 $conf->add_keyword( Version => '${version}' );
96 6         19 for my $kwd ( qw( Requires Libs Conflicts Cflags ) ) {
97 24         92 my $mth = lc $kwd;
98 24 100       101 $conf->add_keyword( $kwd => $opt->$mth )
99             if defined $opt->$mth;
100             }
101              
102             # override from user
103 6         45 $conf->add_keywords( $opt->kwd );
104              
105             #<<< no tidy
106             my @vars_needed =
107             # output all variables, used or not
108             $opt->usevars == ALL_VARS ? $conf->variables
109             # output only requested + keyword dependencies
110 6 50 100     21 : $opt->usevars == REQUESTED_VARS ? ( keys %{ $opt->var } , @{ $opt->auto // [] } )
  2 100       24  
  2 100       13  
111             # only variables actually used by keywords
112             : $opt->usevars == NEEDED_VARS ? ()
113             : die( "unknown filter for variables: ", $opt->usevars, "\n" );
114             #>>>
115              
116 6 100       77 $conf->write(
117             $opt->output,
118             write => ( $opt->usevars == ALL_VARS ? 'all' : 'req' ),
119             vars => \@vars_needed,
120             comments => [
121             "This file was created by $0 ($VERSION) via",
122             join( ' ', $0, @sARGV ) ] );
123             }
124              
125              
126             sub parse_opts {
127              
128 6     6   46 my %Map_usevars = (
129             'all' => ALL_VARS,
130             'requested' => REQUESTED_VARS,
131             'req' => REQUESTED_VARS,
132             'needed' => NEEDED_VARS
133             );
134              
135             my ( $opt, $usage ) = Getopt::Long::Descriptive::describe_options(
136             "%o ",
137             [ 'output|o=s', "output [stdout]" ],
138             [
139             'usevars|u:s',
140             "which variables to output",
141             {
142             default => { reverse %Map_usevars }->{ +NEEDED_VARS },
143             callbacks => {
144             'valid output variables' => sub {
145 5 50   5   23010 return 1 if defined $Map_usevars{ $_[0] };
146 0         0 die(
147             "$_[0] isn't one of 'all', 'req', 'requested', or 'used'\n"
148             );
149             }
150             } }
151             ],
152             [ 'auto:s@', "generate a default set of variables" ],
153             [
154             'list-auto',
155             "output a list of variables generated by the --auto option",
156             { shortcircuit => 1 }
157             ],
158              
159             [],
160             ['Variables:'],
161             [ 'var|variable=s%', 'define variables', { default => {} } ],
162             [ 'prefix=s', "'prefix' variable", ],
163             [ 'package=s', "'package' variable", ],
164             [
165             'modversion|modversion=s',
166             '"version" variable and Version keyword)',
167             { required => 1 }
168             ],
169              
170             [],
171             ['Keywords:'],
172              
173             [ 'kwd|keyword=s%', 'define keywords', { default => {} } ],
174              
175             (
176 6         206 map { [
177             qq(\u$_|\l$_=s),
178             qq("\u$_" keyword),
179             { (
180             $KeywordOptions{$_}{required}
181 42 100 50     302 // 0 ? ( required => 1 ) : ()
182             ),
183             },
184             ]
185             }
186             keys %KeywordOptions
187             ),
188              
189             [],
190             ['Miscellaneous::'],
191              
192             [ 'version|v', 'output version and exit', { shortcircuit => 1 } ],
193              
194             [
195             'help|h',
196             'output short help message and exit',
197             { shortcircuit => 1 }
198             ],
199              
200             [
201             'manual|m',
202             'output full manual page and exit',
203             { shortcircuit => 1 }
204             ],
205             );
206              
207 6 50       16261 print( $usage->text ), exit if $opt->help;
208 6 50       84 print( $VERSION, "\n" ), exit if $opt->version;
209 6 50       46 if ( $opt->manual ) {
210 0         0 require Pod::Usage;
211 0         0 Pod::Usage::pod2usage(
212             { -exitval => 0, -verbose => 2, -output => \*STDOUT } );
213             }
214              
215 6 50       34 if ( $opt->list_auto ) {
216 0         0 require List::Util;
217 0         0 my $length = List::Util::max( map { length( $_ ) } keys %VarsAuto );
  0         0  
218             printf( "%-*s = %s\n", $length, $_, $VarsAuto{$_} )
219 0         0 for sort keys %VarsAuto;
220 0         0 exit;
221             }
222              
223 6 100       34 if ( defined $opt->auto ) {
224 3         16 my @auto = map { split /,/ } @{ $opt->auto };
  3         20  
  3         9  
225 3         11 $opt->{auto} = \@auto;
226             }
227              
228 6         32 $opt->{usevars} = $Map_usevars{ $opt->usevars };
229              
230 6         154 return $opt;
231             }
232              
233              
234              
235             1;
236              
237             #
238             # This file is part of App-mkpkgconfig
239             #
240             # This software is Copyright (c) 2020 by Smithsonian Astrophysical Observatory.
241             #
242             # This is free software, licensed under:
243             #
244             # The GNU General Public License, Version 3, June 2007
245             #
246              
247             __END__