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