3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "binarypackage.h"
14 #include "sourcepackage.h"
20 Creates a buildable package.
22 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
28 tar_files.push_back(source/"Build");
31 Msp::FS::Path SourcePackage::get_temp_dir() const
33 return source/config.get_option("tempdir").value/builder.get_current_arch().get_name()/config.get_option("profile").value;
36 Msp::FS::Path SourcePackage::get_out_dir() const
38 const std::string &arch_name=builder.get_current_arch().get_name();
39 if(arch_name=="native")
40 return source/config.get_option("outdir").value;
42 return source/arch_name/config.get_option("outdir").value;
46 Checks which kinds of things the components of this package install.
48 @return A bitmask of installed things
50 unsigned SourcePackage::get_install_flags()
53 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)
62 if(!i->get_install_headers().empty())
69 LibMode SourcePackage::get_library_mode() const
71 const string &mode=config.get_option("staticlibs").value;
74 else if(mode=="local")
79 throw Exception("Unknown library mode");
85 Processes configuration options that were most likely obtained from the command
88 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
92 StringMap::const_iterator prof=opts.find("profile");
93 if(prof!=opts.end() && flag)
94 config.select_profile(prof->second);
96 config.select_last_profile();
98 if(flag && config.update(opts))
100 if(builder.get_verbose()>=2)
101 cout<<"Configuration of "<<name<<" changed\n";
102 if(!builder.get_dry_run())
108 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
111 const StringList &reqs=i->get_requires();
112 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
113 if(Package *pkg=builder.get_package(*j))
114 requires.push_back(pkg);
119 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
121 const PackageList &reqs=i->get_requires();
122 requires.insert(requires.end(), reqs.begin(), reqs.end());
125 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
127 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
128 if(bpkg && bpkg->get_need_path())
129 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
134 /*for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
135 (*i)->configure(opts, flag&2);*/
139 Initializes configuration options.
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", "Apply compiler optimizations");
147 config.add_option("strip", "0", "Strip symbols from programs");
148 config.add_option("debug", "0", "Produce debugging symbols");
149 config.add_option("cpu", "auto", "CPU type to optimize for");
150 config.add_option("staticlibs", "local", "Use static libraries");
152 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
153 config.add_option("with_"+i->name, "0", i->descr);
155 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
157 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
158 if(bpkg && bpkg->get_need_path())
159 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
164 Fills in build info based on configuration. All required packages must be
165 configured when this is called.
167 void SourcePackage::create_build_info()
169 for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
171 const BuildInfo &ebi=(*i)->get_exported_binfo();
174 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
175 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
176 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
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(lexical_cast<unsigned>(optimize))
196 build_info.cflags.push_back("-O"+optimize);
197 build_info.ldflags.push_back("-O"+optimize);
198 string cpu=config.get_option("cpu").value;
200 build_info.cflags.push_back("-march="+cpu);
203 if(lexical_cast<bool>(config.get_option("debug").value))
205 build_info.cflags.push_back("-ggdb");
206 build_info.defines.push_back("DEBUG");
209 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
210 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
211 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
213 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
215 build_info.add(i->get_build_info());
219 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
221 i->create_build_info();
222 if(i->get_type()==Component::LIBRARY)
223 export_binfo.libs.push_back(i->get_name());
226 export_binfo.unique();
230 SourcePackage::Loader::Loader(Package &p):
233 add("version", &SourcePackage::version);
234 add("description", &SourcePackage::description);
235 add("build_info", &Loader::build_info);
236 add("feature", &Loader::feature);
237 add("if", &Loader::condition);
238 add("program", &Loader::program);
239 add("library", &Loader::library);
240 add("module", &Loader::module);
241 add("headers", &Loader::headers);
242 add("tar_file", &Loader::tar_file);
245 void SourcePackage::Loader::feature(const string &n, const string &d)
247 static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
250 void SourcePackage::Loader::condition(const string &c)
252 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
253 Condition cond(spkg, c);
255 spkg.conditions.push_back(cond);
258 void SourcePackage::Loader::program(const string &n)
260 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
261 Component prog(spkg, Component::PROGRAM, n);
263 spkg.components.push_back(prog);
266 void SourcePackage::Loader::library(const string &n)
268 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
269 Component prog(spkg, Component::LIBRARY, n);
271 spkg.components.push_back(prog);
274 void SourcePackage::Loader::module(const string &n)
276 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
277 Component prog(spkg, Component::MODULE, n);
279 spkg.components.push_back(prog);
282 void SourcePackage::Loader::headers(const string &n)
284 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
285 Component prog(spkg, Component::HEADERS, n);
287 spkg.components.push_back(prog);
290 void SourcePackage::Loader::build_info()
292 load_sub(static_cast<SourcePackage &>(pkg).build_info);
295 void SourcePackage::Loader::tar_file(const string &f)
297 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
298 spkg.tar_files.push_back(spkg.source/f);