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)
57 else if(i->get_type()==Component::HEADERS)
64 LibMode SourcePackage::get_library_mode() const
66 const string &mode = config.get_option("staticlibs").value;
69 else if(mode=="local")
74 throw runtime_error("unknown library mode");
77 string SourcePackage::expand_string(const string &str) const
80 string::size_type dollar = 0;
82 while((dollar = result.find('$'))!=string::npos)
85 throw bad_expansion("nested too deep");
87 string::size_type end;
89 if(dollar+1<result.size() && result[dollar+1]=='{')
91 end = result.find('}', dollar+2);
93 throw bad_expansion("unterminated variable reference");
94 var = result.substr(dollar+2, end-dollar-2);
99 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
100 var = result.substr(dollar+1, end-dollar-1);
104 if(config.is_option(var))
105 value = config.get_option(var).value;
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()))
113 result.replace(dollar, end-dollar, value);
121 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
125 StringMap::const_iterator prof = opts.find("profile");
126 if(prof!=opts.end() && flag)
127 config.select_profile(prof->second);
129 config.select_last_profile();
131 if(flag && config.update(opts))
133 if(builder.get_verbose()>=2)
134 IO::print("Configuration of %s changed\n", name);
135 if(!builder.get_dry_run())
141 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
144 const StringList &reqs = i->get_requires();
145 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
146 if(Package *pkg = builder.get_package(*j))
147 requires.push_back(pkg);
150 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
152 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
153 if(bpkg && bpkg->get_need_path())
154 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
159 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
160 i->configure(opts, flag);
163 void SourcePackage::init_config()
165 config.add_option("profile", "default", "Configuration profile");
166 config.add_option("tempdir", "temp", "Directory for storing temporary files");
167 config.add_option("outdir", ".", "Directory to put build results in");
168 config.add_option("optimize", "0", "Compiler optimization level");
169 config.add_option("strip", "no", "Strip symbols from programs");
170 config.add_option("debug", "no", "Produce debugging symbols");
171 config.add_option("staticlibs", "local", "Use static libraries");
173 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
174 config.add_option("with_"+i->name, i->def_value, i->descr);
176 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
178 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
179 if(bpkg && bpkg->get_need_path())
180 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
184 void SourcePackage::create_build_info()
186 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
187 const StringList &warnings = builder.get_warnings();
188 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
190 unsigned flags = get_install_flags();
192 build_info.incpath.push_back((builder.get_prefix()/"include").str());
193 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
196 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
198 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
200 string optimize = config.get_option("optimize").value;
201 if(!optimize.empty() && optimize!="0")
203 if(optimize=="s" || optimize=="size")
204 build_info.optimize = -1;
206 build_info.optimize = lexical_cast<unsigned>(optimize);
209 if(lexical_cast<bool>(config.get_option("debug").value))
211 build_info.debug = true;
212 build_info.defines["DEBUG"] = "1";
215 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
216 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
217 build_info.defines["WITH_"+toupper(i->name)] = "1";
219 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
221 build_info.update_from(i->get_build_info());
223 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
225 i->create_build_info();
226 if(i->get_type()==Component::LIBRARY)
227 export_binfo.libs.push_back(i->get_name());
231 void SourcePackage::create_targets()
233 bool pc_needed = false;
234 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
237 if(i->get_type()==Component::LIBRARY || i->get_type()==Component::HEADERS)
243 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
244 builder.get_target("install")->add_depend(builder.get_toolchain().get_tool("CP").create_target(*pc));
249 SourcePackage::Loader::Loader(Package &p):
252 add("version", &SourcePackage::version);
253 add("description", &SourcePackage::description);
254 add("build_info", &Loader::build_info);
255 add("feature", &Loader::feature);
256 add("if", &Loader::condition);
257 add("program", &Loader::component<Component::PROGRAM>);
258 add("library", &Loader::component<Component::LIBRARY>);
259 add("module", &Loader::component<Component::MODULE>);
260 add("headers", &Loader::component<Component::HEADERS>);
261 add("install", &Loader::component<Component::INSTALL>);
262 add("datafile", &Loader::component<Component::DATAFILE>);
263 add("tarball", &Loader::tarball);
264 add("tar_file", &Loader::tar_file);
267 void SourcePackage::Loader::finish()
269 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
270 spkg.components.sort(component_sort);
273 void SourcePackage::Loader::feature(const string &n, const string &d)
277 feat.def_value = "no";
279 static_cast<SourcePackage &>(pkg).features.push_back(feat);
282 void SourcePackage::Loader::condition(const string &c)
284 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
285 Condition cond(spkg, c);
287 spkg.conditions.push_back(cond);
290 template<Component::Type t>
291 void SourcePackage::Loader::component(const string &n)
293 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
294 Component comp(spkg, t, n);
296 spkg.components.push_back(comp);
299 void SourcePackage::Loader::build_info()
301 load_sub(static_cast<SourcePackage &>(pkg).build_info);
304 void SourcePackage::Loader::tarball(const string &n)
306 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
309 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
310 if(i->get_type()==Component::TARBALL && i->get_name()==n)
315 Component trbl(spkg, Component::TARBALL, n);
320 void SourcePackage::Loader::tar_file(const string &f)
322 IO::print("%s: Note: tar_file is deprecated\n", get_source());
323 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
324 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
325 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
326 const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());