]> git.tdb.fi Git - builder.git/blobdiff - source/packagemanager.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / packagemanager.cpp
index 93fbbc9c0ed204b56b11fea1dde7a9f33dae9099..f54606f42f1639e09b0e91e11a2b02ecbfd15593 100644 (file)
@@ -1,11 +1,12 @@
 #include <cstdlib>
+#include <msp/core/algorithm.h>
 #include <msp/fs/dir.h>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
-#include <msp/time/units.h>
+#include <msp/time/timedelta.h>
 #include <msp/time/utils.h>
 #include "binarypackage.h"
 #include "builder.h"
@@ -25,8 +26,8 @@ PackageManager::PackageManager(Builder &b):
 
 PackageManager::~PackageManager()
 {
-       for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
-               delete i->second;
+       for(const auto &kvp: packages)
+               delete kvp.second;
 }
 
 void PackageManager::append_package_path(const FS::Path &p)
@@ -61,6 +62,15 @@ void PackageManager::add_package(Package *pkg)
        packages.insert(PackageMap::value_type(pkg->get_name(), pkg));
 }
 
+Package *PackageManager::get_package(const string &name) const
+{
+       PackageMap::const_iterator i = packages.find(name);
+       if(i!=packages.end())
+               return i->second;
+
+       return 0;
+}
+
 Package &PackageManager::get_main_package() const
 {
        if(!main_pkg)
@@ -73,9 +83,8 @@ Package *PackageManager::find_package(const string &name)
        if(not_found.count(name))
                return 0;
 
-       PackageMap::iterator i = packages.find(name);
-       if(i!=packages.end())
-               return i->second;
+       if(Package *pkg = get_package(name))
+               return pkg;
 
        if(!no_externals)
        {
@@ -83,7 +92,7 @@ Package *PackageManager::find_package(const string &name)
                if(!path.empty())
                {
                        builder.load_build_file(path/"Build");
-                       i = packages.find(name);
+                       PackageMap::iterator i = packages.find(name);
                        if(i!=packages.end())
                                return i->second;
                }
@@ -93,23 +102,23 @@ Package *PackageManager::find_package(const string &name)
        if(!path.empty())
        {
                builder.load_build_file(path);
-               i = packages.find(name);
-               if(i!=packages.end())
-                       return i->second;
+               if(Package *pkg = get_package(name))
+                       return pkg;
        }
 
        try
        {
                // Package source not found - create a binary package
                string flags_str = run_pkgconfig(name, "flags");
-               vector<string> flags = split(flags_str);
-               Package *pkg = BinaryPackage::from_flags(builder, name, flags);
+               BinaryPackage::Flags flags = split(flags_str);
+               flags_str = run_pkgconfig(name, "staticflags");
+               BinaryPackage::Flags static_flags = split(flags_str);
+               Package *pkg = BinaryPackage::from_flags(builder, name, flags, static_flags);
                packages.insert(PackageMap::value_type(name, pkg));
                return pkg;
        }
        catch(...)
        {
-               builder.problem(name, "not found");
                not_found.insert(name);
                return 0;
        }
@@ -117,6 +126,7 @@ Package *PackageManager::find_package(const string &name)
 
 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
 {
+#ifndef _WIN32
        if(!env_set)
        {
                const FS::Path &prefix = builder.get_prefix();
@@ -126,10 +136,7 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
                        if(const char *pcp = getenv("PKG_CONFIG_PATH"))
                        {
                                vector<string> path = split(pcp, ':');
-                               bool found = false;
-                               for(vector<string>::const_iterator i=path.begin(); (!found && i!=path.end()); ++i)
-                                       found = (*i==pcdir.str());
-                               if(!found)
+                               if(!any_equals(path, pcdir.str()))
                                {
                                        path.push_back(pcdir.str());
                                        setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
@@ -144,10 +151,12 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
        argv.push_back("pkg-config");
        if(what=="cflags" || what=="libs")
                argv.push_back("--"+what);
-       else if(what=="flags")
+       else if(what=="flags" || what=="staticflags")
        {
                argv.push_back("--cflags");
                argv.push_back("--libs");
+               if(what=="staticflags")
+                       argv.push_back("--static");
        }
        else
                argv.push_back("--variable="+what);
@@ -156,6 +165,11 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
        builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
 
        return ExternalTask::run_and_capture_output(argv);
+#else
+       (void)pkg;
+       (void)what;
+       return string();
+#endif
 }
 
 FS::Path PackageManager::get_package_location(const string &name)
@@ -174,14 +188,13 @@ FS::Path PackageManager::get_package_location(const string &name)
 
        if(pkg_dirs.empty())
        {
-               for(SearchPath::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
+               for(const FS::Path &p: pkg_path)
                {
-                       builder.get_logger().log("files", format("Traversing %s", *i));
-                       list<string> files = list_files(*i);
+                       builder.get_logger().log("files", format("Traversing %s", p));
                        unsigned count = 0;
-                       for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
+                       for(const string &f: list_files(p))
                        {
-                               FS::Path full = *i / *j;
+                               FS::Path full = p/f;
                                if(FS::exists(full/"Build"))
                                {
                                        pkg_dirs.push_back(full);
@@ -189,22 +202,22 @@ FS::Path PackageManager::get_package_location(const string &name)
                                }
                        }
 
-                       builder.get_logger().log("packagemgr", format("%d source packages found in %s", count, *i));
+                       builder.get_logger().log("packagemgr", format("%d source packages found in %s", count, p));
                }
 
                builder.get_logger().log("packagemgr", format("%d source packages found", pkg_dirs.size()));
        }
 
        bool msp = !name.compare(0, 3, "msp");
-       for(SearchPath::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
+       for(const FS::Path &p: pkg_dirs)
        {
-               string base = FS::basename(*i);
+               string base = FS::basename(p);
                unsigned dash = base.rfind('-');
 
                if(!base.compare(0, dash, name))
-                       return *i;
+                       return p;
                else if(msp && !base.compare(0, dash, name, 3, string::npos))
-                       return *i;
+                       return p;
        }
 
        return FS::Path();
@@ -216,24 +229,27 @@ FS::Path PackageManager::get_binary_package_file(const string &name)
 
        if(binpkg_files.empty())
        {
-               for(list<FS::Path>::const_iterator i=binpkg_path.begin(); i!=binpkg_path.end(); ++i)
+               for(const FS::Path &p: binpkg_path)
                {
-                       builder.get_logger().log("files", format("Traversing %s", *i));
-                       list<string> files = list_filtered(*i, "\\.bpk$");
-                       for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
-                               binpkg_files.push_back(*i / *j);
-                       builder.get_logger().log("packagemgr", format("%d binary packages found in %s", files.size(), *i));
+                       builder.get_logger().log("files", format("Traversing %s", p));
+                       vector<string> files = list_filtered(p, "\\.bpk$");
+                       for(const string &f: files)
+                               binpkg_files.push_back(p/f);
+                       builder.get_logger().log("packagemgr", format("%d binary packages found in %s", files.size(), p));
                }
 
                builder.get_logger().log("packagemgr", format("%d binary packages found", binpkg_files.size()));
        }
 
-       for(SearchPath::const_iterator i=binpkg_files.begin(); i!=binpkg_files.end(); ++i)
-       {
-               string base = FS::basepart(FS::basename(*i));
-               if(base==name)
-                       return *i;
-       }
+       auto i = find_if(binpkg_files, [&name](const FS::Path &p){ return FS::basepart(FS::basename(p))==name; });
+       if(i!=binpkg_files.end())
+               return *i;
 
        return FS::Path();
 }
+
+void PackageManager::save_all_caches() const
+{
+       for(const auto &kvp: packages)
+               kvp.second->save_caches();
+}