Quotes should be printed before private prefix; some code clean up.
[llvm/msp430.git] / lib / CompilerDriver / Action.cpp
blobc0a1b849bcdfc40993f522c262ed463c54434ba2
1 //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
19 #include <iostream>
20 #include <stdexcept>
22 using namespace llvm;
23 using namespace llvmc;
25 extern cl::opt<bool> DryRun;
26 extern cl::opt<bool> VerboseMode;
28 namespace {
29 int ExecuteProgram(const std::string& name,
30 const StrVector& args) {
31 sys::Path prog = sys::Program::FindProgramByName(name);
33 if (prog.isEmpty())
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();
47 B!=E; ++B) {
48 if (*B == ">") {
49 ++B;
50 stdout_redirect.set(*B);
51 redirects[1] = &stdout_redirect;
53 else {
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);
72 std::cerr << '\n';
74 if (DryRun)
75 return 0;
76 else
77 return ExecuteProgram(Command_, Args_);