-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_base.pl
executable file
·199 lines (172 loc) · 5.64 KB
/
create_base.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#! /usr/bin/perl
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
& eval 'exec perl -w -S $0 $argv:q'
if 0;
# ******************************************************************
# Author: Chad Elliott
# Date: 9/13/2007
# $Id$
# Description: Generate a base project based on a library project
# ******************************************************************
# ******************************************************************
# Pragma Section
# ******************************************************************
use strict;
use FindBin;
use FileHandle;
use File::Spec;
use File::Basename;
my $basePath = $FindBin::Bin;
if ($^O eq 'VMS') {
$basePath = File::Spec->rel2abs(dirname($0)) if ($basePath eq '');
$basePath = VMS::Filespec::unixify($basePath);
}
unshift(@INC, $basePath . '/modules');
require Creator;
# ******************************************************************
# Data Section
# ******************************************************************
my $version = '0.1';
# ******************************************************************
# Subroutine Section
# ******************************************************************
sub gather_info {
my $name = shift;
my $fh = new FileHandle();
if (open($fh, $name)) {
my @lines = ();
my $pname = undef;
while(<$fh>) {
## Get the line a remove leading and trailing white space
my $line = $_;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
## Look for the project declaration and pull out the name and any
## parents.
if ($line =~ /^project\s*(\(([^\)]+)\))?\s*(:.+)?\s*{$/) {
$pname = $2;
my $parents = $3 || '';
## Create the default project name by removing the extension and
## converting back-slashes, spaces and dashes to underscores.
## This needs to be done regardless of whether the project name
## was provided or not since it's used below in the
## fill_type_name call.
my $def = basename($name);
$def =~ s/\.[^\.]+$//;
$def =~ s/\\/_/g;
$def =~ s/[\s\-]/_/g;
if (!defined $pname || $pname eq '') {
## Take the default project name since one wasn't provided.
$pname = $def;
}
else {
## Convert back-slashes, spaces and dashes to underscores.
$pname =~ s/\\/_/g;
$pname =~ s/[\s\-]/_/g;
}
## If the project has a '*' we need to have MPC fix that up for
## us.
$pname = Creator::fill_type_name(undef, $pname, $def);
push(@lines, "project$parents {");
}
elsif ($line =~ /^(shared|static)name\s*=\s*(.+)$/) {
## Add in the libs and after settings.
my $lib = $2;
if (defined $pname && $lib ne '') {
push(@lines, " libs += $2",
" after += $pname",
"}");
}
last;
}
}
close($fh);
## Only return the lines if there is more than one line. It is
## possible (and likely) that we've read in the project declaration,
## but the project did not contain a sharedname or staticname
## setting.
return @lines if ($#lines > 0);
}
return ();
}
sub write_base {
my($in, $out) = @_;
my @lines = gather_info($in);
if ($#lines >= 0) {
if (-r $out) {
print STDERR "ERROR: $out already exists\n";
}
else {
my $fh = new FileHandle();
if (open($fh, ">$out")) {
foreach my $line (@lines) {
print $fh "$line\n";
}
close($fh);
## Everything was ok, return zero.
return 0;
}
else {
print STDERR "ERROR: Unable to write to $out\n";
}
}
}
else {
if (-r $in) {
print STDERR "ERROR: $in is not a valid MPC file\n";
}
else {
print STDERR "ERROR: Unable to read from $in\n";
}
}
## Non-zero indicates an error (as in the shell $? value).
return 1;
}
sub usageAndExit {
my $str = shift;
print STDERR "$str\n" if (defined $str);
print STDERR "Create Base Project v$version\n",
"Usage: ", basename($0), " <mpc files> <output file or ",
"directory>\n\nThis script will create a base project ",
"based on the contents of the\nsupplied MPC file. ",
"This is only useful if the project ",
"explictly sets\nsharedname or staticname.\n";
exit(0);
}
# ******************************************************************
# Main Section
# ******************************************************************
if ($#ARGV > 1) {
## Get the last argument and make sure it's a directory.
my $dir = pop(@ARGV);
if (!-d $dir) {
usageAndExit("Creating multiple base projects, but the " .
"last argument, $dir, is not a directory");
}
## Process each input file and create the base project with an implicit
## base project file name.
my $status = 0;
foreach my $input (@ARGV) {
my $output = $dir . '/' . lc(basename($input));
$output =~ s/mpc$/mpb/;
$status += write_base($input, $output);
}
exit($status);
}
else {
my $input = shift;
my $output = shift;
## Print the usage and exit if there is no input, output or the input
## file looks like an option.
usageAndExit() if (!defined $output ||
!defined $input || index($input, '-') == 0);
## If the output file is a directory, we will create the output file
## name based on the input file.
if (-d $output) {
$output .= '/' . lc(basename($input));
$output =~ s/mpc$/mpb/;
}
## Create the base project and return the status to the caller of the
## script.
exit(write_base($input, $output));
}