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();
104 list<Target *> inst_list;
109 const Tool &tar = toolchain.get_tool("TAR");
111 list<Target *> files;
112 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
114 FileTarget *file = builder.get_vfs().get_target(*i);
116 file = new File(builder, package, *i);
117 files.push_back(file);
120 string tarname = name;
123 tarname = package.get_name()+"-"+package.get_version();
124 files.insert(files.begin(), &package.get_build_file());
126 const Builder::TargetMap &targets = builder.get_targets();
127 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
128 if(i->second->get_package()==&package && !i->second->is_buildable())
129 if(find(files.begin(), files.end(), i->second)==files.end())
130 files.push_back(i->second);
133 Target *result = tar.create_target(files, tarname);
135 builder.get_target("tarballs")->add_dependency(*result);
139 else if(type==INSTALL)
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_list.push_back(ft);
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 inst_list.push_back(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 inst_list.push_back(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 inst_list.push_back(*i);
232 Target *inst_tgt = builder.get_target("install");
233 const Tool © = toolchain.get_tool("CP");
234 for(list<Target *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
236 Target *inst = copy.create_target(**i, inst_loc);
237 inst_tgt->add_dependency(*inst);
241 Component::SourceList Component::collect_source_files() const
244 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
249 package.get_builder().get_logger().log("files", format("Traversing %s", path));
250 list<string> sfiles = list_files(path);
251 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
252 files.push_back(path / *j);
255 files.push_back(path);
262 Component::Loader::Loader(Component &c):
263 DataFile::ObjectLoader<Component>(c)
265 add("source", &Loader::source);
266 add("install", &Component::install);
267 add("install_map", &Loader::install_map);
268 add("build_info", &Loader::build_info);
269 add("require", &Loader::require);
270 add("default", &Component::deflt);
271 add("use", &Loader::use);
274 void Component::Loader::source(const string &s)
276 obj.sources.push_back((obj.package.get_source_directory()/s).str());
279 void Component::Loader::require(const string &n)
281 Package *req = obj.package.get_builder().get_package_manager().find_package(n);
283 obj.requires.push_back(req);
286 void Component::Loader::build_info()
288 load_sub(obj.build_info);
291 void Component::Loader::install_map()
293 load_sub(obj.install_map, obj.package.get_source_directory());
296 void Component::Loader::use(const string &n)
298 const SourcePackage::ComponentList &components = obj.package.get_components();
299 for(SourcePackage::ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
300 if(i->get_name()==n && i->get_type()==LIBRARY)
302 obj.uses.push_back(&*i);
305 throw invalid_argument("Component::Loader::use");