3 This file is part of builder
4 Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/io/print.h>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "binarypackage.h"
14 #include "sourcepackage.h"
21 bool component_sort(const Component &c1, const Component &c2)
22 { return c1.get_type()<c2.get_type(); }
27 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
33 components.push_back(Component(*this, Component::TARBALL, "@src"));
36 FS::Path SourcePackage::get_temp_dir() const
38 string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
39 return source/config.get_option("tempdir").value/subdir;
42 FS::Path SourcePackage::get_out_dir() const
44 const Architecture &arch = builder.get_current_arch();
46 return source/config.get_option("outdir").value;
48 return source/arch.get_name()/config.get_option("outdir").value;
51 unsigned SourcePackage::get_install_flags()
54 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
57 if(i->get_type()==Component::PROGRAM)
59 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
61 else if(i->get_type()==Component::HEADERS)
68 LibMode SourcePackage::get_library_mode() const
70 const string &mode = config.get_option("staticlibs").value;
73 else if(mode=="local")
78 throw Exception("Unknown library mode");
81 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
85 StringMap::const_iterator prof = opts.find("profile");
86 if(prof!=opts.end() && flag)
87 config.select_profile(prof->second);
89 config.select_last_profile();
91 if(flag && config.update(opts))
93 if(builder.get_verbose()>=2)
94 IO::print("Configuration of %s changed\n", name);
95 if(!builder.get_dry_run())
101 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
104 const StringList &reqs = i->get_requires();
105 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
106 if(Package *pkg = builder.get_package(*j))
107 requires.push_back(pkg);
110 base_reqs = requires;
112 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
114 const PackageList &reqs = i->get_requires();
115 requires.insert(requires.end(), reqs.begin(), reqs.end());
118 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
120 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
121 if(bpkg && bpkg->get_need_path())
122 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
128 void SourcePackage::init_config()
130 config.add_option("profile", "default", "Configuration profile");
131 config.add_option("tempdir", "temp", "Directory for storing temporary files");
132 config.add_option("outdir", ".", "Directory to put build results in");
133 config.add_option("optimize", "0", "Apply compiler optimizations");
134 config.add_option("strip", "no", "Strip symbols from programs");
135 config.add_option("debug", "no", "Produce debugging symbols");
136 config.add_option("staticlibs", "local", "Use static libraries");
138 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
139 config.add_option("with_"+i->name, i->def_value, i->descr);
141 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
143 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
144 if(bpkg && bpkg->get_need_path())
145 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
149 void SourcePackage::create_build_info()
151 build_info.add(builder.get_current_arch().get_build_info());
153 for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
155 const BuildInfo &ebi = (*i)->get_exported_binfo();
158 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
159 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
160 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
163 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
164 const StringList &warnings = builder.get_warnings();
165 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
167 unsigned flags = get_install_flags();
169 build_info.incpath.push_back((builder.get_prefix()/"include").str());
170 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
173 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
175 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
177 string optimize = config.get_option("optimize").value;
178 if(lexical_cast<unsigned>(optimize))
180 build_info.cflags.push_back("-O"+optimize);
181 build_info.ldflags.push_back("-O"+optimize);
184 if(lexical_cast<bool>(config.get_option("debug").value))
186 build_info.cflags.push_back("-ggdb");
187 build_info.defines.push_back("DEBUG");
190 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
191 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
192 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
194 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
196 build_info.add(i->get_build_info());
200 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
202 i->create_build_info();
203 if(i->get_type()==Component::LIBRARY)
204 export_binfo.libs.push_back(i->get_name());
207 export_binfo.unique();
211 SourcePackage::Loader::Loader(Package &p):
214 add("version", &SourcePackage::version);
215 add("description", &SourcePackage::description);
216 add("build_info", &Loader::build_info);
217 add("feature", &Loader::feature);
218 add("if", &Loader::condition);
219 add("program", &Loader::component<Component::PROGRAM>);
220 add("library", &Loader::component<Component::LIBRARY>);
221 add("module", &Loader::component<Component::MODULE>);
222 add("headers", &Loader::component<Component::HEADERS>);
223 add("install", &Loader::component<Component::INSTALL>);
224 add("datafile", &Loader::component<Component::DATAFILE>);
225 add("tarball", &Loader::tarball);
226 add("tar_file", &Loader::tar_file);
229 void SourcePackage::Loader::finish()
231 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
232 spkg.components.sort(component_sort);
235 void SourcePackage::Loader::feature(const string &n, const string &d)
239 feat.def_value = "no";
241 static_cast<SourcePackage &>(pkg).features.push_back(feat);
244 void SourcePackage::Loader::condition(const string &c)
246 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
247 Condition cond(spkg, c);
249 spkg.conditions.push_back(cond);
252 template<Component::Type t>
253 void SourcePackage::Loader::component(const string &n)
255 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
256 Component comp(spkg, t, n);
258 spkg.components.push_back(comp);
261 void SourcePackage::Loader::build_info()
263 load_sub(static_cast<SourcePackage &>(pkg).build_info);
266 void SourcePackage::Loader::tarball(const string &n)
268 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
271 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
272 if(i->get_type()==Component::TARBALL && i->get_name()==n)
277 Component trbl(spkg, Component::TARBALL, n);
282 void SourcePackage::Loader::tar_file(const string &f)
284 IO::print("%s: Note: tar_file is deprecated\n", get_source());
285 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
286 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
287 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
288 const_cast<PathList &>(i->get_sources()).push_back(spkg.source/f);