tools/static_dependencies.pl
branchsystem_startup
changeset 31 f56b9efb7155
parent 25 f213623e3c86
child 80 3ab0df073c86
equal deleted inserted replaced
30:292fee808849 31:f56b9efb7155
    15 
    15 
    16 use strict;
    16 use strict;
    17 use Getopt::Long;
    17 use Getopt::Long;
    18 
    18 
    19 my $inverted_table = 0;
    19 my $inverted_table = 0;
       
    20 my $existing_file;
    20 GetOptions(
    21 GetOptions(
    21   "i|invert" => \$inverted_table,   # add the inverted table
    22   "i|invert" => \$inverted_table,   # add the inverted table
       
    23   "u|update=s" => \$existing_file,  # update existing file
    22   );
    24   );
       
    25 
       
    26 my @existinglines;
       
    27 if ($existing_file)
       
    28 	{
       
    29 	open EXISTING, "<$existing_file" or die("Cannot open $existing_file: $!\n");
       
    30 	print STDERR "Reading existing dependencies from $existing_file\n";
       
    31 	@existinglines = <EXISTING>;
       
    32 	close EXISTING;
       
    33 	}
    23 
    34 
    24 my %romfiles;
    35 my %romfiles;
    25 my @contents;
    36 my @contents;
    26 
    37 
    27 my $line;
    38 my $line;
    28 while ($line = <>)
    39 while ($line = <>)
    29 	{
    40 	{
    30   my ($romfile,$hostfile,@columns) = split /,/, $line;		# first two fields are guaranteed to be simple
    41   my ($romfile,$hostfile,$ibyfile,$package,$cmd,@columns) = split /,/, $line;		# first 5 fields are guaranteed to be simple
    31 	next if (!defined $hostfile);
    42 	next if (!defined $hostfile);
    32 	next if ($romfile eq "ROM file");		# skip header line
    43 	next if ($romfile eq "ROM file");		# skip header line
    33 	
    44 	
    34 	push @contents, "$romfile\t$hostfile";		# for use with "grep" later
    45 	if (lc $cmd eq "stem")
       
    46 		{
       
    47 		$hostfile =~ s/(\/|\\)([^\\\/]+)$/$1stem_$2/;			# use stem version instead
       
    48 		}
       
    49 	push @contents, "$romfile\t$hostfile";
    35 	$romfiles{lc $romfile} = $romfile;
    50 	$romfiles{lc $romfile} = $romfile;
    36 	}
    51 	}
    37 
    52 
       
    53 sub canonical_romfile($)
       
    54 	{
       
    55 	my ($romfile) = @_;
       
    56 	my $canonical = $romfiles{lc $romfile};
       
    57 	return $canonical if (defined $canonical);
       
    58 
       
    59 	# New romfile not seen before - add to table
       
    60 	$romfiles{lc $romfile} = $romfile;	# set the standard for others!
       
    61 	return $romfile;	
       
    62 	}
       
    63 
       
    64 my %outputlines;
       
    65 foreach my $existingline (@existinglines)
       
    66 	{
       
    67 	chomp $existingline;
       
    68 	my ($romfile, $hostfile, $deps) = split /\t/, $existingline;
       
    69 	if (defined $deps)
       
    70 		{
       
    71 		$romfile = canonical_romfile($romfile);
       
    72 		$outputlines{$romfile} = "$romfile\t$hostfile\t$deps";
       
    73 		}
       
    74 	}
       
    75 
    38 my %dependents;
    76 my %dependents;
    39 
    77 
    40 sub print_dependency($$@)
    78 sub print_dependency($$@)
    41 	{
    79 	{
    42 	my ($romfile,$hostfile,@dependencies) = @_;
    80 	my ($romfile,$hostfile,@dependencies) = @_;
    43 	print "$romfile\t$hostfile\t", join(":",@dependencies), "\n";
    81 	$outputlines{$romfile} = "$romfile\t$hostfile\t". join(":",@dependencies);
    44 	
    82 	
    45 	next unless $inverted_table;
    83 	next unless $inverted_table;
    46 	
    84 	
    47 	# Create inverted table 
    85 	# Create inverted table 
    48 	foreach my $dependent (@dependencies)
    86 	foreach my $dependent (@dependencies)
    91 			}
   129 			}
    92 		}
   130 		}
    93 	print_dependency($romfile,$hostfile,"sid=$sid",@imports);
   131 	print_dependency($romfile,$hostfile,"sid=$sid",@imports);
    94 	}
   132 	}
    95 
   133 
       
   134 sub find_exe_names_dependencies($$)
       
   135 	{
       
   136 	my ($romfile,$hostfile) = @_;
       
   137 	
       
   138 	my @strings = `$ENV{"SBS_HOME"}\\win32\\mingw\\bin\\strings $hostfile`;
       
   139 	
       
   140 	my %executables;
       
   141 	foreach my $string (@strings)
       
   142 		{
       
   143 		if ($string =~ /^(.*\\)?([^-\\]+\.(exe|dll))$/i)
       
   144 			{
       
   145 			my $exename = $2;
       
   146 			$exename =~ s/^\s+//;		# strip off leading whitespace (e.g.length byte before "clock.exe")
       
   147 			$exename = canonical_romfile("sys\\bin\\$exename");	# get the exact capitalisation
       
   148 			# print STDERR "Found $exename in $string";
       
   149 			$executables{$exename} = 1;
       
   150 			}
       
   151 		}
       
   152 	if (%executables)
       
   153 		{
       
   154 		print_dependency($romfile,$hostfile,sort keys %executables);
       
   155 		}
       
   156 	else
       
   157 		{
       
   158 		print STDERR "No executable names found in system statup resource $hostfile\n";
       
   159 		}
       
   160 	}
       
   161 
    96 sub find_dependency_in_sys_bin($$$)
   162 sub find_dependency_in_sys_bin($$$)
    97 	{
   163 	{
    98 	my ($romfile,$hostfile,$basename) = @_;
   164 	my ($romfile,$hostfile,$basename) = @_;
    99 	$basename = lc $basename;
   165 	$basename = lc $basename;
   100 	foreach my $extension (".exe",".dll")
   166 	foreach my $extension (".exe",".dll")
   136 		
   202 		
   137 		find_dependency_in_sys_bin($romfile,$hostfile,$executable);
   203 		find_dependency_in_sys_bin($romfile,$hostfile,$executable);
   138 		next;
   204 		next;
   139 		}
   205 		}
   140 
   206 
       
   207 	# System state manager resource files
       
   208 	if ($romfile =~ /private.2000d75b\\.*\.rsc$/i)
       
   209 		{
       
   210 		find_exe_names_dependencies($romfile,$hostfile);
       
   211 		next;
       
   212 		}
       
   213 	
   141 	# Assume that the rest don't depend on anything, and leave them out.
   214 	# Assume that the rest don't depend on anything, and leave them out.
       
   215 	}
       
   216 
       
   217 foreach my $romfile ( sort keys %outputlines)
       
   218 	{
       
   219 	print $outputlines{$romfile}, "\n";
   142 	}
   220 	}
   143 
   221 
   144 if ($inverted_table)
   222 if ($inverted_table)
   145 	{
   223 	{
   146 	print "\n";
   224 	print "\n";