ldc/gen/programs.cpp
Martin 43ff4373d6 Don't try to find a program via environment variable if that is empty (CC)
Whereas most OSs seem to remove the environment variable if it's cleared,
OSX seems to keep it, explicitly cleared so to speak.

I strongly suspect a cleared CC env variable to be the reason for the
triggering assertion when trying to build dmd-testsuite's d_do_test tool
in our OSX CI builds.
2015-10-18 20:10:24 +02:00

79 lines
2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//===-- programs.cpp ------------------------------------------------------===//
//
// LDC the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/programs.h"
#include "mars.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Program.h"
using namespace llvm;
static cl::opt<std::string> gcc("gcc",
cl::desc("GCC to use for assembling and linking"),
cl::Hidden,
cl::ZeroOrMore);
static cl::opt<std::string> ar("ar",
cl::desc("Archiver"),
cl::Hidden,
cl::ZeroOrMore);
static std::string findProgramByName(const std::string& name)
{
#if LDC_LLVM_VER >= 306
llvm::ErrorOr<std::string> res = llvm::sys::findProgramByName(name);
return res ? res.get() : std::string();
#elif LDC_LLVM_VER >= 304
return llvm::sys::FindProgramByName(name);
#else
return llvm::sys::Program::FindProgramByName(name).str();
#endif
}
static std::string getProgram(const char* name, const cl::opt<std::string>* opt, const char* envVar = NULL)
{
std::string path;
const char *prog = NULL;
if (opt && opt->getNumOccurrences() > 0 && opt->length() > 0 && (prog = opt->c_str()))
path = findProgramByName(prog);
if (path.empty() && envVar && (prog = getenv(envVar)) && prog[0] != '\0')
path = findProgramByName(prog);
if (path.empty())
path = findProgramByName(name);
if (path.empty()) {
error(Loc(), "failed to locate %s", name);
fatal();
}
return path;
}
std::string getProgram(const char* name, const char* envVar)
{
return getProgram(name, NULL, envVar);
}
std::string getGcc()
{
#if defined(__FreeBSD__) && __FreeBSD__ >= 10
// Default compiler on FreeBSD 10 is clang
return getProgram("clang", &gcc, "CC");
#else
return getProgram("gcc", &gcc, "CC");
#endif
}
std::string getArchiver()
{
return getProgram("ar", &ar);
}