]> git.tdb.fi Git - builder.git/blob - source/packagemanager.cpp
This ugly hack for arch-specific binary packages is no longer needed
[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/units.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         env_set(false)
23 { }
24
25 PackageManager::~PackageManager()
26 {
27         for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
28                 delete i->second;
29 }
30
31 void PackageManager::append_package_path(const FS::Path &p)
32 {
33         pkg_path.push_back(p);
34 }
35
36 void PackageManager::append_binary_package_path(const FS::Path &p)
37 {
38         binpkg_path.push_back(p);
39 }
40
41 void PackageManager::set_no_externals(bool x)
42 {
43         no_externals = x;
44 }
45
46 void PackageManager::add_package(Package *pkg)
47 {
48         PackageMap::iterator i = packages.find(pkg->get_name());
49         if(i!=packages.end())
50         {
51                 if(i->second!=pkg)
52                         throw invalid_argument("Package name conflict");
53                 else
54                         throw logic_error("Package is already managed");
55         }
56
57         packages.insert(PackageMap::value_type(pkg->get_name(), pkg));
58 }
59
60 Package *PackageManager::find_package(const string &name)
61 {
62         PackageMap::iterator i = packages.find(name);
63         if(i!=packages.end())
64                 return i->second;
65
66         if(!no_externals)
67         {
68                 FS::Path path = get_package_location(name);
69                 if(!path.empty())
70                 {
71                         builder.load_build_file(path/"Build");
72                         i = packages.find(name);
73                         if(i!=packages.end())
74                                 return i->second;
75                 }
76         }
77
78         FS::Path path = get_binary_package_file(name);
79         if(!path.empty())
80         {
81                 builder.load_build_file(path);
82                 i = packages.find(name);
83                 if(i!=packages.end())
84                         return i->second;
85         }
86
87         Package *pkg = 0;
88         try
89         {
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);
94         }
95         catch(...)
96         {
97                 builder.problem(name, "not found");
98         }
99
100         packages.insert(PackageMap::value_type(name, pkg));
101
102         return pkg;
103 }
104
105 string PackageManager::run_pkgconfig(const string &pkg, const string &what)
106 {
107         if(!env_set)
108         {
109                 const FS::Path &prefix = builder.get_prefix();
110                 if(prefix.str()!="/usr")
111                 {
112                         FS::Path pcdir = prefix/"lib/pkgconfig";
113                         if(const char *pcp = getenv("PKG_CONFIG_PATH"))
114                         {
115                                 vector<string> path = split(pcp, ':');
116                                 bool found = false;
117                                 for(vector<string>::const_iterator i=path.begin(); (!found && i!=path.end()); ++i)
118                                         found = (*i==pcdir.str());
119                                 if(!found)
120                                 {
121                                         path.push_back(pcdir.str());
122                                         setenv("PKG_CONFIG_PATH", join(path.begin(), path.end(), ":").c_str(), true);
123                                 }
124                         }
125                         else
126                                 setenv("PKG_CONFIG_PATH", pcdir.str().c_str(), true);
127                 }
128         }
129
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")
135         {
136                 argv.push_back("--cflags");
137                 argv.push_back("--libs");
138         }
139         else
140                 argv.push_back("--variable="+what);
141         argv.push_back(pkg);
142
143         builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
144
145         return ExternalTask::run_and_capture_output(argv);
146 }
147
148 FS::Path PackageManager::get_package_location(const string &name)
149 {
150         builder.get_logger().log("packagemgr", format("Looking for source package %s", name));
151
152         try
153         {
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"))
157                         return srcdir;
158         }
159         catch(...)
160         { }
161
162         if(pkg_dirs.empty())
163         {
164                 for(SearchPath::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
165                 {
166                         builder.get_logger().log("files", format("Traversing %s", *i));
167                         list<string> files = list_files(*i);
168                         unsigned count = 0;
169                         for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
170                         {
171                                 FS::Path full = *i / *j;
172                                 if(FS::exists(full/"Build"))
173                                 {
174                                         pkg_dirs.push_back(full);
175                                         ++count;
176                                 }
177                         }
178
179                         builder.get_logger().log("packagemgr", format("%d source packages found in %s", count, *i));
180                 }
181
182                 builder.get_logger().log("packagemgr", format("%d source packages found", pkg_dirs.size()));
183         }
184
185         bool msp = !name.compare(0, 3, "msp");
186         for(SearchPath::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
187         {
188                 string base = FS::basename(*i);
189                 unsigned dash = base.rfind('-');
190
191                 if(!base.compare(0, dash, name))
192                         return *i;
193                 else if(msp && !base.compare(0, dash, name, 3, string::npos))
194                         return *i;
195         }
196
197         return FS::Path();
198 }
199
200 FS::Path PackageManager::get_binary_package_file(const string &name)
201 {
202         builder.get_logger().log("packagemgr", format("Looking for binary package %s", name));
203
204         if(binpkg_files.empty())
205         {
206                 for(list<FS::Path>::const_iterator i=binpkg_path.begin(); i!=binpkg_path.end(); ++i)
207                 {
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));
213                 }
214
215                 builder.get_logger().log("packagemgr", format("%d binary packages found", binpkg_files.size()));
216         }
217
218         for(SearchPath::const_iterator i=binpkg_files.begin(); i!=binpkg_files.end(); ++i)
219         {
220                 string base = FS::basepart(FS::basename(*i));
221                 if(base==name)
222                         return *i;
223         }
224
225         return FS::Path();
226 }