]> git.tdb.fi Git - builder.git/commitdiff
Utility function for capturing command output
authorMikko Rasa <tdb@tdb.fi>
Sat, 14 Jul 2012 19:18:15 +0000 (22:18 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jul 2012 14:05:48 +0000 (17:05 +0300)
source/externaltask.cpp
source/externaltask.h
source/gnucxxcompiler.cpp
source/packagemanager.cpp

index 0da81e519cc05eba437855e02da99d81722302d1..fea331a72207004e14acd85b5ebfb94fb910a840 100644 (file)
@@ -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();
+}
index 8127c75762459f9a4c73f1c532412a7567c45281..d997f9a7b4891ad473aea0d2cfdffb8bcd53bef5 100644 (file)
@@ -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
index 18c6728464d7d34f6fd7d2f0a68a7485acf724b7..05782e4ecb3ce09d0586b8332085816184121108 100644 (file)
@@ -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
index 68189205abb8022ba1f272c0a066e4b1a210fa1f..5cc768a648c6d607507342cf977b2879191bd416 100644 (file)
@@ -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)