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>
9 #include "csourcefile.h"
11 #include "executable.h"
13 #include "objectfile.h"
14 #include "sharedlibrary.h"
15 #include "sourcepackage.h"
16 #include "staticlibrary.h"
20 #include "toolchain.h"
25 Component::Component(SourcePackage &p, Type t, const string &n):
33 void Component::prepare()
35 for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
39 void Component::create_build_info()
41 BuildInfo final_build_info;
43 const Package::Requirements &pkg_reqs = package.get_required_packages();
44 Package::Requirements direct_reqs = requires;
45 direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
47 Package::Requirements all_reqs = direct_reqs;
48 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
50 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
51 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
52 level = BuildInfo::DEPENDENCY;
53 final_build_info.update_from((*i)->get_exported_build_info(), level);
55 const Package::Requirements &reqs = (*i)->get_required_packages();
56 for(Package::Requirements::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
57 if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
58 all_reqs.push_back(*j);
61 final_build_info.update_from(package.get_build_info());
63 for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
64 *i = (package.get_source_directory() / *i).str();
65 for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
66 *i = (package.get_source_directory() / *i).str();
68 final_build_info.update_from(build_info);
69 build_info = final_build_info;
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);
84 if(type==LIBRARY || type==MODULE)
85 if(build_info.libmode<BuildInfo::DYNAMIC)
86 build_info.libmode = BuildInfo::DYNAMIC;
88 if(build_info.libmode<BuildInfo::DYNAMIC)
90 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
92 const BuildInfo &ebi = (*i)->get_exported_build_info();
93 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
98 void Component::create_targets() const
100 Builder &builder = package.get_builder();
101 const Toolchain &toolchain = builder.get_toolchain();
103 SourceList source_filenames = collect_source_files();
108 const Tool &tar = toolchain.get_tool("TAR");
110 list<Target *> files;
111 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
113 FileTarget *file = builder.get_vfs().get_target(*i);
115 file = new File(builder, package, *i);
116 files.push_back(file);
119 string tarname = name;
122 tarname = package.get_name()+"-"+package.get_version();
123 files.insert(files.begin(), &package.get_build_file());
125 const Builder::TargetMap &targets = builder.get_targets();
126 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
127 if(i->second->get_package()==&package && !i->second->is_buildable())
128 if(find(files.begin(), files.end(), i->second)==files.end())
129 files.push_back(i->second);
132 Target *result = tar.create_target(files, tarname);
134 builder.get_target("tarballs")->add_dependency(*result);
138 else if(type==INSTALL)
140 Target *inst = builder.get_target("install");
141 const Tool © = toolchain.get_tool("CP");
142 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
145 if(Target *tgt = builder.get_vfs().get_target(*i))
146 ft = dynamic_cast<FileTarget *>(tgt);
148 ft = new File(builder, package, *i);
149 inst->add_dependency(*copy.create_target(*ft, name));
152 else if(type==DATAFILE)
154 const Tool &dcomp = toolchain.get_tool("DATA");
157 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
158 source = dynamic_cast<File *>(tgt);
160 source = new File(builder, package, source_filenames.front());
161 Target *result = dcomp.create_target(*source);
163 builder.add_primary_target(*result);
165 builder.add_installed_target(*result);
168 if(type==PROGRAM || type==LIBRARY || type==MODULE)
171 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
173 string ext = FS::extpart(FS::basename(*i));
174 const Tool *tool = toolchain.get_tool_for_suffix(ext, true);
177 Target *src = tool->create_source(*this, *i);
181 if(tool->accepts_suffix(ext))
183 Target *obj = tool->create_target(*src);
187 if(type==LIBRARY && install && dynamic_cast<FileTarget *>(src)->is_installable())
188 builder.add_installed_target(*src);
192 const Tool &linker = toolchain.get_tool("LINK");
194 list<Target *> results;
197 const Tool &archiver = toolchain.get_tool("AR");
198 results.push_back(linker.create_target(objs, "shared"));
199 results.push_back(archiver.create_target(objs));
201 else if(type==MODULE)
202 results.push_back(linker.create_target(objs, "shared"));
204 results.push_back(linker.create_target(objs));
206 const Target::Dependencies &world_deps = builder.get_target("world")->get_dependencies();
207 for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
209 /* The world target depends on all primary targets; look for the
210 static library belonging to the component. This is a bit roundabout
211 way but gets the job done. */
213 for(Target::Dependencies::const_iterator j=world_deps.begin(); j!=world_deps.end(); ++j)
214 if((*j)->get_component()==*i && dynamic_cast<StaticLibrary *>(*j))
216 results.front()->add_dependency(**j);
221 builder.problem(package.get_name(), format("Can't find static library %s for component %s", (*i)->get_name(), name));
224 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
226 builder.add_primary_target(**i);
228 builder.add_installed_target(**i);
233 Component::SourceList Component::collect_source_files() const
236 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
241 package.get_builder().get_logger().log("files", format("Traversing %s", path));
242 list<string> sfiles = list_files(path);
243 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
244 files.push_back(path / *j);
247 files.push_back(path);
254 Component::Loader::Loader(Component &c):
255 DataFile::ObjectLoader<Component>(c)
257 add("source", &Loader::source);
258 add("install", &Component::install);
259 add("install_map", &Loader::install_map);
260 add("build_info", &Loader::build_info);
261 add("require", &Loader::require);
262 add("default", &Component::deflt);
263 add("use", &Loader::use);
266 void Component::Loader::source(const string &s)
268 obj.sources.push_back((obj.package.get_source_directory()/s).str());
271 void Component::Loader::require(const string &n)
273 Package *req = obj.package.get_builder().get_package_manager().find_package(n);
275 obj.requires.push_back(req);
278 void Component::Loader::build_info()
280 load_sub(obj.build_info);
283 void Component::Loader::install_map()
285 load_sub(obj.install_map, obj.package.get_source_directory());
288 void Component::Loader::use(const string &n)
290 const SourcePackage::ComponentList &components = obj.package.get_components();
291 for(SourcePackage::ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
292 if(i->get_name()==n && i->get_type()==LIBRARY)
294 obj.uses.push_back(&*i);
297 throw invalid_argument("Component::Loader::use");