2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
8 #include "pkgconfigfile.h"
10 #include "sourcepackage.h"
17 bool component_sort(const Component &c1, const Component &c2)
18 { return c1.get_type()<c2.get_type(); }
23 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
29 components.push_back(Component(*this, Component::TARBALL, "@src"));
32 FS::Path SourcePackage::get_temp_dir() const
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;
38 FS::Path SourcePackage::get_out_dir() const
40 const Architecture &arch = builder.get_current_arch();
42 return source/config.get_option("outdir").value;
44 return source/arch.get_name()/config.get_option("outdir").value;
47 unsigned SourcePackage::get_install_flags()
50 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
53 if(i->get_type()==Component::PROGRAM)
55 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
62 LibMode SourcePackage::get_library_mode() const
64 const string &mode = config.get_option("staticlibs").value;
67 else if(mode=="local")
72 throw runtime_error("unknown library mode");
75 string SourcePackage::expand_string(const string &str) const
78 string::size_type dollar = 0;
80 while((dollar = result.find('$'))!=string::npos)
83 throw bad_expansion("nested too deep");
85 string::size_type end;
87 if(dollar+1<result.size() && result[dollar+1]=='{')
89 end = result.find('}', dollar+2);
91 throw bad_expansion("unterminated variable reference");
92 var = result.substr(dollar+2, end-dollar-2);
97 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
98 var = result.substr(dollar+1, end-dollar-1);
102 if(config.is_option(var))
103 value = config.get_option(var).value;
105 value = builder.get_current_arch().get_name();
106 else if(var=="system")
107 value = builder.get_current_arch().get_system();
108 else if(const char *ptr = getenv(var.c_str()))
111 result.replace(dollar, end-dollar, value);
119 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
123 StringMap::const_iterator prof = opts.find("profile");
124 if(prof!=opts.end() && flag)
125 config.select_profile(prof->second);
127 config.select_last_profile();
129 if(flag && config.update(opts))
130 builder.get_logger().log("configure", format("Configuration of %s changed", name));
134 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
137 const StringList &reqs = i->get_requires();
138 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
139 if(Package *pkg = builder.get_package_manager().find_package(*j))
140 requires.push_back(pkg);
143 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
145 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
146 if(bpkg && bpkg->get_need_path())
147 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
152 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
153 i->configure(opts, flag);
156 void SourcePackage::init_config()
158 config.add_option("profile", "default", "Configuration profile");
159 config.add_option("tempdir", "temp", "Directory for storing temporary files");
160 config.add_option("outdir", ".", "Directory to put build results in");
161 config.add_option("optimize", "0", "Compiler optimization level");
162 config.add_option("strip", "no", "Strip symbols from programs");
163 config.add_option("debug", "no", "Produce debugging symbols");
164 config.add_option("staticlibs", "local", "Use static libraries");
166 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
167 config.add_option("with_"+i->name, i->def_value, i->descr);
169 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
171 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
172 if(bpkg && bpkg->get_need_path())
173 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
177 void SourcePackage::create_build_info()
179 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
180 const StringList &warnings = builder.get_warnings();
181 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
183 unsigned flags = get_install_flags();
185 build_info.incpath.push_back((builder.get_prefix()/"include").str());
186 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
189 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
191 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
193 string optimize = config.get_option("optimize").value;
194 if(!optimize.empty() && optimize!="0")
196 if(optimize=="s" || optimize=="size")
197 build_info.optimize = -1;
199 build_info.optimize = lexical_cast<unsigned>(optimize);
202 if(lexical_cast<bool>(config.get_option("debug").value))
204 build_info.debug = true;
205 build_info.defines["DEBUG"] = "1";
208 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
209 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
210 build_info.defines["WITH_"+toupper(i->name)] = "1";
212 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
214 build_info.update_from(i->get_build_info());
216 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
218 i->create_build_info();
219 if(i->get_type()==Component::LIBRARY)
220 export_binfo.libs.push_back(i->get_name());
224 void SourcePackage::create_targets()
226 bool pc_needed = false;
227 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
230 if(i->get_type()==Component::LIBRARY)
236 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
237 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
241 void SourcePackage::save_caches()
248 SourcePackage::Loader::Loader(SourcePackage &p):
249 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
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::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);
266 void SourcePackage::Loader::finish()
268 obj.components.sort(component_sort);
270 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
272 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
274 const StringList &sources = j->get_sources();
275 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
277 if(!i->first.compare(0, k->size(), *k))
279 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
286 void SourcePackage::Loader::feature(const string &n, const string &d)
290 feat.def_value = "no";
292 obj.features.push_back(feat);
295 void SourcePackage::Loader::condition(const string &c)
297 Condition cond(obj, c);
299 obj.conditions.push_back(cond);
302 template<Component::Type t>
303 void SourcePackage::Loader::component(const string &n)
305 Component comp(obj, t, n);
307 obj.components.push_back(comp);
310 void SourcePackage::Loader::build_info()
312 load_sub(obj.build_info);
315 void SourcePackage::Loader::headers(const string &n)
317 IO::print("%s: Note: headers components are deprecated\n", get_source());
318 Component comp(obj, Component::LIBRARY, n);
320 const StringList &sources = comp.get_sources();
321 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
322 install_map[*i] = "include/"+comp.get_name();
325 void SourcePackage::Loader::tarball(const string &n)
329 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
330 if(i->get_type()==Component::TARBALL && i->get_name()==n)
335 Component trbl(obj, Component::TARBALL, n);
340 void SourcePackage::Loader::tar_file(const string &f)
342 IO::print("%s: Note: tar_file is deprecated\n", get_source());
343 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
344 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
345 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());