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 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= - directory to extract to --plugin= - 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 {