SystemUtils.cpp revision bcd2498f4f1682dbdc41452add5b9bc72cbd6b3f
1//===- SystemUtils.cpp - Utilities for low-level system tasks -------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
19using namespace llvm;
20
21bool llvm::CheckBytecodeOutputToConsole(std::ostream* stream_to_check,
22                                        bool print_warning) {
23  if (stream_to_check == cout.stream() &&
24      sys::Process::StandardOutIsDisplayed()) {
25    if (print_warning) {
26      cerr << "WARNING: You're attempting to print out a bytecode file.\n"
27           << "This is inadvisable as it may cause display problems. If\n"
28           << "you REALLY want to taste LLVM bytecode first-hand, you\n"
29           << "can force output with the `-f' option.\n\n";
30    }
31    return true;
32  }
33  return false;
34}
35
36/// FindExecutable - Find a named executable, giving the argv[0] of program
37/// being executed. This allows us to find another LLVM tool if it is built
38/// into the same directory, but that directory is neither the current
39/// directory, nor in the PATH.  If the executable cannot be found, return an
40/// empty string.
41///
42#undef FindExecutable   // needed on windows :(
43sys::Path llvm::FindExecutable(const std::string &ExeName,
44                               const std::string &ProgramPath) {
45  // First check the directory that the calling program is in.  We can do this
46  // if ProgramPath contains at least one / character, indicating that it is a
47  // relative path to bugpoint itself.
48  sys::Path Result ( ProgramPath );
49  Result.eraseComponent();
50  if (!Result.isEmpty()) {
51    Result.appendComponent(ExeName);
52    if (Result.canExecute())
53      return Result;
54  }
55
56  return sys::Program::FindProgramByName(ExeName);
57}
58