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