blob: 5dcd3347258621052ca2c0b00c3e8212246c687b [file] [log] [blame]
Misha Brukman4b585812004-06-18 15:38:49 +00001//===- SystemUtils.cpp - Utilities for low-level system tasks -------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00009//
10// This file contains functions used to do a variety of low-level, often
11// system-specific, tasks.
12//
13//===----------------------------------------------------------------------===//
14
Bill Wendling3750ae22006-11-26 10:52:51 +000015#include "llvm/Support/Streams.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000016#include "llvm/Support/SystemUtils.h"
Reid Spencer1263cfd2005-01-01 23:56:20 +000017#include "llvm/System/Process.h"
Misha Brukman7937b072005-04-22 19:13:22 +000018#include "llvm/System/Program.h"
Bill Wendling30c0f332006-12-07 23:41:45 +000019#include <ostream>
Chris Lattnerc9499b62003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Gabor Greife16561c2007-07-05 17:07:56 +000022bool llvm::CheckBitcodeOutputToConsole(std::ostream* stream_to_check,
23 bool print_warning) {
Bill Wendling355fc5a2006-12-07 20:28:15 +000024 if (stream_to_check == cout.stream() &&
25 sys::Process::StandardOutIsDisplayed()) {
Reid Spencer4e8dd442005-01-02 00:10:03 +000026 if (print_warning) {
Gabor Greife16561c2007-07-05 17:07:56 +000027 cerr << "WARNING: You're attempting to print out a bitcode file.\n"
Bill Wendlingf3baad32006-12-07 01:30:32 +000028 << "This is inadvisable as it may cause display problems. If\n"
Gabor Greife16561c2007-07-05 17:07:56 +000029 << "you REALLY want to taste LLVM bitcode first-hand, you\n"
Bill Wendlingf3baad32006-12-07 01:30:32 +000030 << "can force output with the `-f' option.\n\n";
Reid Spencer4e8dd442005-01-02 00:10:03 +000031 }
Reid Spencer1263cfd2005-01-01 23:56:20 +000032 return true;
33 }
Brian Gaeke5ade5012004-04-02 21:26:04 +000034 return false;
Chris Lattner454e1832004-04-02 05:04:03 +000035}
36
Misha Brukmane4d58ec2003-08-07 21:34:25 +000037/// FindExecutable - Find a named executable, giving the argv[0] of program
Misha Brukman34c96102003-09-29 22:40:07 +000038/// being executed. This allows us to find another LLVM tool if it is built
39/// into the same directory, but that directory is neither the current
40/// directory, nor in the PATH. If the executable cannot be found, return an
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000041/// empty string. Return the input string if given a full path to an executable.
Reid Spencerd44f8452004-12-19 18:00:09 +000042///
Chris Lattnerb6d6b932004-05-28 01:20:58 +000043#undef FindExecutable // needed on windows :(
Reid Spencerdc49d862004-12-13 23:41:37 +000044sys::Path llvm::FindExecutable(const std::string &ExeName,
Reid Spencerd44f8452004-12-19 18:00:09 +000045 const std::string &ProgramPath) {
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000046 // First check if the given name is a fully qualified path to an executable
47 sys::Path Result(ExeName);
48 if (Result.isAbsolute() && Result.canExecute())
49 return Result;
50
51 // Otherwise check the directory that the calling program is in. We can do
52 // this if ProgramPath contains at least one / character, indicating that it
53 // is a relative path to bugpoint itself.
54 Result = ProgramPath;
Reid Spencerc9c04732005-07-07 23:21:43 +000055 Result.eraseComponent();
Reid Spencerdc49d862004-12-13 23:41:37 +000056 if (!Result.isEmpty()) {
Reid Spencerc9c04732005-07-07 23:21:43 +000057 Result.appendComponent(ExeName);
Reid Spencer5b891e92005-07-07 18:21:42 +000058 if (Result.canExecute())
Reid Spencerd44f8452004-12-19 18:00:09 +000059 return Result;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000060 }
61
Reid Spencerdc49d862004-12-13 23:41:37 +000062 return sys::Program::FindProgramByName(ExeName);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000063}