2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/format.h>
7 #include <msp/strings/utils.h>
8 #include <msp/time/units.h>
9 #include <msp/time/utils.h>
10 #include "binarypackage.h"
12 #include "externaltask.h"
14 #include "packagemanager.h"
19 PackageManager::PackageManager(Builder &b):
25 PackageManager::~PackageManager()
27 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
31 void PackageManager::append_package_path(const FS::Path &p)
33 pkg_path.push_back(p);
36 void PackageManager::append_binary_package_path(const FS::Path &p)
38 binpkg_path.push_back(p);
41 void PackageManager::set_no_externals(bool x)
46 void PackageManager::add_package(Package *pkg)
48 PackageMap::iterator i = packages.find(pkg->get_name());
52 throw invalid_argument("Package name conflict");
54 throw logic_error("Package is already managed");
57 packages.insert(PackageMap::value_type(pkg->get_name(), pkg));
60 Package *PackageManager::find_package(const string &name)
62 PackageMap::iterator i = packages.find(name);
68 FS::Path path = get_package_location(name);
71 builder.load_build_file(path/"Build");
72 i = packages.find(name);
78 FS::Path path = get_binary_package_file(name);
81 builder.load_build_file(path);
82 i = packages.find(name);
90 // Package source not found - create a binary package
91 string flags_str = run_pkgconfig(name, "flags");
92 vector<string> flags = split(flags_str);
93 pkg = BinaryPackage::from_flags(builder, name, flags);
97 builder.problem(name, "not found");
100 packages.insert(PackageMap::value_type(name, pkg));
105 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
109 const FS::Path &prefix = builder.get_prefix();
110 if(prefix.str()!="/usr")
112 FS::Path pcdir = prefix/"lib/pkgconfig";
113 if(const char *pcp = getenv("PKG_CONFIG_PATH"))
115 vector<string> path = split(pcp, ':');
117 for(vector<string>::const_iterator i=path.begin(); (!found && i!=path.end()); ++i)
118 found = (*i==pcdir.str());
121 path.push_back(pcdir.str());
122 setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
126 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
130 ExternalTask::Arguments argv;
131 argv.push_back("pkg-config");
132 if(what=="cflags" || what=="libs")
133 argv.push_back("--"+what);
134 else if(what=="flags")
136 argv.push_back("--cflags");
137 argv.push_back("--libs");
140 argv.push_back("--variable="+what);
143 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
145 return ExternalTask::run_and_capture_output(argv);
148 FS::Path PackageManager::get_package_location(const string &name)
150 builder.get_logger().log("packagemgr", format("Looking for source package %s", name));
154 // Try to get source directory with pkgconfig
155 string srcdir = strip(run_pkgconfig(name, "source"));
156 if(!srcdir.empty() && FS::exists(FS::Path(srcdir)/"Build"))
164 for(SearchPath::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
166 builder.get_logger().log("files", format("Traversing %s", *i));
167 list<string> files = list_files(*i);
169 for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
171 FS::Path full = *i / *j;
172 if(FS::exists(full/"Build"))
174 pkg_dirs.push_back(full);
179 builder.get_logger().log("packagemgr", format("%d source packages found in %s", count, *i));
182 builder.get_logger().log("packagemgr", format("%d source packages found", pkg_dirs.size()));
185 bool msp = !name.compare(0, 3, "msp");
186 for(SearchPath::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
188 string base = FS::basename(*i);
189 unsigned dash = base.rfind('-');
191 if(!base.compare(0, dash, name))
193 else if(msp && !base.compare(0, dash, name, 3, string::npos))
200 FS::Path PackageManager::get_binary_package_file(const string &name)
202 builder.get_logger().log("packagemgr", format("Looking for binary package %s", name));
204 if(binpkg_files.empty())
206 for(list<FS::Path>::const_iterator i=binpkg_path.begin(); i!=binpkg_path.end(); ++i)
208 builder.get_logger().log("files", format("Traversing %s", *i));
209 list<string> files = list_filtered(*i, "\\.bpk$");
210 for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
211 binpkg_files.push_back(*i / *j);
212 builder.get_logger().log("packagemgr", format("%d binary packages found in %s", files.size(), *i));
215 builder.get_logger().log("packagemgr", format("%d binary packages found", binpkg_files.size()));
218 for(SearchPath::const_iterator i=binpkg_files.begin(); i!=binpkg_files.end(); ++i)
220 string base = FS::basepart(FS::basename(*i));