blob: 9d07811c896cae27331cd361d598c57a624a876a [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 Glushenkovbe9d9a12008-05-06 18:08:59 +000070 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000071 }
72
73 void print_string (const std::string& str) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000074 errs() << str << ' ';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000075 }
76}
77
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000078namespace llvmc {
79 void AppendToGlobalTimeLog(const std::string& cmd, double time);
80}
81
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000082int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000083 if (DryRun || VerboseMode) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000084 errs() << Command_ << " ";
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000085 std::for_each(Args_.begin(), Args_.end(), print_string);
Bill Wendling9cdd4f52009-06-30 04:07:12 +000086 errs() << '\n';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000087 }
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000088 if (!DryRun) {
89 if (Time) {
90 sys::TimeValue now = sys::TimeValue::now();
91 int ret = ExecuteProgram(Command_, Args_);
92 sys::TimeValue now2 = sys::TimeValue::now();
93 now2 -= now;
94 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
95 AppendToGlobalTimeLog(Command_, elapsed);
96
97 return ret;
98 }
99 else {
100 return ExecuteProgram(Command_, Args_);
101 }
102 }
103
104 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000105}