Factored out tool invocation code into separate file.

This commit is contained in:
David Nadlinger
2013-02-11 07:22:19 +01:00
parent 8b71c78571
commit 243083b88c
5 changed files with 78 additions and 74 deletions

View File

@@ -151,6 +151,7 @@ set(DRV_SRC
driver/cl_options.cpp
driver/configfile.cpp
driver/toobj.cpp
driver/tool.cpp
driver/linker.cpp
driver/main.cpp
)
@@ -159,6 +160,7 @@ set(DRV_HDR
driver/cl_options.h
driver/configfile.h
driver/toobj.h
driver/tool.h
)
# exclude idgen and impcnvgen and generated sources, just in case
list(REMOVE_ITEM FE_SRC

View File

@@ -12,6 +12,7 @@
#include "module.h"
#include "root.h"
#include "driver/cl_options.h"
#include "driver/tool.h"
#include "gen/llvm.h"
#include "gen/logger.h"
#include "gen/optimizer.h"
@@ -40,63 +41,6 @@ static bool endsWith(const std::string &str, const std::string &end)
return (str.length() >= end.length() && std::equal(end.rbegin(), end.rend(), str.rbegin()));
}
typedef std::vector<llvm::Module*> Module_vector;
void linkModules(llvm::Module* dst, const Module_vector& MV)
{
if (MV.empty())
return;
llvm::Linker linker("ldc", dst);
std::string err;
for (Module_vector::const_iterator i=MV.begin(); i!=MV.end(); ++i)
{
if (!linker.LinkInModule(*i, &err))
{
error("%s", err.c_str());
fatal();
}
}
}
//////////////////////////////////////////////////////////////////////////////
static int ExecuteToolAndWait(llvm::sys::Path tool, std::vector<std::string> args, bool verbose = false)
{
// Construct real argument list.
// First entry is the tool itself, last entry must be NULL.
std::vector<const char *> realargs;
realargs.reserve(args.size() + 2);
realargs.push_back(tool.c_str());
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
{
realargs.push_back((*it).c_str());
}
realargs.push_back(NULL);
// Print command line if requested
if (verbose)
{
// Print it
for (size_t i = 0; i < realargs.size()-1; i++)
printf("%s ", realargs[i]);
printf("\n");
fflush(stdout);
}
// Execute tool.
std::string errstr;
if (int status = llvm::sys::Program::ExecuteAndWait(tool, &realargs[0], NULL, NULL, 0, 0, &errstr))
{
error("%s failed with status: %d", tool.c_str(), status);
if (!errstr.empty())
error("message: %s", errstr.c_str());
return status;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
static void CreateDirectoryOnDisk(llvm::StringRef fileName)
@@ -266,7 +210,7 @@ static int linkObjToBinaryGcc(bool sharedLib)
logstr << "\n"; // FIXME where's flush ?
// try to call linker
return ExecuteToolAndWait(gcc, args, !quiet || global.params.verbose);
return executeToolAndWait(gcc, args, !quiet || global.params.verbose);
}
//////////////////////////////////////////////////////////////////////////////
@@ -372,7 +316,7 @@ static int linkObjToBinaryWin(bool sharedLib)
logstr << "\n"; // FIXME where's flush ?
// try to call linker
return ExecuteToolAndWait(tool, args, !quiet || global.params.verbose);
return executeToolAndWait(tool, args, !quiet || global.params.verbose);
}
//////////////////////////////////////////////////////////////////////////////
@@ -449,7 +393,7 @@ void createStaticLibrary()
CreateDirectoryOnDisk(libName);
// try to call archiver
ExecuteToolAndWait(tool, args, !quiet || global.params.verbose);
executeToolAndWait(tool, args, !quiet || global.params.verbose);
}
//////////////////////////////////////////////////////////////////////////////
@@ -473,7 +417,7 @@ int runExecutable()
assert(gExePath.isValid());
// Run executable
int status = ExecuteToolAndWait(gExePath, opts::runargs, !quiet || global.params.verbose);
int status = executeToolAndWait(gExePath, opts::runargs, !quiet || global.params.verbose);
if (status < 0)
{
#if defined(_MSC_VER)

View File

@@ -16,22 +16,9 @@
#define LDC_DRIVER_LINKER_H
#include "llvm/Support/CommandLine.h"
#include <vector>
extern llvm::cl::opt<bool> quiet;
namespace llvm
{
class Module;
}
/**
* Links the modules given in MV in to dst.
* @param dst Destination module.
* @param MV Vector of modules to link in to destination.
*/
void linkModules(llvm::Module* dst, const std::vector<llvm::Module*>& MV);
/**
* Link an executable only from object files.
* @param argv0 the argv[0] value as passed to main

47
driver/tool.cpp Normal file
View File

@@ -0,0 +1,47 @@
//===-- tool.cpp ----------------------------------------------------------===//
//
// LDC the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "driver/tool.h"
#include "mars.h"
#include "llvm/Support/Program.h"
int executeToolAndWait(llvm::sys::Path tool, std::vector<std::string> const & args, bool verbose)
{
// Construct real argument list.
// First entry is the tool itself, last entry must be NULL.
std::vector<const char *> realargs;
realargs.reserve(args.size() + 2);
realargs.push_back(tool.c_str());
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
{
realargs.push_back((*it).c_str());
}
realargs.push_back(NULL);
// Print command line if requested
if (verbose)
{
// Print it
for (size_t i = 0; i < realargs.size()-1; i++)
printf("%s ", realargs[i]);
printf("\n");
fflush(stdout);
}
// Execute tool.
std::string errstr;
if (int status = llvm::sys::Program::ExecuteAndWait(tool, &realargs[0], NULL, NULL, 0, 0, &errstr))
{
error("%s failed with status: %d", tool.c_str(), status);
if (!errstr.empty())
error("message: %s", errstr.c_str());
return status;
}
return 0;
}

24
driver/tool.h Normal file
View File

@@ -0,0 +1,24 @@
//===-- driver/tool.h - External tool invocation helpers ---------*- C++ -*-===//
//
// LDC the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Functionaliy for invoking external tools executables, such as the system
// assembler, linker, ...
//
//===----------------------------------------------------------------------===//
#ifndef LDC_DRIVER_TOOL_H
#define LDC_DRIVER_TOOL_H
#include "llvm/Support/PathV1.h"
#include <vector>
#include <string>
int executeToolAndWait(llvm::sys::Path tool, std::vector<std::string> const & args, bool verbose = false);
#endif