rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | use strict;
|
| 2 | use Cwd qw(abs_path getcwd);
|
| 3 |
|
| 4 | my $flag_print = 0;
|
| 5 | my $flag_create = 2;
|
| 6 | my $flag_modis = 0;
|
| 7 | if ($ARGV[0] =~ /^--append$/i) {
|
| 8 | $flag_create = 1;
|
| 9 | shift @ARGV;
|
| 10 | } elsif ($ARGV[0] =~ /^--extract$/i) {
|
| 11 | $flag_create = 0;
|
| 12 | shift @ARGV;
|
| 13 | } elsif ($ARGV[0] =~ /^--modis$/i) {
|
| 14 | $flag_modis = 2;
|
| 15 | shift @ARGV;
|
| 16 | }
|
| 17 |
|
| 18 | if ($#ARGV < 3) {
|
| 19 | die "Usage: pack_dep_gen.pl output.dep target.obj input/parse/dir regular_expression [file_prefix]\n";
|
| 20 | }
|
| 21 |
|
| 22 | my $depFile = shift @ARGV;
|
| 23 | my $objFile = shift @ARGV;
|
| 24 | my $depDirs = shift @ARGV;
|
| 25 | my $filters = shift @ARGV;
|
| 26 | my $preDirs = shift @ARGV;
|
| 27 |
|
| 28 | my $current = getcwd();
|
| 29 | $current =~ s/\\/\//g;
|
| 30 | $current =~ s/\/$//g;
|
| 31 | $current = quotemeta($current);
|
| 32 | $preDirs .= "/" if ($preDirs ne "");
|
| 33 |
|
| 34 | if ($flag_create > 0) {
|
| 35 | my @cgenDep;
|
| 36 | &CollectDepend(\@cgenDep, $depFile, $depDirs, $filters);
|
| 37 | print "\n" if ($flag_modis == 0);
|
| 38 | &WriteDepend(\@cgenDep, $depFile, $objFile, $preDirs);
|
| 39 | } else {
|
| 40 | &ExtractDepend($depFile, $depDirs);
|
| 41 | }
|
| 42 |
|
| 43 | sub CollectDepend {
|
| 44 | my $refArray = shift;
|
| 45 | my $outputDep = shift;
|
| 46 | my $inputDir = shift;
|
| 47 | my $exprRule = shift;
|
| 48 |
|
| 49 | my $DIRHANDLE;
|
| 50 | my $FILEHANDLE;
|
| 51 | if (opendir($DIRHANDLE, $inputDir)) {
|
| 52 | } else {
|
| 53 | print "Skip for opendir " . $inputDir . "\n";
|
| 54 | return 0;
|
| 55 | }
|
| 56 |
|
| 57 | my @fileDep = readdir $DIRHANDLE;
|
| 58 | closedir $DIRHANDLE;
|
| 59 | print "Processing" if ($flag_modis == 0);
|
| 60 | foreach my $dep (@fileDep) {
|
| 61 | next if ($dep !~ /$exprRule\b/i);
|
| 62 | if ($outputDep =~ /\b${dep}$/) {
|
| 63 | next if ($flag_create != 1);
|
| 64 | }
|
| 65 | print " " . $dep if ($flag_modis == 0);
|
| 66 | my $line;
|
| 67 | open $FILEHANDLE, "<$inputDir/$dep";
|
| 68 | if ($dep =~ /\.lis$/i) {
|
| 69 | # for cgen.exe -src or xcopy
|
| 70 | while ($line = <$FILEHANDLE>) {
|
| 71 | if ($line =~ /^(\S+\.\w+)\s*$/i) {
|
| 72 | my $src = $1;
|
| 73 | push @$refArray, $src;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | elsif ($dep =~ /mmi_copy\.log$/i) {
|
| 78 | # for ResGenerator_OP
|
| 79 | my $log_flag;
|
| 80 | my $path;
|
| 81 | while ($line = <$FILEHANDLE>) {
|
| 82 | my $src;
|
| 83 | if ($line =~ /^\s*(.+?)\s*copied from (\S+) to \S+/i) {
|
| 84 | my $line2 = $1;
|
| 85 | $path = $2;
|
| 86 | $log_flag = "";
|
| 87 | if ($line2 =~ /^.*?\((\w+\.\w+)\)$/) {
|
| 88 | $src = $1;
|
| 89 | } elsif ($line2 =~ /^\w+\.\w+$/) {
|
| 90 | $src = $line2;
|
| 91 | } elsif ($line2 =~ /^.*?\(\*\.(\w+)\)$/) {
|
| 92 | $log_flag = $1;
|
| 93 | }
|
| 94 | } elsif ($log_flag ne "") {
|
| 95 | if ($line =~ /(\S+?\.$log_flag)\s*$/i) {
|
| 96 | $src = $1;
|
| 97 | }
|
| 98 | } else {
|
| 99 | $log_flag = "";
|
| 100 | $path = "";
|
| 101 | }
|
| 102 | if (($src ne "") && ($path ne "")) {
|
| 103 | $path =~ s/^\\//;
|
| 104 | $src = $path . "\\" . $src;
|
| 105 | push @$refArray, $src;
|
| 106 | }
|
| 107 | }
|
| 108 | } elsif ($dep =~ /\.log$/i) {
|
| 109 | # for MoDIS cl.exe or pregen log
|
| 110 | while ($line = <$FILEHANDLE>) {
|
| 111 | my $line2;
|
| 112 | $line =~ s/[\r\n]//g;
|
| 113 | if ($line =~ /\[Dependency\]\s*(.*?)\s*$/i) {
|
| 114 | $line2 = $1;
|
| 115 | } elsif ($line =~ /Note: including file:\s*(.*?)\s*$/i) {
|
| 116 | $line2 = $1;
|
| 117 | } elsif ($line =~ /Parsing\s+File\s*:\s*(.*?)\s*$/i) {
|
| 118 | $line2 = $1;
|
| 119 | } elsif ($line =~ /Processing archive:\s*(.*?)\s*$/i) {
|
| 120 | $line2 = $1;
|
| 121 | } elsif ($line =~ /^Res:\s*(\S+\.\w+)\s*$/i) {
|
| 122 | $line2 = $1;
|
| 123 | } elsif ($line =~ /^==VenusXML Begin==\s+\[(\S+\.\w+)\]\s*$/i) {
|
| 124 | $line2 = $1;
|
| 125 | }
|
| 126 | next if ($line2 =~ /(Program Files)|(Microsoft Visual Studio)/i);
|
| 127 | foreach my $src (split(/\s+/, $line2)) {
|
| 128 | #if (($src ne "") && ($src !~ /\.obj$/i))
|
| 129 | if ($src =~ /\.(c|cpp)$/i) {
|
| 130 | unshift(@$refArray, $src);
|
| 131 | } elsif ($src ne "") {
|
| 132 | push(@$refArray, $src);
|
| 133 | }
|
| 134 | }
|
| 135 | if ($line2 =~ /\\(Audio|Image)\.zip$/i) {
|
| 136 | # for resgen
|
| 137 | $line2 =~ s/\\(Audio|Image)\.zip$//i;
|
| 138 | print "\n" if ($flag_modis == 0);
|
| 139 | my @resDep;
|
| 140 | &CollectDepend(\@resDep, $outputDep, $line2, "_file_list\.ini");
|
| 141 | $line2 =~ s/^(\w+mmi\\+Customer\\+ResGenerator\\+).*$/$1/i;
|
| 142 | foreach my $src (@resDep) {
|
| 143 | if ($src =~ /\\+Customer\\+Images\\+\w+\\+(Main|Sub)LCD\\+/i) {
|
| 144 | } elsif ($src =~ /\\+Customer\\+Audio\\+(PLUTO|NEPTUNE)\\+\w+\\+/i) {
|
| 145 | } else {
|
| 146 | push @$refArray, $line2 . $src;
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 | } elsif ($dep =~ /\.ini$/i) {
|
| 152 | # for audio_file_list.ini binary_file_list.ini images_file_list.ini
|
| 153 | while ($line = <$FILEHANDLE>) {
|
| 154 | next if ($line =~ /^\[.+\]\s*$/i);
|
| 155 | if ($line =~ /^file name\s*\w+\s*=\s*(.*?)\s*$/i) {
|
| 156 | push @$refArray, $1;
|
| 157 | }
|
| 158 | }
|
| 159 | } else {
|
| 160 | # for Target armcc.exe
|
| 161 | while ($line = <$FILEHANDLE>) {
|
| 162 | next if ($line =~ /(Program Files)|(Microsoft Visual Studio)/i);
|
| 163 | next if ($line =~ /^\#/i);
|
| 164 | $line =~ s/[\r\n]//g;
|
| 165 | $line =~ s/^\s*(\S+\:)?\s*(.*?)\s*(\\)$/$2/;
|
| 166 | foreach my $src (split(/\s+/, $line)) {
|
| 167 | next if ($src =~ /mcd[\\\/]include[\\\/]mcd_l3\.h/i);
|
| 168 | if (($src ne "") && ($src !~ /[\\\/]~/) && ($src !~ /^~/)) {
|
| 169 | if ($src =~ /\.(c|cpp)$/i) {
|
| 170 | unshift(@$refArray, $src);
|
| 171 | } else {
|
| 172 | push(@$refArray, $src);
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 | close $FILEHANDLE;
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | sub WriteDepend {
|
| 183 | my $refArray = shift;
|
| 184 | my $outputDep = shift;
|
| 185 | my $targetDep = shift;
|
| 186 | my $prefixDir = shift;
|
| 187 | my $objFirst = 1;
|
| 188 | my $targetFirst = 1;
|
| 189 | my $outText;
|
| 190 | my %saw;
|
| 191 | my @fileDep = grep (!$saw{$_}++, @$refArray);
|
| 192 | my %fileSrc;
|
| 193 | my %fileObj;
|
| 194 | my $pre_dep;
|
| 195 | my %hash_dep;
|
| 196 |
|
| 197 | foreach my $dep (@fileDep) {
|
| 198 | if ($dep ne "") {
|
| 199 | $dep = $prefixDir . $dep if ($dep !~ /^[a-zA-Z]:\\/);
|
| 200 | $dep =~ s/[\\\/]+/\//g;
|
| 201 | $dep =~ s/\/\.\//\//g;
|
| 202 | $dep =~ s/^\.\///;
|
| 203 | while ($dep =~ /\w+\/\.\.\//) {
|
| 204 | $dep =~ s/\w+\/\.\.\///;
|
| 205 | }
|
| 206 | while ($dep =~ /^(\/)?\.\.\// and $dep !~ /^(\/)?\.\.\/common\//) {
|
| 207 | $dep =~ s/^(\/)?\.\.\///;# remove "..\" except universal common
|
| 208 | }
|
| 209 | while ($dep =~ /^\w\:\/\.\.\//) {
|
| 210 | $dep =~ s/^(\w\:\/)\.\.\//$1/;# remove "k:\..\"
|
| 211 | }
|
| 212 | $dep =~ s/^$current\///i;
|
| 213 | if ($flag_modis) {
|
| 214 | $dep =~ s/^[k-z]\:[\/]+//i;# for subst
|
| 215 | if ($dep =~ /.+\/(\S+?)\.(c|cpp)$/i) {
|
| 216 | $fileSrc{lc($1)} = $dep;
|
| 217 | if ($^O eq "MSWin32") {
|
| 218 | $fileSrc{lc($1)} =~ s/\//\\/g;
|
| 219 | }
|
| 220 | next;
|
| 221 | } elsif ($dep =~ /.+\/(\S+?)\.obj$/i) {
|
| 222 | $fileObj{lc($1)} = $fileSrc{lc($1)};
|
| 223 | next;
|
| 224 | } elsif ($dep =~ /\/auto_header\.h/i) {
|
| 225 | next;
|
| 226 | }
|
| 227 | }
|
| 228 | # no need to do anything for xxxx.h:
|
| 229 | if (! ($dep =~ /(\S+)\:/) ) {
|
| 230 | next if (! -e $dep);
|
| 231 | }
|
| 232 |
|
| 233 | if (($^O eq "MSWin32") && ($flag_modis)) {
|
| 234 | $dep =~ s/\//\\/g;
|
| 235 | }
|
| 236 |
|
| 237 | if ($objFirst) {
|
| 238 | $outText .= " $dep";
|
| 239 | $objFirst = 0;
|
| 240 | } else {
|
| 241 | if (! ($dep =~ /(\S+)\:/) ) {
|
| 242 | $outText .= " \\\n $dep";
|
| 243 | }
|
| 244 | }
|
| 245 | #remove ":" from target and push to hash
|
| 246 | $dep =~ s/\://;
|
| 247 | $hash_dep{$dep} = 1;
|
| 248 | }
|
| 249 | }
|
| 250 | #creating make target for every prerequisite files
|
| 251 | $outText .= "\n";
|
| 252 | for (sort(keys %hash_dep)) {
|
| 253 | $outText .= "\n$_:";
|
| 254 | }
|
| 255 |
|
| 256 | my $FILEHANDLE;
|
| 257 | open $FILEHANDLE, ">$outputDep" or die "Fail to write $outputDep\n";
|
| 258 | if ($flag_modis) {
|
| 259 | foreach my $key (keys %fileSrc) {
|
| 260 | next if (exists $fileObj{$key});
|
| 261 | my $value = $fileSrc{$key};
|
| 262 | $outText .= " \\\n $value";
|
| 263 | print "include " . $value . " for " . $key ."\n" if ($flag_print);
|
| 264 | }
|
| 265 | foreach my $key (sort keys %fileObj) {
|
| 266 | print $FILEHANDLE "#UPDATE#\n";
|
| 267 | print $FILEHANDLE $key . ".obj: " . $fileObj{$key} . " \\\n ";
|
| 268 | print $FILEHANDLE $outText . "\n";
|
| 269 | print $FILEHANDLE "#ENDUPDATE#\n";
|
| 270 | }
|
| 271 | } else {
|
| 272 | print $FILEHANDLE $targetDep . ":";
|
| 273 | print $FILEHANDLE $outText . "\n";
|
| 274 | }
|
| 275 | close $FILEHANDLE;
|
| 276 | }
|
| 277 |
|
| 278 | sub ExtractDepend {
|
| 279 | my $inputDep = shift;
|
| 280 | my $outputDir = shift;
|
| 281 | my $FILEHANDLE;
|
| 282 | open $FILEHANDLE, "<$inputDep" or die "Fail to open $inputDep\n";
|
| 283 | my @fileDep = <$FILEHANDLE>;
|
| 284 | close $FILEHANDLE;
|
| 285 | my $targetDep;
|
| 286 | foreach my $line (@fileDep) {
|
| 287 | if ($line =~ /\#UPDATE\#/i) {
|
| 288 |
|
| 289 | } elsif ($line =~ /^(\S+?):\s*(\S+)\s*\\/) {
|
| 290 | if ($targetDep ne "") {
|
| 291 | close $FILEHANDLE;
|
| 292 | $targetDep = "";
|
| 293 | }
|
| 294 | $targetDep = $1;
|
| 295 | $targetDep =~ s/^.*\\//;
|
| 296 | $targetDep =~ s/\.\w+$/.det/;
|
| 297 | open $FILEHANDLE, ">$outputDir/$targetDep" or die "Fail to write $outputDir/$targetDep\n";
|
| 298 | print $FILEHANDLE "\#UPDATE\#\n";
|
| 299 | print $FILEHANDLE $line;
|
| 300 | }
|
| 301 | elsif ($line =~ /^\s*(\S+)\s*\\?/)
|
| 302 | {
|
| 303 | print $FILEHANDLE $line;
|
| 304 | }
|
| 305 | }
|
| 306 | if ($targetDep ne "")
|
| 307 | {
|
| 308 | close $FILEHANDLE;
|
| 309 | $targetDep = "";
|
| 310 | }
|
| 311 | }
|