blob: a8d625c7ac04ce431bf560c24d57e6da99e86089 [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"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000021#include "llvm/Support/Program.h"
22#include "llvm/Support/TimeValue.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000023
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
Mikhail Glushenkov6674b772010-11-02 20:47:41 +000056 int ExecuteProgram (const std::string& name, const StrVector& args) {
Mikhail Glushenkov9f36e732010-10-28 19:33:08 +000057 sys::Path prog(name);
58
Michael J. Spencer965841c2010-12-17 21:21:31 +000059 if (sys::path::is_relative(prog.str())) {
Mikhail Glushenkovd66f2b72010-11-03 16:14:16 +000060 prog = PrependMainExecutablePath(name, ProgramName,
61 (void *)(intptr_t)&Main);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000062
Mikhail Glushenkov6674b772010-11-02 20:47:41 +000063 if (!prog.canExecute()) {
64 prog = sys::Program::FindProgramByName(name);
65 if (prog.isEmpty()) {
66 PrintError("Can't find program '" + name + "'");
67 return -1;
68 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000069 }
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000070 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000071 if (!prog.canExecute()) {
72 PrintError("Program '" + name + "' is not executable.");
73 return -1;
74 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000075
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000076 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000077 const sys::Path* redirects[3] = {0,0,0};
78 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000079
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000080 std::vector<const char*> argv;
81 argv.reserve((args.size()+2));
82 argv.push_back(name.c_str());
83
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000084 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000085 B!=E; ++B) {
86 if (*B == ">") {
87 ++B;
88 stdout_redirect.set(*B);
89 redirects[1] = &stdout_redirect;
90 }
91 else {
92 argv.push_back((*B).c_str());
93 }
94 }
95 argv.push_back(0); // null terminate list.
96
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000097 // Invoke the program.
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000098 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
99
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000100 if (IsSegmentationFault(ret)) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +0000101 errs() << "Segmentation fault: ";
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000102 PrintCommand(name, args);
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +0000103 }
104
105 return ret;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000106 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000107}
108
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000109namespace llvmc {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000110 void AppendToGlobalTimeLog (const std::string& cmd, double time);
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000111}
112
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000113int llvmc::Action::Execute () const {
114 if (DryRun || VerboseMode)
115 PrintCommand(Command_, Args_);
116
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000117 if (!DryRun) {
118 if (Time) {
119 sys::TimeValue now = sys::TimeValue::now();
120 int ret = ExecuteProgram(Command_, Args_);
121 sys::TimeValue now2 = sys::TimeValue::now();
122 now2 -= now;
123 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
124 AppendToGlobalTimeLog(Command_, elapsed);
125
126 return ret;
127 }
128 else {
129 return ExecuteProgram(Command_, Args_);
130 }
131 }
132
133 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000134}