Update/Makefile.pm

filter: Statementstmt 99.1% 118 / 119 Branchbran 92.1% 70 / 76 Conditioncond 75.0% 27 / 36 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 90.3% 235 / 260 CC 53 SCAR 38.3
CC
53
Cov
93%
update_makefile
52.0
_get_ext
2.0
__ANON__
2.0
CRAP 45.9
↑ Summary Update/VCProj.pm »
1 package Makefile::Update::Makefile;
2 # ABSTRACT: Update lists of files in makefile variables.
3  
4 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our @EXPORT = qw(update_makefile);
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.4'; # VERSION
11  
12  
13  
14 sub update_makefile
15 20 ▶{
Subroutine update_makefile: called CC=51 SCAR=39.5
Pod documented
16 20 my ($in, $out, $vars) = @_;
17  
18 # Variable whose contents is being currently replaced and its original
19 # name in the makefile.
20 20 my ($var, $makevar);
21  
22 # Hash with files defined for the specified variable as keys and 0 or 1
23 # depending on whether we have seen them in the input file as values.
24 20 my %files;
25  
26 # Array of lines in the existing makefile.
27 20 my @values;
28  
29 # True if the values are in alphabetical order: we use this to add new
30 # entries in alphabetical order too if the existing ones use it, otherwise
31 # we just append them at the end.
32 20 my $sorted = 1;
33  
34 # Extensions of the files in the files list (they're keys of this hash,
35 # the values are not used), there can be more than one (e.g. ".c" and
36 # ".cpp").
37 20 my %src_exts;
38  
39 # Extension of the files in the makefiles: here there can also be more
40 # than one, but in this case we just give up and don't perform any
41 # extensions translation because we don't have enough information to do it
42 # (e.g. which extension should be used for the new files in the makefile?).
43 # Such case is indicated by make_ext being empty (as opposed to its
44 # initial undefined value).
45 my $make_ext;
46  
47 # Helper to get the extension. Note that the "extension" may be a make
48 # variable, e.g. the file could be something like "foo.$(obj)", so don't
49 # restrict it to just word characters.
50 88 ▶ sub _get_ext { $_[0] =~ /(\.\S+)$/ ? $1 : undef }
Subroutine _get_ext: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(\.\S+)$/ ? : 87 1 88
51  
52 # Indent and the part after the value (typically some amount of spaces and
53 # a backslash) for normal lines and, separately, for the last one, as it
54 # may or not have backslash after it.
55 20 my ($indent, $tail, $last_tail);
56  
57 # We can't use the usual check for EOF inside while itself because this
58 # wouldn't work for files with no new line after the last line, so check
59 # for the EOF manually.
60 20 my $eof = 0;
61  
62 # Set to 1 if we made any changes.
63 20 my $changed = 0;
64 20 while (1) {
65 113 my $line = <$in>;
66 113 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 93 20 113
67 93 chomp $line;
68 } else {
69 20 $line = '';
70 20 $eof = 1;
71 }
72  
73 # If we're inside the variable definition, parse the current line as
74 # another file name,
75 113 ▶ if (defined $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 68 45 113
76 68 ▶ if ($line =~ /^(?<indent>\s*)(?<file>[^ ]+)(?<tail>\s*\\?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(?<indent>\s*)(?<file>[^ ]+)(?<tail>\s*\\?)$/) 45 23 68
77 45 ▶ if (defined $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent) { } 22 23 45
78 warn qq{Inconsistent indent at line $. in the } .
79 qq{definition of the variable "$makevar".\n}
80 22 ▶ if $+{indent} ne $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $+{"indent"} ne $indent 1 21 22
81 } else {
82 23 $indent = $+{indent};
83 }
84  
85 45 $last_tail = $+{tail};
86 45 my $file_orig = $+{file};
87  
88 45 ▶ $tail = $last_tail if !defined $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tail 19 26 45
89  
90 # Check if we have something with the correct extension and
91 # preserve unchanged all the rest -- we don't want to remove
92 # expansions of other makefile variables from this one, for
93 # example, but such expansions would never be in the files
94 # list as they don't make sense for the other formats.
95 45 my $file = $file_orig;
96 45 ▶ if (defined (my $file_ext = _get_ext($file))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file_ext = _get_ext($file))) 44 1 45
97 44 ▶ if (defined $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $make_ext) { } 21 23 44
98 21 ▶ if ($file_ext ne $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ext ne $make_ext) 0 21 21
99 # As explained in the comment before make_ext
100 # definition, just don't do anything in this case.
101 0 $make_ext = '';
102 }
103 } else {
104 23 $make_ext = $file_ext;
105 }
106  
107 # We need to try this file with all of the source
108 # extensions we have as it can correspond to any of them.
109 44 for my $src_ext (keys %src_exts) {
110 48 ▶ if ($file_ext ne $src_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ext ne $src_ext) 16 32 48
111 16 (my $file_try = $file) =~ s/\Q$file_ext\E$/$src_ext/;
112 16 ▶ if (exists $files{$file_try}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files{$file_try}) 7 9 16
113 7 $file = $file_try;
114 last
115 7 }
116 }
117 }
118  
119 44 ▶ if (!exists $files{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $files{$file}) 14 30 44
120 # This file was removed.
121 14 $changed = 1;
122  
123 # Don't store this line in @values below.
124 14 next;
125 }
126 }
127  
128 31 ▶ if (exists $files{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files{$file}) 30 1 31
129 30 ▶ if ($files{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$file}) { } 1 29 30
130 1 warn qq{Duplicate file "$file" in the definition of the } .
131 qq{variable "$makevar" at line $.\n}
132 } else {
133 29 $files{$file} = 1;
134 }
135 }
136  
137 # Are we still sorted?
138 31 ▶ if (@values && lc $line lt $values[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values and lc $line lt $values[-1]) 3 28 31
Condition100%3/3
@values and lc $line lt $values[-1]
!ll&&!rl&&r
15133
MC/DC100%2/2
@values and lc $line lt $values[-1]
@values lc $line lt $values[-1]
Truth table100%3/3
A and B
A= @valuesB= lc $line lt $values[-1]
ABresult
0X0
100
111
139 3 $sorted = 0;
140 }
141  
142 31 push @values, $line;
143 31 next;
144 }
145  
146 # If the last line had a continuation character, the file list
147 # should only end if there is nothing else on the following line.
148 23 ▶ if ($last_tail =~ /\\$/ && $line =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_tail =~ /\\$/ and $line =~ /\S/) 1 22 23
Condition100%3/3
$last_tail =~ /\\$/ and $line =~ /\S/
!ll&&!rl&&r
2111
MC/DC100%2/2
$last_tail =~ /\\$/ and $line =~ /\S/
$last_tail =~ /\\$/ $line =~ /\S/
Truth table100%3/3
A and B
A= $last_tail =~ /\\$/B= $line =~ /\S/
ABresult
0X0
100
111
149 1 warn qq{Expected blank line at line $..\n};
150 }
151  
152 # End of variable definition, add new lines.
153  
154 # We can only map the extensions if we have a single extension to
155 # map them to (i.e. make_ext is not empty) and we only need to do
156 # it if are using more than one extension in the source files list
157 # or the single extension that we use is different from make_ext.
158 23 ▶ if (defined $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $make_ext) 23 0 23
159 23 ▶ if ($make_ext eq '' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($make_ext eq "" or keys %src_exts == 1 and exists $src_exts{$make_ext}) 19 4 23
Condition66%4/6
$make_ext eq "" or keys %src_exts == 1 and exists $src_exts{$make_ext}
l!l&&r!l&&!r
0194
keys %src_exts == 1 and exists $src_exts{$make_ext}
!ll&&!rl&&r
4019
MC/DC33%1/3
$make_ext eq "" or keys %src_exts == 1 and exists $src_exts{$make_ext}
$make_ext eq "" keys %src_exts == 1 exists $src_exts{$make_ext}
Truth table50%2/4
A or B and C
A= $make_ext eq ""B= keys %src_exts == 1C= exists $src_exts{$make_ext}
ABCresult
1XX1
0111
00X0
0100
160 (keys %src_exts == 1 && exists $src_exts{$make_ext})) {
161 19 undef $make_ext
162 }
163 }
164  
165 23 my $new_files = 0;
166 23 ▶ while (my ($file, $seen) = each(%files)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %files) 43 23 66
167 43 ▶ next if $seen;
Branch: 1 branch
BranchTrueFalseTotal
if $seen 29 14 43
168  
169 # This file was wasn't present in the input, add it.
170  
171 # If this is the first file we add, ensure that the last line
172 # present in the makefile so far has the line continuation
173 # character at the end as this might not have been the case.
174 14 ▶ if (!$new_files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_files) 12 2 14
175 12 $new_files = 1;
176  
177 12 ▶ if (@values && $values[-1] !~ /\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values and not $values[-1] =~ /\\$/) 4 8 12
Condition100%3/3
@values and not $values[-1] =~ /\\$/
!ll&&!rl&&r
714
MC/DC100%2/2
@values and not $values[-1] =~ /\\$/
@values not $values[-1] =~ /\\$/
Truth table100%3/3
A and not(B)
A= @valuesB= not $values[-1] =~ /\\$/
ABresult
0X0
100
111
178 4 $values[-1] .= $tail;
179 }
180 }
181  
182 # Next give it the right extension.
183 14 ▶ if (defined $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $make_ext) 3 11 14
184 3 $file =~ s/\.\S+$/$make_ext/
185 }
186  
187 # Finally store it.
188 14 push @values, "$indent$file$tail";
189 }
190  
191 23 ▶ if ($new_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_files) 12 11 23
192 12 $changed = 1;
193  
194 # Sort them if necessary using the usual Schwartzian transform.
195 12 ▶ if ($sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted) 12 0 12
196 26 @values = map { $_->[0] }
197 24 sort { $a->[1] cmp $b->[1] }
198 12 map { [$_, lc $_] } @values;
199 }
200  
201 # Fix up the tail of the last line to be the same as that of
202 # the previous last line.
203 12 $values[-1] =~ s/\s*\\$/$last_tail/;
204 }
205  
206 23 undef $var;
207  
208 23 print $out join("\n", @values), "\n";
209 }
210  
211 # We're only interested in variable or target declarations, and does
212 # not look like target-specific variable (this would contain an equal
213 # sign after the target).
214 68 ▶ if ($line =~ /^\s*(?<var>\S+)\s*(?::?=|:)(?<tail>[^=]*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(?<var>\S+)\s*(?::?=|:)(?<tail>[^=]*)$/) 24 44 68
215 24 $makevar = $+{var};
216 24 my $tail = $+{tail};
217  
218 # And only those of them for which we have values, but this is
219 # where it gets tricky as we try to be smart to accommodate common
220 # use patterns with minimal effort.
221 24 ▶ if (exists $vars->{$makevar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vars->{$makevar}) { } 6 18 24
222 6 $var = $makevar;
223 } else {
224 # Helper: return name if a variable with such name exists or
225 # undef otherwise.
226 18 ▶ my $var_if_exists = sub { exists $vars->{$_[0]} ? $_[0] : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $$vars{$_[0]} ? : 18 9 27
227  
228 18 ▶ if ($makevar =~ /^objects$/i || $makevar =~ /^obj$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($makevar =~ /^objects$/i or $makevar =~ /^obj$/i) { } 1 17 18
elsif ($makevar =~ /^(\w+)_(objects|obj|sources|src)$/i) { } 16 1 17
elsif ($makevar =~ /^(\w+)\$\(\w+\)/) { } 1 0 1
Condition66%2/3
$makevar =~ /^objects$/i or $makevar =~ /^obj$/i
l!l&&r!l&&!r
1017
MC/DC50%1/2
$makevar =~ /^objects$/i or $makevar =~ /^obj$/i
$makevar =~ /^objects$/i $makevar =~ /^obj$/i
Truth table66%2/3
A or B
A= $makevar =~ /^objects$/iB= $makevar =~ /^obj$/i
ABresult
1X1
011
000
229 # Special case: map it to "sources" as we work with the
230 # source, not object, files.
231 1 $var = $var_if_exists->('sources');
232 } elsif ($makevar =~ /^(\w+)_(objects|obj|sources|src)$/i) {
233 # Here we deal with "foo_sources" typically found in
234 # hand-written makefiles but also "foo_SOURCES" used in
235 # automake ones, but the latter also uses libfoo_a_SOURCES
236 # for static libraries and libfoo_la_SOURCES for the
237 # libtool libraries, be smart about it to allow defining
238 # just "foo" or "foo_sources" variables usable with all
239 # kinds of make/project files.
240 16 ▶ $var = $var_if_exists->($1) || $var_if_exists->("$1_sources");
Condition100%3/3
&$var_if_exists($1) || &$var_if_exists("$1_sources")
l!l&&r!l&&!r
1213
MC/DC100%2/2
&$var_if_exists($1) || &$var_if_exists("$1_sources")
&$var_if_exists($1) &$var_if_exists("$1_sources")
Truth table100%3/3
A || B
A= &$var_if_exists($1)B= &$var_if_exists("$1_sources")
ABresult
1X1
011
000
241 16 ▶ if (!defined $var && $2 eq 'SOURCES' && $1 =~ /^(\w+)_l?a$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $var and $2 eq "SOURCES" and $1 =~ /^(\w+)_l?a$/) 3 13 16
Condition66%4/6
not defined $var and $2 eq "SOURCES" and $1 =~ /^(\w+)_l?a$/
!ll&&!rl&&r
1303
not defined $var and $2 eq "SOURCES"
!ll&&!rl&&r
1303
MC/DC33%1/3
not defined $var and $2 eq "SOURCES" and $1 =~ /^(\w+)_l?a$/
not defined $var $2 eq "SOURCES" $1 =~ /^(\w+)_l?a$/
Truth table50%2/4
not(A) and B and C
A= not defined $varB= $2 eq "SOURCES"C= $1 =~ /^(\w+)_l?a$/
ABCresult
0XX0
10X0
1100
1111
242 3 ▶ $var = $var_if_exists->($1) || $var_if_exists->("$1_sources");
Condition66%2/3
&$var_if_exists($1) || &$var_if_exists("$1_sources")
l!l&&r!l&&!r
201
MC/DC50%1/2
&$var_if_exists($1) || &$var_if_exists("$1_sources")
&$var_if_exists($1) &$var_if_exists("$1_sources")
Truth table66%2/3
A || B
A= &$var_if_exists($1)B= &$var_if_exists("$1_sources")
ABresult
1X1
011
000
243 3 ▶ if (!defined $var && $1 =~ /^lib(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $var and $1 =~ /^lib(\w+)$/) 1 2 3
Condition66%2/3
not defined $var and $1 =~ /^lib(\w+)$/
!ll&&!rl&&r
201
MC/DC50%1/2
not defined $var and $1 =~ /^lib(\w+)$/
not defined $var $1 =~ /^lib(\w+)$/
Truth table66%2/3
not(A) and B
A= not defined $varB= $1 =~ /^lib(\w+)$/
ABresult
0X0
100
111
244 1 ▶ $var = $var_if_exists->($1) || $var_if_exists->("$1_sources");
Condition33%1/3
&$var_if_exists($1) || &$var_if_exists("$1_sources")
l!l&&r!l&&!r
100
MC/DC0%0/2
&$var_if_exists($1) || &$var_if_exists("$1_sources")
&$var_if_exists($1) &$var_if_exists("$1_sources")
Truth table33%1/3
A || B
A= &$var_if_exists($1)B= &$var_if_exists("$1_sources")
ABresult
1X1
011
000
245 }
246 }
247 } elsif ($makevar =~ /^(\w+)\$\(\w+\)/) {
248 # This one is meant to catch relatively common makefile
249 # constructions like "target$(exe_ext)".
250 1 $var = $var_if_exists->($1);
251 }
252 }
253  
254 24 ▶ if (defined $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 24 0 24
255 24 ▶ if ($tail !~ /\s*\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tail =~ /\s*\\$/) { } 1 23 24
256 1 warn qq{Unsupported format for variable "$makevar" at line $..\n};
257 1 undef $var;
258 } else {
259 23 %files = map { $_ => 0 } @{$vars->{$var}};
260  
261 23 @values = ();
262  
263 # Find all the extensions used by files in this variable.
264 23 for my $file (@{$vars->{$var}}) {
265 43 ▶ if (defined (my $src_ext = _get_ext($file))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $src_ext = _get_ext($file))) 43 0 43
266 43 $src_exts{$src_ext} = 1;
267 }
268 }
269  
270 # Not known yet.
271 23 undef $make_ext;
272  
273 23 undef $indent;
274 23 $tail = $tail;
275 23 undef $last_tail;
276  
277 # Not unsorted so far.
278 23 $sorted = 1;
279 }
280 }
281 }
282  
283 68 print $out "$line";
284  
285 # Don't add an extra new line at the EOF if it hadn't been there.
286 68 ▶ last if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 20 48 68
287  
288 48 print $out "\n";
289 }
290  
291 $changed
292 20 }
293  
294 __END__
↑ Summary Update/VCProj.pm »