simplify some error recovery stuff.
[oota-llvm.git] / utils / GenLibDeps.pl
index 74eedd3383bcac469dd2252a04e9de6a9d073bc9..6d0b13ee4361eb5183bf417f5b03c1d50d5580c3 100755 (executable)
@@ -38,6 +38,10 @@ if (!$FLAT) {
   die "Can't find 'dot'" if (! -x "$DotPath");
 }
 
+if (defined($ENV{NM})) {
+  chomp($nmPath=$ENV{NM});
+}
+
 if (!defined($nmPath) || $nmPath eq "") {
   chomp($nmPath=`which nm`);
   die "Can't find 'nm'" if (! -x "$nmPath");
@@ -48,7 +52,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
@@ -66,7 +70,7 @@ foreach my $lib (@libs ) {
                    # this strips both LF and CRLF.
     $libdefs{$_} = $lib;
   }
-  close DEFS;
+  close DEFS or die "nm failed";
 }
 
 # Gather definitions from the object files.
@@ -79,7 +83,7 @@ foreach my $obj (@objs ) {
                    # this strips both LF and CRLF.    
     $objdefs{$_} = $obj;
   }
-  close DEFS;
+  close DEFS or die "nm failed";
 }
 
 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
@@ -96,7 +100,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 -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
   my %DepLibs;
   while (<UNDEFS>) {
     chomp;
@@ -113,7 +117,34 @@ sub gen_one_entry {
       }
     }
   }
-  close UNDEFS;
+  close UNDEFS or die "nm failed";
+  unless(keys %DepLibs) {
+    # above failed
+    open UNDEFS, "$nmPath -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{$_}}}, $_);
+        }
+      }
+    }
+    close UNDEFS or die "nm failed";
+  }
+
   for my $key (sort keys %DepLibs) {
     if ($FLAT) {
       print " $key";