| 1 | package Makefile::Update::VCProj; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Update list of sources and headers in Visual C++ projects. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | our @EXPORT = qw(update_vcproj); | |||||||||||||||||||||||||||
| 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_vcproj | ||||||||||||||||||||||||||||
| 15 | { | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | my ($in, $out, $sources, $headers, $filter_cb) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_vcproj: called CC=25 SCAR=34.5
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Use standard/default classifier for the files if none is explicitly | ||||||||||||||||||||||||||||
| 19 | # specified. | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | if (!defined $filter_cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | $filter_cb = sub { | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | my ($file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | return 'Source Files' if $file =~ q{\.c(c|pp|xx|\+\+)?$}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | return 'Header Files' if $file =~ q{\.h(h|pp|xx|\+\+)?$}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | warn qq{No filter defined for the file "$file".\n}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | undef | ||||||||||||||||||||||||||||
| 30 | 0 | } | |||||||||||||||||||||||||||
| 31 | 1 | } | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Hash mapping the filter to all the files using it (whether sources or | ||||||||||||||||||||||||||||
| 34 | # headers). | ||||||||||||||||||||||||||||
| 35 | 1 | my %files_by_filter; | |||||||||||||||||||||||||||
| 36 | 1 | foreach my $file (@$sources, @$headers) { | |||||||||||||||||||||||||||
| 37 | 6 | push @{$files_by_filter{$filter_cb->($file)}}, $file | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Name of the current filter, if any. | ||||||||||||||||||||||||||||
| 41 | 1 | my $filter; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Hash containing 0 or 1 for each file using the current filter. | ||||||||||||||||||||||||||||
| 44 | my %seen; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Indicates whether the closing angle bracket of "<File>" tags is on its | ||||||||||||||||||||||||||||
| 47 | # own line (which is how MSVS 2005 and 2008 format their files) or on the | ||||||||||||||||||||||||||||
| 48 | # same line as "RelativePath" attribute (which is how MSVS 2003 does it). | ||||||||||||||||||||||||||||
| 49 | 1 | my $angle_bracket_on_same_line = 0; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Set to 1 if we made any changes. | ||||||||||||||||||||||||||||
| 52 | 1 | my $changed = 0; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | while (defined (my $line_with_eol = <$in>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 32 | (my $line = $line_with_eol) =~ s/\r?\n$//; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 32 | ▶ | if ($line =~ /^\s*<Filter$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | if (defined($filter)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | warn qq{Nested <Filter> tag at line $. while parsing filter } . | |||||||||||||||||||||||||||
| 60 | qq{"$filter" is not supported.\n}; | ||||||||||||||||||||||||||||
| 61 | 0 | next; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | print $out $line_with_eol; | |||||||||||||||||||||||||||
| 65 | 2 | $line_with_eol = <$in>; | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | if (defined $line_with_eol && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $line_with_eol and $line_with_eol =~ /^\s*Name="(.*)"\r?\n$/
MC/DC0%0/2
defined $line_with_eol and $line_with_eol =~ /^\s*Name="(.*)"\r?\n$/
defined $line_with_eol
$line_with_eol =~ /^\s*Name="(.*)"\r?\n$/
Truth table33%1/3
A and B
A= defined $line_with_eolB= $line_with_eol =~ /^\s*Name="(.*)"\r?\n$/
| |||||||||||||||||||||||||||||
| 67 | $line_with_eol =~ /^\s*Name="(.*)"\r?\n$/) { | ||||||||||||||||||||||||||||
| 68 | 2 | $filter = $1; | |||||||||||||||||||||||||||
| 69 | 2 | ▶ | if (!exists $files_by_filter{$filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | # If we don't have any files for this filter, don't remove | ||||||||||||||||||||||||||||
| 71 | # all the files from it, just skip it entirely instead. | ||||||||||||||||||||||||||||
| 72 | 0 | undef $filter; | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 2 | %seen = map { $_ => 0 } @{$files_by_filter{$filter}}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 0 | warn qq{Unrecognized format for <Filter> tag at line $..\n}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } elsif (defined $filter) { | ||||||||||||||||||||||||||||
| 80 | 17 | ▶ | if ($line =~ /^\s*<File$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | 7 | my $line_file_start = $line_with_eol; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | $line_with_eol = <$in>; | |||||||||||||||||||||||||||
| 84 | 7 | ▶ | if (defined $line_with_eol && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $line_with_eol and $line_with_eol =~ /^\s*RelativePath="(.*)"(>?)\r?\n$/
MC/DC0%0/2
defined $line_with_eol and $line_with_eol =~ /^\s*RelativePath="(.*)"(>?)\r?\n$/
defined $line_with_eol
$line_with_eol =~ /^\s*RelativePath="(.*)"(>?)\r?\n$/
Truth table33%1/3
A and B
A= defined $line_with_eolB= $line_with_eol =~ /^\s*RelativePath="(.*)"(>?)\r?\n$/
| |||||||||||||||||||||||||||||
| 85 | $line_with_eol =~ /^\s*RelativePath="(.*)"(>?)\r?\n$/) { | ||||||||||||||||||||||||||||
| 86 | 7 | $angle_bracket_on_same_line = $2 eq '>'; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Normalize path separators to Unix and remove the leading | ||||||||||||||||||||||||||||
| 89 | # dot which MSVC likes to use for some reason. | ||||||||||||||||||||||||||||
| 90 | 7 | (my $file = $1) =~ s@\\@/@g; | |||||||||||||||||||||||||||
| 91 | 7 | $file =~ s@^\./@@; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Special hack for resource files that sometimes occur in | ||||||||||||||||||||||||||||
| 94 | # the "Source Files" section of MSVC projects too: don't | ||||||||||||||||||||||||||||
| 95 | # remove them, even if they don't appear in the master | ||||||||||||||||||||||||||||
| 96 | # files list, because they are never going to appear in it. | ||||||||||||||||||||||||||||
| 97 | 7 | ▶ | if ($file !~ /\.rc$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | if (!exists $seen{$file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | # This file is not in the master file list any | ||||||||||||||||||||||||||||
| 100 | # more, delete it from the project file as well by | ||||||||||||||||||||||||||||
| 101 | # not copying the lines corresponding to it to the | ||||||||||||||||||||||||||||
| 102 | # output. | ||||||||||||||||||||||||||||
| 103 | 3 | $changed = 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Skip the next line unless we had already seen | ||||||||||||||||||||||||||||
| 106 | # the angle bracket. | ||||||||||||||||||||||||||||
| 107 | 3 | ▶ | if (!$angle_bracket_on_same_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if (<$in> !~ /^\s*>\r?\n$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | warn qq{Expected closing '>' on the line $.\n} | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # And skip everything up to and including the | ||||||||||||||||||||||||||||
| 114 | # closing </File> tag in any case. | ||||||||||||||||||||||||||||
| 115 | 3 | ▶ | while (<$in>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | ▶ | last if qr{^\s*</File>\r?\n$} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | next; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # This file is still in the files list, mark it as seen. | ||||||||||||||||||||||||||||
| 123 | 4 | ▶ | if ($seen{$file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | warn qq{Duplicate file "$file" in the project at line $.\n}; | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 4 | $seen{$file} = 1; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | warn qq{Unrecognized format for <File> tag inside filter } . | |||||||||||||||||||||||||||
| 131 | qq{"$filter" at line $..\n}; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Don't lose the original line, it won't be printed at the | ||||||||||||||||||||||||||||
| 135 | # end of the loop any more. | ||||||||||||||||||||||||||||
| 136 | 4 | print $out $line_file_start; | |||||||||||||||||||||||||||
| 137 | } elsif ($line =~ qr{^\s*</Filter>$}) { | ||||||||||||||||||||||||||||
| 138 | 2 | ▶ | my $angle_bracket = $angle_bracket_on_same_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | ? '>' | ||||||||||||||||||||||||||||
| 140 | : "\n\t\t\t\t>"; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Add new files, if any. | ||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||
| 144 | # TODO Insert them in alphabetical order. | ||||||||||||||||||||||||||||
| 145 | 2 | ▶ | while (my ($file, $seen) = each(%seen)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 6 | ▶ | if (!$seen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | # Convert path separator to the one used by MSVC. | ||||||||||||||||||||||||||||
| 148 | 2 | $file =~ s@/@\\@g; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # And use path even for the files in this directory. | ||||||||||||||||||||||||||||
| 151 | 2 | ▶ | $file = ".\\$file" if $file !~ /\\/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | print $out <<END | |||||||||||||||||||||||||||
| 154 | \t\t\t<File | ||||||||||||||||||||||||||||
| 155 | \t\t\t\tRelativePath="$file"$angle_bracket | ||||||||||||||||||||||||||||
| 156 | \t\t\t</File> | ||||||||||||||||||||||||||||
| 157 | END | ||||||||||||||||||||||||||||
| 158 | ; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | $changed = 1; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | undef $filter; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 29 | print $out $line_with_eol; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | $changed | ||||||||||||||||||||||||||||
| 172 | 1 | } | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||