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