blob: 49a87e405acfec9a09c11feb397f33f188d14c4d [file] [log] [blame]
Misha Brukmanebb0faa2004-06-18 15:38:49 +00001//===- SystemUtils.cpp - Utilities for low-level system tasks -------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-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 Wendlingfe6b1462006-11-26 10:52:51 +000015#include "llvm/Support/Streams.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/Support/SystemUtils.h"
Reid Spencer436f23e2005-01-01 23:56:20 +000017#include "llvm/System/Process.h"
Misha Brukmandca79782005-04-22 19:13:22 +000018#include "llvm/System/Program.h"
Bill Wendling1a097e32006-12-07 23:41:45 +000019#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Dan Gohmand890de12009-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 Greifa99be512007-07-05 17:07:56 +000037bool llvm::CheckBitcodeOutputToConsole(std::ostream* stream_to_check,
38 bool print_warning) {
Bill Wendlingbcd24982006-12-07 20:28:15 +000039 if (stream_to_check == cout.stream() &&
40 sys::Process::StandardOutIsDisplayed()) {
Reid Spencer52b50a62005-01-02 00:10:03 +000041 if (print_warning) {
Gabor Greifa99be512007-07-05 17:07:56 +000042 cerr << "WARNING: You're attempting to print out a bitcode file.\n"
Bill Wendlinge8156192006-12-07 01:30:32 +000043 << "This is inadvisable as it may cause display problems. If\n"
Gabor Greifa99be512007-07-05 17:07:56 +000044 << "you REALLY want to taste LLVM bitcode first-hand, you\n"
Bill Wendlinge8156192006-12-07 01:30:32 +000045 << "can force output with the `-f' option.\n\n";
Reid Spencer52b50a62005-01-02 00:10:03 +000046 }
Reid Spencer436f23e2005-01-01 23:56:20 +000047 return true;
48 }
Brian Gaeke8507ecb2004-04-02 21:26:04 +000049 return false;
Chris Lattnerb234d462004-04-02 05:04:03 +000050}
51
Misha Brukmanf7066c72003-08-07 21:34:25 +000052/// FindExecutable - Find a named executable, giving the argv[0] of program
Misha Brukman44f8a342003-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 Dunbar93696a32009-07-01 15:26:13 +000056/// empty string. Return the input string if given a full path to an executable.
Reid Spencera4348052004-12-19 18:00:09 +000057///
Chris Lattner49f61c42004-05-28 01:20:58 +000058#undef FindExecutable // needed on windows :(
Reid Spencer9665e8a2004-12-13 23:41:37 +000059sys::Path llvm::FindExecutable(const std::string &ExeName,
Reid Spencera4348052004-12-19 18:00:09 +000060 const std::string &ProgramPath) {
Daniel Dunbar74a81812009-07-01 21:36:28 +000061 // First check if the given name is already a valid path to an executable.
Daniel Dunbar93696a32009-07-01 15:26:13 +000062 sys::Path Result(ExeName);
Daniel Dunbar74a81812009-07-01 21:36:28 +000063 Result.makeAbsolute();
64 if (Result.canExecute())
Daniel Dunbar93696a32009-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 Dunbara00e85c2009-07-12 20:23:56 +000069 // is a relative path to the executable itself.
Daniel Dunbar93696a32009-07-01 15:26:13 +000070 Result = ProgramPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000071 Result.eraseComponent();
Reid Spencer9665e8a2004-12-13 23:41:37 +000072 if (!Result.isEmpty()) {
Reid Spencerdd04df02005-07-07 23:21:43 +000073 Result.appendComponent(ExeName);
Reid Spencerc7f08322005-07-07 18:21:42 +000074 if (Result.canExecute())
Reid Spencera4348052004-12-19 18:00:09 +000075 return Result;
Chris Lattner4a106452002-12-23 23:50:16 +000076 }
77
Reid Spencer9665e8a2004-12-13 23:41:37 +000078 return sys::Program::FindProgramByName(ExeName);
Chris Lattner4a106452002-12-23 23:50:16 +000079}