MC: Eliminate an unnecessary copy.
[llvm.git] / lib / CompilerDriver / Action.cpp
blob5f30dce5855fd42293b3b58663bce7c249809865
1 //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Action class - implementation and auxiliary functions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/Action.h"
15 #include "llvm/CompilerDriver/BuiltinOptions.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/SystemUtils.h"
19 #include "llvm/System/Program.h"
20 #include "llvm/System/TimeValue.h"
22 #include <stdexcept>
23 #include <string>
25 using namespace llvm;
26 using namespace llvmc;
28 namespace llvmc {
30 extern int Main(int argc, char** argv);
31 extern const char* ProgramName;
35 namespace {
37 void PrintString (const std::string& str) {
38 errs() << str << ' ';
41 void PrintCommand (const std::string& Cmd, const StrVector& Args) {
42 errs() << Cmd << ' ';
43 std::for_each(Args.begin(), Args.end(), &PrintString);
44 errs() << '\n';
47 bool IsSegmentationFault (int returnCode) {
48 #ifdef LLVM_ON_WIN32
49 return (returnCode >= 0xc0000000UL)
50 #else
51 return (returnCode < 0);
52 #endif
55 int ExecuteProgram (const std::string& name,
56 const StrVector& args) {
57 sys::Path prog = sys::Program::FindProgramByName(name);
59 if (prog.isEmpty()) {
60 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
61 if (prog.isEmpty())
62 throw std::runtime_error("Can't find program '" + name + "'");
64 if (!prog.canExecute())
65 throw std::runtime_error("Program '" + name + "' is not executable.");
67 // Build the command line vector and the redirects array.
68 const sys::Path* redirects[3] = {0,0,0};
69 sys::Path stdout_redirect;
71 std::vector<const char*> argv;
72 argv.reserve((args.size()+2));
73 argv.push_back(name.c_str());
75 for (StrVector::const_iterator B = args.begin(), E = args.end();
76 B!=E; ++B) {
77 if (*B == ">") {
78 ++B;
79 stdout_redirect.set(*B);
80 redirects[1] = &stdout_redirect;
82 else {
83 argv.push_back((*B).c_str());
86 argv.push_back(0); // null terminate list.
88 // Invoke the program.
89 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
91 if (IsSegmentationFault(ret)) {
92 errs() << "Segmentation fault: ";
93 PrintCommand(name, args);
96 return ret;
100 namespace llvmc {
101 void AppendToGlobalTimeLog (const std::string& cmd, double time);
104 int llvmc::Action::Execute () const {
105 if (DryRun || VerboseMode)
106 PrintCommand(Command_, Args_);
108 if (!DryRun) {
109 if (Time) {
110 sys::TimeValue now = sys::TimeValue::now();
111 int ret = ExecuteProgram(Command_, Args_);
112 sys::TimeValue now2 = sys::TimeValue::now();
113 now2 -= now;
114 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
115 AppendToGlobalTimeLog(Command_, elapsed);
117 return ret;
119 else {
120 return ExecuteProgram(Command_, Args_);
124 return 0;