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 builder.get_logger().log("configure", format("Configuration of %s changed", name));
134 if(!builder.get_dry_run())
140 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
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);
149 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
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);
158 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
159 i->configure(opts, flag);
162 void SourcePackage::init_config()
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");
172 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
173 config.add_option("with_"+i->name, i->def_value, i->descr);
175 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
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());
183 void SourcePackage::create_build_info()
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());
189 unsigned flags = get_install_flags();
191 build_info.incpath.push_back((builder.get_prefix()/"include").str());
192 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
195 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
197 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
199 string optimize = config.get_option("optimize").value;
200 if(!optimize.empty() && optimize!="0")
202 if(optimize=="s" || optimize=="size")
203 build_info.optimize = -1;
205 build_info.optimize = lexical_cast<unsigned>(optimize);
208 if(lexical_cast<bool>(config.get_option("debug").value))
210 build_info.debug = true;
211 build_info.defines["DEBUG"] = "1";
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";
218 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
220 build_info.update_from(i->get_build_info());
222 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
224 i->create_build_info();
225 if(i->get_type()==Component::LIBRARY)
226 export_binfo.libs.push_back(i->get_name());
230 void SourcePackage::create_targets()
232 bool pc_needed = false;
233 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
236 if(i->get_type()==Component::LIBRARY || i->get_type()==Component::HEADERS)
242 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
243 builder.get_target("install")->add_depend(builder.get_toolchain().get_tool("CP").create_target(*pc));
248 SourcePackage::Loader::Loader(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::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);
266 void SourcePackage::Loader::finish()
268 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
269 spkg.components.sort(component_sort);
272 void SourcePackage::Loader::feature(const string &n, const string &d)
276 feat.def_value = "no";
278 static_cast<SourcePackage &>(pkg).features.push_back(feat);
281 void SourcePackage::Loader::condition(const string &c)
283 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
284 Condition cond(spkg, c);
286 spkg.conditions.push_back(cond);
289 template<Component::Type t>
290 void SourcePackage::Loader::component(const string &n)
292 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
293 Component comp(spkg, t, n);
295 spkg.components.push_back(comp);
298 void SourcePackage::Loader::build_info()
300 load_sub(static_cast<SourcePackage &>(pkg).build_info);
303 void SourcePackage::Loader::tarball(const string &n)
305 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
308 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
309 if(i->get_type()==Component::TARBALL && i->get_name()==n)
314 Component trbl(spkg, Component::TARBALL, n);
319 void SourcePackage::Loader::tar_file(const string &f)
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());