| 1 |  | 
|---|
| 2 | @libraries = (); | 
|---|
| 3 | @includes = (); | 
|---|
| 4 | %defines = (); | 
|---|
| 5 | %read_files = (); | 
|---|
| 6 |  | 
|---|
| 7 | $debug = 0; | 
|---|
| 8 |  | 
|---|
| 9 | $includes[++$#includes] = "."; | 
|---|
| 10 | $filename = ""; | 
|---|
| 11 |  | 
|---|
| 12 | foreach $arg (@ARGV) { | 
|---|
| 13 | if ($arg =~ /^-d$/) { | 
|---|
| 14 | $debug = 1; | 
|---|
| 15 | } | 
|---|
| 16 | elsif ($arg =~ /^-D(.*)$/) { | 
|---|
| 17 | local($def) = $1; | 
|---|
| 18 | local($symbol) = $1; | 
|---|
| 19 | $def =~ s/^.*=//; | 
|---|
| 20 | $symbol =~ s/=.*$//; | 
|---|
| 21 | $defines{$symbol} = $def; | 
|---|
| 22 | } | 
|---|
| 23 | elsif ($arg =~ /^-I(.*)$/) { | 
|---|
| 24 | $includes[++$#includes] = $1; | 
|---|
| 25 | } | 
|---|
| 26 | else { | 
|---|
| 27 | $filename = $arg; | 
|---|
| 28 | } | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | if ($filename eq "") { | 
|---|
| 32 | print STDERR "listlibs.pl: require a filename\n"; | 
|---|
| 33 | exit 1; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | if (-d "$filename") { | 
|---|
| 37 | %current_includes = (); | 
|---|
| 38 | @libraries = (); | 
|---|
| 39 | %known_libs = (); | 
|---|
| 40 | %known_includes = (); | 
|---|
| 41 | &process_directory($filename); | 
|---|
| 42 | } | 
|---|
| 43 | else { | 
|---|
| 44 | &process_file($filename); | 
|---|
| 45 | %current_includes = (); | 
|---|
| 46 | @libraries = (); | 
|---|
| 47 | %known_libs = (); | 
|---|
| 48 | %known_includes = (); | 
|---|
| 49 | &find_libraries($filename); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | @libraries = reverse(@libraries); | 
|---|
| 53 |  | 
|---|
| 54 | print "got $#libraries of them\n" if ($debug); | 
|---|
| 55 |  | 
|---|
| 56 | &substitute_defines(); | 
|---|
| 57 |  | 
|---|
| 58 | foreach $i (0..$#libraries) { | 
|---|
| 59 | printf "%s", $libraries[$i]; | 
|---|
| 60 | if ($i < $#libraries) { printf " "; } | 
|---|
| 61 | } | 
|---|
| 62 | printf "\n"; | 
|---|
| 63 |  | 
|---|
| 64 | ########################################################################### | 
|---|
| 65 |  | 
|---|
| 66 | sub process_file { | 
|---|
| 67 | local($filename) = shift; | 
|---|
| 68 | if ($debug) { | 
|---|
| 69 | printf "process_file: filename: %s\n", $filename; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | # find the file | 
|---|
| 73 | local($ifile) = ""; | 
|---|
| 74 | if ($filename =~ /^\//) { | 
|---|
| 75 | $ifile = $filename; | 
|---|
| 76 | } | 
|---|
| 77 | else { | 
|---|
| 78 | foreach $include (@includes) { | 
|---|
| 79 | $ifile = "$include/$filename"; | 
|---|
| 80 | if ($debug) { | 
|---|
| 81 | #printf "process_file: looking for: %s\n", $ifile; | 
|---|
| 82 | } | 
|---|
| 83 | if (-f $ifile) { last; } | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | if ($ifile eq "" || ! -f $ifile) { | 
|---|
| 87 | print STDERR "listlibs.pl: couldn't find file $file\n"; | 
|---|
| 88 | exit 1; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | # read the file | 
|---|
| 92 | local($filecontents) = ""; | 
|---|
| 93 | open(IFILE,"<$ifile"); | 
|---|
| 94 | while (<IFILE>) { | 
|---|
| 95 | if (/^\s*$/) { next; } | 
|---|
| 96 | $filecontents = "$filecontents$_"; | 
|---|
| 97 | } | 
|---|
| 98 | close(IFILE); | 
|---|
| 99 | $read_files{$filename} = $filecontents; | 
|---|
| 100 | # an empty file will look like a new file below so put in a newline | 
|---|
| 101 | if ($read_files{$filename} eq "") { | 
|---|
| 102 | $read_files{$filename} = "\n" | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | # read in other files referenced by this file | 
|---|
| 106 | foreach $line (&get_lines($filecontents)) { | 
|---|
| 107 | if ($line =~ /^\#\s*include\s*<(.+)>/) { | 
|---|
| 108 | local($newfile) = $1; | 
|---|
| 109 | if ($read_files{$newfile} eq "") { | 
|---|
| 110 | &process_file($newfile); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | sub get_lines { | 
|---|
| 117 | local($filecontents) = shift; | 
|---|
| 118 | local(@lines) = (); | 
|---|
| 119 | local($ifdepth) = 0; | 
|---|
| 120 | while ($filecontents ne "") { | 
|---|
| 121 | # get next line | 
|---|
| 122 | $filecontents =~ s/^(.*)\n//; | 
|---|
| 123 | local($line) = $1; | 
|---|
| 124 | # remove comments | 
|---|
| 125 | $line =~ s/\/\/.*$//; | 
|---|
| 126 | # remove leading trailing whitespace | 
|---|
| 127 | $line =~ s/^\s*//; | 
|---|
| 128 | $line =~ s/\s*$//; | 
|---|
| 129 | # this only handles ifdef's that are one level deep | 
|---|
| 130 | if ($line =~ /\#\s*ifdef\s+([a-zA-Z_]\w*)/) { | 
|---|
| 131 | local($symbol) = $1; | 
|---|
| 132 | if (! defined $defines{$symbol}) { | 
|---|
| 133 | while ($filecontents ne "") { | 
|---|
| 134 | $filecontents =~ s/^(.*)\n//; | 
|---|
| 135 | local($tline) = $1; | 
|---|
| 136 | last if ($tline =~ /\#\s*endif/); | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 | elsif ($line =~ /\#\s*endif/) { | 
|---|
| 141 | # eat this endif | 
|---|
| 142 | } | 
|---|
| 143 | else { | 
|---|
| 144 | $lines[++$#lines] = $line if ($line ne ""); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | return @lines; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | sub find_libraries { | 
|---|
| 151 | local($filename) = shift; | 
|---|
| 152 | if ($current_includes{$filename} == 1) { | 
|---|
| 153 | print STDERR "listlibs.pl: recursive include detected for $filename\n"; | 
|---|
| 154 | exit 1; | 
|---|
| 155 | } | 
|---|
| 156 | $current_includes{$filename} = 1; | 
|---|
| 157 | foreach $line (reverse(&get_lines($read_files{$filename}))) { | 
|---|
| 158 | if ($line =~ /^\#\s*include\s*<(.+)>/) { | 
|---|
| 159 | local($newfile) = $1; | 
|---|
| 160 | if ($known_includes{$newfile} != 1) { | 
|---|
| 161 | $known_includes{$newfile} = 1; | 
|---|
| 162 | &find_libraries($newfile); | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 | elsif ($known_libs{$line} != 1) { | 
|---|
| 166 | $known_libs{$line} = 1; | 
|---|
| 167 | $libraries[++$#libraries] = $line; | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 | delete $current_includes{$filename}; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | sub substitute_defines { | 
|---|
| 174 | local($i); | 
|---|
| 175 | local($symbol); | 
|---|
| 176 | foreach $i (0..$#libraries) { | 
|---|
| 177 | foreach $symbol (keys(%defines)) { | 
|---|
| 178 | $libraries[$i] =~ s/$symbol/$defines{$symbol}/g; | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | sub process_directory { | 
|---|
| 184 | local ($dir) = shift; | 
|---|
| 185 | opendir(DIR,"$dir"); | 
|---|
| 186 | local (@files) = readdir(DIR); | 
|---|
| 187 | closedir(DIR); | 
|---|
| 188 | local ($i); | 
|---|
| 189 | foreach $i (@files) { | 
|---|
| 190 | if ("$i" eq "." || "$i" eq "..") { | 
|---|
| 191 | # skip | 
|---|
| 192 | } | 
|---|
| 193 | elsif (-d "$dir/$i") { | 
|---|
| 194 | process_directory("$dir/$i"); | 
|---|
| 195 | } | 
|---|
| 196 | elsif ("$i" eq "LIBS.h") { | 
|---|
| 197 | process_file("$dir/$i"); | 
|---|
| 198 | &find_libraries("$dir/$i"); | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 | } | 
|---|