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/Support/SystemUtils.h"
19 #include "llvm/System/Program.h"
20 #include "llvm/System/TimeValue.h"
26 using namespace llvmc;
30 extern int Main(int argc, char** argv);
31 extern const char* ProgramName;
36 int ExecuteProgram(const std::string& name,
37 const StrVector& args) {
38 sys::Path prog = sys::Program::FindProgramByName(name);
41 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
43 throw std::runtime_error("Can't find program '" + name + "'");
45 if (!prog.canExecute())
46 throw std::runtime_error("Program '" + name + "' is not executable.");
48 // Build the command line vector and the redirects array.
49 const sys::Path* redirects[3] = {0,0,0};
50 sys::Path stdout_redirect;
52 std::vector<const char*> argv;
53 argv.reserve((args.size()+2));
54 argv.push_back(name.c_str());
56 for (StrVector::const_iterator B = args.begin(), E = args.end();
60 stdout_redirect.set(*B);
61 redirects[1] = &stdout_redirect;
64 argv.push_back((*B).c_str());
67 argv.push_back(0); // null terminate list.
69 // Invoke the program.
70 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
73 const char** B = &argv[0];
75 errs() << "Segmentation fault:";
77 errs() << ' ' << *(B++);
86 void print_string (const std::string& str) {
92 void AppendToGlobalTimeLog(const std::string& cmd, double time);
95 int llvmc::Action::Execute() const {
96 if (DryRun || VerboseMode) {
97 errs() << Command_ << " ";
98 std::for_each(Args_.begin(), Args_.end(), print_string);
103 sys::TimeValue now = sys::TimeValue::now();
104 int ret = ExecuteProgram(Command_, Args_);
105 sys::TimeValue now2 = sys::TimeValue::now();
107 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
108 AppendToGlobalTimeLog(Command_, elapsed);
113 return ExecuteProgram(Command_, Args_);