From 243083b88c38dfd16cbf0b034c5693946f2605a6 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Mon, 11 Feb 2013 07:22:19 +0100 Subject: [PATCH] Factored out tool invocation code into separate file. --- CMakeLists.txt | 2 ++ driver/linker.cpp | 66 ++++------------------------------------------- driver/linker.h | 13 ---------- driver/tool.cpp | 47 +++++++++++++++++++++++++++++++++ driver/tool.h | 24 +++++++++++++++++ 5 files changed, 78 insertions(+), 74 deletions(-) create mode 100644 driver/tool.cpp create mode 100644 driver/tool.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 280979f2..afb801e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/driver/linker.cpp b/driver/linker.cpp index 31351fc1..aa259032 100644 --- a/driver/linker.cpp +++ b/driver/linker.cpp @@ -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 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 args, bool verbose = false) -{ - // Construct real argument list. - // First entry is the tool itself, last entry must be NULL. - std::vector realargs; - realargs.reserve(args.size() + 2); - realargs.push_back(tool.c_str()); - for (std::vector::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) diff --git a/driver/linker.h b/driver/linker.h index 404c3ddc..38fa66d8 100644 --- a/driver/linker.h +++ b/driver/linker.h @@ -16,22 +16,9 @@ #define LDC_DRIVER_LINKER_H #include "llvm/Support/CommandLine.h" -#include extern llvm::cl::opt 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& MV); - /** * Link an executable only from object files. * @param argv0 the argv[0] value as passed to main diff --git a/driver/tool.cpp b/driver/tool.cpp new file mode 100644 index 00000000..25e23906 --- /dev/null +++ b/driver/tool.cpp @@ -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 const & args, bool verbose) +{ + // Construct real argument list. + // First entry is the tool itself, last entry must be NULL. + std::vector realargs; + realargs.reserve(args.size() + 2); + realargs.push_back(tool.c_str()); + for (std::vector::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; +} diff --git a/driver/tool.h b/driver/tool.h new file mode 100644 index 00000000..e3d9e78a --- /dev/null +++ b/driver/tool.h @@ -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 +#include + +int executeToolAndWait(llvm::sys::Path tool, std::vector const & args, bool verbose = false); + +#endif