blob: 8c63a4da53ef08a8e8b2e478f40d6e681440bcaa [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 {
36 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000037 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000038 sys::Path prog = sys::Program::FindProgramByName(name);
39
Mikhail Glushenkov0d613492010-03-05 04:46:28 +000040 if (prog.isEmpty()) {
41 prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
42 if (prog.isEmpty())
43 throw std::runtime_error("Can't find program '" + name + "'");
44 }
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000045 if (!prog.canExecute())
46 throw std::runtime_error("Program '" + name + "' is not executable.");
47
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000048 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000049 const sys::Path* redirects[3] = {0,0,0};
50 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000051
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000052 std::vector<const char*> argv;
53 argv.reserve((args.size()+2));
54 argv.push_back(name.c_str());
55
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000056 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000057 B!=E; ++B) {
58 if (*B == ">") {
59 ++B;
60 stdout_redirect.set(*B);
61 redirects[1] = &stdout_redirect;
62 }
63 else {
64 argv.push_back((*B).c_str());
65 }
66 }
67 argv.push_back(0); // null terminate list.
68
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000069 // Invoke the program.
Mikhail Glushenkov139c9e12010-05-19 19:24:32 +000070 int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
71
72 if (ret < 0) {
73 const char** B = &argv[0];
74
75 errs() << "Segmentation fault:";
76 while (*B)
77 errs() << ' ' << *(B++);
78 errs() << '\n';
79
80 return 1;
81 }
82
83 return ret;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000084 }
85
86 void print_string (const std::string& str) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000087 errs() << str << ' ';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000088 }
89}
90
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000091namespace llvmc {
92 void AppendToGlobalTimeLog(const std::string& cmd, double time);
93}
94
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000095int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000096 if (DryRun || VerboseMode) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000097 errs() << Command_ << " ";
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000098 std::for_each(Args_.begin(), Args_.end(), print_string);
Bill Wendling9cdd4f52009-06-30 04:07:12 +000099 errs() << '\n';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000100 }
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000101 if (!DryRun) {
102 if (Time) {
103 sys::TimeValue now = sys::TimeValue::now();
104 int ret = ExecuteProgram(Command_, Args_);
105 sys::TimeValue now2 = sys::TimeValue::now();
106 now2 -= now;
107 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
108 AppendToGlobalTimeLog(Command_, elapsed);
109
110 return ret;
111 }
112 else {
113 return ExecuteProgram(Command_, Args_);
114 }
115 }
116
117 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000118}