Integrate module gen/programs into driver/tool

This commit is contained in:
Martin 2017-03-13 23:31:32 +01:00
parent 0842277572
commit 95c1d38e2e
7 changed files with 74 additions and 105 deletions

View file

@ -19,7 +19,6 @@
#include "gen/llvm.h" #include "gen/llvm.h"
#include "gen/logger.h" #include "gen/logger.h"
#include "gen/optimizer.h" #include "gen/optimizer.h"
#include "gen/programs.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/IRReader/IRReader.h" #include "llvm/IRReader/IRReader.h"
#include "llvm/Linker/Linker.h" #include "llvm/Linker/Linker.h"

View file

@ -16,7 +16,6 @@
#include "gen/irstate.h" #include "gen/irstate.h"
#include "gen/logger.h" #include "gen/logger.h"
#include "gen/optimizer.h" #include "gen/optimizer.h"
#include "gen/programs.h"
#include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/AssemblyAnnotationWriter.h"
#include "llvm/IR/Verifier.h" #include "llvm/IR/Verifier.h"
#if LDC_LLVM_VER >= 309 #if LDC_LLVM_VER >= 309

View file

@ -10,6 +10,7 @@
#include "driver/tool.h" #include "driver/tool.h"
#include "mars.h" #include "mars.h"
#include "driver/exe_path.h" #include "driver/exe_path.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
@ -18,16 +19,79 @@
#include <Windows.h> #include <Windows.h>
#endif #endif
//////////////////////////////////////////////////////////////////////////////
static llvm::cl::opt<std::string>
gcc("gcc", llvm::cl::desc("GCC to use for assembling and linking"),
llvm::cl::Hidden, llvm::cl::ZeroOrMore);
static llvm::cl::opt<std::string> 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<std::string> 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<std::string> *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_, int executeToolAndWait(const std::string &tool_,
std::vector<std::string> const &args, bool verbose) { std::vector<std::string> const &args, bool verbose) {
auto fullToolOrError = llvm::sys::findProgramByName(tool_); const auto tool = findProgramByName(tool_);
if (fullToolOrError.getError()) { if (tool.empty()) {
error(Loc(), "failed to locate binary %s", tool_.c_str()); error(Loc(), "failed to locate binary %s", tool_.c_str());
return -1; return -1;
} }
const auto tool = std::move(*fullToolOrError);
// Construct real argument list. // Construct real argument list.
// First entry is the tool itself, last entry must be NULL. // First entry is the tool itself, last entry must be NULL.
std::vector<const char *> realargs; std::vector<const char *> realargs;
@ -61,7 +125,7 @@ int executeToolAndWait(const std::string &tool_,
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#ifdef _WIN32 #ifdef _WIN32
@ -139,8 +203,8 @@ int executeAndWait(const char *commandLine) {
#endif #endif
// according to MSDN, only CreateProcessW (unicode) may modify the passed // according to MSDN, only CreateProcessW (unicode) may modify the passed
// command line // command line
if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si,
NULL, NULL, &si, &pi)) { &pi)) {
exitCode = -1; exitCode = -1;
} else { } else {
if (WaitForSingleObject(pi.hProcess, INFINITE) != 0 || if (WaitForSingleObject(pi.hProcess, INFINITE) != 0 ||

View file

@ -18,6 +18,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
std::string getGcc();
std::string getArchiver();
int executeToolAndWait(const std::string &tool, int executeToolAndWait(const std::string &tool,
std::vector<std::string> const &args, std::vector<std::string> const &args,
bool verbose = false); bool verbose = false);

View file

@ -31,7 +31,6 @@
#include "gen/logger.h" #include "gen/logger.h"
#include "gen/moduleinfo.h" #include "gen/moduleinfo.h"
#include "gen/optimizer.h" #include "gen/optimizer.h"
#include "gen/programs.h"
#include "gen/runtime.h" #include "gen/runtime.h"
#include "gen/structs.h" #include "gen/structs.h"
#include "gen/tollvm.h" #include "gen/tollvm.h"

View file

@ -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<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(llvm::StringRef name) {
#if LDC_LLVM_VER >= 306
llvm::ErrorOr<std::string> 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<std::string> *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); }

View file

@ -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 <string>
std::string getProgram(const char *name, const char *envVar = nullptr);
std::string getGcc();
std::string getArchiver();
#endif