blob: 1c35c953e61dff5fddac6eecb4b62c6ef150cf0a [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001use strict;
2use File::Basename;
3
4my ($fileOut,$depDir,$hList,$hTempFOlder) = @ARGV;
5
6if ($#ARGV < 1)
7{
8 print "pack_dep_gcc.pl output_filename dep_input_dir htogether_list header_temp\n";
9 die "DIE\n";
10}
11
12$hTempFOlder =~ s/^\.\///;
13my %Hpath;
14if (defined $hList && -d "$hTempFOlder") {
15 open HEADER_LIST,"<$hList" or die "Error: cannot open $hList";
16 while(<HEADER_LIST>) {
17 if ($_ =~ /cp(\s+)(\S+)(\s+)(\S+)/) {
18 my $origi_file = $2;
19 $origi_file =~ s/^\.\///;
20 my $headerF = basename($origi_file);
21 $Hpath{"$hTempFOlder/$headerF"} = $origi_file;
22 }
23 }
24 close HEADER_LIST;
25}
26
27my $depContent;
28opendir DIRHANDLE, "$depDir" or die "Cannot open dir $depDir: $!";
29my @fileList = readdir DIRHANDLE;
30closedir DIRHANDLE;
31foreach my $file (sort @fileList)
32{
33 next if (($file eq ".") || ($file eq ".."));
34 if ($file =~ /\.d$/i)
35 {
36 ParseDep(\$depContent, "$depDir/$file");
37 }
38}
39exit 0 if($depContent eq "");
40open OUTPUTHANDLE, ">$fileOut" or die "Cannot write $fileOut\n";
41print OUTPUTHANDLE "$depContent";
42close OUTPUTHANDLE;
43
44sub ParseDep
45{
46 my %header_list;
47 my $refContent = shift @_;
48 my $fileInput = shift @_;
49 my $Backup = $/;
50 undef $/;
51 open INPUTHANDLE, "<$fileInput" or die "Cannot open file $fileInput\n";
52 my $fileContent = <INPUTHANDLE>;
53 close INPUTHANDLE;
54 $/ = $Backup;
55 $fileContent =~ s/\\\n//gs;
56 $fileContent =~ s/\\/\//gs;
57 my @fileList = split(/\s+/, $fileContent);
58 my $index = 0;
59 while (($fileList[$index] =~ /^\s*(\/)?$/) && ($index <= $#fileList))
60 {
61 $index++;
62 }
63 if ($index <= $#fileList)
64 {
65 $fileList[$index] =~ s/(\S*\/)?(\S+?)\.(o|obj)\:/$2.obj\:/i;
66 $$refContent .= "#UPDATE#\n";
67 $$refContent .= $fileList[$index];
68 $index++;
69 while ($index <= $#fileList)
70 {
71 my $line;
72 my $file = $fileList[$index];
73 if (exists $Hpath{$file}){
74 $line = " " . $Hpath{$file};
75 } else {
76 $line = " " . $file;
77 }
78 #$line = " " . $fileList[$index];
79 $line =~ s/\s*(\S*\/)?(\S+?)\.(o|obj)\:\s*//i;
80 if ($line =~ /tools[\\\/]gcc/i)
81 {
82 ++$index;
83 next;
84 }elsif(defined $header_list{"$line"})
85 {
86 ++$index;
87 next;
88 }elsif(($line =~ /(.*)\:/) && !($line =~ /(.*)\.(o|obj)\:/))
89 {
90 ++$index;
91 next;
92 }
93 $$refContent .= "$line \\\n";
94 $header_list{"$line"} = 1;
95 $index++;
96 }
97 while ($$refContent =~ /\s*\\\n$/)
98 {
99 $$refContent =~ s/\s*\\\n$/\n/;
100 }
101 $$refContent .= "#ENDUPDATE#\n";
102 }
103 else
104 {
105 print "Unexpected file: " . $fileInput . "\n";
106 }
107}