blob: 7bcd30a8e0e75141700244db8dba3898cd3ce661 [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 Glushenkovb90cd832008-05-06 16:34:12 +000018#include "llvm/System/Program.h"
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000019#include "llvm/System/TimeValue.h"
20
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000021#include <stdexcept>
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000022#include <string>
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000023
24using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000025using namespace llvmc;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000026
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000027namespace {
28 int ExecuteProgram(const std::string& name,
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000029 const StrVector& args) {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000030 sys::Path prog = sys::Program::FindProgramByName(name);
31
32 if (prog.isEmpty())
33 throw std::runtime_error("Can't find program '" + name + "'");
34 if (!prog.canExecute())
35 throw std::runtime_error("Program '" + name + "' is not executable.");
36
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000037 // Build the command line vector and the redirects array.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000038 const sys::Path* redirects[3] = {0,0,0};
39 sys::Path stdout_redirect;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000040
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000041 std::vector<const char*> argv;
42 argv.reserve((args.size()+2));
43 argv.push_back(name.c_str());
44
Mikhail Glushenkovaa3bb172008-05-30 06:17:29 +000045 for (StrVector::const_iterator B = args.begin(), E = args.end();
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000046 B!=E; ++B) {
47 if (*B == ">") {
48 ++B;
49 stdout_redirect.set(*B);
50 redirects[1] = &stdout_redirect;
51 }
52 else {
53 argv.push_back((*B).c_str());
54 }
55 }
56 argv.push_back(0); // null terminate list.
57
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000058 // Invoke the program.
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000059 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000060 }
61
62 void print_string (const std::string& str) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000063 errs() << str << ' ';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000064 }
65}
66
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000067namespace llvmc {
68 void AppendToGlobalTimeLog(const std::string& cmd, double time);
69}
70
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000071int llvmc::Action::Execute() const {
Mikhail Glushenkov7ef36062008-05-30 18:48:52 +000072 if (DryRun || VerboseMode) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +000073 errs() << Command_ << " ";
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000074 std::for_each(Args_.begin(), Args_.end(), print_string);
Bill Wendling9cdd4f52009-06-30 04:07:12 +000075 errs() << '\n';
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000076 }
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000077 if (!DryRun) {
78 if (Time) {
79 sys::TimeValue now = sys::TimeValue::now();
80 int ret = ExecuteProgram(Command_, Args_);
81 sys::TimeValue now2 = sys::TimeValue::now();
82 now2 -= now;
83 double elapsed = now2.seconds() + now2.microseconds() / 1000000.0;
84 AppendToGlobalTimeLog(Command_, elapsed);
85
86 return ret;
87 }
88 else {
89 return ExecuteProgram(Command_, Args_);
90 }
91 }
92
93 return 0;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000094}