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 LibMode SourcePackage::get_library_mode() const
49 const string &mode = config.get_option("staticlibs").value;
52 else if(mode=="local")
57 throw runtime_error("unknown library mode");
60 string SourcePackage::expand_string(const string &str) const
63 string::size_type dollar = 0;
65 while((dollar = result.find('$'))!=string::npos)
68 throw bad_expansion("nested too deep");
70 string::size_type end;
72 if(dollar+1<result.size() && result[dollar+1]=='{')
74 end = result.find('}', dollar+2);
76 throw bad_expansion("unterminated variable reference");
77 var = result.substr(dollar+2, end-dollar-2);
82 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
83 var = result.substr(dollar+1, end-dollar-1);
87 if(config.is_option(var))
88 value = config.get_option(var).value;
90 value = builder.get_current_arch().get_name();
91 else if(var=="system")
92 value = builder.get_current_arch().get_system();
93 else if(const char *ptr = getenv(var.c_str()))
96 result.replace(dollar, end-dollar, value);
104 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
108 StringMap::const_iterator prof = opts.find("profile");
109 if(prof!=opts.end() && flag)
110 config.select_profile(prof->second);
112 config.select_last_profile();
114 if(flag && config.update(opts))
115 builder.get_logger().log("configure", format("Configuration of %s changed", name));
119 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
122 const StringList &reqs = i->get_requires();
123 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
124 if(Package *pkg = builder.get_package_manager().find_package(*j))
125 requires.push_back(pkg);
128 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
130 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
131 if(bpkg && bpkg->get_need_path())
132 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
137 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
138 i->configure(opts, flag);
141 void SourcePackage::init_config()
143 config.add_option("profile", "default", "Configuration profile");
144 config.add_option("tempdir", "temp", "Directory for storing temporary files");
145 config.add_option("outdir", ".", "Directory to put build results in");
146 config.add_option("optimize", "0", "Compiler optimization level");
147 config.add_option("strip", "no", "Strip symbols from programs");
148 config.add_option("debug", "no", "Produce debugging symbols");
149 config.add_option("staticlibs", "local", "Use static libraries");
151 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
152 config.add_option("with_"+i->name, i->def_value, i->descr);
154 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
156 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
157 if(bpkg && bpkg->get_need_path())
158 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
162 void SourcePackage::create_build_info()
164 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
165 const StringList &warnings = builder.get_warnings();
166 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
168 build_info.incpath.push_back((builder.get_prefix()/"include").str());
169 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
171 bool export_paths = false;
172 for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
173 export_paths = (i->get_type()==Component::LIBRARY);
177 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
178 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
181 string optimize = config.get_option("optimize").value;
182 if(!optimize.empty() && optimize!="0")
184 if(optimize=="s" || optimize=="size")
185 build_info.optimize = -1;
187 build_info.optimize = lexical_cast<unsigned>(optimize);
190 if(lexical_cast<bool>(config.get_option("debug").value))
192 build_info.debug = true;
193 build_info.defines["DEBUG"] = "1";
196 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
197 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
198 build_info.defines["WITH_"+toupper(i->name)] = "1";
200 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
202 build_info.update_from(i->get_build_info());
204 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
206 i->create_build_info();
207 if(i->get_type()==Component::LIBRARY)
208 export_binfo.libs.push_back(i->get_name());
212 void SourcePackage::create_targets()
214 bool pc_needed = false;
215 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
218 if(i->get_type()==Component::LIBRARY)
224 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
225 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
229 void SourcePackage::save_caches()
236 SourcePackage::Loader::Loader(SourcePackage &p):
237 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
239 add("version", &SourcePackage::version);
240 add("description", &SourcePackage::description);
241 add("build_info", &Loader::build_info);
242 add("feature", &Loader::feature);
243 add("if", &Loader::condition);
244 add("program", &Loader::component<Component::PROGRAM>);
245 add("library", &Loader::component<Component::LIBRARY>);
246 add("module", &Loader::component<Component::MODULE>);
247 add("headers", &Loader::headers);
248 add("install", &Loader::component<Component::INSTALL>);
249 add("datafile", &Loader::component<Component::DATAFILE>);
250 add("tarball", &Loader::tarball);
251 add("tar_file", &Loader::tar_file);
254 void SourcePackage::Loader::finish()
256 obj.components.sort(component_sort);
258 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
260 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
262 const StringList &sources = j->get_sources();
263 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
265 if(!i->first.compare(0, k->size(), *k))
267 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
274 void SourcePackage::Loader::feature(const string &n, const string &d)
278 feat.def_value = "no";
280 obj.features.push_back(feat);
283 void SourcePackage::Loader::condition(const string &c)
285 Condition cond(obj, c);
287 obj.conditions.push_back(cond);
290 template<Component::Type t>
291 void SourcePackage::Loader::component(const string &n)
293 Component comp(obj, t, n);
295 obj.components.push_back(comp);
298 void SourcePackage::Loader::build_info()
300 load_sub(obj.build_info);
303 void SourcePackage::Loader::headers(const string &n)
305 IO::print("%s: Note: headers components are deprecated\n", get_source());
306 Component comp(obj, Component::LIBRARY, n);
308 const StringList &sources = comp.get_sources();
309 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
310 install_map[*i] = "include/"+comp.get_name();
313 void SourcePackage::Loader::tarball(const string &n)
317 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
318 if(i->get_type()==Component::TARBALL && i->get_name()==n)
323 Component trbl(obj, Component::TARBALL, n);
328 void SourcePackage::Loader::tar_file(const string &f)
330 IO::print("%s: Note: tar_file is deprecated\n", get_source());
331 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
332 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
333 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());