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" |
Mikhail Glushenkov | 6533afe | 2009-11-07 06:33:58 +0000 | [diff] [blame] | 16 | |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 18 | #include "llvm/System/Program.h" |
Mikhail Glushenkov | 6533afe | 2009-11-07 06:33:58 +0000 | [diff] [blame] | 19 | #include "llvm/System/TimeValue.h" |
| 20 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 21 | #include <stdexcept> |
Mikhail Glushenkov | 6533afe | 2009-11-07 06:33:58 +0000 | [diff] [blame] | 22 | #include <string> |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 25 | using namespace llvmc; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 26 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | int ExecuteProgram(const std::string& name, |
Mikhail Glushenkov | aa3bb17 | 2008-05-30 06:17:29 +0000 | [diff] [blame] | 29 | const StrVector& args) { |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 30 | sys::Path prog = sys::Program::FindProgramByName(name); |
| 31 | |
| 32 | if (prog.isEmpty()) |
| 33 | throw std::runtime_error("Can't find program '" + name + "'"); |
| 34 | if (!prog.canExecute()) |
| 35 | throw std::runtime_error("Program '" + name + "' is not executable."); |
| 36 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 37 | // Build the command line vector and the redirects array. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 38 | const sys::Path* redirects[3] = {0,0,0}; |
| 39 | sys::Path stdout_redirect; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 40 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 41 | std::vector<const char*> argv; |
| 42 | argv.reserve((args.size()+2)); |
| 43 | argv.push_back(name.c_str()); |
| 44 | |
Mikhail Glushenkov | aa3bb17 | 2008-05-30 06:17:29 +0000 | [diff] [blame] | 45 | for (StrVector::const_iterator B = args.begin(), E = args.end(); |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 46 | B!=E; ++B) { |
| 47 | if (*B == ">") { |
| 48 | ++B; |
| 49 | stdout_redirect.set(*B); |
| 50 | redirects[1] = &stdout_redirect; |
| 51 | } |
| 52 | else { |
| 53 | argv.push_back((*B).c_str()); |
| 54 | } |
| 55 | } |
| 56 | argv.push_back(0); // null terminate list. |
| 57 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 58 | // Invoke the program. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 59 | return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]); |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void print_string (const std::string& str) { |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame] | 63 | errs() << str << ' '; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Mikhail Glushenkov | 6533afe | 2009-11-07 06:33:58 +0000 | [diff] [blame] | 67 | namespace llvmc { |
| 68 | void AppendToGlobalTimeLog(const std::string& cmd, double time); |
| 69 | } |
| 70 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 71 | int llvmc::Action::Execute() const { |
Mikhail Glushenkov | 7ef3606 | 2008-05-30 18:48:52 +0000 | [diff] [blame] | 72 | if (DryRun || VerboseMode) { |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame] | 73 | errs() << Command_ << " "; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 74 | std::for_each(Args_.begin(), Args_.end(), print_string); |
Bill Wendling | 9cdd4f5 | 2009-06-30 04:07:12 +0000 | [diff] [blame] | 75 | errs() << '\n'; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 76 | } |
Mikhail Glushenkov | 6533afe | 2009-11-07 06:33:58 +0000 | [diff] [blame] | 77 | if (!DryRun) { |
| 78 | if (Time) { |
| 79 | sys::TimeValue now = sys::TimeValue::now(); |
| 80 | int ret = ExecuteProgram(Command_, Args_); |
| 81 | sys::TimeValue now2 = sys::TimeValue::now(); |
| 82 | now2 -= now; |
| 83 | double elapsed = now2.seconds() + now2.microseconds() / 1000000.0; |
| 84 | AppendToGlobalTimeLog(Command_, elapsed); |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | else { |
| 89 | return ExecuteProgram(Command_, Args_); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return 0; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 94 | } |