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"
16 #include "llvm/CompilerDriver/Error.h"
17 #include "llvm/CompilerDriver/Main.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/SystemUtils.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/TimeValue.h"
28 using namespace llvmc;
32 extern const char* ProgramName;
38 void PrintString (const std::string& str) {
42 void PrintCommand (const std::string& Cmd, const StrVector& Args) {
44 std::for_each(Args.begin(), Args.end(), &PrintString);
48 bool IsSegmentationFault (int returnCode) {
50 return (returnCode >= 0xc0000000UL)
52 return (returnCode < 0);
56 int ExecuteProgram (const std::string& name, const StrVector& args) {
59 if (sys::path::is_relative(prog.str())) {
60 prog = PrependMainExecutablePath(name, ProgramName,
61 (void *)(intptr_t)&Main);
63 if (!prog.canExecute()) {
64 prog = sys::Program::FindProgramByName(name);
66 PrintError("Can't find program '" + name + "'");
71 if (!prog.canExecute()) {
72 PrintError("Program '" + name + "' is not executable.");
76 // Build the command line vector and the redirects array.
77 const sys::Path* redirects[3] = {0,0,0};
78 sys::Path stdout_redirect;
80 std::vector<const char*> argv;
81 argv.reserve((args.size()+2));
82 argv.push_back(name.c_str());
84 for (StrVector::const_iterator B = args.begin(), E = args.end();
88 stdout_redirect.set(*B);
89 redirects[1] = &stdout_redirect;
92 argv.push_back((*B).c_str());
95 argv.push_back(0); // null terminate list.
97 // Invoke the program.
98 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
100 if (IsSegmentationFault(ret)) {
101 errs() << "Segmentation fault: ";
102 PrintCommand(name, args);
110 void AppendToGlobalTimeLog (const std::string& cmd, double time);
113 int llvmc::Action::Execute () const {
114 if (DryRun || VerboseMode)
115 PrintCommand(Command_, Args_);
119 sys::TimeValue now = sys::TimeValue::now();
120 int ret = ExecuteProgram(Command_, Args_);
121 sys::TimeValue now2 = sys::TimeValue::now();
123 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
124 AppendToGlobalTimeLog(Command_, elapsed);
129 return ExecuteProgram(Command_, Args_);