blob: 816f793bc07dc5ae5d6ba40e156d85a3317d6fd0 [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 Glushenkov7defa2d2009-06-25 18:20:10 +000015#include "llvm/CompilerDriver/BuiltinOptions.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000016
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000017#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 Glushenkovb90cd832008-05-06 16:34:12 +000025namespace {
26 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000027 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000028 sys::Path prog = sys::Program::FindProgramByName(name);
29
30 if (prog.isEmpty())
31 throw std::runtime_error("Can't find program '" + name + "'");
32 if (!prog.canExecute())
33 throw std::runtime_error("Program '" + name + "' is not executable.");
34
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000035 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000036 const sys::Path* redirects[3] = {0,0,0};
37 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000038
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000039 std::vector<const char*> argv;
40 argv.reserve((args.size()+2));
41 argv.push_back(name.c_str());
42
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000043 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000044 B!=E; ++B) {
45 if (*B == ">") {
46 ++B;
47 stdout_redirect.set(*B);
48 redirects[1] = &stdout_redirect;
49 }
50 else {
51 argv.push_back((*B).c_str());
52 }
53 }
54 argv.push_back(0); // null terminate list.
55
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000056 // Invoke the program.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000057 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000058 }
59
60 void print_string (const std::string& str) {
61 std::cerr << str << ' ';
62 }
63}
64
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000065int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000066 if (DryRun || VerboseMode) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000067 std::cerr << Command_ << " ";
68 std::for_each(Args_.begin(), Args_.end(), print_string);
69 std::cerr << '\n';
70 }
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000071 if (DryRun)
72 return 0;
73 else
74 return ExecuteProgram(Command_, Args_);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000075}