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/Support/raw_ostream.h"
17 #include "llvm/System/Program.h"
21 using namespace llvmc
;
24 int ExecuteProgram(const std::string
& name
,
25 const StrVector
& args
) {
26 sys::Path prog
= sys::Program::FindProgramByName(name
);
29 throw std::runtime_error("Can't find program '" + name
+ "'");
30 if (!prog
.canExecute())
31 throw std::runtime_error("Program '" + name
+ "' is not executable.");
33 // Build the command line vector and the redirects array.
34 const sys::Path
* redirects
[3] = {0,0,0};
35 sys::Path stdout_redirect
;
37 std::vector
<const char*> argv
;
38 argv
.reserve((args
.size()+2));
39 argv
.push_back(name
.c_str());
41 for (StrVector::const_iterator B
= args
.begin(), E
= args
.end();
45 stdout_redirect
.set(*B
);
46 redirects
[1] = &stdout_redirect
;
49 argv
.push_back((*B
).c_str());
52 argv
.push_back(0); // null terminate list.
54 // Invoke the program.
55 return sys::Program::ExecuteAndWait(prog
, &argv
[0], 0, &redirects
[0]);
58 void print_string (const std::string
& str
) {
63 int llvmc::Action::Execute() const {
64 if (DryRun
|| VerboseMode
) {
65 errs() << Command_
<< " ";
66 std::for_each(Args_
.begin(), Args_
.end(), print_string
);
72 return ExecuteProgram(Command_
, Args_
);