]> git.tdb.fi Git - builder.git/blob - source/packagemanager.cpp
Use default member initializers and constructor delegation
[builder.git] / source / packagemanager.cpp
1 #include <cstdlib>
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"
11 #include "builder.h"
12 #include "externaltask.h"
13 #include "package.h"
14 #include "packagemanager.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 PackageManager::PackageManager(Builder &b):
20         builder(b)
21 { }
22
23 PackageManager::~PackageManager()
24 {
25         for(const auto &kvp: packages)
26                 delete kvp.second;
27 }
28
29 void PackageManager::append_package_path(const FS::Path &p)
30 {
31         pkg_path.push_back(p);
32 }
33
34 void PackageManager::append_binary_package_path(const FS::Path &p)
35 {
36         binpkg_path.push_back(p);
37 }
38
39 void PackageManager::set_no_externals(bool x)
40 {
41         no_externals = x;
42 }
43
44 void PackageManager::add_package(Package *pkg)
45 {
46         auto i = packages.find(pkg->get_name());
47         if(i!=packages.end())
48         {
49                 if(i->second!=pkg)
50                         throw invalid_argument("Package name conflict");
51                 else
52                         throw logic_error("Package is already managed");
53         }
54
55         if(packages.empty())
56                 main_pkg = pkg;
57
58         packages.insert({ pkg->get_name(), pkg });
59 }
60
61 Package *PackageManager::get_package(const string &name) const
62 {
63         auto i = packages.find(name);
64         if(i!=packages.end())
65                 return i->second;
66
67         return 0;
68 }
69
70 Package &PackageManager::get_main_package() const
71 {
72         if(!main_pkg)
73                 throw logic_error("No packages");
74         return *main_pkg;
75 }
76
77 Package *PackageManager::find_package(const string &name)
78 {
79         if(not_found.count(name))
80                 return 0;
81
82         if(Package *pkg = get_package(name))
83                 return pkg;
84
85         if(!no_externals)
86         {
87                 FS::Path path = get_package_location(name);
88                 if(!path.empty())
89                 {
90                         builder.load_build_file(path/"Build");
91                         auto i = packages.find(name);
92                         if(i!=packages.end())
93                                 return i->second;
94                 }
95         }
96
97         FS::Path path = get_binary_package_file(name);
98         if(!path.empty())
99         {
100                 builder.load_build_file(path);
101                 if(Package *pkg = get_package(name))
102                         return pkg;
103         }
104
105         try
106         {
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 });
114                 return pkg;
115         }
116         catch(...)
117         {
118                 not_found.insert(name);
119                 return 0;
120         }
121 }
122
123 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
124 {
125 #ifndef _WIN32
126         if(!env_set)
127         {
128                 const FS::Path &prefix = builder.get_prefix();
129                 if(prefix.str()!="/usr")
130                 {
131                         FS::Path pcdir = prefix/"lib/pkgconfig";
132                         if(const char *pcp = getenv("PKG_CONFIG_PATH"))
133                         {
134                                 vector<string> path = split(pcp, ':');
135                                 if(!any_equals(path, pcdir.str()))
136                                 {
137                                         path.push_back(pcdir.str());
138                                         setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
139                                 }
140                         }
141                         else
142                                 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
143                 }
144         }
145
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")
151         {
152                 argv.push_back("--cflags");
153                 argv.push_back("--libs");
154                 if(what=="staticflags")
155                         argv.push_back("--static");
156         }
157         else
158                 argv.push_back("--variable="+what);
159         argv.push_back(pkg);
160
161         builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
162
163         return ExternalTask::run_and_capture_output(argv);
164 #else
165         (void)pkg;
166         (void)what;
167         return string();
168 #endif
169 }
170
171 FS::Path PackageManager::get_package_location(const string &name)
172 {
173         builder.get_logger().log("packagemgr", "Looking for source package %s", name);
174
175         try
176         {
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"))
180                         return srcdir;
181         }
182         catch(...)
183         { }
184
185         if(pkg_dirs.empty())
186         {
187                 for(const FS::Path &p: pkg_path)
188                 {
189                         builder.get_logger().log("files", "Traversing %s", p);
190                         unsigned count = 0;
191                         for(const string &f: list_files(p))
192                         {
193                                 FS::Path full = p/f;
194                                 if(FS::exists(full/"Build"))
195                                 {
196                                         pkg_dirs.push_back(full);
197                                         ++count;
198                                 }
199                         }
200
201                         builder.get_logger().log("packagemgr", "%d source packages found in %s", count, p);
202                 }
203
204                 builder.get_logger().log("packagemgr", "%d source packages found", pkg_dirs.size());
205         }
206
207         bool msp = !name.compare(0, 3, "msp");
208         for(const FS::Path &p: pkg_dirs)
209         {
210                 string base = FS::basename(p);
211                 unsigned dash = base.rfind('-');
212
213                 if(!base.compare(0, dash, name))
214                         return p;
215                 else if(msp && !base.compare(0, dash, name, 3, string::npos))
216                         return p;
217         }
218
219         return FS::Path();
220 }
221
222 FS::Path PackageManager::get_binary_package_file(const string &name)
223 {
224         builder.get_logger().log("packagemgr", "Looking for binary package %s", name);
225
226         if(binpkg_files.empty())
227         {
228                 for(const FS::Path &p: binpkg_path)
229                 {
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);
235                 }
236
237                 builder.get_logger().log("packagemgr", "%d binary packages found", binpkg_files.size());
238         }
239
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())
242                 return *i;
243
244         return FS::Path();
245 }
246
247 void PackageManager::save_all_caches() const
248 {
249         for(const auto &kvp: packages)
250                 kvp.second->save_caches();
251 }