2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/lexicalcast.h>
7 #include "booleanevaluator.h"
10 #include "csourcefile.h"
12 #include "executable.h"
14 #include "objectfile.h"
15 #include "sharedlibrary.h"
16 #include "sourcepackage.h"
17 #include "staticlibrary.h"
21 #include "toolchain.h"
26 Component::Component(SourcePackage &p, Type t, const string &n):
34 void Component::prepare()
36 for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
40 void Component::create_build_info()
42 BuildInfo final_build_info;
44 const Package::Requirements &pkg_reqs = package.get_required_packages();
45 Package::Requirements direct_reqs = requires;
46 direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
48 Package::Requirements all_reqs = direct_reqs;
49 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
51 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
52 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
53 level = BuildInfo::DEPENDENCY;
54 final_build_info.update_from((*i)->get_exported_build_info(), level);
56 const Package::Requirements &reqs = (*i)->get_required_packages();
57 for(Package::Requirements::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
58 if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
59 all_reqs.push_back(*j);
62 final_build_info.update_from(package.get_build_info());
63 final_build_info.update_from(build_info);
64 build_info = final_build_info;
66 for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
67 *i = (package.get_source_directory() / *i).str();
68 for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
69 *i = (package.get_source_directory() / *i).str();
71 for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
73 /* Select an include path that contains all the sources for this and the
74 used component. This should produce a sensible result in most cases. */
76 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
77 base = base.empty() ? *j : FS::common_ancestor(base, *j);
78 const SourceList &use_sources = (*i)->get_sources();
79 for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
80 base = FS::common_ancestor(base, *j);
81 build_info.incpath.push_back(base);
82 build_info.libs.push_back((*i)->get_name());
83 if(!(*i)->get_install())
85 build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
86 build_info.libpath.push_back((*i)->get_package().get_source_directory());
90 if(type==LIBRARY || type==MODULE)
91 if(build_info.libmode<BuildInfo::DYNAMIC)
92 build_info.libmode = BuildInfo::DYNAMIC;
94 if(build_info.libmode<BuildInfo::DYNAMIC)
96 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
98 const BuildInfo &ebi = (*i)->get_exported_build_info();
99 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
104 BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
106 // XXX Cache these and check that the directories actually exist before adding them
107 BuildInfo binfo = build_info;
108 if(!overlays.empty())
110 FS::Path dir = FS::dirname(path);
111 string last = FS::basename(dir);
112 for(OverlayList::const_iterator i=overlays.begin(); i!=overlays.end(); ++i)
115 dir = FS::dirname(dir);
119 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
122 binfo.local_incpath.push_back(dir);
123 for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
124 binfo.local_incpath.push_back(*i/ *j);
130 void Component::create_targets() const
132 Builder &builder = package.get_builder();
133 BuildGraph &build_graph = builder.get_build_graph();
134 const Toolchain &toolchain = builder.get_toolchain();
136 SourceList source_filenames = collect_source_files();
141 Tool &tar = toolchain.get_tool("TAR");
143 list<Target *> files;
144 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
146 FileTarget *file = builder.get_vfs().get_target(*i);
148 file = new File(builder, package, *i);
149 files.push_back(file);
152 string tarname = name;
155 tarname = package.get_name()+"-"+package.get_version();
156 files.insert(files.begin(), &package.get_build_file());
158 const BuildGraph::TargetMap &targets = build_graph.get_targets();
159 for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
160 if(i->second->get_package()==&package && !i->second->is_buildable())
161 if(find(files.begin(), files.end(), i->second)==files.end())
162 files.push_back(i->second);
165 Target *result = tar.create_target(files, tarname);
167 build_graph.get_target("tarballs")->add_dependency(*result);
171 else if(type==INSTALL)
173 Target *inst = build_graph.get_target("install");
174 Tool © = toolchain.get_tool("CP");
175 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
178 if(Target *tgt = builder.get_vfs().get_target(*i))
179 ft = dynamic_cast<FileTarget *>(tgt);
181 ft = new File(builder, package, *i);
182 inst->add_dependency(*copy.create_target(*ft, name));
185 else if(type==DATAPACK)
187 Tool &dcomp = toolchain.get_tool("DATA");
189 list<Target *> files;
190 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
192 string ext = FS::extpart(FS::basename(*i));
195 Target *src = dcomp.create_source(*this, *i);
196 files.push_back(dcomp.create_target(*src, "collection"));
198 else if(Target *tgt = builder.get_vfs().get_target(*i))
199 files.push_back(tgt);
201 files.push_back(new File(builder, package, *i));
204 Target *result = dcomp.create_target(files, "pack");
206 build_graph.add_primary_target(*result);
208 build_graph.add_installed_target(*result);
211 if(type==PROGRAM || type==LIBRARY || type==MODULE)
214 const Toolchain &pkg_tools = package.get_toolchain();
215 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
217 string ext = FS::extpart(FS::basename(*i));
220 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
223 Target *tmpl = gen->create_source(*this, *i);
226 src = gen->create_target(*tmpl);
227 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
231 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
235 src = tool->create_source(*this, *i);
239 if(tool->accepts_suffix(ext))
241 Target *obj = tool->create_target(*src);
245 if(type==LIBRARY && install)
247 if(dynamic_cast<FileTarget *>(src)->is_installable())
248 build_graph.add_installed_target(*src);
250 const Target::Dependencies &side_effects = src->get_side_effects();
251 for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
252 if(dynamic_cast<FileTarget *>(*j)->is_installable())
253 build_graph.add_installed_target(**j);
258 Tool &linker = toolchain.get_tool("LINK");
260 list<Target *> results;
263 Tool &archiver = toolchain.get_tool("AR");
264 results.push_back(linker.create_target(objs, "shared"));
265 results.push_back(archiver.create_target(objs));
267 else if(type==MODULE)
268 results.push_back(linker.create_target(objs, "shared"));
270 results.push_back(linker.create_target(objs));
272 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
274 build_graph.add_primary_target(**i);
276 build_graph.add_installed_target(**i);
281 Component::SourceList Component::collect_source_files() const
284 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
290 dirs.push_back(path);
291 for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
293 FS::Path opath = path / *j;
294 if(FS::is_dir(opath))
295 dirs.push_back(opath);
297 for(SourceList::const_iterator j=dirs.begin(); j!=dirs.end(); ++j)
299 package.get_builder().get_logger().log("files", format("Traversing %s", *j));
300 list<string> sfiles = list_files(*j);
301 for(list<string>::iterator k=sfiles.begin(); k!=sfiles.end(); ++k)
302 files.push_back(*j / *k);
307 files.push_back(path);
308 for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
310 FS::Path opath = FS::dirname(path)/ *j/FS::basename(path);
311 if(FS::is_reg(opath))
312 files.push_back(opath);
321 Component::Loader::Loader(Component &c):
322 DataFile::ObjectLoader<Component>(c)
324 add("if_arch", &Loader::if_arch);
325 add("if_feature", &Loader::if_feature);
326 add("overlay", &Loader::overlay);
327 add("source", &Loader::source);
328 add("install", &Component::install);
329 add("install_map", &Loader::install_map);
330 add("build_info", &Loader::build_info);
331 add("require", &Loader::require);
332 add("default", &Component::deflt);
333 add("use", &Loader::use);
336 void Component::Loader::build_info()
338 load_sub(obj.build_info);
341 void Component::Loader::if_arch(const string &cond)
343 BooleanEvaluator eval(sigc::hide<1>(sigc::mem_fun(&obj.package.get_builder().get_current_arch(), &Architecture::match_name)), false);
344 bool match = eval.evaluate(cond);
345 obj.package.get_builder().get_logger().log("configure",
346 format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
348 load_sub_with(*this);
351 void Component::Loader::if_feature(const string &cond)
353 BooleanEvaluator eval(sigc::mem_fun(&obj.package, &SourcePackage::match_feature));
354 bool match = eval.evaluate(cond);
355 obj.package.get_builder().get_logger().log("configure",
356 format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
358 load_sub_with(*this);
361 void Component::Loader::install_map()
363 load_sub(obj.install_map, obj.package.get_source_directory());
366 void Component::Loader::overlay(const string &o)
368 obj.overlays.push_back(o);
371 void Component::Loader::require(const string &n)
373 Package *req = obj.package.get_builder().get_package_manager().find_package(n);
375 obj.requires.push_back(req);
377 obj.problems.push_back(format("Required package %s not found", n));
380 void Component::Loader::source(const string &s)
382 obj.sources.push_back((obj.package.get_source_directory()/s).str());
385 void Component::Loader::use(const string &n)
387 const SourcePackage::ComponentList &components = obj.package.get_components();
388 for(SourcePackage::ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
389 if(i->get_name()==n && i->get_type()==LIBRARY)
391 obj.uses.push_back(&*i);
394 throw invalid_argument("Component::Loader::use");