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;
37 void PrintString (const std::string& str) {
41 void PrintCommand (const std::string& Cmd, const StrVector& Args) {
43 std::for_each(Args.begin(), Args.end(), &PrintString);
47 bool IsSegmentationFault (int returnCode) {
49 return (returnCode >= 0xc0000000UL)
51 return (returnCode < 0);
55 int ExecuteProgram (const std::string& name,
56 const StrVector& args) {
57 sys::Path prog = sys::Program::FindProgramByName(name);
60 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
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();
79 stdout_redirect.set(*B);
80 redirects[1] = &stdout_redirect;
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);
101 void AppendToGlobalTimeLog (const std::string& cmd, double time);
104 int llvmc::Action::Execute () const {
105 if (DryRun || VerboseMode)
106 PrintCommand(Command_, Args_);
110 sys::TimeValue now = sys::TimeValue::now();
111 int ret = ExecuteProgram(Command_, Args_);
112 sys::TimeValue now2 = sys::TimeValue::now();
114 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
115 AppendToGlobalTimeLog(Command_, elapsed);
120 return ExecuteProgram(Command_, Args_);