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"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Support/SystemUtils.h"
20 #include "llvm/System/Program.h"
21 #include "llvm/System/TimeValue.h"
27 using namespace llvmc
;
31 extern int Main(int argc
, char** argv
);
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
,
57 const StrVector
& args
) {
58 sys::Path prog
= sys::Program::FindProgramByName(name
);
61 prog
= FindExecutable(name
, ProgramName
, (void *)(intptr_t)&Main
);
63 PrintError("Can't find program '" + name
+ "'");
67 if (!prog
.canExecute()) {
68 PrintError("Program '" + name
+ "' is not executable.");
72 // Build the command line vector and the redirects array.
73 const sys::Path
* redirects
[3] = {0,0,0};
74 sys::Path stdout_redirect
;
76 std::vector
<const char*> argv
;
77 argv
.reserve((args
.size()+2));
78 argv
.push_back(name
.c_str());
80 for (StrVector::const_iterator B
= args
.begin(), E
= args
.end();
84 stdout_redirect
.set(*B
);
85 redirects
[1] = &stdout_redirect
;
88 argv
.push_back((*B
).c_str());
91 argv
.push_back(0); // null terminate list.
93 // Invoke the program.
94 int ret
= sys::Program::ExecuteAndWait(prog
, &argv
[0], 0, &redirects
[0]);
96 if (IsSegmentationFault(ret
)) {
97 errs() << "Segmentation fault: ";
98 PrintCommand(name
, args
);
106 void AppendToGlobalTimeLog (const std::string
& cmd
, double time
);
109 int llvmc::Action::Execute () const {
110 if (DryRun
|| VerboseMode
)
111 PrintCommand(Command_
, Args_
);
115 sys::TimeValue now
= sys::TimeValue::now();
116 int ret
= ExecuteProgram(Command_
, Args_
);
117 sys::TimeValue now2
= sys::TimeValue::now();
119 double elapsed
= now2
.seconds() + now2
.microseconds() / 1000000.0;
120 AppendToGlobalTimeLog(Command_
, elapsed
);
125 return ExecuteProgram(Command_
, Args_
);