blob: 5fd63eefc523ebb3c0d7e0a2a8048b91d7c960e0 [file] [log] [blame]
Mikhail Glushenkovac8af6f2008-05-30 06:17:29 +00001//===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
Mikhail Glushenkovafbeae92008-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 Glushenkov62ab3112008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/Action.h"
Mikhail Glushenkovfe4d0552009-06-25 18:20:10 +000015#include "llvm/CompilerDriver/BuiltinOptions.h"
Bill Wendlingc544fab2009-06-30 04:07:12 +000016#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000017#include "llvm/System/Program.h"
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000018#include <stdexcept>
19
20using namespace llvm;
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000021using namespace llvmc;
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000022
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000023namespace {
24 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovac8af6f2008-05-30 06:17:29 +000025 const StrVector& args) {
Mikhail Glushenkovafbeae92008-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 Glushenkovedd7ff92008-05-06 18:10:53 +000033 // Build the command line vector and the redirects array.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000034 const sys::Path* redirects[3] = {0,0,0};
35 sys::Path stdout_redirect;
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000036
Mikhail Glushenkov34307a92008-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 Glushenkovac8af6f2008-05-30 06:17:29 +000041 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkov34307a92008-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 Glushenkov49254b52008-05-06 18:10:20 +000054 // Invoke the program.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000055 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000056 }
57
58 void print_string (const std::string& str) {
Bill Wendlingc544fab2009-06-30 04:07:12 +000059 errs() << str << ' ';
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000060 }
61}
62
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000063int llvmc::Action::Execute() const {
Mikhail Glushenkov0bd4dba2008-05-30 18:48:52 +000064 if (DryRun || VerboseMode) {
Bill Wendlingc544fab2009-06-30 04:07:12 +000065 errs() << Command_ << " ";
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000066 std::for_each(Args_.begin(), Args_.end(), print_string);
Bill Wendlingc544fab2009-06-30 04:07:12 +000067 errs() << '\n';
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000068 }
Mikhail Glushenkov0bd4dba2008-05-30 18:48:52 +000069 if (DryRun)
70 return 0;
71 else
72 return ExecuteProgram(Command_, Args_);
Mikhail Glushenkovafbeae92008-05-06 16:34:12 +000073}