[lld] r239238 - COFF: Fix default output file path.

Rui Ueyama ruiu at google.com
Sat Jun 6 17:20:32 PDT 2015


Author: ruiu
Date: Sat Jun  6 19:20:32 2015
New Revision: 239238

URL: http://llvm.org/viewvc/llvm-project?rev=239238&view=rev
Log:
COFF: Fix default output file path.

Default output filename is the same as the first object file's
name with its extension replaced with ".exe".

Added:
    lld/trunk/test/COFF/out.test
Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=239238&r1=239237&r2=239238&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Sat Jun  6 19:20:32 2015
@@ -29,6 +29,7 @@ public:
   bool Verbose = false;
   WindowsSubsystem Subsystem = llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
   StringRef EntryName;
+  std::string OutputFile;
 
   // Symbols in this set are considered as live by the garbage collector.
   std::set<StringRef> GCRoots;

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=239238&r1=239237&r2=239238&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Sat Jun  6 19:20:32 2015
@@ -49,17 +49,11 @@ bool link(int Argc, const char *Argv[])
   return Driver->link(Argc, Argv);
 }
 
-static std::string getOutputPath(llvm::opt::InputArgList *Args) {
-  if (auto *Arg = Args->getLastArg(OPT_out))
-    return Arg->getValue();
-  for (auto *Arg : Args->filtered(OPT_INPUT)) {
-    if (!StringRef(Arg->getValue()).endswith_lower(".obj"))
-      continue;
-    SmallString<128> Val = StringRef(Arg->getValue());
-    llvm::sys::path::replace_extension(Val, ".exe");
-    return Val.str();
-  }
-  llvm_unreachable("internal error");
+// Drop directory components and replace extension with ".exe".
+static std::string getOutputPath(StringRef Path) {
+  auto P = Path.find_last_of("\\/");
+  StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
+  return (S.substr(0, S.rfind('.')) + ".exe").str();
 }
 
 // Opens a file. Path has to be resolved already.
@@ -78,6 +72,8 @@ ErrorOr<std::unique_ptr<InputFile>> Link
     return std::unique_ptr<InputFile>(new ArchiveFile(MBRef));
   if (Magic == file_magic::bitcode)
     return std::unique_ptr<InputFile>(new BitcodeFile(MBRef));
+  if (Config->OutputFile == "")
+    Config->OutputFile = getOutputPath(Path);
   return std::unique_ptr<InputFile>(new ObjectFile(MBRef));
 }
 
@@ -226,6 +222,10 @@ bool LinkerDriver::link(int Argc, const
     return false;
   }
 
+  // Handle /out
+  if (auto *Arg = Args->getLastArg(OPT_out))
+    Config->OutputFile = Arg->getValue();
+
   // Handle /verbose
   if (Args->hasArg(OPT_verbose))
     Config->Verbose = true;
@@ -412,7 +412,7 @@ bool LinkerDriver::link(int Argc, const
 
   // Write the result.
   Writer Out(&Symtab);
-  if (auto EC = Out.write(getOutputPath(Args.get()))) {
+  if (auto EC = Out.write(Config->OutputFile)) {
     llvm::errs() << EC.message() << "\n";
     return false;
   }

Added: lld/trunk/test/COFF/out.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/out.test?rev=239238&view=auto
==============================================================================
--- lld/trunk/test/COFF/out.test (added)
+++ lld/trunk/test/COFF/out.test Sat Jun  6 19:20:32 2015
@@ -0,0 +1,16 @@
+# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.obj
+
+# RUN: mkdir -p %T/out/tmp
+# RUN: cp %t.obj %T/out/out1.obj
+# RUN: cp %t.obj %T/out/tmp/out2
+# RUN: cp %t.obj %T/out/tmp/out3.xyz
+
+# RUN: lld -flavor link2 %T/out/out1.obj
+# RUN: lld -flavor link2 %T/out/tmp/out2
+# RUN: lld -flavor link2 %T/out/tmp/out3.xyz
+
+# RUN: llvm-readobj out1.exe | FileCheck %s
+# RUN: llvm-readobj out2.exe | FileCheck %s
+# RUN: llvm-readobj out3.exe | FileCheck %s
+
+CHECK: File:





More information about the llvm-commits mailing list