Update/Makefile.pm

filter: Statementstmt 92.6% 88 / 95 Branchbran 79.0% 49 / 62 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 83.1% 153 / 184 CC 36 SCAR 34.7
CC
36
Cov
85%
update_makefile
38.6
__ANON__
2.1
_get_ext
2.0
CRAP 32.3
↑ Summary Update/VCProj.pm »
1 package Makefile::Update::Makefile;
2 # ABSTRACT: Update lists of files in makefile variables.
3  
4 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our @EXPORT = qw(update_makefile);
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.2'; # VERSION
11  
12  
13  
14 sub update_makefile
15 {
16 1 ▶ my ($in, $out, $vars) = @_;
Subroutine update_makefile: called CC=34 SCAR=36.5
Pod documented
17  
18 # Variable whose contents is being currently replaced.
19 1 my $var;
20  
21 # Hash with files defined for the specified variable as keys and 0 or 1
22 # depending on whether we have seen them in the input file as values.
23 my %files;
24  
25 # Array of lines in the existing makefile.
26 1 my @values;
27  
28 # True if the values are in alphabetical order: we use this to add new
29 # entries in alphabetical order too if the existing ones use it, otherwise
30 # we just append them at the end.
31 1 my $sorted = 1;
32  
33 # Extension of the files in the files list and in the makefile, can be
34 # different (e.g. ".cpp" and ".o") and we translate between them then.
35 1 my ($src_ext, $make_ext);
36  
37 # Helper to get the extension. Note that the "extension" may be a make
38 # variable, e.g. the file could be something like "foo.$(obj)", so don't
39 # restrict it to just word characters.
40 12 ▶ sub _get_ext { $_[0] =~ /(\.\S+)$/ ? $1 : undef }
Subroutine _get_ext: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(\.\S+)$/ ? : 9 3 12
41  
42 # Indent and the part after the value (typically some amount of spaces and
43 # a backslash) for normal lines and, separately, for the last one, as it
44 # may or not have backslash after it.
45 1 my ($indent, $tail, $last_tail);
46  
47 # Set to 1 if we made any changes.
48 1 my $changed = 0;
49 1 ▶ while (defined(my $line = <$in>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 18 1 19
50 18 chomp $line;
51  
52 # If we're inside the variable definition, parse the current line as
53 # another file name,
54 18 ▶ if (defined $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 12 6 18
55 12 ▶ if ($line =~ /^(?<indent>\s*)(?<file>[^ ]+)(?<tail>\s*\\?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(?<indent>\s*)(?<file>[^ ]+)(?<tail>\s*\\?)$/) 9 3 12
56 9 ▶ if (defined $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent) { } 6 3 9
57 warn qq{Inconsistent indent at line $. in the } .
58 qq{definition of the variable "$var".\n"}
59 6 ▶ if $+{indent} ne $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $+{"indent"} ne $indent 0 6 6
60 } else {
61 3 $indent = $+{indent};
62 }
63  
64 9 $last_tail = $+{tail};
65 9 my $file_orig = $+{file};
66  
67 9 ▶ $tail = $last_tail if !defined $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tail 1 8 9
68  
69 # Check if we have something with the correct extension and
70 # preserve unchanged all the rest -- we don't want to remove
71 # expansions of other makefile variables from this one, for
72 # example, but such expansions would never be in the files
73 # list as they don't make sense for the other formats.
74 9 my $file = $file_orig;
75 9 ▶ if (defined (my $file_ext = _get_ext($file))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file_ext = _get_ext($file))) 7 2 9
76 7 ▶ if (defined $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $make_ext) { } 5 2 7
77 5 ▶ if ($file_ext ne $make_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ext ne $make_ext) 0 5 5
78 0 warn qq{Values of variable "$var" use both } .
79 qq{"$file_ext" and "$make_ext" extensions.\n};
80 }
81 } else {
82 2 $make_ext = $file_ext;
83 }
84  
85 7 ▶ if ($file_ext ne $src_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ext ne $src_ext) 7 0 7
86 7 $file =~ s/\Q$file_ext\E$/$src_ext/
87 }
88  
89 7 ▶ if (exists $files{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files{$file}) { } 5 2 7
90 5 ▶ if ($files{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$file}) { } 0 5 5
91 0 warn qq{Duplicate file "$file" in the definition of the } .
92 qq{variable "$var" at line $.\n}
93 } else {
94 5 $files{$file} = 1;
95 }
96 } else {
97 # This file was removed.
98 2 $changed = 1;
99  
100 # Don't store this line in @values below.
101 2 next;
102 }
103 }
104  
105 # Are we still sorted?
106 7 ▶ if (@values && lc $line lt $values[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values and lc $line lt $values[-1]) 0 7 7
Condition66%2/3
@values and lc $line lt $values[-1]
!ll&&!rl&&r
340
MC/DC0%0/2
@values and lc $line lt $values[-1]
@values lc $line lt $values[-1]
Truth table66%2/3
A and B
A= @valuesB= lc $line lt $values[-1]
ABresult
0X0
100
111
107 0 $sorted = 0;
108 }
109  
110 7 push @values, $line;
111 7 next;
112 }
113  
114 # The variable definition is expected to end with a blank line.
115 3 ▶ warn qq{Expected blank line at line $..\n} if $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\S/ 0 3 3
116  
117 # End of variable definition, add new lines.
118 3 my $new_files = 0;
119 3 ▶ while (my ($file, $seen) = each(%files)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %files) 10 3 13
120 10 ▶ next if $seen;
Branch: 1 branch
BranchTrueFalseTotal
if $seen 5 5 10
121  
122 # This file was wasn't present in the input, add it.
123  
124 # If this is the first file we add, ensure that the last line
125 # present in the makefile so far has the line continuation
126 # character at the end as this might not have been the case.
127 5 ▶ if (!$new_files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_files) 2 3 5
128 2 $new_files = 1;
129  
130 2 ▶ if (@values && $values[-1] !~ /\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values and not $values[-1] =~ /\\$/) 1 1 2
Condition66%2/3
@values and not $values[-1] =~ /\\$/
!ll&&!rl&&r
011
MC/DC50%1/2
@values and not $values[-1] =~ /\\$/
@values not $values[-1] =~ /\\$/
Truth table66%2/3
A and not(B)
A= @valuesB= not $values[-1] =~ /\\$/
ABresult
0X0
100
111
131 1 $values[-1] .= $tail;
132 }
133 }
134  
135 # Next give it the right extension.
136 5 ▶ if (defined $make_ext && $make_ext ne $src_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $make_ext and $make_ext ne $src_ext) 2 3 5
Condition66%2/3
defined $make_ext and $make_ext ne $src_ext
!ll&&!rl&&r
302
MC/DC50%1/2
defined $make_ext and $make_ext ne $src_ext
defined $make_ext $make_ext ne $src_ext
Truth table66%2/3
A and B
A= defined $make_extB= $make_ext ne $src_ext
ABresult
0X0
100
111
137 2 $file =~ s/\Q$src_ext\E$/$make_ext/
138 }
139  
140 # Finally store it.
141 5 push @values, "$indent$file$tail";
142 }
143  
144 3 ▶ if ($new_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_files) 2 1 3
145 2 $changed = 1;
146  
147 # Sort them if necessary using the usual Schwartzian transform.
148 2 ▶ if ($sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted) 2 0 2
149 10 @values = map { $_->[0] }
150 18 sort { $a->[1] cmp $b->[1] }
151 2 map { [$_, lc $_] } @values;
152 }
153  
154 # Fix up the tail of the last line to be the same as that of
155 # the previous last line.
156 2 $values[-1] =~ s/\s*\\$/$last_tail/;
157 }
158  
159 3 undef $var;
160  
161 3 print $out join("\n", @values), "\n";
162 }
163  
164 # We're only interested in variable or target declarations.
165 9 ▶ if ($line =~ /^\s*(?<var>\S+)\s*(?::?=|:)(?<tail>.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(?<var>\S+)\s*(?::?=|:)(?<tail>.*)/) 3 6 9
166 3 $var = $+{var};
167 3 my $tail = $+{tail};
168  
169 # And only those of them for which we have values, but this is
170 # where it gets tricky as we try to be smart to accommodate common
171 # use patterns with minimal effort.
172 3 ▶ if (!exists $vars->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $vars->{$var}) 2 1 3
173 # Helper: return name if a variable with such name exists or
174 # undef otherwise.
175 2 ▶ my $var_if_exists = sub { exists $vars->{$_[0]} ? $_[0] : undef };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
exists $$vars{$_[0]} ? : 2 0 2
176  
177 2 ▶ if ($var =~ /^objects$/i || $var =~ /^obj$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($var =~ /^objects$/i or $var =~ /^obj$/i) { } 0 2 2
elsif ($var =~ /^(\w+)_(objects|obj|sources|src|headers|hdr)$/i) { } 1 1 2
elsif ($var =~ /^(\w+)\$\(\w+\)/) { } 1 0 1
Condition33%1/3
$var =~ /^objects$/i or $var =~ /^obj$/i
l!l&&r!l&&!r
002
MC/DC0%0/2
$var =~ /^objects$/i or $var =~ /^obj$/i
$var =~ /^objects$/i $var =~ /^obj$/i
Truth table33%1/3
A or B
A= $var =~ /^objects$/iB= $var =~ /^obj$/i
ABresult
1X1
011
000
178 # Special case: map it to "sources" as we work with the
179 # source, not object, files.
180 0 $var = $var_if_exists->('sources');
181 } elsif ($var =~ /^(\w+)_(objects|obj|sources|src|headers|hdr)$/i) {
182 1 ▶ $var = $var_if_exists->($1) or $var_if_exists->("$1_sources");
Branch: 1 branch
BranchTrueFalseTotal
$var = &$var_if_exists($1) or &$var_if_exists("$1_sources") 0 1 1
183 } elsif ($var =~ /^(\w+)\$\(\w+\)/) {
184 # This one is meant to catch relatively common makefile
185 # constructions like "target$(exe_ext)".
186 1 $var = $var_if_exists->($1);
187 } else {
188 0 undef $var;
189 }
190 }
191  
192 3 ▶ if (defined $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 3 0 3
193 3 ▶ if ($tail !~ /\s*\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tail =~ /\s*\\$/) { } 0 3 3
194 0 warn qq{Unsupported format for variable "$var" at line $..\n};
195 0 undef $var;
196 } else {
197 3 %files = map { $_ => 0 } @{$vars->{$var}};
198  
199 3 @values = ();
200  
201 # We assume all files have the same extension (it's not
202 # clear what could we do if this were not the case anyhow).
203 3 $src_ext = _get_ext(${$vars->{$var}}[0]);
204  
205 # Not known yet.
206 3 undef $make_ext;
207  
208 3 undef $indent;
209 3 $tail = $tail;
210 3 undef $last_tail;
211 }
212 }
213 }
214  
215 9 print $out "$line\n";
216 }
217  
218 $changed
219 1 }
220  
221 __END__
↑ Summary Update/VCProj.pm »