I swear I did a make clean and make before committing all this...
[oota-llvm.git] / tools / gold / gold-plugin.cpp
index e7161a64a8e48feb0f7744e568ba5dff46de51ad..6e608631b6e6c12b0b341093a498a7538079cb93 100644 (file)
 
 #include "llvm-c/lto.h"
 
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Errno.h"
-#include "llvm/System/Path.h"
-#include "llvm/System/Program.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/Errno.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
 
 #include <cerrno>
 #include <cstdlib>
@@ -66,9 +66,11 @@ namespace options {
   static generate_bc generate_bc_file = BC_NO;
   static std::string bc_path;
   static std::string as_path;
+  static std::vector<std::string> as_args;
   static std::vector<std::string> pass_through;
   static std::string extra_library_path;
   static std::string triple;
+  static std::string mcpu;
   // Additional options to pass into the code generator.
   // Note: This array will contain all plugin options which are not claimed
   // as plugin exclusive to pass to the code generator.
@@ -84,6 +86,8 @@ namespace options {
 
     if (opt == "generate-api-file") {
       generate_api_file = true;
+    } else if (opt.startswith("mcpu=")) {
+      mcpu = opt.substr(strlen("mcpu="));
     } else if (opt.startswith("as=")) {
       if (!as_path.empty()) {
         (*message)(LDPL_WARNING, "Path to as specified twice. "
@@ -91,12 +95,15 @@ namespace options {
       } else {
         as_path = opt.substr(strlen("as="));
       }
+    } else if (opt.startswith("as-arg=")) {
+      llvm::StringRef item = opt.substr(strlen("as-arg="));
+      as_args.push_back(item.str());
     } else if (opt.startswith("extra-library-path=")) {
       extra_library_path = opt.substr(strlen("extra_library_path="));
     } else if (opt.startswith("pass-through=")) {
       llvm::StringRef item = opt.substr(strlen("pass-through="));
       pass_through.push_back(item.str());
-    } else if (opt == "mtriple=") {
+    } else if (opt.startswith("mtriple=")) {
       triple = opt.substr(strlen("mtriple="));
     } else if (opt == "emit-llvm") {
       generate_bc_file = BC_ONLY;
@@ -401,6 +408,17 @@ static ld_plugin_status all_symbols_read_hook(void) {
     sys::Path p = sys::Program::FindProgramByName(options::as_path);
     lto_codegen_set_assembler_path(cg, p.c_str());
   }
+  if (!options::as_args.empty()) {
+    std::vector<const char *> as_args_p;
+    for (std::vector<std::string>::iterator I = options::as_args.begin(),
+           E = options::as_args.end(); I != E; ++I) {
+      as_args_p.push_back(I->c_str());
+    }
+    lto_codegen_set_assembler_args(cg, &as_args_p[0], as_args_p.size());
+  }
+  if (!options::mcpu.empty())
+    lto_codegen_set_cpu(cg, options::mcpu.c_str());
+
   // Pass through extra options to the code generator.
   if (!options::extra.empty()) {
     for (std::vector<std::string>::iterator it = options::extra.begin();
@@ -435,15 +453,22 @@ static ld_plugin_status all_symbols_read_hook(void) {
     (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
     return LDPS_ERR;
   }
-  raw_fd_ostream objFile(uniqueObjPath.c_str(), ErrMsg,
-                         raw_fd_ostream::F_Binary);
+  tool_output_file objFile(uniqueObjPath.c_str(), ErrMsg,
+                           raw_fd_ostream::F_Binary);
   if (!ErrMsg.empty()) {
     (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
     return LDPS_ERR;
   }
 
-  objFile.write(buffer, bufsize);
-  objFile.close();
+  objFile.os().write(buffer, bufsize);
+  objFile.os().close();
+  if (objFile.os().has_error()) {
+    (*message)(LDPL_ERROR, "Error writing output file '%s'",
+               uniqueObjPath.c_str());
+    objFile.os().clear_error();
+    return LDPS_ERR;
+  }
+  objFile.keep();
 
   lto_codegen_dispose(cg);