+#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"
using namespace std;
using namespace Msp;
-PackageManager::PackageManager(Builder &b):
- builder(b),
- no_externals(false)
+PackageManager::~PackageManager()
{
- pkg_path.push_back(builder.get_cwd()/".");
- pkg_path.push_back(builder.get_cwd()/"..");
+ for(const auto &kvp: packages)
+ delete kvp.second;
}
-PackageManager::~PackageManager()
+void PackageManager::append_package_path(const FS::Path &p)
+{
+ pkg_path.push_back(p);
+}
+
+void PackageManager::append_binary_package_path(const FS::Path &p)
{
- for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
- delete i->second;
+ binpkg_path.push_back(p);
}
void PackageManager::set_no_externals(bool x)
void PackageManager::add_package(Package *pkg)
{
- PackageMap::iterator i = packages.find(pkg->get_name());
+ auto i = packages.find(pkg->get_name());
if(i!=packages.end())
{
if(i->second!=pkg)
throw logic_error("Package is already managed");
}
- packages.insert(PackageMap::value_type(pkg->get_name(), pkg));
+ if(packages.empty())
+ main_pkg = pkg;
+
+ packages.insert({ pkg->get_name(), pkg });
}
-Package *PackageManager::find_package(const string &name)
+Package *PackageManager::get_package(const string &name) const
{
- PackageMap::iterator i = packages.find(format("%s/%s", name, builder.get_current_arch().get_system()));
- if(i==packages.end())
- i = packages.find(name);
+ auto i = packages.find(name);
if(i!=packages.end())
return i->second;
+ return 0;
+}
+
+Package &PackageManager::get_main_package() const
+{
+ if(!main_pkg)
+ throw logic_error("No packages");
+ return *main_pkg;
+}
+
+Package *PackageManager::find_package(const string &name)
+{
+ if(not_found.count(name))
+ return 0;
+
+ if(Package *pkg = get_package(name))
+ return pkg;
+
if(!no_externals)
{
FS::Path path = get_package_location(name);
- if(!path.empty() && !builder.load_build_file(path/"Build"))
+ if(!path.empty())
{
- i = packages.find(name);
+ builder.load_build_file(path/"Build");
+ auto i = packages.find(name);
if(i!=packages.end())
return i->second;
}
}
- Package *pkg = 0;
+ FS::Path path = get_binary_package_file(name);
+ if(!path.empty())
+ {
+ builder.load_build_file(path);
+ 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);
- 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({ name, pkg });
+ return pkg;
}
catch(...)
{
- builder.problem(name, "not found");
+ not_found.insert(name);
+ return 0;
}
-
- packages.insert(PackageMap::value_type(name, pkg));
-
- return pkg;
}
string PackageManager::run_pkgconfig(const string &pkg, const string &what)
{
+#ifndef _WIN32
+ if(!env_set)
+ {
+ const FS::Path &prefix = builder.get_prefix();
+ if(prefix.str()!="/usr")
+ {
+ FS::Path pcdir = prefix/"lib/pkgconfig";
+ if(const char *pcp = getenv("PKG_CONFIG_PATH"))
+ {
+ vector<string> path = split(pcp, ':');
+ if(!any_equals(path, pcdir.str()))
+ {
+ path.push_back(pcdir.str());
+ setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
+ }
+ }
+ else
+ setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
+ }
+ }
+
ExternalTask::Arguments argv;
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);
argv.push_back(pkg);
- 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));
+ builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
- return task.get_output();
+ return ExternalTask::run_and_capture_output(argv);
+#else
+ (void)pkg;
+ (void)what;
+ return string();
+#endif
}
FS::Path PackageManager::get_package_location(const string &name)
{
- builder.get_logger().log("packagemgr", format("Looking for package %s", name));
+ builder.get_logger().log("packagemgr", "Looking for source package %s", name);
try
{
// Try to get source directory with pkgconfig
string srcdir = strip(run_pkgconfig(name, "source"));
- if(!srcdir.empty())
+ if(!srcdir.empty() && FS::exists(FS::Path(srcdir)/"Build"))
return srcdir;
}
catch(...)
if(pkg_dirs.empty())
{
- for(list<FS::Path>::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
+ for(const FS::Path &p: pkg_path)
{
- list<string> files = list_files(*i);
- for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
+ builder.get_logger().log("files", "Traversing %s", p);
+ unsigned count = 0;
+ 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);
+ ++count;
+ }
}
+
+ builder.get_logger().log("packagemgr", "%d source packages found in %s", count, p);
}
- builder.get_logger().log("packagemgr", format("%d packages found in path", pkg_dirs.size()));
+
+ builder.get_logger().log("packagemgr", "%d source packages found", pkg_dirs.size());
}
bool msp = !name.compare(0, 3, "msp");
- for(list<FS::Path>::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
+ for(const FS::Path &p: pkg_dirs)
{
- string base = basename(*i);
+ string base = FS::basename(p);
unsigned dash = base.rfind('-');
if(!base.compare(0, dash, name))
- return *i;
- else if(msp && !base.compare(0, dash-3, name, 3, string::npos))
- return *i;
+ return p;
+ else if(msp && !base.compare(0, dash, name, 3, string::npos))
+ return p;
}
return FS::Path();
}
+
+FS::Path PackageManager::get_binary_package_file(const string &name)
+{
+ builder.get_logger().log("packagemgr", "Looking for binary package %s", name);
+
+ if(binpkg_files.empty())
+ {
+ for(const FS::Path &p: binpkg_path)
+ {
+ builder.get_logger().log("files", "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", "%d binary packages found in %s", files.size(), p);
+ }
+
+ builder.get_logger().log("packagemgr", "%d binary packages found", binpkg_files.size());
+ }
+
+ 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();
+}