Merge pull request #20 from jwillemsen/master
[MPC.git] / combine_dsw.pl
blobd5b4d95cbfda2d0bd3a64a8fd753811a7df34b6b
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: 4/8/2004
9 # Description: Combined multiple dsw's into a single dsw
10 # ******************************************************************
12 # ******************************************************************
13 # Pragma Section
14 # ******************************************************************
16 use strict;
17 use FileHandle;
18 use File::Basename;
20 # ******************************************************************
21 # Data Section
22 # ******************************************************************
24 my $version = '1.3';
26 # ******************************************************************
27 # Subroutine Section
28 # ******************************************************************
30 sub usageAndExit {
31 my $str = shift;
32 print STDERR "$str\n" if (defined $str);
33 print STDERR "Combine DSW v$version\n",
34 "Usage: ", basename($0),
35 " [-u] <output file> <input files...>\n\n",
36 "-u Each input file will be removed after successful ",
37 "combination\n\n",
38 "NOTE: This script will work for vcw's too.\n\n",
39 "Combine multiple dsw's into a single dsw. You can use ",
40 "MPC to generate\n",
41 "dynamic projects and then generate static projects using ",
42 "the -static,\n",
43 "-name_modifier and -apply_project options together. You ",
44 "can then run this\n",
45 "script to combine the workspaces into one.\n";
46 exit(0);
49 # ******************************************************************
50 # Main Section
51 # ******************************************************************
53 my $output;
54 my $unlink;
55 my @input;
57 for(my $i = 0; $i <= $#ARGV; $i++) {
58 my $arg = $ARGV[$i];
59 if ($arg =~ /^-/) {
60 if ($arg eq '-u') {
61 $unlink = 1;
63 else {
64 usageAndExit("Unknown option: $arg");
67 else {
68 if (!defined $output) {
69 $output = $arg;
71 else {
72 push(@input, $arg);
77 ## Print the usage if there is no output file provided or there isn't at
78 ## least two input files.
79 usageAndExit() if (!defined $output ||
80 !defined $input[0] || !defined $input[1]);
82 my $tmp = "$output.$$.tmp";
83 my $oh = new FileHandle();
85 if (open($oh, ">$tmp")) {
86 my $msident;
87 for(my $i = 0; $i <= $#input; ++$i) {
88 ## We only want to take the global settings from the last input file.
89 my $input = $input[$i];
90 my $global = ($i == $#input);
92 ## Read in the input file and write out the parts that are common
93 ## between multiple workspace files (but only on the first input
94 ## file). After that, write out the project information from each
95 ## input file.
96 my $fh = new FileHandle();
97 if (open($fh, $input)) {
98 my $in_global = 0;
99 while(<$fh>) {
100 if (/Microsoft\s+(Developer\s+Studio|eMbedded\s+Visual)/) {
101 ## We only want to print out the identifier from the first
102 ## input file.
103 if (!$msident) {
104 $msident = 1;
105 print $oh $_;
108 else {
109 if (/^Global:/) {
110 $in_global = 1;
112 elsif ($in_global && /^[#]{79,}/) {
113 $in_global = 0;
114 $_ = '';
117 ## Only print out the line if it's not part of the global
118 ## settings (unless this is the last input file).
119 print $oh $_ if (!$in_global || ($global && $in_global));
122 close($fh);
124 else {
125 print STDERR "ERROR: Unable to open '$input' for reading\n";
126 exit(2);
129 close($oh);
131 ## If the user wants us to remove the input files after successfully
132 ## combining them, then do so here.
133 unlink(@input) if ($unlink);
135 ## We have written the new workspace to a temporary file to allow an
136 ## input file to be the same as an output file. Once we're done, we
137 ## need to move it to it's correct output name.
138 unlink($output);
139 rename($tmp, $output);
141 else {
142 print STDERR "ERROR: Unable to open '$tmp' for writing\n";
143 exit(1);