blob: 5fd63eefc523ebb3c0d7e0a2a8048b91d7c960e0 [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"
Bill Wendling9cdd4f52009-06-30 04:07:12 +000016#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000017#include "llvm/System/Program.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000018#include <stdexcept>
19
20using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000021using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000022
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000023namespace {
24 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000025 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000026 sys::Path prog = sys::Program::FindProgramByName(name);
27
28 if (prog.isEmpty())
29 throw std::runtime_error("Can't find program '" + name + "'");
30 if (!prog.canExecute())
31 throw std::runtime_error("Program '" + name + "' is not executable.");
32
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000033 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000034 const sys::Path* redirects[3] = {0,0,0};
35 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000036
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000037 std::vector<const char*> argv;
38 argv.reserve((args.size()+2));
39 argv.push_back(name.c_str());
40
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000041 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000042 B!=E; ++B) {
43 if (*B == ">") {
44 ++B;
45 stdout_redirect.set(*B);
46 redirects[1] = &stdout_redirect;
47 }
48 else {
49 argv.push_back((*B).c_str());
50 }
51 }
52 argv.push_back(0); // null terminate list.
53
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000054 // Invoke the program.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000055 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000056 }
57
58 void print_string (const std::string& str) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000059 errs() << str << ' ';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000060 }
61}
62
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000063int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000064 if (DryRun || VerboseMode) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000065 errs() << Command_ << " ";
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000066 std::for_each(Args_.begin(), Args_.end(), print_string);
Bill Wendling9cdd4f52009-06-30 04:07:12 +000067 errs() << '\n';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000068 }
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000069 if (DryRun)
70 return 0;
71 else
72 return ExecuteProgram(Command_, Args_);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000073}