3 This file is part of builder
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/io/print.h>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "binarypackage.h"
14 #include "sourcepackage.h"
19 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
25 tar_files.push_back(source/"Build");
28 Msp::FS::Path SourcePackage::get_temp_dir() const
30 return source/config.get_option("tempdir").value/builder.get_current_arch().get_name()/config.get_option("profile").value;
33 Msp::FS::Path SourcePackage::get_out_dir() const
35 const Architecture &arch=builder.get_current_arch();
37 return source/config.get_option("outdir").value;
39 return source/arch.get_name()/config.get_option("outdir").value;
42 unsigned SourcePackage::get_install_flags()
45 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
48 if(i->get_type()==Component::PROGRAM)
50 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
52 else if(i->get_type()==Component::HEADERS)
59 LibMode SourcePackage::get_library_mode() const
61 const string &mode=config.get_option("staticlibs").value;
64 else if(mode=="local")
69 throw Exception("Unknown library mode");
72 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
76 StringMap::const_iterator prof=opts.find("profile");
77 if(prof!=opts.end() && flag)
78 config.select_profile(prof->second);
80 config.select_last_profile();
82 if(flag && config.update(opts))
84 if(builder.get_verbose()>=2)
85 IO::print("Configuration of %s changed\n", name);
86 if(!builder.get_dry_run())
92 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
95 const StringList &reqs=i->get_requires();
96 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
97 if(Package *pkg=builder.get_package(*j))
98 requires.push_back(pkg);
103 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
105 const PackageList &reqs=i->get_requires();
106 requires.insert(requires.end(), reqs.begin(), reqs.end());
109 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
111 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
112 if(bpkg && bpkg->get_need_path())
113 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
118 /*for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
119 (*i)->configure(opts, flag&2);*/
122 void SourcePackage::init_config()
124 config.add_option("profile", "default", "Configuration profile");
125 config.add_option("tempdir", "temp", "Directory for storing temporary files");
126 config.add_option("outdir", ".", "Directory to put build results in");
127 config.add_option("optimize", "0", "Apply compiler optimizations");
128 config.add_option("strip", "0", "Strip symbols from programs");
129 config.add_option("debug", "0", "Produce debugging symbols");
130 config.add_option("cpu", "auto", "CPU type to optimize for");
131 config.add_option("staticlibs", "local", "Use static libraries");
133 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
134 config.add_option("with_"+i->name, "0", i->descr);
136 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
138 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
139 if(bpkg && bpkg->get_need_path())
140 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
144 void SourcePackage::create_build_info()
146 for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
148 const BuildInfo &ebi=(*i)->get_exported_binfo();
151 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
152 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
153 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
156 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
157 const StringList &warnings=builder.get_warnings();
158 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
160 unsigned flags=get_install_flags();
162 build_info.incpath.push_back((builder.get_prefix()/"include").str());
163 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
166 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
168 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
170 string optimize=config.get_option("optimize").value;
171 if(lexical_cast<unsigned>(optimize))
173 build_info.cflags.push_back("-O"+optimize);
174 build_info.ldflags.push_back("-O"+optimize);
175 string cpu=config.get_option("cpu").value;
177 build_info.cflags.push_back("-march="+cpu);
180 if(lexical_cast<bool>(config.get_option("debug").value))
182 build_info.cflags.push_back("-ggdb");
183 build_info.defines.push_back("DEBUG");
186 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
187 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
188 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
190 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
192 build_info.add(i->get_build_info());
196 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
198 i->create_build_info();
199 if(i->get_type()==Component::LIBRARY)
200 export_binfo.libs.push_back(i->get_name());
203 export_binfo.unique();
207 SourcePackage::Loader::Loader(Package &p):
210 add("version", &SourcePackage::version);
211 add("description", &SourcePackage::description);
212 add("build_info", &Loader::build_info);
213 add("feature", &Loader::feature);
214 add("if", &Loader::condition);
215 add("program", &Loader::program);
216 add("library", &Loader::library);
217 add("module", &Loader::module);
218 add("headers", &Loader::headers);
219 add("tar_file", &Loader::tar_file);
222 void SourcePackage::Loader::feature(const string &n, const string &d)
224 static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
227 void SourcePackage::Loader::condition(const string &c)
229 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
230 Condition cond(spkg, c);
232 spkg.conditions.push_back(cond);
235 void SourcePackage::Loader::program(const string &n)
237 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
238 Component prog(spkg, Component::PROGRAM, n);
240 spkg.components.push_back(prog);
243 void SourcePackage::Loader::library(const string &n)
245 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
246 Component prog(spkg, Component::LIBRARY, n);
248 spkg.components.push_back(prog);
251 void SourcePackage::Loader::module(const string &n)
253 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
254 Component prog(spkg, Component::MODULE, n);
256 spkg.components.push_back(prog);
259 void SourcePackage::Loader::headers(const string &n)
261 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
262 Component prog(spkg, Component::HEADERS, n);
264 spkg.components.push_back(prog);
267 void SourcePackage::Loader::build_info()
269 load_sub(static_cast<SourcePackage &>(pkg).build_info);
272 void SourcePackage::Loader::tar_file(const string &f)
274 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
275 spkg.tar_files.push_back(spkg.source/f);