| 1 | package Makefile::Update::MSBuild; | ||||||||||||||||||||
| 2 | # ABSTRACT: Update list of sources and headers in MSBuild projects. | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | our @EXPORT = qw(update_msbuild_project update_msbuild update_msbuild_filters); | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 5 | our $VERSION = '0.4'; # VERSION | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | sub update_msbuild_project | ||||||||||||||||||||
| 15 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
update_msbuild_project: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 16 | 3 | my ($file_or_options, $sources, $headers) = @_; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 5 | ▶ | use Makefile::Update; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 3 | ▶ | if (!Makefile::Update::upmake($file_or_options, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | \&update_msbuild, $sources, $headers | ||||||||||||||||||||
| 22 | )) { | ||||||||||||||||||||
| 23 | 1 | return 0; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | my $args; | |||||||||||||||||||
| 27 | 2 | ▶ | if (ref $file_or_options eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | # Need to make a copy to avoid modifying the callers hash. | ||||||||||||||||||||
| 29 | 2 | $args = { %$file_or_options }; | |||||||||||||||||||
| 30 | 2 | $args->{file} .= ".filters" | |||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||
| 32 | 0 | $args = "$file_or_options.filters" | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | return Makefile::Update::upmake($args, | |||||||||||||||||||
| 36 | \&update_msbuild_filters, $sources, $headers | ||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | sub update_msbuild | ||||||||||||||||||||
| 43 | 7 | ▶ | { | ||||||||||||||||||
|
Subroutine
update_msbuild: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||
| 44 | 7 | my ($in, $out, $sources, $headers) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # Hashes mapping the sources/headers names to 1 if they have been seen in | ||||||||||||||||||||
| 47 | # the project or 0 otherwise. | ||||||||||||||||||||
| 48 | 7 | my %sources = map { $_ => 0 } @$sources; | |||||||||||||||||||
| 49 | 7 | my %headers = map { $_ => 0 } @$headers; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Reference to the hash corresponding to the files currently being | ||||||||||||||||||||
| 52 | # processed. | ||||||||||||||||||||
| 53 | 7 | my $files; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # Set to 1 when we are inside any <ItemGroup> tag. | ||||||||||||||||||||
| 56 | 7 | my $in_group = 0; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Set to 1 when we are inside an item group containing sources or headers | ||||||||||||||||||||
| 59 | # respectively. | ||||||||||||||||||||
| 60 | 7 | my ($in_sources, $in_headers) = 0; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # Set to 1 if we made any changes. | ||||||||||||||||||||
| 63 | 7 | my $changed = 0; | |||||||||||||||||||
| 64 | 7 | ▶ | while (my $line_with_eol = <$in>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 128 | (my $line = $line_with_eol) =~ s/\r?\n?$//; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 128 | ▶ | if ($line =~ /^\s*<ItemGroup>$/) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 68 | 11 | $in_group = 1; | |||||||||||||||||||
| 69 | } elsif ($line =~ m{^\s*</ItemGroup>$}) { | ||||||||||||||||||||
| 70 | 18 | ▶ | if (defined $files) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 11 | ▶ | my $kind = $in_sources ? 'Compile' : 'Include'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # Check if we have any new files. | ||||||||||||||||||||
| 74 | # | ||||||||||||||||||||
| 75 | # TODO Insert them in alphabetical order. | ||||||||||||||||||||
| 76 | 11 | ▶ | while (my ($file, $seen) = each(%$files)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 14 | ▶ | if (!$seen) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | # Convert path separator to the one used by MSBuild. | ||||||||||||||||||||
| 79 | 5 | $file =~ s@/@\\@g; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 5 | print $out qq{ <Cl$kind Include="$file" />\r\n}; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 5 | $changed = 1; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 11 | $in_sources = $in_headers = 0; | |||||||||||||||||||
| 88 | 11 | $files = undef; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 18 | $in_group = 0; | |||||||||||||||||||
| 92 | } elsif ($in_group) { | ||||||||||||||||||||
| 93 | 19 | ▶ | if ($line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>$}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 19 | my $kind = $+{kind}; | |||||||||||||||||||
| 95 | 19 | ▶ | if ($kind eq 'Compile') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 11 | ▶ | warn "Mix of sources and headers at line $.\n" if $in_headers; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 11 | $in_sources = 1; | |||||||||||||||||||
| 98 | 11 | $files = \%sources; | |||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||
| 100 | 8 | ▶ | warn "Mix of headers and sources at line $.\n" if $in_sources; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 8 | $in_headers = 1; | |||||||||||||||||||
| 102 | 8 | $files = \%headers; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 19 | my $closed_tag = defined $+{slash}; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # Normalize the path separator, we always use Unix ones but the | ||||||||||||||||||||
| 108 | # project files use Windows one. | ||||||||||||||||||||
| 109 | 19 | my $file = $+{file}; | |||||||||||||||||||
| 110 | 19 | $file =~ s@\\@/@g; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 19 | ▶ | if (not exists $files->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | # This file was removed. | ||||||||||||||||||||
| 114 | 8 | $changed = 1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 8 | ▶ | if (!$closed_tag) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | # We have just the opening <ClCompile> or <ClInclude> | ||||||||||||||||||||
| 118 | # tag, ignore everything until the matching closing one. | ||||||||||||||||||||
| 119 | 1 | my $tag = "Cl$kind"; | |||||||||||||||||||
| 120 | 1 | ▶ | while (<$in>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 2 | ▶ | last if m{^\s*</$tag>\r?\n$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # In any case skip either this line containing the full | ||||||||||||||||||||
| 126 | # <ClCompile/> tag or the line with the closing tag. | ||||||||||||||||||||
| 127 | 8 | next; | |||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||
| 129 | 11 | ▶ | if ($files->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 1 | warn qq{Duplicate file "$file" in the project at line $.\n}; | |||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||
| 132 | 10 | $files->{$file} = 1; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 120 | print $out $line_with_eol; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | $changed | ||||||||||||||||||||
| 142 | 7 | } | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | sub update_msbuild_filters | ||||||||||||||||||||
| 146 | 7 | ▶ | { | ||||||||||||||||||
|
Subroutine
update_msbuild_filters: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 147 | 7 | my ($in, $out, $sources, $headers, $filter_cb) = @_; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | # Use standard/default classifier for the files if none is explicitly | ||||||||||||||||||||
| 150 | # specified. | ||||||||||||||||||||
| 151 | 7 | ▶ | if (!defined $filter_cb) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 4 | ▶ | $filter_cb = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 153 | 4 | my ($file) = @_; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 4 | ▶ | return 'Source Files' if $file =~ q{\.c(c|pp|xx|\+\+)?$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 2 | ▶ | return 'Header Files' if $file =~ q{\.h(h|pp|xx|\+\+)?$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | warn qq{No filter defined for the file "$file".\n}; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | undef | ||||||||||||||||||||
| 161 | 1 | } | |||||||||||||||||||
| 162 | 6 | } | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # Hashes mapping the sources/headers names to the text representing them in | ||||||||||||||||||||
| 165 | # the input file if they have been seen in it or nothing otherwise. | ||||||||||||||||||||
| 166 | 7 | my %sources = map { $_ => undef } @$sources; | |||||||||||||||||||
| 167 | 7 | my %headers = map { $_ => undef } @$headers; | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | # Reference to the hash corresponding to the files currently being | ||||||||||||||||||||
| 170 | # processed. | ||||||||||||||||||||
| 171 | 7 | my $files; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # Set to 1 when we are inside any <ItemGroup> tag. | ||||||||||||||||||||
| 174 | 7 | my $in_group = 0; | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | # Set to 1 when we are inside an item group containing sources or headers | ||||||||||||||||||||
| 177 | # respectively. | ||||||||||||||||||||
| 178 | 7 | my ($in_sources, $in_headers) = 0; | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | # Set to 1 if we made any changes. | ||||||||||||||||||||
| 181 | 7 | my $changed = 0; | |||||||||||||||||||
| 182 | 7 | ▶ | while (my $line_with_eol = <$in>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 119 | (my $line = $line_with_eol) =~ s/\r?\n?$//; | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 119 | ▶ | if ($line =~ /^\s*<ItemGroup>?$/) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 186 | 17 | $in_group = 1; | |||||||||||||||||||
| 187 | } elsif ($line =~ m{^\s*</ItemGroup>?$}) { | ||||||||||||||||||||
| 188 | 17 | ▶ | if (defined $files) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | # Output the group contents now, all at once, inserting any new | ||||||||||||||||||||
| 190 | # files: we must do it like this to ensure that they are | ||||||||||||||||||||
| 191 | # inserted in alphabetical order. | ||||||||||||||||||||
| 192 | 10 | ▶ | my $kind = $in_sources ? 'Compile' : 'Include'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 10 | foreach my $file (sort keys %$files) { | |||||||||||||||||||
| 195 | 15 | ▶ | if (defined $files->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | 8 | print $out $files->{$file}; | |||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||
| 198 | 7 | my $filter = $filter_cb->($file); | |||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | # Convert path separator to the one used by MSBuild. | ||||||||||||||||||||
| 201 | 7 | $file =~ s@/@\\@g; | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 7 | my $indent = ' ' x 2; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 7 | print $out qq{$indent$indent<Cl$kind Include="$file"}; | |||||||||||||||||||
| 206 | 7 | ▶ | if (defined $filter) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | 5 | print $out ">\r\n$indent$indent$indent<Filter>$filter</Filter>\r\n$indent$indent</Cl$kind>\r\n"; | |||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||
| 209 | 2 | print $out " />\r\n"; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 7 | $changed = 1; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 10 | $in_sources = $in_headers = 0; | |||||||||||||||||||
| 217 | 10 | $files = undef; | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 17 | $in_group = 0; | |||||||||||||||||||
| 221 | ▶ | } elsif ($in_group && | |||||||||||||||||||
|
Condition100%3/3
$in_group and $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
MC/DC100%2/2
$in_group and $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
$in_group
$line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
Truth table100%3/3
A and B
A= $in_groupB= $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
| |||||||||||||||||||||
| 222 | $line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$}) { | ||||||||||||||||||||
| 223 | 18 | my $kind = $+{kind}; | |||||||||||||||||||
| 224 | 18 | ▶ | if ($kind eq 'Compile') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | 11 | ▶ | warn "Mix of sources and headers at line $.\n" if $in_headers; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 226 | 11 | $in_sources = 1; | |||||||||||||||||||
| 227 | 11 | $files = \%sources; | |||||||||||||||||||
| 228 | } else { | ||||||||||||||||||||
| 229 | 7 | ▶ | warn "Mix of headers and sources at line $.\n" if $in_sources; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 230 | 7 | $in_headers = 1; | |||||||||||||||||||
| 231 | 7 | $files = \%headers; | |||||||||||||||||||
| 232 | } | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 18 | my $closed_tag = defined $+{slash}; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | # Normalize the path separator, we always use Unix ones but the | ||||||||||||||||||||
| 237 | # project files use Windows one. | ||||||||||||||||||||
| 238 | 18 | my $file = $+{file}; | |||||||||||||||||||
| 239 | 18 | $file =~ s@\\@/@g; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 18 | my $text = $line_with_eol; | |||||||||||||||||||
| 242 | 18 | ▶ | if (!$closed_tag) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | # We have just the opening <ClCompile> tag, get everything | ||||||||||||||||||||
| 244 | # until the next </ClCompile>. | ||||||||||||||||||||
| 245 | 14 | ▶ | while (<$in>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | 28 | $text .= $_; | |||||||||||||||||||
| 247 | 28 | ▶ | last if m{^\s*</Cl$kind>\r?\n?$}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 18 | ▶ | if (not exists $files->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 252 | # This file was removed. | ||||||||||||||||||||
| 253 | 8 | $changed = 1; | |||||||||||||||||||
| 254 | } else { | ||||||||||||||||||||
| 255 | 10 | ▶ | if ($files->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 256 | 1 | warn qq{Duplicate file "$file" in the project at line $.\n}; | |||||||||||||||||||
| 257 | } else { | ||||||||||||||||||||
| 258 | 9 | $files->{$file} = $text; | |||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | # Don't output this line yet, wait until the end of the group. | ||||||||||||||||||||
| 263 | next | ||||||||||||||||||||
| 264 | 18 | } | |||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | 101 | print $out $line_with_eol; | |||||||||||||||||||
| 267 | } | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | $changed | ||||||||||||||||||||
| 270 | 7 | } | |||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | __END__ | ||||||||||||||||||||