[PATCH] D69418: [llvm-ar] Add output option for extract operation

Yi Kong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 26 02:13:04 PDT 2019


kongyi updated this revision to Diff 226536.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69418/new/

https://reviews.llvm.org/D69418

Files:
  llvm/test/tools/llvm-ar/extract.test
  llvm/tools/llvm-ar/llvm-ar.cpp


Index: llvm/tools/llvm-ar/llvm-ar.cpp
===================================================================
--- llvm/tools/llvm-ar/llvm-ar.cpp
+++ llvm/tools/llvm-ar/llvm-ar.cpp
@@ -80,6 +80,7 @@
     =gnu                -   gnu
     =darwin             -   darwin
     =bsd                -   bsd
+  --output=<string>     - directory to extract to
   --plugin=<string>     - ignored for compatibility
   -h --help             - display this help and exit
   --version             - print the version and exit
@@ -171,6 +172,7 @@
 }
 
 static Format FormatType = Default;
+static std::string outputPath;
 
 static std::string Options;
 
@@ -425,6 +427,8 @@
            "operations");
   if (OriginalDates && Operation != Extract)
     fail("the 'o' modifier is only applicable to the 'x' operation");
+  if (!outputPath.empty() && Operation != Extract)
+    fail("the 'output' option is only applicable to the 'x' operation");
   if (OnlyUpdate && Operation != ReplaceOrInsert)
     fail("the 'u' modifier is only applicable to the 'r' operation");
   if (AddLibrary && Operation != QuickAppend)
@@ -530,8 +534,17 @@
   failIfError(ModeOrErr.takeError());
   sys::fs::perms Mode = ModeOrErr.get();
 
+  llvm::StringRef outputFilePath;
+  llvm::SmallString<128> path;
+  if (!outputPath.empty()) {
+    sys::path::append(path, outputPath, sys::path::filename(Name));
+    outputFilePath = path.str();
+  } else {
+    outputFilePath = sys::path::filename(Name);
+  }
+
   int FD;
-  failIfError(sys::fs::openFileForWrite(sys::path::filename(Name), FD,
+  failIfError(sys::fs::openFileForWrite(outputFilePath, FD,
                                         sys::fs::CD_CreateAlways,
                                         sys::fs::OF_None, Mode),
               Name);
@@ -1065,9 +1078,9 @@
       fail("unknown command: " + CommandStr);
     }
   }
-  
+
   ParsingMRIScript = false;
-  
+
   // Nothing to do if not saved.
   if (Saved)
     performOperation(ReplaceOrInsert, &NewMembers);
@@ -1130,6 +1143,8 @@
                          .Default(Unknown);
         if (FormatType == Unknown)
           fail(std::string("Invalid format ") + match);
+      } else if (MatchFlagWithArg("output")) {
+        outputPath = match;
       } else if (MatchFlagWithArg("plugin")) {
         // Ignored.
       } else {
Index: llvm/test/tools/llvm-ar/extract.test
===================================================================
--- llvm/test/tools/llvm-ar/extract.test
+++ llvm/test/tools/llvm-ar/extract.test
@@ -13,10 +13,16 @@
 
 # Single member:
 RUN: cd %t/extracted && llvm-ar x %t/archive.a a.txt
-RUN: diff %t/a.txt %t/extracted/a.txt 
+RUN: diff %t/a.txt %t/extracted/a.txt
 
-# All members:
-RUN: rm %t/extracted/a.txt
+# All members, extracting to the current directory:
+RUN: rm -f %t/extracted/a.txt
 RUN: cd %t/extracted && llvm-ar x %t/archive.a
-RUN: diff %t/a.txt %t/extracted/a.txt 
-RUN: diff %t/b.txt %t/extracted/b.txt 
+RUN: diff %t/a.txt %t/extracted/a.txt
+RUN: diff %t/b.txt %t/extracted/b.txt
+
+# All members, output directory specified:
+RUN: rm -f %t/extracted/a.txt %t/extracted/b.txt
+RUN: llvm-ar --output=%t/extracted x %t/archive.a
+RUN: diff %t/a.txt %t/extracted/a.txt
+RUN: diff %t/b.txt %t/extracted/b.txt


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69418.226536.patch
Type: text/x-patch
Size: 3238 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191026/886f4dfc/attachment.bin>


More information about the llvm-commits mailing list