Mon Jan 6 08:15:22 UTC 2014 Johnny Willemsen <jwillemsen@remedy.nl>
[MPC.git] / vs_postclean.pl
blobbc9ceff22bbcfde9d13c64995ef7e5d40ca9a854
1 #! /usr/bin/perl
2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
4 if 0;
6 # ******************************************************************
7 # Author: Chad Elliott
8 # Date: 7/10/2008
9 # Description: Visual Studio doesn't support a post clean build step,
10 # so this script will do it.
11 # $Id$
12 # ******************************************************************
14 # ******************************************************************
15 # Pragma Section
16 # ******************************************************************
18 use strict;
19 use FileHandle;
20 use File::Basename;
21 use Cwd;
23 # ******************************************************************
24 # Data Section
25 # ******************************************************************
27 my $version = '1.0';
29 # ******************************************************************
30 # Subroutine Section
31 # ******************************************************************
33 sub read_proj {
34 my($cfg, $file) = @_;
35 my $fh = new FileHandle();
36 my $cmd;
38 if (open($fh, $file)) {
39 my $cfg_ok;
40 my $next_name;
41 my $next_command;
42 while(<$fh>) {
43 ## Locate the start of a Configuration section
44 if (/<Configuration\s*$/) {
45 $next_name = 1;
47 ## Next, find the configuration name
48 elsif ($next_name && /Name="(.+)"/) {
49 $cfg_ok = ($1 eq $cfg);
50 $next_name = undef;
52 ## Next, find the post clean event
53 elsif ($cfg_ok && /Name="VCPostCleanEventTool"/) {
54 $next_command = 1;
56 ## And finally, get the postclean command line
57 elsif ($next_command && /CommandLine="(.+)"/) {
58 $cmd = $1;
59 last;
62 close($fh);
65 ## Convert frequently used XML sequences to plain characters.
66 $cmd =~ s/&amp;/&/g;
67 $cmd =~ s/&quot;/\"/g;
68 $cmd =~ s/&gt;/>/g;
69 $cmd =~ s/&lt;/</g;
71 ## Return the command line (undef if there was no postclean)
72 return $cmd;
75 sub clean_proj {
76 my($cfg, $file) = @_;
78 ## Read the postclean command from the project
79 my $cmd = read_proj($cfg, $file);
81 ## Move to the directory of the project and run the command
82 if (defined $cmd) {
83 my $current_dir = getcwd();
84 if (chdir(dirname($file))) {
85 system($cmd);
86 chdir($current_dir);
88 else {
89 ## We'll only warn about files that we can't deal with.
90 print "WARNING: Unable to postclean $file\n";
95 sub clean_sln {
96 my($cfg, $file) = @_;
97 my $fh = new FileHandle();
99 ## For a solution, just read in and clean each project file we find.
100 if (open($fh, $file)) {
101 while (<$fh>) {
102 if (/^Project\([^)]+\)\s*=\s*"[^\"]+",\s*"([^\"]+)"/) {
103 clean_proj($cfg, $1);
106 close($fh);
110 # ******************************************************************
111 # Main Section
112 # ******************************************************************
114 if ($#ARGV == -1) {
115 print "PostClean v$version\n",
116 "Usage: ", basename($0), " [CFG=<configuration>] <project or solution files>\n\n",
117 "Executes the MPC generated VCPostCleanEventTool commands in a project.\n",
118 "These events are ignored by VisualStudio and must be handled by an outside\n",
119 "tool (i.e., this script).\n";
120 exit(0);
123 ## Determine the project or solution configuration (defaulting to the
124 ## default created by MPC).
125 my $cfg = 'Debug|Win32';
126 if ($ARGV[0] =~ /^CFG=(.+)/) {
127 $cfg = $1;
128 shift(@ARGV);
131 foreach my $file (@ARGV) {
132 if ($file =~ /\.sln$/) {
133 clean_sln($cfg, $file);
135 else {
136 ## It's not a solution file, we'll assume it's a project
137 clean_proj($cfg, $file);