Support names like llvm-ar-3.4 and llvm-ranlib-3.4.
[oota-llvm.git] / tools / llvm-ar / llvm-ar.cpp
index 261446cc7826c868b85101e8de296949fd71b000..64ef3fa1bb9b35fe0296c6d64440c78ce435f89e 100644 (file)
@@ -63,20 +63,13 @@ static void failIfError(error_code EC, Twine Context = "") {
   fail(Context + ": " + EC.message());
 }
 
-// Option for compatibility with AIX, not used but must allow it to be present.
-static cl::opt<bool>
-X32Option ("X32_64", cl::Hidden,
-            cl::desc("Ignored option for compatibility with AIX"));
-
-// llvm-ar operation code and modifier flags. This must come first.
-static cl::opt<std::string>
-Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
-
-// llvm-ar remaining positional arguments.
+// llvm-ar/llvm-ranlib remaining positional arguments.
 static cl::list<std::string>
 RestOfArgs(cl::Positional, cl::OneOrMore,
     cl::desc("[relpos] [count] <archive-file> [members]..."));
 
+std::string Options;
+
 // MoreHelp - Provide additional help output explaining the operations and
 // modifiers of llvm-ar. This object instructs the CommandLine library
 // to print the text of the constructor when the --help option is given.
@@ -112,7 +105,8 @@ enum ArchiveOperation {
   QuickAppend,      ///< Quickly append to end of archive
   ReplaceOrInsert,  ///< Replace or Insert members
   DisplayTable,     ///< Display the table of contents
-  Extract           ///< Extract files back to file system
+  Extract,          ///< Extract files back to file system
+  CreateSymTab      ///< Create a symbol table in an existing archive
 };
 
 // Modifiers to follow operation to vary behavior
@@ -155,6 +149,13 @@ static void getRelPos() {
   RestOfArgs.erase(RestOfArgs.begin());
 }
 
+static void getOptions() {
+  if(RestOfArgs.size() == 0)
+    show_help("Expected options");
+  Options = RestOfArgs[0];
+  RestOfArgs.erase(RestOfArgs.begin());
+}
+
 // getArchive - Get the archive file name from the command line
 static void getArchive() {
   if(RestOfArgs.size() == 0)
@@ -174,6 +175,7 @@ static void getMembers() {
 // operation specified. Process all modifiers and check to make sure that
 // constraints on modifier/operation pairs have not been violated.
 static ArchiveOperation parseCommandLine() {
+  getOptions();
 
   // Keep track of number of operations. We can only specify one
   // per execution.
@@ -186,6 +188,8 @@ static ArchiveOperation parseCommandLine() {
   // Keep track of which operation was requested
   ArchiveOperation Operation;
 
+  bool MaybeJustCreateSymTab = false;
+
   for(unsigned i=0; i<Options.size(); ++i) {
     switch(Options[i]) {
     case 'd': ++NumOperations; Operation = Delete; break;
@@ -200,6 +204,7 @@ static ArchiveOperation parseCommandLine() {
     case 'o': OriginalDates = true; break;
     case 's':
       Symtab = true;
+      MaybeJustCreateSymTab = true;
       break;
     case 'S':
       Symtab = false;
@@ -233,6 +238,13 @@ static ArchiveOperation parseCommandLine() {
   // Everything on the command line at this point is a member.
   getMembers();
 
+ if (NumOperations == 0 && MaybeJustCreateSymTab) {
+    NumOperations = 1;
+    Operation = CreateSymTab;
+    if (!Members.empty())
+      show_help("The s operation takes only an archive as argument");
+  }
+
   // Perform various checks on the operation/modifier specification
   // to make sure we are dealing with a legal request.
   if (NumOperations == 0)
@@ -340,6 +352,7 @@ static bool shouldCreateArchive(ArchiveOperation Op) {
   case Move:
   case DisplayTable:
   case Extract:
+  case CreateSymTab:
     return false;
 
   case QuickAppend:
@@ -810,6 +823,19 @@ static void performWriteOperation(ArchiveOperation Operation,
   TemporaryOutput = NULL;
 }
 
+static void createSymbolTable(object::Archive *OldArchive) {
+  // When an archive is created or modified, if the s option is given, the
+  // resulting archive will have a current symbol table. If the S option
+  // is given, it will have no symbol table.
+  // In summary, we only need to update the symbol table if we have none.
+  // This is actually very common because of broken build systems that think
+  // they have to run ranlib.
+  if (OldArchive->hasSymbolTable())
+    return;
+
+  performWriteOperation(CreateSymTab, OldArchive);
+}
+
 static void performOperation(ArchiveOperation Operation,
                              object::Archive *OldArchive) {
   switch (Operation) {
@@ -825,10 +851,16 @@ static void performOperation(ArchiveOperation Operation,
   case ReplaceOrInsert:
     performWriteOperation(Operation, OldArchive);
     return;
+  case CreateSymTab:
+    createSymbolTable(OldArchive);
+    return;
   }
   llvm_unreachable("Unknown operation.");
 }
 
+static int ar_main(char **argv);
+static int ranlib_main();
+
 // main - main program for llvm-ar .. see comments in the code
 int main(int argc, char **argv) {
   ToolName = argv[0];
@@ -844,15 +876,36 @@ int main(int argc, char **argv) {
     "  This program archives bitcode files into single libraries\n"
   );
 
+  StringRef Stem = sys::path::stem(ToolName);
+  if (Stem.find("ar") != StringRef::npos)
+    return ar_main(argv);
+  if (Stem.find("ranlib") != StringRef::npos)
+    return ranlib_main();
+  fail("Not ranlib or ar!");
+}
+
+static int performOperation(ArchiveOperation Operation);
+
+int ranlib_main() {
+  if (RestOfArgs.size() != 1)
+    fail(ToolName + "takes just one archive as argument");
+  ArchiveName = RestOfArgs[0];
+  return performOperation(CreateSymTab);
+}
+
+int ar_main(char **argv) {
   // Do our own parsing of the command line because the CommandLine utility
   // can't handle the grouped positional parameters without a dash.
   ArchiveOperation Operation = parseCommandLine();
+  return performOperation(Operation);
+}
 
+static int performOperation(ArchiveOperation Operation) {
   // Create or open the archive object.
   OwningPtr<MemoryBuffer> Buf;
   error_code EC = MemoryBuffer::getFile(ArchiveName, Buf, -1, false);
   if (EC && EC != llvm::errc::no_such_file_or_directory) {
-    errs() << argv[0] << ": error opening '" << ArchiveName
+    errs() << ToolName << ": error opening '" << ArchiveName
            << "': " << EC.message() << "!\n";
     return 1;
   }
@@ -861,7 +914,7 @@ int main(int argc, char **argv) {
     object::Archive Archive(Buf.take(), EC);
 
     if (EC) {
-      errs() << argv[0] << ": error loading '" << ArchiveName
+      errs() << ToolName << ": error loading '" << ArchiveName
              << "': " << EC.message() << "!\n";
       return 1;
     }
@@ -876,7 +929,7 @@ int main(int argc, char **argv) {
   } else {
     if (!Create) {
       // Produce a warning if we should and we're creating the archive
-      errs() << argv[0] << ": creating " << ArchiveName << "\n";
+      errs() << ToolName << ": creating " << ArchiveName << "\n";
     }
   }