2 #include <msp/core/algorithm.h>
3 #include <msp/fs/dir.h>
4 #include <msp/fs/stat.h>
5 #include <msp/fs/utils.h>
6 #include <msp/io/print.h>
7 #include <msp/strings/utils.h>
8 #include <msp/time/timedelta.h>
9 #include <msp/time/utils.h>
10 #include "binarypackage.h"
12 #include "externaltask.h"
14 #include "packagemanager.h"
19 PackageManager::PackageManager(Builder &b):
23 PackageManager::~PackageManager()
25 for(const auto &kvp: packages)
29 void PackageManager::append_package_path(const FS::Path &p)
31 pkg_path.push_back(p);
34 void PackageManager::append_binary_package_path(const FS::Path &p)
36 binpkg_path.push_back(p);
39 void PackageManager::set_no_externals(bool x)
44 void PackageManager::add_package(Package *pkg)
46 auto i = packages.find(pkg->get_name());
50 throw invalid_argument("Package name conflict");
52 throw logic_error("Package is already managed");
58 packages.insert({ pkg->get_name(), pkg });
61 Package *PackageManager::get_package(const string &name) const
63 auto i = packages.find(name);
70 Package &PackageManager::get_main_package() const
73 throw logic_error("No packages");
77 Package *PackageManager::find_package(const string &name)
79 if(not_found.count(name))
82 if(Package *pkg = get_package(name))
87 FS::Path path = get_package_location(name);
90 builder.load_build_file(path/"Build");
91 auto i = packages.find(name);
97 FS::Path path = get_binary_package_file(name);
100 builder.load_build_file(path);
101 if(Package *pkg = get_package(name))
107 // Package source not found - create a binary package
108 string flags_str = run_pkgconfig(name, "flags");
109 BinaryPackage::Flags flags = split(flags_str);
110 flags_str = run_pkgconfig(name, "staticflags");
111 BinaryPackage::Flags static_flags = split(flags_str);
112 Package *pkg = BinaryPackage::from_flags(builder, name, flags, static_flags);
113 packages.insert({ name, pkg });
118 not_found.insert(name);
123 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
128 const FS::Path &prefix = builder.get_prefix();
129 if(prefix.str()!="/usr")
131 FS::Path pcdir = prefix/"lib/pkgconfig";
132 if(const char *pcp = getenv("PKG_CONFIG_PATH"))
134 vector<string> path = split(pcp, ':');
135 if(!any_equals(path, pcdir.str()))
137 path.push_back(pcdir.str());
138 setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
142 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
146 ExternalTask::Arguments argv;
147 argv.push_back("pkg-config");
148 if(what=="cflags" || what=="libs")
149 argv.push_back("--"+what);
150 else if(what=="flags" || what=="staticflags")
152 argv.push_back("--cflags");
153 argv.push_back("--libs");
154 if(what=="staticflags")
155 argv.push_back("--static");
158 argv.push_back("--variable="+what);
161 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
163 return ExternalTask::run_and_capture_output(argv);
171 FS::Path PackageManager::get_package_location(const string &name)
173 builder.get_logger().log("packagemgr", "Looking for source package %s", name);
177 // Try to get source directory with pkgconfig
178 string srcdir = strip(run_pkgconfig(name, "source"));
179 if(!srcdir.empty() && FS::exists(FS::Path(srcdir)/"Build"))
187 for(const FS::Path &p: pkg_path)
189 builder.get_logger().log("files", "Traversing %s", p);
191 for(const string &f: list_files(p))
194 if(FS::exists(full/"Build"))
196 pkg_dirs.push_back(full);
201 builder.get_logger().log("packagemgr", "%d source packages found in %s", count, p);
204 builder.get_logger().log("packagemgr", "%d source packages found", pkg_dirs.size());
207 bool msp = !name.compare(0, 3, "msp");
208 for(const FS::Path &p: pkg_dirs)
210 string base = FS::basename(p);
211 unsigned dash = base.rfind('-');
213 if(!base.compare(0, dash, name))
215 else if(msp && !base.compare(0, dash, name, 3, string::npos))
222 FS::Path PackageManager::get_binary_package_file(const string &name)
224 builder.get_logger().log("packagemgr", "Looking for binary package %s", name);
226 if(binpkg_files.empty())
228 for(const FS::Path &p: binpkg_path)
230 builder.get_logger().log("files", "Traversing %s", p);
231 vector<string> files = list_filtered(p, "\\.bpk$");
232 for(const string &f: files)
233 binpkg_files.push_back(p/f);
234 builder.get_logger().log("packagemgr", "%d binary packages found in %s", files.size(), p);
237 builder.get_logger().log("packagemgr", "%d binary packages found", binpkg_files.size());
240 auto i = find_if(binpkg_files, [&name](const FS::Path &p){ return FS::basepart(FS::basename(p))==name; });
241 if(i!=binpkg_files.end())
247 void PackageManager::save_all_caches() const
249 for(const auto &kvp: packages)
250 kvp.second->save_caches();