Use the testcase from PR2791.
[oota-llvm.git] / lib / CompilerDriver / Action.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CompilerDriver/Action.h"
15
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/System/Program.h"
18
19 #include <iostream>
20 #include <stdexcept>
21
22 using namespace llvm;
23 using namespace llvmc;
24
25 extern cl::opt<bool> DryRun;
26 extern cl::opt<bool> VerboseMode;
27
28 namespace {
29   int ExecuteProgram(const std::string& name,
30                      const StrVector& args) {
31     sys::Path prog = sys::Program::FindProgramByName(name);
32
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.");
37
38     // Build the command line vector and the redirects array.
39     const sys::Path* redirects[3] = {0,0,0};
40     sys::Path stdout_redirect;
41
42     std::vector<const char*> argv;
43     argv.reserve((args.size()+2));
44     argv.push_back(name.c_str());
45
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;
52       }
53       else {
54         argv.push_back((*B).c_str());
55       }
56     }
57     argv.push_back(0);  // null terminate list.
58
59     // Invoke the program.
60     return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
61   }
62
63   void print_string (const std::string& str) {
64     std::cerr << str << ' ';
65   }
66 }
67
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';
73   }
74   if (DryRun)
75     return 0;
76   else
77     return ExecuteProgram(Command_, Args_);
78 }