Use assertions to check for conditions that should never happen.
[oota-llvm.git] / utils / GenLibDeps.pl
index 16aed89474895983f348a5b4d80cf4f3eab53165..e501774251887a15b5f39c452c6418dfe11e5c48 100755 (executable)
@@ -48,7 +48,7 @@ if (!defined($nmPath) || $nmPath eq "") {
 opendir DIR,$Directory;
 my @files = readdir DIR;
 closedir DIR;
-my @libs = grep(/libLLVM.*\.a$/,sort(@files));
+my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
 my @objs = grep(/LLVM.*\.o$/,sort(@files));
 
 # Declare the hashes we will use to keep track of the library and object file
@@ -62,7 +62,8 @@ foreach my $lib (@libs ) {
   while (<DEFS>) {
     next if (! / [ABCDGRST] /);
     s/^[^ ]* [ABCDGRST] //;    
-    chomp($_);
+    s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
+                   # this strips both LF and CRLF.
     $libdefs{$_} = $lib;
   }
   close DEFS;
@@ -74,7 +75,8 @@ foreach my $obj (@objs ) {
   while (<DEFS>) {
     next if (! / [ABCDGRST] /);
     s/^[^ ]* [ABCDGRST] //;
-    chomp($_);
+    s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
+                   # this strips both LF and CRLF.    
     $objdefs{$_} = $obj;
   }
   close DEFS;
@@ -94,7 +96,7 @@ sub gen_one_entry {
     print "  <dt><b>$lib</b</dt><dd><ul>\n";
   }
   open UNDEFS, 
-    "$nmPath -g -u $Directory/$lib | sed -e 's/^  *U //' | sort | uniq |";
+    "$nmPath -g -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
   my %DepLibs;
   while (<UNDEFS>) {
     chomp;
@@ -112,6 +114,32 @@ sub gen_one_entry {
     }
   }
   close UNDEFS;
+  unless(keys %DepLibs) {
+    # above failed
+    open UNDEFS, "$nmPath -g -u $Directory/$lib |";
+    while (<UNDEFS>) {
+      # to bypass non-working sed
+      if ('  ' eq substr($_,0,2) and index($_,'U ')) {
+        $_ = substr($_,index($_,'U ')+2)
+      };
+      $_ = substr($_,index($_,'  *U ')+5) if -1!=index($_,'  *U ');
+
+      chomp;
+      my $lib_printed = 0;
+      if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
+        $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
+        push(@{$DepLibs{$libdefs{$_}}}, $_);
+      } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
+        my $libroot = $lib;
+        $libroot =~ s/lib(.*).a/$1/;
+        if ($objdefs{$_} ne "$libroot.o") {
+          $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
+          push(@{$DepLibs{$objdefs{$_}}}, $_);
+        }
+      }
+    }
+  }
+
   for my $key (sort keys %DepLibs) {
     if ($FLAT) {
       print " $key";