1 //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/System/Program.h"
19 #include "llvm/System/TimeValue.h"
25 using namespace llvmc
;
28 int ExecuteProgram(const std::string
& name
,
29 const StrVector
& args
) {
30 sys::Path prog
= sys::Program::FindProgramByName(name
);
33 throw std::runtime_error("Can't find program '" + name
+ "'");
34 if (!prog
.canExecute())
35 throw std::runtime_error("Program '" + name
+ "' is not executable.");
37 // Build the command line vector and the redirects array.
38 const sys::Path
* redirects
[3] = {0,0,0};
39 sys::Path stdout_redirect
;
41 std::vector
<const char*> argv
;
42 argv
.reserve((args
.size()+2));
43 argv
.push_back(name
.c_str());
45 for (StrVector::const_iterator B
= args
.begin(), E
= args
.end();
49 stdout_redirect
.set(*B
);
50 redirects
[1] = &stdout_redirect
;
53 argv
.push_back((*B
).c_str());
56 argv
.push_back(0); // null terminate list.
58 // Invoke the program.
59 return sys::Program::ExecuteAndWait(prog
, &argv
[0], 0, &redirects
[0]);
62 void print_string (const std::string
& str
) {
68 void AppendToGlobalTimeLog(const std::string
& cmd
, double time
);
71 int llvmc::Action::Execute() const {
72 if (DryRun
|| VerboseMode
) {
73 errs() << Command_
<< " ";
74 std::for_each(Args_
.begin(), Args_
.end(), print_string
);
79 sys::TimeValue now
= sys::TimeValue::now();
80 int ret
= ExecuteProgram(Command_
, Args_
);
81 sys::TimeValue now2
= sys::TimeValue::now();
83 double elapsed
= now2
.seconds() + now2
.microseconds() / 1000000.0;
84 AppendToGlobalTimeLog(Command_
, elapsed
);
89 return ExecuteProgram(Command_
, Args_
);