blob: 0be80496a3cb7a872aa40ce3005025f20a7a63e9 [file] [log] [blame]
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +00001//===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
Mikhail Glushenkovb90cd832008-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 Glushenkov4a1a77c2008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/Action.h"
Mikhail Glushenkov7defa2d2009-06-25 18:20:10 +000015#include "llvm/CompilerDriver/BuiltinOptions.h"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000016#include "llvm/CompilerDriver/Error.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000017
Bill Wendling9cdd4f52009-06-30 04:07:12 +000018#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000019#include "llvm/Support/SystemUtils.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000020#include "llvm/System/Program.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000021#include "llvm/System/TimeValue.h"
22
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000023#include <stdexcept>
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000024#include <string>
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000025
26using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000027using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000028
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000029namespace llvmc {
30
31extern int Main(int argc, char** argv);
32extern const char* ProgramName;
33
34}
35
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000036namespace {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000037
38 void PrintString (const std::string& str) {
39 errs() << str << ' ';
40 }
41
42 void PrintCommand (const std::string& Cmd, const StrVector& Args) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +000043 errs() << Cmd << ' ';
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000044 std::for_each(Args.begin(), Args.end(), &PrintString);
45 errs() << '\n';
46 }
47
48 bool IsSegmentationFault (int returnCode) {
49#ifdef LLVM_ON_WIN32
50 return (returnCode >= 0xc0000000UL)
51#else
52 return (returnCode < 0);
53#endif
54 }
55
56 int ExecuteProgram (const std::string& name,
57 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000058 sys::Path prog = sys::Program::FindProgramByName(name);
59
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000060 if (prog.isEmpty()) {
61 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000062 if (prog.isEmpty()) {
63 PrintError("Can't find program '" + name + "'");
64 return -1;
65 }
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000066 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000067 if (!prog.canExecute()) {
68 PrintError("Program '" + name + "' is not executable.");
69 return -1;
70 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000071
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000072 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000073 const sys::Path* redirects[3] = {0,0,0};
74 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000075
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000076 std::vector<const char*> argv;
77 argv.reserve((args.size()+2));
78 argv.push_back(name.c_str());
79
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000080 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000081 B!=E; ++B) {
82 if (*B == ">") {
83 ++B;
84 stdout_redirect.set(*B);
85 redirects[1] = &stdout_redirect;
86 }
87 else {
88 argv.push_back((*B).c_str());
89 }
90 }
91 argv.push_back(0); // null terminate list.
92
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000093 // Invoke the program.
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000094 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
95
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000096 if (IsSegmentationFault(ret)) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +000097 errs() << "Segmentation fault: ";
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000098 PrintCommand(name, args);
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000099 }
100
101 return ret;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000102 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000103}
104
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000105namespace llvmc {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000106 void AppendToGlobalTimeLog (const std::string& cmd, double time);
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000107}
108
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000109int llvmc::Action::Execute () const {
110 if (DryRun || VerboseMode)
111 PrintCommand(Command_, Args_);
112
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000113 if (!DryRun) {
114 if (Time) {
115 sys::TimeValue now = sys::TimeValue::now();
116 int ret = ExecuteProgram(Command_, Args_);
117 sys::TimeValue now2 = sys::TimeValue::now();
118 now2 -= now;
119 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
120 AppendToGlobalTimeLog(Command_, elapsed);
121
122 return ret;
123 }
124 else {
125 return ExecuteProgram(Command_, Args_);
126 }
127 }
128
129 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000130}