1ebb0faabd3df5eab24178da692b75aa79b0d65c7Misha Brukman//===- SystemUtils.cpp - Utilities for low-level system tasks -------------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
94a10645c70199c8d8567fbc46312158c419720abChris Lattner//
104a10645c70199c8d8567fbc46312158c419720abChris Lattner// This file contains functions used to do a variety of low-level, often
114a10645c70199c8d8567fbc46312158c419720abChris Lattner// system-specific, tasks.
124a10645c70199c8d8567fbc46312158c419720abChris Lattner//
134a10645c70199c8d8567fbc46312158c419720abChris Lattner//===----------------------------------------------------------------------===//
144a10645c70199c8d8567fbc46312158c419720abChris Lattner
15551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/SystemUtils.h"
161f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Process.h"
171f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Program.h"
1874382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner#include "llvm/Support/raw_ostream.h"
192cdd21c2e4d855500dfb53f77aa74da53ccf9de6Chris Lattnerusing namespace llvm;
20d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
21b683ea4712836e22b98d24bf8e40e599224d024eChris Lattnerbool llvm::CheckBitcodeOutputToConsole(raw_ostream &stream_to_check,
22d890de10af6a4d01fe0b35f09ccd674d771e4732Dan Gohman                                       bool print_warning) {
23ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman  if (stream_to_check.is_displayed()) {
24d890de10af6a4d01fe0b35f09ccd674d771e4732Dan Gohman    if (print_warning) {
25b683ea4712836e22b98d24bf8e40e599224d024eChris Lattner      errs() << "WARNING: You're attempting to print out a bitcode file.\n"
26303f350f7d4fffac042d6e36ca35f751ab62488bDan Gohman                "This is inadvisable as it may cause display problems. If\n"
27303f350f7d4fffac042d6e36ca35f751ab62488bDan Gohman                "you REALLY want to taste LLVM bitcode first-hand, you\n"
28303f350f7d4fffac042d6e36ca35f751ab62488bDan Gohman                "can force output with the `-f' option.\n\n";
2952b50a64ecd31ebb03e742d8ad6b6a86b541a53dReid Spencer    }
30436f23ebf58418611f798faf7e00de05c7a42e21Reid Spencer    return true;
31436f23ebf58418611f798faf7e00de05c7a42e21Reid Spencer  }
328507ecb36d8b755671861d0b3550c66a322d3f04Brian Gaeke  return false;
33b234d460b25fea41531cd6edf3da67ec1601233bChris Lattner}
34b234d460b25fea41531cd6edf3da67ec1601233bChris Lattner
35d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// PrependMainExecutablePath - Prepend the path to the program being executed
36d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// to \p ExeName, given the value of argv[0] and the address of main()
37d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// itself. This allows us to find another LLVM tool if it is built in the same
38d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// directory. An empty string is returned on error; note that this function
39d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// just mainpulates the path and doesn't check for executability.
40d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov/// @brief Find a named executable.
41d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkovsys::Path llvm::PrependMainExecutablePath(const std::string &ExeName,
42d66f2b74f2f01bb1f6c7d0f1b8408bd24ffa654cMikhail Glushenkov                                          const char *Argv0, void *MainAddr) {
43197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  // Check the directory that the calling program is in.  We can do
4493696a38b709361aee960fd8537973205ea8cfefDaniel Dunbar  // this if ProgramPath contains at least one / character, indicating that it
45a00e85c9b1d57d3ba89592b95f55c571170a8ab9Daniel Dunbar  // is a relative path to the executable itself.
46197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  sys::Path Result = sys::Path::GetMainExecutable(Argv0, MainAddr);
47dd04df0ec33a903ee7fc747701bafde622f77d8bReid Spencer  Result.eraseComponent();
48345b344e64ae3e0b595e660a7e0c1833ead42e18Mikhail Glushenkov
499665e8a697d1fa91aeca17f13a81dd2de2f11aa5Reid Spencer  if (!Result.isEmpty()) {
50dd04df0ec33a903ee7fc747701bafde622f77d8bReid Spencer    Result.appendComponent(ExeName);
51ed724fd43a2388f93460ca397c69948d346ec4dbMikhail Glushenkov    Result.appendSuffix(sys::Path::GetEXESuffix());
524a10645c70199c8d8567fbc46312158c419720abChris Lattner  }
534a10645c70199c8d8567fbc46312158c419720abChris Lattner
548a5ff478db80fdc0753378563b8f1c139136607aMikhail Glushenkov  return Result;
554a10645c70199c8d8567fbc46312158c419720abChris Lattner}
56