blob: c0a1b849bcdfc40993f522c262ed463c54434ba2 [file] [log] [blame]
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +00001//===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00002//
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
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/Action.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000015
16#include "llvm/Support/CommandLine.h"
17#include "llvm/System/Program.h"
18
19#include <iostream>
20#include <stdexcept>
21
22using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000023using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000025extern cl::opt<bool> DryRun;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000026extern cl::opt<bool> VerboseMode;
27
28namespace {
29 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000030 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000031 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
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000038 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000039 const sys::Path* redirects[3] = {0,0,0};
40 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000041
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000042 std::vector<const char*> argv;
43 argv.reserve((args.size()+2));
44 argv.push_back(name.c_str());
45
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000046 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000047 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
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000059 // Invoke the program.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000060 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000061 }
62
63 void print_string (const std::string& str) {
64 std::cerr << str << ' ';
65 }
66}
67
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000068int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000069 if (DryRun || VerboseMode) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000070 std::cerr << Command_ << " ";
71 std::for_each(Args_.begin(), Args_.end(), print_string);
72 std::cerr << '\n';
73 }
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000074 if (DryRun)
75 return 0;
76 else
77 return ExecuteProgram(Command_, Args_);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000078}