]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Don't copy cpu from a potentially incompatible architecture
[builder.git] / source / sourcepackage.cpp
1 #include <cstdlib>
2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
6 #include "builder.h"
7 #include "misc.h"
8 #include "pkgconfigfile.h"
9 #include "tool.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace {
16
17 bool component_sort(const Component &c1, const Component &c2)
18 { return c1.get_type()<c2.get_type(); }
19
20 }
21
22
23 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
24         Package(b, n),
25         source(s),
26         config(*this),
27         deps_cache(*this)
28 {
29         components.push_back(Component(*this, Component::TARBALL, "@src"));
30 }
31
32 FS::Path SourcePackage::get_temp_dir() const
33 {
34         string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
35         return source/config.get_option("tempdir").value/subdir;
36 }
37
38 FS::Path SourcePackage::get_out_dir() const
39 {
40         const Architecture &arch = builder.get_current_arch();
41         if(arch.is_native())
42                 return source/config.get_option("outdir").value;
43         else
44                 return source/arch.get_name()/config.get_option("outdir").value;
45 }
46
47 unsigned SourcePackage::get_install_flags()
48 {
49         unsigned flags = 0;
50         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
51                 if(i->get_install())
52                 {
53                         if(i->get_type()==Component::PROGRAM)
54                                 flags |= BIN;
55                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
56                                 flags |= LIB;
57                         else if(i->get_type()==Component::HEADERS)
58                                 flags |= INCLUDE;
59                 }
60
61         return flags;
62 }
63
64 LibMode SourcePackage::get_library_mode() const
65 {
66         const string &mode = config.get_option("staticlibs").value;
67         if(mode=="all")
68                 return ALL_STATIC;
69         else if(mode=="local")
70                 return LOCAL_STATIC;
71         else if(mode=="none")
72                 return DYNAMIC;
73         else
74                 throw runtime_error("unknown library mode");
75 }
76
77 string SourcePackage::expand_string(const string &str) const
78 {
79         string result = str;
80         string::size_type dollar = 0;
81         unsigned n = 0;
82         while((dollar = result.find('$'))!=string::npos)
83         {
84                 if(n>1000)
85                         throw bad_expansion("nested too deep");
86
87                 string::size_type end;
88                 string var;
89                 if(dollar+1<result.size() && result[dollar+1]=='{')
90                 {
91                         end = result.find('}', dollar+2);
92                         if(end==string::npos)
93                                 throw bad_expansion("unterminated variable reference");
94                         var = result.substr(dollar+2, end-dollar-2);
95                         ++end;
96                 }
97                 else
98                 {
99                         for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
100                         var = result.substr(dollar+1, end-dollar-1);
101                 }
102
103                 string value;
104                 if(config.is_option(var))
105                         value = config.get_option(var).value;
106                 else if(var=="arch")
107                         value = builder.get_current_arch().get_name();
108                 else if(var=="system")
109                         value = builder.get_current_arch().get_system();
110                 else if(const char *ptr = getenv(var.c_str()))
111                         value = ptr;
112
113                 result.replace(dollar, end-dollar, value);
114
115                 ++n;
116         }
117
118         return result;
119 }
120
121 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
122 {
123         init_config();
124
125         StringMap::const_iterator prof = opts.find("profile");
126         if(prof!=opts.end() && flag)
127                 config.select_profile(prof->second);
128         else
129                 config.select_last_profile();
130
131         if(flag && config.update(opts))
132         {
133                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
134                 if(!builder.get_dry_run())
135                         config.save();
136         }
137
138         config.finish();
139
140         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
141                 if(i->eval())
142                 {
143                         const StringList &reqs = i->get_requires();
144                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
145                                 if(Package *pkg = builder.get_package_manager().find_package(*j))
146                                         requires.push_back(pkg);
147                 }
148
149         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
150         {
151                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
152                 if(bpkg && bpkg->get_need_path())
153                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
154         }
155
156         deps_cache.load();
157
158         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
159                 i->configure(opts, flag);
160 }
161
162 void SourcePackage::init_config()
163 {
164         config.add_option("profile",    "default", "Configuration profile");
165         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
166         config.add_option("outdir",     ".",       "Directory to put build results in");
167         config.add_option("optimize",   "0",       "Compiler optimization level");
168         config.add_option("strip",      "no",      "Strip symbols from programs");
169         config.add_option("debug",      "no",      "Produce debugging symbols");
170         config.add_option("staticlibs", "local",   "Use static libraries");
171
172         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
173                 config.add_option("with_"+i->name, i->def_value, i->descr);
174
175         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
176         {
177                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
178                 if(bpkg && bpkg->get_need_path())
179                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
180         }
181 }
182
183 void SourcePackage::create_build_info()
184 {
185         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
186         const StringList &warnings = builder.get_warnings();
187         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
188
189         unsigned flags = get_install_flags();
190
191         build_info.incpath.push_back((builder.get_prefix()/"include").str());
192         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
193
194         if(flags&INCLUDE)
195                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
196         if(flags&LIB)
197                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
198
199         string optimize = config.get_option("optimize").value;
200         if(!optimize.empty() && optimize!="0")
201         {
202                 if(optimize=="s" || optimize=="size")
203                         build_info.optimize = -1;
204                 else
205                         build_info.optimize = lexical_cast<unsigned>(optimize);
206         }
207
208         if(lexical_cast<bool>(config.get_option("debug").value))
209         {
210                 build_info.debug = true;
211                 build_info.defines["DEBUG"] = "1";
212         }
213
214         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
215                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
216                         build_info.defines["WITH_"+toupper(i->name)] = "1";
217
218         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
219                 if(i->eval())
220                         build_info.update_from(i->get_build_info());
221
222         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
223         {
224                 i->create_build_info();
225                 if(i->get_type()==Component::LIBRARY)
226                         export_binfo.libs.push_back(i->get_name());
227         }
228 }
229
230 void SourcePackage::create_targets()
231 {
232         bool pc_needed = false;
233         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
234         {
235                 i->create_targets();
236                 if(i->get_type()==Component::LIBRARY || i->get_type()==Component::HEADERS)
237                         pc_needed = true;
238         }
239
240         if(pc_needed)
241         {
242                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
243                 builder.get_target("install")->add_depend(builder.get_toolchain().get_tool("CP").create_target(*pc));
244         }
245 }
246
247
248 SourcePackage::Loader::Loader(Package &p):
249         Package::Loader(p)
250 {
251         add("version",     &SourcePackage::version);
252         add("description", &SourcePackage::description);
253         add("build_info",  &Loader::build_info);
254         add("feature",     &Loader::feature);
255         add("if",          &Loader::condition);
256         add("program",     &Loader::component<Component::PROGRAM>);
257         add("library",     &Loader::component<Component::LIBRARY>);
258         add("module",      &Loader::component<Component::MODULE>);
259         add("headers",     &Loader::component<Component::HEADERS>);
260         add("install",     &Loader::component<Component::INSTALL>);
261         add("datafile",    &Loader::component<Component::DATAFILE>);
262         add("tarball",     &Loader::tarball);
263         add("tar_file",    &Loader::tar_file);
264 }
265
266 void SourcePackage::Loader::finish()
267 {
268         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
269         spkg.components.sort(component_sort);
270 }
271
272 void SourcePackage::Loader::feature(const string &n, const string &d)
273 {
274         Feature feat(n);
275         feat.descr = d;
276         feat.def_value = "no";
277         load_sub(feat);
278         static_cast<SourcePackage &>(pkg).features.push_back(feat);
279 }
280
281 void SourcePackage::Loader::condition(const string &c)
282 {
283         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
284         Condition cond(spkg, c);
285         load_sub(cond);
286         spkg.conditions.push_back(cond);
287 }
288
289 template<Component::Type t>
290 void SourcePackage::Loader::component(const string &n)
291 {
292         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
293         Component comp(spkg, t, n);
294         load_sub(comp);
295         spkg.components.push_back(comp);
296 }
297
298 void SourcePackage::Loader::build_info()
299 {
300         load_sub(static_cast<SourcePackage &>(pkg).build_info);
301 }
302
303 void SourcePackage::Loader::tarball(const string &n)
304 {
305         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
306         if(n=="@src")
307         {
308                 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
309                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
310                                 load_sub(*i);
311         }
312         else
313         {
314                 Component trbl(spkg, Component::TARBALL, n);
315                 load_sub(trbl);
316         }
317 }
318
319 void SourcePackage::Loader::tar_file(const string &f)
320 {
321         IO::print("%s: Note: tar_file is deprecated\n", get_source());
322         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
323         for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
324                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
325                         const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());
326 }