LLVM API Documentation
00001 //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open 00006 // Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Action class - implementation and auxiliary functions. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CompilerDriver/Action.h" 00015 #include "llvm/CompilerDriver/BuiltinOptions.h" 00016 #include "llvm/Support/raw_ostream.h" 00017 #include "llvm/System/Program.h" 00018 #include <stdexcept> 00019 00020 using namespace llvm; 00021 using namespace llvmc; 00022 00023 namespace { 00024 int ExecuteProgram(const std::string& name, 00025 const StrVector& args) { 00026 sys::Path prog = sys::Program::FindProgramByName(name); 00027 00028 if (prog.isEmpty()) 00029 throw std::runtime_error("Can't find program '" + name + "'"); 00030 if (!prog.canExecute()) 00031 throw std::runtime_error("Program '" + name + "' is not executable."); 00032 00033 // Build the command line vector and the redirects array. 00034 const sys::Path* redirects[3] = {0,0,0}; 00035 sys::Path stdout_redirect; 00036 00037 std::vector<const char*> argv; 00038 argv.reserve((args.size()+2)); 00039 argv.push_back(name.c_str()); 00040 00041 for (StrVector::const_iterator B = args.begin(), E = args.end(); 00042 B!=E; ++B) { 00043 if (*B == ">") { 00044 ++B; 00045 stdout_redirect.set(*B); 00046 redirects[1] = &stdout_redirect; 00047 } 00048 else { 00049 argv.push_back((*B).c_str()); 00050 } 00051 } 00052 argv.push_back(0); // null terminate list. 00053 00054 // Invoke the program. 00055 return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]); 00056 } 00057 00058 void print_string (const std::string& str) { 00059 errs() << str << ' '; 00060 } 00061 } 00062 00063 int llvmc::Action::Execute() const { 00064 if (DryRun || VerboseMode) { 00065 errs() << Command_ << " "; 00066 std::for_each(Args_.begin(), Args_.end(), print_string); 00067 errs() << '\n'; 00068 } 00069 if (DryRun) 00070 return 0; 00071 else 00072 return ExecuteProgram(Command_, Args_); 00073 }