]> git.tdb.fi Git - builder.git/blob - source/packagemanager.cpp
Adapt to changes in mspcore
[builder.git] / source / packagemanager.cpp
1 #include <cstdlib>
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/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         no_externals(false),
22         main_pkg(0),
23         env_set(false)
24 { }
25
26 PackageManager::~PackageManager()
27 {
28         for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
29                 delete i->second;
30 }
31
32 void PackageManager::append_package_path(const FS::Path &p)
33 {
34         pkg_path.push_back(p);
35 }
36
37 void PackageManager::append_binary_package_path(const FS::Path &p)
38 {
39         binpkg_path.push_back(p);
40 }
41
42 void PackageManager::set_no_externals(bool x)
43 {
44         no_externals = x;
45 }
46
47 void PackageManager::add_package(Package *pkg)
48 {
49         PackageMap::iterator i = packages.find(pkg->get_name());
50         if(i!=packages.end())
51         {
52                 if(i->second!=pkg)
53                         throw invalid_argument("Package name conflict");
54                 else
55                         throw logic_error("Package is already managed");
56         }
57
58         if(packages.empty())
59                 main_pkg = pkg;
60
61         packages.insert(PackageMap::value_type(pkg->get_name(), pkg));
62 }
63
64 Package *PackageManager::get_package(const string &name) const
65 {
66         PackageMap::const_iterator i = packages.find(name);
67         if(i!=packages.end())
68                 return i->second;
69
70         return 0;
71 }
72
73 Package &PackageManager::get_main_package() const
74 {
75         if(!main_pkg)
76                 throw logic_error("No packages");
77         return *main_pkg;
78 }
79
80 Package *PackageManager::find_package(const string &name)
81 {
82         if(not_found.count(name))
83                 return 0;
84
85         if(Package *pkg = get_package(name))
86                 return pkg;
87
88         if(!no_externals)
89         {
90                 FS::Path path = get_package_location(name);
91                 if(!path.empty())
92                 {
93                         builder.load_build_file(path/"Build");
94                         PackageMap::iterator i = packages.find(name);
95                         if(i!=packages.end())
96                                 return i->second;
97                 }
98         }
99
100         FS::Path path = get_binary_package_file(name);
101         if(!path.empty())
102         {
103                 builder.load_build_file(path);
104                 if(Package *pkg = get_package(name))
105                         return pkg;
106         }
107
108         try
109         {
110                 // Package source not found - create a binary package
111                 string flags_str = run_pkgconfig(name, "flags");
112                 BinaryPackage::Flags flags = split(flags_str);
113                 flags_str = run_pkgconfig(name, "staticflags");
114                 BinaryPackage::Flags static_flags = split(flags_str);
115                 Package *pkg = BinaryPackage::from_flags(builder, name, flags, static_flags);
116                 packages.insert(PackageMap::value_type(name, pkg));
117                 return pkg;
118         }
119         catch(...)
120         {
121                 not_found.insert(name);
122                 return 0;
123         }
124 }
125
126 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
127 {
128 #ifndef _WIN32
129         if(!env_set)
130         {
131                 const FS::Path &prefix = builder.get_prefix();
132                 if(prefix.str()!="/usr")
133                 {
134                         FS::Path pcdir = prefix/"lib/pkgconfig";
135                         if(const char *pcp = getenv("PKG_CONFIG_PATH"))
136                         {
137                                 vector<string> path = split(pcp, ':');
138                                 bool found = false;
139                                 for(vector<string>::const_iterator i=path.begin(); (!found && i!=path.end()); ++i)
140                                         found = (*i==pcdir.str());
141                                 if(!found)
142                                 {
143                                         path.push_back(pcdir.str());
144                                         setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
145                                 }
146                         }
147                         else
148                                 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
149                 }
150         }
151
152         ExternalTask::Arguments argv;
153         argv.push_back("pkg-config");
154         if(what=="cflags" || what=="libs")
155                 argv.push_back("--"+what);
156         else if(what=="flags" || what=="staticflags")
157         {
158                 argv.push_back("--cflags");
159                 argv.push_back("--libs");
160                 if(what=="staticflags")
161                         argv.push_back("--static");
162         }
163         else
164                 argv.push_back("--variable="+what);
165         argv.push_back(pkg);
166
167         builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
168
169         return ExternalTask::run_and_capture_output(argv);
170 #else
171         (void)pkg;
172         (void)what;
173         return string();
174 #endif
175 }
176
177 FS::Path PackageManager::get_package_location(const string &name)
178 {
179         builder.get_logger().log("packagemgr", format("Looking for source package %s", name));
180
181         try
182         {
183                 // Try to get source directory with pkgconfig
184                 string srcdir = strip(run_pkgconfig(name, "source"));
185                 if(!srcdir.empty() && FS::exists(FS::Path(srcdir)/"Build"))
186                         return srcdir;
187         }
188         catch(...)
189         { }
190
191         if(pkg_dirs.empty())
192         {
193                 for(SearchPath::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
194                 {
195                         builder.get_logger().log("files", format("Traversing %s", *i));
196                         vector<string> files = list_files(*i);
197                         unsigned count = 0;
198                         for(vector<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
199                         {
200                                 FS::Path full = *i / *j;
201                                 if(FS::exists(full/"Build"))
202                                 {
203                                         pkg_dirs.push_back(full);
204                                         ++count;
205                                 }
206                         }
207
208                         builder.get_logger().log("packagemgr", format("%d source packages found in %s", count, *i));
209                 }
210
211                 builder.get_logger().log("packagemgr", format("%d source packages found", pkg_dirs.size()));
212         }
213
214         bool msp = !name.compare(0, 3, "msp");
215         for(SearchPath::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
216         {
217                 string base = FS::basename(*i);
218                 unsigned dash = base.rfind('-');
219
220                 if(!base.compare(0, dash, name))
221                         return *i;
222                 else if(msp && !base.compare(0, dash, name, 3, string::npos))
223                         return *i;
224         }
225
226         return FS::Path();
227 }
228
229 FS::Path PackageManager::get_binary_package_file(const string &name)
230 {
231         builder.get_logger().log("packagemgr", format("Looking for binary package %s", name));
232
233         if(binpkg_files.empty())
234         {
235                 for(list<FS::Path>::const_iterator i=binpkg_path.begin(); i!=binpkg_path.end(); ++i)
236                 {
237                         builder.get_logger().log("files", format("Traversing %s", *i));
238                         vector<string> files = list_filtered(*i, "\\.bpk$");
239                         for(vector<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
240                                 binpkg_files.push_back(*i / *j);
241                         builder.get_logger().log("packagemgr", format("%d binary packages found in %s", files.size(), *i));
242                 }
243
244                 builder.get_logger().log("packagemgr", format("%d binary packages found", binpkg_files.size()));
245         }
246
247         for(SearchPath::const_iterator i=binpkg_files.begin(); i!=binpkg_files.end(); ++i)
248         {
249                 string base = FS::basepart(FS::basename(*i));
250                 if(base==name)
251                         return *i;
252         }
253
254         return FS::Path();
255 }
256
257 void PackageManager::save_all_caches() const
258 {
259         for(PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
260                 i->second->save_caches();
261 }