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