2 #include <msp/core/algorithm.h>
3 #include <msp/core/except.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/stat.h>
6 #include <msp/fs/utils.h>
7 #include <msp/io/print.h>
8 #include <msp/strings/utils.h>
9 #include <msp/time/timedelta.h>
10 #include <msp/time/utils.h>
11 #include "binarypackage.h"
13 #include "externaltask.h"
15 #include "packagemanager.h"
20 PackageManager::~PackageManager()
22 for(const auto &kvp: packages)
26 void PackageManager::append_package_path(const FS::Path &p)
28 pkg_path.push_back(p);
31 void PackageManager::append_binary_package_path(const FS::Path &p)
33 binpkg_path.push_back(p);
36 void PackageManager::set_no_externals(bool x)
41 void PackageManager::add_package(Package *pkg)
43 auto i = packages.find(pkg->get_name());
47 throw invalid_argument("Package name conflict");
49 throw logic_error("Package is already managed");
55 packages.insert({ pkg->get_name(), pkg });
58 Package *PackageManager::get_package(const string &name) const
60 auto i = packages.find(name);
67 Package &PackageManager::get_main_package() const
70 throw logic_error("No packages");
74 Package *PackageManager::find_package(const string &name)
76 if(not_found.count(name))
79 if(Package *pkg = get_package(name))
84 FS::Path path = get_package_location(name);
87 builder.load_build_file(path/"Build");
88 auto i = packages.find(name);
94 FS::Path path = get_binary_package_file(name);
97 builder.load_build_file(path);
98 if(Package *pkg = get_package(name))
104 // Package source not found - create a binary package
105 string flags_str = run_pkgconfig(name, "flags");
106 BinaryPackage::Flags flags = split(flags_str);
107 flags_str = run_pkgconfig(name, "staticflags");
108 BinaryPackage::Flags static_flags = split(flags_str);
109 Package *pkg = BinaryPackage::from_flags(builder, name, flags, static_flags);
110 packages.insert({ name, pkg });
115 not_found.insert(name);
120 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
125 const FS::Path &prefix = builder.get_prefix();
126 if(prefix.str()!="/usr")
128 FS::Path pcdir = prefix/"lib/pkgconfig";
129 if(const char *pcp = getenv("PKG_CONFIG_PATH"))
131 vector<string> path = split(pcp, ':');
132 if(!any_equals(path, pcdir.str()))
134 path.push_back(pcdir.str());
135 setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
139 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
143 ExternalTask::Arguments argv;
144 argv.push_back("pkg-config");
145 if(what=="cflags" || what=="libs")
146 argv.push_back("--"+what);
147 else if(what=="flags" || what=="staticflags")
149 argv.push_back("--cflags");
150 argv.push_back("--libs");
151 if(what=="staticflags")
152 argv.push_back("--static");
155 argv.push_back("--variable="+what);
158 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
160 return ExternalTask::run_and_capture_output(argv);
164 throw unsupported("run_pkgconfig");
168 FS::Path PackageManager::get_package_location(const string &name)
170 builder.get_logger().log("packagemgr", "Looking for source package %s", name);
174 // Try to get source directory with pkgconfig
175 string srcdir = strip(run_pkgconfig(name, "source"));
176 if(!srcdir.empty() && FS::exists(FS::Path(srcdir)/"Build"))
184 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)
233 builder.get_logger().log("files", "Traversing %s", p);
234 vector<string> files = list_filtered(p, "\\.bpk$");
235 for(const string &f: files)
236 binpkg_files.push_back(p/f);
237 builder.get_logger().log("packagemgr", "%d binary packages found in %s", files.size(), p);
240 builder.get_logger().log("packagemgr", "%d binary packages found", binpkg_files.size());
243 auto i = find_if(binpkg_files, [&name](const FS::Path &p){ return FS::basepart(FS::basename(p))==name; });
244 if(i!=binpkg_files.end())
250 void PackageManager::save_all_caches() const
252 for(const auto &kvp: packages)
253 kvp.second->save_caches();