blob: 49a87e405acfec9a09c11feb397f33f188d14c4d [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
Dan Gohman69353322009-07-15 17:04:50 +000022bool 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
Gabor Greife16561c2007-07-05 17:07:56 +000037bool llvm::CheckBitcodeOutputToConsole(std::ostream* stream_to_check,
38 bool print_warning) {
Bill Wendling355fc5a2006-12-07 20:28:15 +000039 if (stream_to_check == cout.stream() &&
40 sys::Process::StandardOutIsDisplayed()) {
Reid Spencer4e8dd442005-01-02 00:10:03 +000041 if (print_warning) {
Gabor Greife16561c2007-07-05 17:07:56 +000042 cerr << "WARNING: You're attempting to print out a bitcode file.\n"
Bill Wendlingf3baad32006-12-07 01:30:32 +000043 << "This is inadvisable as it may cause display problems. If\n"
Gabor Greife16561c2007-07-05 17:07:56 +000044 << "you REALLY want to taste LLVM bitcode first-hand, you\n"
Bill Wendlingf3baad32006-12-07 01:30:32 +000045 << "can force output with the `-f' option.\n\n";
Reid Spencer4e8dd442005-01-02 00:10:03 +000046 }
Reid Spencer1263cfd2005-01-01 23:56:20 +000047 return true;
48 }
Brian Gaeke5ade5012004-04-02 21:26:04 +000049 return false;
Chris Lattner454e1832004-04-02 05:04:03 +000050}
51
Misha Brukmane4d58ec2003-08-07 21:34:25 +000052/// FindExecutable - Find a named executable, giving the argv[0] of program
Misha Brukman34c96102003-09-29 22:40:07 +000053/// 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
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000056/// empty string. Return the input string if given a full path to an executable.
Reid Spencerd44f8452004-12-19 18:00:09 +000057///
Chris Lattnerb6d6b932004-05-28 01:20:58 +000058#undef FindExecutable // needed on windows :(
Reid Spencerdc49d862004-12-13 23:41:37 +000059sys::Path llvm::FindExecutable(const std::string &ExeName,
Reid Spencerd44f8452004-12-19 18:00:09 +000060 const std::string &ProgramPath) {
Daniel Dunbar4aef67c2009-07-01 21:36:28 +000061 // First check if the given name is already a valid path to an executable.
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000062 sys::Path Result(ExeName);
Daniel Dunbar4aef67c2009-07-01 21:36:28 +000063 Result.makeAbsolute();
64 if (Result.canExecute())
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000065 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
Daniel Dunbardf555fd2009-07-12 20:23:56 +000069 // is a relative path to the executable itself.
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000070 Result = ProgramPath;
Reid Spencerc9c04732005-07-07 23:21:43 +000071 Result.eraseComponent();
Reid Spencerdc49d862004-12-13 23:41:37 +000072 if (!Result.isEmpty()) {
Reid Spencerc9c04732005-07-07 23:21:43 +000073 Result.appendComponent(ExeName);
Reid Spencer5b891e92005-07-07 18:21:42 +000074 if (Result.canExecute())
Reid Spencerd44f8452004-12-19 18:00:09 +000075 return Result;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000076 }
77
Reid Spencerdc49d862004-12-13 23:41:37 +000078 return sys::Program::FindProgramByName(ExeName);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000079}