blob: 09c0b19aa9c0bead5b178406227208cfeab1c771 [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
Dan Gohman46ffffa2009-08-05 20:21:17 +000053/// being executed. This allows us to find another LLVM tool if it is built in
54/// the same directory. If the executable cannot be found, return an
55/// empty string.
56/// @brief Find a named executable.
Chris Lattnerb6d6b932004-05-28 01:20:58 +000057#undef FindExecutable // needed on windows :(
Reid Spencerdc49d862004-12-13 23:41:37 +000058sys::Path llvm::FindExecutable(const std::string &ExeName,
Dan Gohman46ffffa2009-08-05 20:21:17 +000059 const char *Argv0, void *MainAddr) {
60 // Check the directory that the calling program is in. We can do
Daniel Dunbarb8f779d2009-07-01 15:26:13 +000061 // this if ProgramPath contains at least one / character, indicating that it
Daniel Dunbardf555fd2009-07-12 20:23:56 +000062 // is a relative path to the executable itself.
Dan Gohman46ffffa2009-08-05 20:21:17 +000063 sys::Path Result = sys::Path::GetMainExecutable(Argv0, MainAddr);
Reid Spencerc9c04732005-07-07 23:21:43 +000064 Result.eraseComponent();
Reid Spencerdc49d862004-12-13 23:41:37 +000065 if (!Result.isEmpty()) {
Reid Spencerc9c04732005-07-07 23:21:43 +000066 Result.appendComponent(ExeName);
Reid Spencer5b891e92005-07-07 18:21:42 +000067 if (Result.canExecute())
Reid Spencerd44f8452004-12-19 18:00:09 +000068 return Result;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000069 }
70
Dan Gohman46ffffa2009-08-05 20:21:17 +000071 return sys::Path();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000072}