blob: 82a1e0c4c96ce13d2e373060345ffd0dfd657b9b [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- Tools.h - 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 "Action.h"
15
16#include "llvm/Support/CommandLine.h"
17#include "llvm/System/Program.h"
18
19#include <iostream>
20#include <stdexcept>
21
22using namespace llvm;
23
24extern cl::opt<bool> VerboseMode;
25
26namespace {
27 int ExecuteProgram(const std::string& name,
28 const std::vector<std::string>& args) {
29 sys::Path prog = sys::Program::FindProgramByName(name);
30
31 if (prog.isEmpty())
32 throw std::runtime_error("Can't find program '" + name + "'");
33 if (!prog.canExecute())
34 throw std::runtime_error("Program '" + name + "' is not executable.");
35
36 // Invoke the program
37 std::vector<const char*> argv((args.size()+2));
38 argv[0] = name.c_str();
39 for (unsigned i = 1; i <= args.size(); ++i)
40 argv[i] = args[i-1].c_str();
41 argv[args.size()+1] = 0; // null terminate list.
42
43 return sys::Program::ExecuteAndWait(prog, &argv[0]);
44 }
45
46 void print_string (const std::string& str) {
47 std::cerr << str << ' ';
48 }
49}
50
51int llvmcc::Action::Execute() {
52 if (VerboseMode) {
53 std::cerr << Command_ << " ";
54 std::for_each(Args_.begin(), Args_.end(), print_string);
55 std::cerr << '\n';
56 }
57 return ExecuteProgram(Command_, Args_);
58}