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
|
@libraries = ();
@includes = ();
%defines = ();
%read_files = ();
$debug = 0;
$includes[++$#includes] = ".";
$filename = "";
foreach $arg (@ARGV) {
if ($arg =~ /^-d$/) {
$debug = 1;
}
elsif ($arg =~ /^-D(.*)$/) {
local($def) = $1;
local($symbol) = $1;
$def =~ s/^.*=//;
$symbol =~ s/=.*$//;
$defines{$symbol} = $def;
}
elsif ($arg =~ /^-I(.*)$/) {
$includes[++$#includes] = $1;
}
else {
$filename = $arg;
}
}
if ($filename eq "") {
print STDERR "listlibs.pl: require a filename\n";
exit 1;
}
if (-d "$filename") {
%current_includes = ();
@libraries = ();
%known_libs = ();
%known_includes = ();
&process_directory($filename);
}
else {
&process_file($filename);
%current_includes = ();
@libraries = ();
%known_libs = ();
%known_includes = ();
&find_libraries($filename);
}
@libraries = reverse(@libraries);
print "got $#libraries of them\n" if ($debug);
&substitute_defines();
foreach $i (0..$#libraries) {
printf "%s", $libraries[$i];
if ($i < $#libraries) { printf " "; }
}
printf "\n";
###########################################################################
sub process_file {
local($filename) = shift;
if ($debug) {
printf "process_file: filename: %s\n", $filename;
}
# find the file
local($ifile) = "";
if ($filename =~ /^\//) {
$ifile = $filename;
}
else {
foreach $include (@includes) {
$ifile = "$include/$filename";
if ($debug) {
#printf "process_file: looking for: %s\n", $ifile;
}
if (-f $ifile) { last; }
}
}
if ($ifile eq "" || ! -f $ifile) {
print STDERR "listlibs.pl: couldn't find file $file\n";
exit 1;
}
# read the file
local($filecontents) = "";
open(IFILE,"<$ifile");
while (<IFILE>) {
if (/^\s*$/) { next; }
$filecontents = "$filecontents$_";
}
close(IFILE);
$read_files{$filename} = $filecontents;
# an empty file will look like a new file below so put in a newline
if ($read_files{$filename} eq "") {
$read_files{$filename} = "\n"
}
# read in other files referenced by this file
foreach $line (&get_lines($filecontents)) {
if ($line =~ /^\#\s*include\s*<(.+)>/) {
local($newfile) = $1;
if ($read_files{$newfile} eq "") {
&process_file($newfile);
}
}
}
}
sub get_lines {
local($filecontents) = shift;
local(@lines) = ();
local($ifdepth) = 0;
while ($filecontents ne "") {
# get next line
$filecontents =~ s/^(.*)\n//;
local($line) = $1;
# remove comments
$line =~ s/\/\/.*$//;
# remove leading trailing whitespace
$line =~ s/^\s*//;
$line =~ s/\s*$//;
# this only handles ifdef's that are one level deep
if ($line =~ /\#\s*ifdef\s+([a-zA-Z_]\w*)/) {
local($symbol) = $1;
if (! defined $defines{$symbol}) {
while ($filecontents ne "") {
$filecontents =~ s/^(.*)\n//;
local($tline) = $1;
last if ($tline =~ /\#\s*endif/);
}
}
}
elsif ($line =~ /\#\s*endif/) {
# eat this endif
}
else {
$lines[++$#lines] = $line if ($line ne "");
}
}
return @lines;
}
sub find_libraries {
local($filename) = shift;
if ($current_includes{$filename} == 1) {
print STDERR "listlibs.pl: recursive include detected for $filename\n";
exit 1;
}
$current_includes{$filename} = 1;
foreach $line (reverse(&get_lines($read_files{$filename}))) {
if ($line =~ /^\#\s*include\s*<(.+)>/) {
local($newfile) = $1;
if ($known_includes{$newfile} != 1) {
$known_includes{$newfile} = 1;
&find_libraries($newfile);
}
}
elsif ($known_libs{$line} != 1) {
$known_libs{$line} = 1;
$libraries[++$#libraries] = $line;
}
}
delete $current_includes{$filename};
}
sub substitute_defines {
local($i);
local($symbol);
foreach $i (0..$#libraries) {
foreach $symbol (keys(%defines)) {
$libraries[$i] =~ s/$symbol/$defines{$symbol}/g;
}
}
}
sub process_directory {
local ($dir) = shift;
opendir(DIR,"$dir");
local (@files) = readdir(DIR);
closedir(DIR);
local ($i);
foreach $i (@files) {
if ("$i" eq "." || "$i" eq "..") {
# skip
}
elsif (-d "$dir/$i") {
process_directory("$dir/$i");
}
elsif ("$i" eq "LIBS.h") {
process_file("$dir/$i");
&find_libraries("$dir/$i");
}
}
}
|