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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
package Driver;
# ************************************************************
# Description : Generate dependencies for Make and NMake.
# Author : Chad Elliott
# Create Date : 3/21/2007
# ************************************************************
# ************************************************************
# Pragma Section
# ************************************************************
use strict;
use File::Basename;
use DependencyEditor;
# ************************************************************
# Data Section
# ************************************************************
my $version = '1.2';
my $os = ($^O eq 'MSWin32' ? 'Windows' : 'UNIX');
my %types;
my %defaults = ('UNIX' => 'make',
'Windows' => 'nmake',
);
# ************************************************************
# Subroutine Section
# ************************************************************
sub BEGIN {
my $fh = new FileHandle();
my(%writers, %generators);
## Find all the dependency writers and object generators
foreach my $dir (@INC) {
if (opendir($fh, $dir)) {
foreach my $module (readdir($fh)) {
if ($module =~ /(.+)DependencyWriter\.pm$/) {
my $type = lc($1);
my $class = $module;
$class =~ s/\.pm$//;
require $module;
$writers{$type} = $class;
$types{$type} = 1;
}
elsif ($module =~ /(.+)ObjectGenerator\.pm$/) {
my $type = lc($1);
my $class = $module;
$class =~ s/\.pm$//;
require $module;
$generators{$type} = $class;
}
}
closedir($fh);
}
}
## Register them with the right factory
DependencyWriterFactory::register(\%writers);
ObjectGeneratorFactory::register(\%generators);
}
sub new {
my $class = shift;
my $self = bless {'automatic' => [],
}, $class;
foreach my $add (@_) {
if ($add =~ /(UNIX|Windows)=(.*)/) {
$defaults{$1} = $2;
}
elsif ($add =~ /automatic=(.*)/) {
my @auto = split(/,/, $1);
$self->{'automatic'} = \@auto;
}
else {
print "WARNING: Unknown parameter: $add\n";
}
}
return $self;
}
sub usageAndExit {
my($self, $opt) = @_;
my $base = basename($0);
if (defined $opt) {
print "$opt.\n";
}
print "$base v$version\n" .
"Usage: $base [-D<MACRO>[=VALUE]] [-I<include dir>] ",
(defined $self->{'automatic'}->[0] ? "[-A] " : ''),
"[-R <VARNAME>]\n" .
" " . (" " x length($base)) .
" [-e <file>] [-f <output file>] [-i] [-t <type>] [-n]\n" .
" " . (" " x length($base)) . " <files...>\n" .
"\n";
if (defined $self->{'automatic'}->[0]) {
print "-A Replace paths equal to the following variables with ",
"the corresponding \$()\n value: ",
join(', ', @{$self->{'automatic'}}), ".\n";
}
print "-D This option sets a macro to an optional value.\n" .
"-I The -I option adds an include directory.\n" .
"-R Replace \$VARNAME paths with \$(VARNAME).\n" .
"-e Exclude dependencies generated by <file>, but not <file> " .
"itself.\n" .
"-f Specifies the output file. This file will be edited if it " .
"already\n exists.\n" .
"-i Do not print an error if no source files are provided.\n" .
"-n Do not include inline files (ending in .i or .inl) in the " .
"dependencies.\n" .
"-t Use specified type (";
my @keys = sort keys %types;
for(my $i = 0; $i <= $#keys; ++$i) {
print "$keys[$i]" .
($i != $#keys ? $i == $#keys - 1 ? ' or ' : ', ' : '');;
}
print ") instead of the default.\n" .
" The default is ";
@keys = sort keys %defaults;
for(my $i = 0; $i <= $#keys; ++$i) {
my $def = $keys[$i];
print $defaults{$def} . " on $def" .
($i != $#keys ? $i == $#keys - 1 ? ' and ' : ', ' : '');
}
print ".\n";
exit(0);
}
sub setReplace {
my($self, $replace, $name, $value) = @_;
if (defined $name) {
## The key will be used in a regular expression.
## So, we need to escape some special characters.
$name = File::Spec->canonpath($name);
$name =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
$$replace{$name} = $value;
}
}
sub run {
my($self, $args) = @_;
my $argc = scalar(@$args);
my $type = $defaults{$os};
my $output = '-';
my $needsrc = 1;
my($noinline, @files, %macros, @ipaths, %replace, %exclude);
for(my $i = 0; $i < $argc; ++$i) {
my $arg = $$args[$i];
if ($arg =~ /^\-D(\w+)(=(.*))?/) {
$macros{$1} = $3;
}
elsif ($arg =~ /^\-I(.*)/) {
push(@ipaths, File::Spec->canonpath($1));
}
elsif ($arg eq '-A') {
foreach my $auto (@{$self->{'automatic'}}) {
$self->setReplace(\%replace, $ENV{$auto}, '$(' . $auto . ')');
}
}
elsif ($arg eq '-R') {
++$i;
$arg = $$args[$i];
if (defined $arg) {
my $val = $ENV{$arg};
if (defined $val) {
$self->setReplace(\%replace, $val, "\$($arg)");
}
}
else {
$self->usageAndExit('Invalid use of -R');
}
}
elsif ($arg eq '-e') {
++$i;
$arg = $$args[$i];
if (defined $arg) {
$exclude{$arg} = 1;
}
else {
$self->usageAndExit('Invalid use of -e');
}
}
elsif ($arg eq '-f') {
++$i;
$arg = $$args[$i];
if (defined $arg) {
$output = $arg;
}
else {
$self->usageAndExit('Invalid use of -f');
}
}
elsif ($arg eq '-i') {
$needsrc = undef;
}
elsif ($arg eq '-n') {
$noinline = 1;
}
elsif ($arg eq '-h') {
$self->usageAndExit();
}
elsif ($arg eq '-t') {
++$i;
$arg = $$args[$i];
if (defined $arg && defined $types{$arg}) {
$type = $arg;
}
else {
$self->usageAndExit('Invalid use of -t');
}
}
elsif ($arg =~ /^[\-+]/) {
## We will ignore unknown options
## Some options for aCC start with +
}
else {
push(@files, $arg);
}
}
if (!defined $files[0]) {
if ($needsrc) {
$self->usageAndExit('No files specified');
}
}
my $editor = new DependencyEditor();
return $editor->process($output, $type, $noinline, \%macros,
\@ipaths, \%replace, \%exclude, \@files);
}
|