| 1 |
|
| 2 |
|
| 3 |
eval 'exec perl -x $0 ${1+"$@"}' |
| 4 |
if 0; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
use strict; |
| 16 |
use warnings; |
| 17 |
|
| 18 |
use File::Basename; |
| 19 |
|
| 20 |
my @inputs = (); |
| 21 |
my @outputs = (); |
| 22 |
my @outdirs = (); |
| 23 |
my %vars = %ENV; |
| 24 |
|
| 25 |
sub expand_vars |
| 26 |
{ |
| 27 |
my ( $line, $vars ) = @_; |
| 28 |
|
| 29 |
chomp $line; |
| 30 |
my ( $varname, $value ) = split( "=", $line, 2 ); |
| 31 |
|
| 32 |
$varname =~ s/^\s*set(var)?\s+//; # remove 'set ', if we are in csh |
| 33 |
$varname =~ s/^\s+|\s+$//g; # remove leading and trailing spaces from variable name |
| 34 |
|
| 35 |
$value =~ s/^\s+|\s+$//g; # remove leading and trailing spaces |
| 36 |
|
| 37 |
if( $value !~ /\'.+\'/ || $value =~ /^".+"$/ ) { |
| 38 |
|
| 39 |
$value =~ s/\$(\w+)/\$\{$1\}/g; # eclose var names in braces: $NAME -> ${NAME} |
| 40 |
|
| 41 |
# |
| 42 |
# expand simple variables: VAR=val; ${VAR} -> val |
| 43 |
# |
| 44 |
for my $oldvarname ( sort keys %{$vars} ) { |
| 45 |
$value =~ s/\$\{$oldvarname\}/$vars->{$oldvarname}/g; |
| 46 |
$value =~ s/\$$oldvarname/$vars->{$oldvarname}/g; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
if( $value =~ /\$\((.+)\)/ ) { |
| 53 |
my $expansion = `$1`; |
| 54 |
chomp $expansion; |
| 55 |
$expansion =~ s/\n/ \\\n/mg; |
| 56 |
$value =~ s/\$\(.+\)/$expansion/g; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if( $value =~ /\`(.+)\`/ ) { |
| 63 |
my $expansion = `$1`; |
| 64 |
chomp $expansion; |
| 65 |
$value =~ s/\`.+\`/$expansion/g; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if( $value =~ /^'.+'$/ ) { |
| 70 |
$value =~ s/^\s*'|'\s*$//g; # remove single quotes at the end or at the beginning |
| 71 |
} else { |
| 72 |
$value =~ s/^\s*"|"\s*$//g; # remove double quotes at the end or at the beginning |
| 73 |
} |
| 74 |
|
| 75 |
$vars->{$varname} = $value; |
| 76 |
return wantarray ? ( $varname, $value ) : $value; |
| 77 |
} |
| 78 |
|
| 79 |
my $line = ""; |
| 80 |
|
| 81 |
my $basename; |
| 82 |
my $dirname; |
| 83 |
my $prefix; |
| 84 |
|
| 85 |
while( <> ) { |
| 86 |
|
| 87 |
if( !defined $basename ) { |
| 88 |
$basename = File::Basename::basename( $ARGV, ".com" ); |
| 89 |
$dirname = File::Basename::dirname( $ARGV ); |
| 90 |
$prefix = "${dirname}/${basename}"; |
| 91 |
$vars{"0"} = "$ARGV"; |
| 92 |
} |
| 93 |
|
| 94 |
if( /\\$/ ) { |
| 95 |
chomp; |
| 96 |
local $/ = "\\"; |
| 97 |
chomp; |
| 98 |
$line .= $_; |
| 99 |
next unless eof; |
| 100 |
} |
| 101 |
|
| 102 |
if( $line ) { |
| 103 |
$_ = $line . $_; |
| 104 |
$line = ""; |
| 105 |
} |
| 106 |
|
| 107 |
if( /^\s*((set|setvar)\s+)?INPUT[^=]*=(.+)$/ ) { |
| 108 |
my $depend = expand_vars( $_, \%vars ); |
| 109 |
push( @inputs, $depend ); |
| 110 |
next; |
| 111 |
} |
| 112 |
if( /^\s*((set|setvar)\s+)?OUTPUT[^=]*=(.+)$/ ) { |
| 113 |
my $depend = expand_vars( $_, \%vars ); |
| 114 |
push( @outputs, $depend ); |
| 115 |
next; |
| 116 |
} |
| 117 |
if( /^\s*((set|setvar)\s+)?OUTDIR[^=]*=(.+)$/ ) { |
| 118 |
my $depend = expand_vars( $_, \%vars ); |
| 119 |
push( @outdirs, $depend ); |
| 120 |
next; |
| 121 |
} |
| 122 |
if( /^\s*((set|setvar)\s+)?([a-zA-Z0-9_]+)\s*=(.+)$/ ) { |
| 123 |
expand_vars( $_, \%vars ); |
| 124 |
next; |
| 125 |
} |
| 126 |
if( /\s*#BEGIN\s+DEPEND/ ) { |
| 127 |
@inputs = (); |
| 128 |
@outputs = (); |
| 129 |
next; |
| 130 |
} |
| 131 |
if( /\s*#END\s+DEPEND/ || eof ) { |
| 132 |
if( !eof ) { |
| 133 |
close( ARGV ); |
| 134 |
} |
| 135 |
for( @inputs ) { |
| 136 |
print "${prefix}.log: $_\n"; |
| 137 |
} |
| 138 |
for( @outputs ) { |
| 139 |
print "$_: ${prefix}.log\n"; |
| 140 |
print "\t\@cd ${dirname}; test -f $_ || ". |
| 141 |
"sh -xc './${basename}.com > ${basename}.log 2>&1'\n"; |
| 142 |
print "\t\@cd ${dirname}; test -f $_ && touch $_\n"; |
| 143 |
} |
| 144 |
for( @outdirs ) { |
| 145 |
print "$_: ${prefix}.log\n"; |
| 146 |
print "\t\@cd ${dirname}; test -d $_ || ". |
| 147 |
"sh -xc './${basename}.com > ${basename}.log 2>&1'\n"; |
| 148 |
print "\t\@cd ${dirname}; test -d $_ && touch $_\n"; |
| 149 |
} |
| 150 |
if( @outputs || @outdirs ) { |
| 151 |
my $clean_subtargets = ""; |
| 152 |
if( @outputs ) { |
| 153 |
$clean_subtargets .= " cleanfile${basename}"; |
| 154 |
print "CLEAN_FILES += "; |
| 155 |
my $space = ""; |
| 156 |
for( @outputs ) { |
| 157 |
print "${space}$_"; |
| 158 |
$space = " "; |
| 159 |
} |
| 160 |
print "\n"; |
| 161 |
print "CLEAN_FILE_TARGETS += cleanfile${basename}\n"; |
| 162 |
print "cleanfile${basename}:\n"; |
| 163 |
for( @outputs ) { |
| 164 |
print "\trm -f $_\n"; |
| 165 |
} |
| 166 |
} |
| 167 |
if( @outdirs ) { |
| 168 |
$clean_subtargets .= " cleandir${basename}"; |
| 169 |
print "CLEAN_DIRS += "; |
| 170 |
my $space = ""; |
| 171 |
for( @outdirs ) { |
| 172 |
print "${space}$_"; |
| 173 |
$space = " "; |
| 174 |
} |
| 175 |
print "\n"; |
| 176 |
print "CLEAN_DIR_TARGETS += cleandir${basename}\n"; |
| 177 |
print "cleandir${basename}:\n"; |
| 178 |
for( @outdirs ) { |
| 179 |
print "\t" . "find '$_' -name .svn -prune -o " . |
| 180 |
"-type f -print0 | xargs -0 rm -fv\n"; |
| 181 |
} |
| 182 |
} |
| 183 |
print "CLEAN_TARGETS +=clean${basename}\n"; |
| 184 |
print "clean${basename}:${clean_subtargets}\n"; |
| 185 |
print "\trm -f ${prefix}.log\n"; |
| 186 |
} else { |
| 187 |
print "CLEAN_TARGETS +=clean${basename}\n"; |
| 188 |
print "clean${basename}:\n"; |
| 189 |
print "\trm -f ${prefix}.log\n"; |
| 190 |
} |
| 191 |
%vars = %ENV; |
| 192 |
@inputs = (); |
| 193 |
@outputs = (); |
| 194 |
} |
| 195 |
if( eof ) { |
| 196 |
$basename = $dirname = $prefix = undef; |
| 197 |
} |
| 198 |
} |