blob: 3503bd39db2714a6b8b6d39be5d15f6c353681e0 [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 Glushenkov9f36e732010-10-28 19:33:08 +000017#include "llvm/CompilerDriver/Main.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000018
Bill Wendling9cdd4f52009-06-30 04:07:12 +000019#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000020#include "llvm/Support/SystemUtils.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000021#include "llvm/System/Program.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000022#include "llvm/System/TimeValue.h"
23
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024#include <stdexcept>
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000025#include <string>
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000026
27using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000028using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000029
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000030namespace llvmc {
31
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000032extern 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 Glushenkov9f36e732010-10-28 19:33:08 +000058 sys::Path prog(name);
59
60 if (!prog.isAbsolute())
61 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000062
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000063 if (prog.isEmpty()) {
Mikhail Glushenkov9f36e732010-10-28 19:33:08 +000064 prog = sys::Program::FindProgramByName(name);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000065 if (prog.isEmpty()) {
66 PrintError("Can't find program '" + name + "'");
67 return -1;
68 }
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000069 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000070 if (!prog.canExecute()) {
71 PrintError("Program '" + name + "' is not executable.");
72 return -1;
73 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000074
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000075 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000076 const sys::Path* redirects[3] = {0,0,0};
77 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000078
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000079 std::vector<const char*> argv;
80 argv.reserve((args.size()+2));
81 argv.push_back(name.c_str());
82
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000083 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000084 B!=E; ++B) {
85 if (*B == ">") {
86 ++B;
87 stdout_redirect.set(*B);
88 redirects[1] = &stdout_redirect;
89 }
90 else {
91 argv.push_back((*B).c_str());
92 }
93 }
94 argv.push_back(0); // null terminate list.
95
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000096 // Invoke the program.
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000097 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
98
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000099 if (IsSegmentationFault(ret)) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +0000100 errs() << "Segmentation fault: ";
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000101 PrintCommand(name, args);
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +0000102 }
103
104 return ret;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000105 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000106}
107
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000108namespace llvmc {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000109 void AppendToGlobalTimeLog (const std::string& cmd, double time);
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000110}
111
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000112int llvmc::Action::Execute () const {
113 if (DryRun || VerboseMode)
114 PrintCommand(Command_, Args_);
115
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000116 if (!DryRun) {
117 if (Time) {
118 sys::TimeValue now = sys::TimeValue::now();
119 int ret = ExecuteProgram(Command_, Args_);
120 sys::TimeValue now2 = sys::TimeValue::now();
121 now2 -= now;
122 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
123 AppendToGlobalTimeLog(Command_, elapsed);
124
125 return ret;
126 }
127 else {
128 return ExecuteProgram(Command_, Args_);
129 }
130 }
131
132 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000133}