blob: 5f30dce5855fd42293b3b58663bce7c249809865 [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 Glushenkov6533afe2009-11-07 06:33:58 +000016
Bill Wendling9cdd4f52009-06-30 04:07:12 +000017#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000018#include "llvm/Support/SystemUtils.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000019#include "llvm/System/Program.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000020#include "llvm/System/TimeValue.h"
21
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000022#include <stdexcept>
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000023#include <string>
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024
25using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000026using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000027
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000028namespace llvmc {
29
30extern int Main(int argc, char** argv);
31extern const char* ProgramName;
32
33}
34
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000035namespace {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000036
37 void PrintString (const std::string& str) {
38 errs() << str << ' ';
39 }
40
41 void PrintCommand (const std::string& Cmd, const StrVector& Args) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +000042 errs() << Cmd << ' ';
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000043 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 Glushenkovb90cd832008-05-06 16:34:12 +000057 sys::Path prog = sys::Program::FindProgramByName(name);
58
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000059 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 Glushenkovb90cd832008-05-06 16:34:12 +000064 if (!prog.canExecute())
65 throw std::runtime_error("Program '" + name + "' is not executable.");
66
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000067 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000068 const sys::Path* redirects[3] = {0,0,0};
69 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000070
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000071 std::vector<const char*> argv;
72 argv.reserve((args.size()+2));
73 argv.push_back(name.c_str());
74
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000075 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000076 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 Glushenkov35a85e82008-05-06 18:10:20 +000088 // Invoke the program.
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000089 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
90
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000091 if (IsSegmentationFault(ret)) {
Mikhail Glushenkovdcc44672010-05-20 21:11:37 +000092 errs() << "Segmentation fault: ";
Mikhail Glushenkoveb097152010-05-20 19:23:47 +000093 PrintCommand(name, args);
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000094 }
95
96 return ret;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000097 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000098}
99
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000100namespace llvmc {
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000101 void AppendToGlobalTimeLog (const std::string& cmd, double time);
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000102}
103
Mikhail Glushenkoveb097152010-05-20 19:23:47 +0000104int llvmc::Action::Execute () const {
105 if (DryRun || VerboseMode)
106 PrintCommand(Command_, Args_);
107
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000108 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 Glushenkovb90cd832008-05-06 16:34:12 +0000125}