Mikhail Glushenkov | aa3bb17 | 2008-05-30 06:17:29 +0000 | [diff] [blame] | 1 | //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===// |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 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 | |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 14 | #include "llvm/CompilerDriver/Action.h" |
Mikhail Glushenkov | 7defa2d | 2009-06-25 18:20:10 +0000 | [diff] [blame] | 15 | #include "llvm/CompilerDriver/BuiltinOptions.h" |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame^] | 16 | #include "llvm/Support/raw_ostream.h" |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 17 | #include "llvm/System/Program.h" |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 18 | #include <stdexcept> |
| 19 | |
| 20 | using namespace llvm; |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 21 | using namespace llvmc; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 22 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | int ExecuteProgram(const std::string& name, |
Mikhail Glushenkov | aa3bb17 | 2008-05-30 06:17:29 +0000 | [diff] [blame] | 25 | const StrVector& args) { |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 26 | 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 Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 33 | // Build the command line vector and the redirects array. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 34 | const sys::Path* redirects[3] = {0,0,0}; |
| 35 | sys::Path stdout_redirect; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 36 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 37 | std::vector<const char*> argv; |
| 38 | argv.reserve((args.size()+2)); |
| 39 | argv.push_back(name.c_str()); |
| 40 | |
Mikhail Glushenkov | aa3bb17 | 2008-05-30 06:17:29 +0000 | [diff] [blame] | 41 | for (StrVector::const_iterator B = args.begin(), E = args.end(); |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 42 | 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 Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 54 | // Invoke the program. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 55 | return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]); |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void print_string (const std::string& str) { |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame^] | 59 | errs() << str << ' '; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 63 | int llvmc::Action::Execute() const { |
Mikhail Glushenkov | 7ef3606 | 2008-05-30 18:48:52 +0000 | [diff] [blame] | 64 | if (DryRun || VerboseMode) { |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame^] | 65 | errs() << Command_ << " "; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 66 | std::for_each(Args_.begin(), Args_.end(), print_string); |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame^] | 67 | errs() << '\n'; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 68 | } |
Mikhail Glushenkov | 7ef3606 | 2008-05-30 18:48:52 +0000 | [diff] [blame] | 69 | if (DryRun) |
| 70 | return 0; |
| 71 | else |
| 72 | return ExecuteProgram(Command_, Args_); |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 73 | } |