From 06678c3bb61404c483e4b9c39eaa7e7fb3f40c62 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 14 Jul 2012 22:18:15 +0300 Subject: [PATCH] Utility function for capturing command output --- source/externaltask.cpp | 13 +++++++++++++ source/externaltask.h | 4 ++++ source/gnucxxcompiler.cpp | 12 ++++-------- source/packagemanager.cpp | 12 +----------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/source/externaltask.cpp b/source/externaltask.cpp index 0da81e5..fea331a 100644 --- a/source/externaltask.cpp +++ b/source/externaltask.cpp @@ -138,3 +138,16 @@ void ExternalTask::set_stderr(Destination d) { stderr_dest = d; } + +string ExternalTask::run_and_capture_output(const Arguments &argv, const FS::Path &wd) +{ + ExternalTask task(argv, wd); + task.set_stdout(CAPTURE); + task.set_stderr(IGNORE); + task.start(); + Task::Status status; + while((status=task.check())==RUNNING) ; + if(status!=SUCCESS) + throw runtime_error(format("%s failed", argv.front())); + return task.get_output(); +} diff --git a/source/externaltask.h b/source/externaltask.h index 8127c75..d997f9a 100644 --- a/source/externaltask.h +++ b/source/externaltask.h @@ -49,6 +49,10 @@ public: void set_stdout(Destination); void set_stderr(Destination); const std::string &get_output() const { return output; } + + /** Executes a command and captures its output. Stderr is ignored, but if + the command exits with a nonzero status, an exception is thrown. */ + static std::string run_and_capture_output(const Arguments &, const Msp::FS::Path & = Msp::FS::Path()); }; #endif diff --git a/source/gnucxxcompiler.cpp b/source/gnucxxcompiler.cpp index 18c6728..05782e4 100644 --- a/source/gnucxxcompiler.cpp +++ b/source/gnucxxcompiler.cpp @@ -21,15 +21,9 @@ GnuCxxCompiler::GnuCxxCompiler(Builder &b, const Architecture &a): argv.push_back(executable->get_path().str()); argv.push_back("-dumpversion"); builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end()))); - ExternalTask task(argv); - task.set_stdout(ExternalTask::CAPTURE); - task.set_stderr(ExternalTask::IGNORE); - task.start(); - Task::Status status; - while((status=task.check())==Task::RUNNING) ; - if(status==Task::SUCCESS) + try { - string cxx_ver = strip(task.get_output()); + string cxx_ver = strip(ExternalTask::run_and_capture_output(argv)); FS::Path cxx_path = FS::Path("/usr/include/c++")/cxx_ver; if(FS::is_dir(cxx_path)) { @@ -37,6 +31,8 @@ GnuCxxCompiler::GnuCxxCompiler(Builder &b, const Architecture &a): system_path.push_back(cxx_path); } } + catch(const runtime_error &) + { } } Target *GnuCxxCompiler::create_source(const Component &comp, const FS::Path &path) const diff --git a/source/packagemanager.cpp b/source/packagemanager.cpp index 6818920..5cc768a 100644 --- a/source/packagemanager.cpp +++ b/source/packagemanager.cpp @@ -102,17 +102,7 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what) builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end()))); - ExternalTask task(argv); - task.set_stdout(ExternalTask::CAPTURE); - task.set_stderr(ExternalTask::IGNORE); - task.start(); - Task::Status status; - while((status=task.check())==Task::RUNNING) - Time::sleep(10*Time::msec); - if(status==Task::ERROR) - throw runtime_error(format("pkg-config for package %s failed", pkg)); - - return task.get_output(); + return ExternalTask::run_and_capture_output(argv); } FS::Path PackageManager::get_package_location(const string &name) -- 2.45.2