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"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/System/Program.h"
23 using namespace llvmc
;
25 extern cl::opt
<bool> DryRun
;
26 extern cl::opt
<bool> VerboseMode
;
29 int ExecuteProgram(const std::string
& name
,
30 const StrVector
& args
) {
31 sys::Path prog
= sys::Program::FindProgramByName(name
);
34 throw std::runtime_error("Can't find program '" + name
+ "'");
35 if (!prog
.canExecute())
36 throw std::runtime_error("Program '" + name
+ "' is not executable.");
38 // Build the command line vector and the redirects array.
39 const sys::Path
* redirects
[3] = {0,0,0};
40 sys::Path stdout_redirect
;
42 std::vector
<const char*> argv
;
43 argv
.reserve((args
.size()+2));
44 argv
.push_back(name
.c_str());
46 for (StrVector::const_iterator B
= args
.begin(), E
= args
.end();
50 stdout_redirect
.set(*B
);
51 redirects
[1] = &stdout_redirect
;
54 argv
.push_back((*B
).c_str());
57 argv
.push_back(0); // null terminate list.
59 // Invoke the program.
60 return sys::Program::ExecuteAndWait(prog
, &argv
[0], 0, &redirects
[0]);
63 void print_string (const std::string
& str
) {
64 std::cerr
<< str
<< ' ';
68 int llvmc::Action::Execute() const {
69 if (DryRun
|| VerboseMode
) {
70 std::cerr
<< Command_
<< " ";
71 std::for_each(Args_
.begin(), Args_
.end(), print_string
);
77 return ExecuteProgram(Command_
, Args_
);