1 //===- SystemUtils.cpp - Utilities for low-level system tasks -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains functions used to do a variety of low-level, often 11 // system-specific, tasks. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Support/Streams.h" 16 #include "llvm/Support/SystemUtils.h" 17 #include "llvm/System/Process.h" 18 #include "llvm/System/Program.h" 19 #include <ostream> 20 using namespace llvm; 21 22 bool llvm::CheckBitcodeOutputToConsole(raw_ostream* stream_to_check, 23 bool print_warning) { 24 if (stream_to_check == &outs() && 25 sys::Process::StandardOutIsDisplayed()) { 26 if (print_warning) { 27 cerr << "WARNING: You're attempting to print out a bitcode file.\n" 28 << "This is inadvisable as it may cause display problems. If\n" 29 << "you REALLY want to taste LLVM bitcode first-hand, you\n" 30 << "can force output with the `-f' option.\n\n"; 31 } 32 return true; 33 } 34 return false; 35 } 36 37 bool llvm::CheckBitcodeOutputToConsole(std::ostream* stream_to_check, 38 bool print_warning) { 39 if (stream_to_check == cout.stream() && 40 sys::Process::StandardOutIsDisplayed()) { 41 if (print_warning) { 42 cerr << "WARNING: You're attempting to print out a bitcode file.\n" 43 << "This is inadvisable as it may cause display problems. If\n" 44 << "you REALLY want to taste LLVM bitcode first-hand, you\n" 45 << "can force output with the `-f' option.\n\n"; 46 } 47 return true; 48 } 49 return false; 50 } 51 52 /// FindExecutable - Find a named executable, giving the argv[0] of program 53 /// being executed. This allows us to find another LLVM tool if it is built 54 /// into the same directory, but that directory is neither the current 55 /// directory, nor in the PATH. If the executable cannot be found, return an 56 /// empty string. Return the input string if given a full path to an executable. 57 /// 58 #undef FindExecutable // needed on windows :( 59 sys::Path llvm::FindExecutable(const std::string &ExeName, 60 const std::string &ProgramPath) { 61 // First check if the given name is already a valid path to an executable. 62 sys::Path Result(ExeName); 63 Result.makeAbsolute(); 64 if (Result.canExecute()) 65 return Result; 66 67 // Otherwise check the directory that the calling program is in. We can do 68 // this if ProgramPath contains at least one / character, indicating that it 69 // is a relative path to the executable itself. 70 Result = ProgramPath; 71 Result.eraseComponent(); 72 if (!Result.isEmpty()) { 73 Result.appendComponent(ExeName); 74 if (Result.canExecute()) 75 return Result; 76 } 77 78 return sys::Program::FindProgramByName(ExeName); 79 } 80