From ceafbc8f55e00345a85d5e6674d3339a45cbbf76 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Thu, 19 Mar 2009 08:01:45 +0000 Subject: [PATCH] Driver: Executing piped jobs with a single command is easy. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67295 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/Compilation.h | 5 +++ lib/Driver/Compilation.cpp | 53 +++++++++++++++++------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/include/clang/Driver/Compilation.h b/include/clang/Driver/Compilation.h index eaa57914b8..1a6d7b02ea 100644 --- a/include/clang/Driver/Compilation.h +++ b/include/clang/Driver/Compilation.h @@ -109,6 +109,11 @@ private: void PrintJob(llvm::raw_ostream &OS, const Job &J, const char *Terminator, bool Quote) const; + /// ExecuteCommand - Execute an actual command. + /// + /// \return The result code of the subprocess. + int ExecuteCommand(const Command &C) const; + /// ExecuteJob - Execute a single job. /// /// \return The accumulated result code of the job. diff --git a/lib/Driver/Compilation.cpp b/lib/Driver/Compilation.cpp index 5234165ab5..4b323a6457 100644 --- a/lib/Driver/Compilation.cpp +++ b/lib/Driver/Compilation.cpp @@ -108,32 +108,39 @@ bool Compilation::CleanupFileList(const ArgStringList &Files, return Success; } +int Compilation::ExecuteCommand(const Command &C) const { + llvm::sys::Path Prog(C.getExecutable()); + const char **Argv = new const char*[C.getArguments().size() + 2]; + Argv[0] = C.getExecutable(); + std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1); + Argv[C.getArguments().size() + 1] = 0; + + if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v)) + PrintJob(llvm::errs(), C, "\n", false); + + std::string Error; + int Res = + llvm::sys::Program::ExecuteAndWait(Prog, Argv, + /*env*/0, /*redirects*/0, + /*secondsToWait*/0, /*memoryLimit*/0, + &Error); + if (!Error.empty()) { + assert(Res && "Error string set with 0 result code!"); + getDriver().Diag(clang::diag::err_drv_command_failure) << Error; + } + + delete[] Argv; + return Res; +} + int Compilation::ExecuteJob(const Job &J) const { if (const Command *C = dyn_cast(&J)) { - llvm::sys::Path Prog(C->getExecutable()); - const char **Argv = new const char*[C->getArguments().size() + 2]; - Argv[0] = C->getExecutable(); - std::copy(C->getArguments().begin(), C->getArguments().end(), Argv+1); - Argv[C->getArguments().size() + 1] = 0; - - if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v)) - PrintJob(llvm::errs(), J, "\n", false); - - std::string Error; - int Res = - llvm::sys::Program::ExecuteAndWait(Prog, Argv, - /*env*/0, /*redirects*/0, - /*secondsToWait*/0, /*memoryLimit*/0, - &Error); - if (!Error.empty()) { - assert(Res && "Error string set with 0 result code!"); - getDriver().Diag(clang::diag::err_drv_command_failure) << Error; - } - - delete[] Argv; - return Res; + return ExecuteCommand(*C); } else if (const PipedJob *PJ = dyn_cast(&J)) { - (void) PJ; + // Piped commands with a single job are easy. + if (PJ->size() == 1) + return ExecuteCommand(**PJ->begin()); + getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe"; return 1; } else { -- 2.40.0