diff --git a/driver/linker.cpp b/driver/linker.cpp index f9d6db08b5..52fa84abf6 100644 --- a/driver/linker.cpp +++ b/driver/linker.cpp @@ -19,7 +19,6 @@ #include "gen/llvm.h" #include "gen/logger.h" #include "gen/optimizer.h" -#include "gen/programs.h" #include "llvm/ADT/Triple.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Linker/Linker.h" diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 27f90dac89..d8f6035ca6 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -16,7 +16,6 @@ #include "gen/irstate.h" #include "gen/logger.h" #include "gen/optimizer.h" -#include "gen/programs.h" #include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/Verifier.h" #if LDC_LLVM_VER >= 309 diff --git a/driver/tool.cpp b/driver/tool.cpp index c409210581..9d4487fa84 100644 --- a/driver/tool.cpp +++ b/driver/tool.cpp @@ -10,6 +10,7 @@ #include "driver/tool.h" #include "mars.h" #include "driver/exe_path.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" @@ -18,16 +19,79 @@ #include #endif +////////////////////////////////////////////////////////////////////////////// + +static llvm::cl::opt + gcc("gcc", llvm::cl::desc("GCC to use for assembling and linking"), + llvm::cl::Hidden, llvm::cl::ZeroOrMore); + +static llvm::cl::opt ar("ar", llvm::cl::desc("Archiver"), + llvm::cl::Hidden, llvm::cl::ZeroOrMore); + +////////////////////////////////////////////////////////////////////////////// + +namespace { + +std::string findProgramByName(llvm::StringRef name) { +#if LDC_LLVM_VER >= 306 + llvm::ErrorOr res = llvm::sys::findProgramByName(name); + return res ? res.get() : std::string(); +#else + return llvm::sys::FindProgramByName(name); +#endif +} + +std::string getProgram(const char *name, + const llvm::cl::opt *opt = nullptr, + const char *envVar = nullptr) { + std::string path; + const char *prog = nullptr; + + if (opt && !opt->empty()) { + path = findProgramByName(opt->c_str()); + } + + 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; +} + +} // anonymous namespace + +//////////////////////////////////////////////////////////////////////////////// + +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); } + +//////////////////////////////////////////////////////////////////////////////// + int executeToolAndWait(const std::string &tool_, std::vector const &args, bool verbose) { - auto fullToolOrError = llvm::sys::findProgramByName(tool_); - if (fullToolOrError.getError()) { + const auto tool = findProgramByName(tool_); + if (tool.empty()) { error(Loc(), "failed to locate binary %s", tool_.c_str()); return -1; } - const auto tool = std::move(*fullToolOrError); - // Construct real argument list. // First entry is the tool itself, last entry must be NULL. std::vector realargs; @@ -61,7 +125,7 @@ int executeToolAndWait(const std::string &tool_, return 0; } -////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// #ifdef _WIN32 @@ -139,8 +203,8 @@ int executeAndWait(const char *commandLine) { #endif // according to MSDN, only CreateProcessW (unicode) may modify the passed // command line - if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, - NULL, NULL, &si, &pi)) { + if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, + &pi)) { exitCode = -1; } else { if (WaitForSingleObject(pi.hProcess, INFINITE) != 0 || diff --git a/driver/tool.h b/driver/tool.h index c96f851abc..b966e40e92 100644 --- a/driver/tool.h +++ b/driver/tool.h @@ -18,6 +18,9 @@ #include #include +std::string getGcc(); +std::string getArchiver(); + int executeToolAndWait(const std::string &tool, std::vector const &args, bool verbose = false); diff --git a/gen/modules.cpp b/gen/modules.cpp index df2dd7dbfd..d660bfb77c 100644 --- a/gen/modules.cpp +++ b/gen/modules.cpp @@ -31,7 +31,6 @@ #include "gen/logger.h" #include "gen/moduleinfo.h" #include "gen/optimizer.h" -#include "gen/programs.h" #include "gen/runtime.h" #include "gen/structs.h" #include "gen/tollvm.h" diff --git a/gen/programs.cpp b/gen/programs.cpp deleted file mode 100644 index c03d814bc5..0000000000 --- a/gen/programs.cpp +++ /dev/null @@ -1,71 +0,0 @@ -//===-- 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 - gcc("gcc", cl::desc("GCC to use for assembling and linking"), cl::Hidden, - cl::ZeroOrMore); - -static cl::opt ar("ar", cl::desc("Archiver"), cl::Hidden, - cl::ZeroOrMore); - -static std::string findProgramByName(llvm::StringRef name) { -#if LDC_LLVM_VER >= 306 - llvm::ErrorOr res = llvm::sys::findProgramByName(name); - return res ? res.get() : std::string(); -#else - return llvm::sys::FindProgramByName(name); -#endif -} - -static std::string getProgram(const char *name, const cl::opt *opt, - const char *envVar = nullptr) { - std::string path; - const char *prog = nullptr; - - if (opt && !opt->empty()) { - path = findProgramByName(opt->c_str()); - } - - 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, nullptr, 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); } diff --git a/gen/programs.h b/gen/programs.h deleted file mode 100644 index c26ac70664..0000000000 --- a/gen/programs.h +++ /dev/null @@ -1,24 +0,0 @@ -//===-- gen/programs.h - External tool discovery ----------------*- C++ -*-===// -// -// LDC – the LLVM D compiler -// -// This file is distributed under the BSD-style LDC license. See the LICENSE -// file for details. -// -//===----------------------------------------------------------------------===// -// -// Functions for discovering the external tools used for linking, etc. -// -//===----------------------------------------------------------------------===// - -#ifndef LDC_GEN_PROGRAMS_H -#define LDC_GEN_PROGRAMS_H - -#include - -std::string getProgram(const char *name, const char *envVar = nullptr); - -std::string getGcc(); -std::string getArchiver(); - -#endif