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::configure(const StringMap &opts, unsigned flag)
35 for(StringList::iterator i=sources.begin(); i!=sources.end(); ++i)
36 *i = (pkg.get_source()/pkg.expand_string(*i)).str();
38 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
39 (*i)->configure(opts, flag&2);
42 void Component::create_build_info()
44 const PackageList &pkg_reqs = pkg.get_requires();
45 PackageList direct_reqs = requires;
46 direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
48 PackageList all_reqs = direct_reqs;
49 for(PackageList::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 build_info.update_from((*i)->get_exported_binfo(), level);
56 const PackageList &reqs = (*i)->get_requires();
57 for(PackageList::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 build_info.update_from(pkg.get_build_info());
64 for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
65 *i = (pkg.get_source() / *i).str();
66 for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
67 *i = (pkg.get_source() / *i).str();
69 if(pkg.get_library_mode()!=DYNAMIC)
71 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
73 const BuildInfo &ebi = (*i)->get_exported_binfo();
74 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
79 build_info.strip = lexical_cast<bool>(pkg.get_config().get_option("strip").value);
82 void Component::create_targets() const
84 Builder &builder = pkg.get_builder();
85 const Toolchain &toolchain = builder.get_toolchain();
86 Target *world = builder.get_target("world");
87 Target *def_tgt = builder.get_target("default");
89 PathList source_filenames = collect_source_files();
90 list<Target *> inst_list;
95 //const Tool &tar = toolchain.get_tool("TAR");
97 string tarname = name;
100 tarname = pkg.get_name()+"-"+pkg.get_version();
101 source_filenames.push_back(pkg.get_source()/"Build");
104 list<Target *> files;
105 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
107 FileTarget *file = builder.get_vfs().get_target(*i);
109 file = new File(builder, *i);
110 files.push_back(file);
115 const Builder::TargetMap &targets = builder.get_targets();
116 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
117 if(i->second->get_package()==&pkg && !i->second->is_buildable())
118 files.push_back(i->second);
121 /* XXX The source files don't have a package at the moment, so we can't
122 create the tarball target until things get fixed up a bit */
123 /*Target *result = tar.create_target(files, tarname);
125 Target *tarballs_tgt = builder.get_target("tarballs");
126 tarballs_tgt->add_depend(result);*/
130 else if(type==INSTALL)
133 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
136 if(Target *tgt = builder.get_vfs().get_target(*i))
137 ft = dynamic_cast<FileTarget *>(tgt);
139 ft = new File(builder, pkg, *i);
140 inst_list.push_back(ft);
143 else if(type==DATAFILE)
145 const Tool &dcomp = toolchain.get_tool("DATA");
148 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
149 source = dynamic_cast<File *>(tgt);
151 source = new File(builder, pkg, source_filenames.front());
152 Target *result = dcomp.create_target(*source);
154 if(&pkg==builder.get_main_package() && deflt)
155 def_tgt->add_depend(result);
157 world->add_depend(result);
159 inst_list.push_back(result);
163 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
165 string ext = FS::extpart(FS::basename(*i));
168 FileTarget *hdr = builder.get_vfs().get_target(*i);
170 hdr = new CSourceFile(builder, *this, *i);
172 // Install headers if requested
173 if(type==HEADERS && install)
174 inst_list.push_back(hdr);
179 if(type==PROGRAM || type==LIBRARY || type==MODULE)
182 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
184 string ext = FS::extpart(FS::basename(*i));
185 const Tool *tool = toolchain.get_tool_for_suffix(ext);
188 Target *src = tool->create_source(*this, *i);
189 Target *obj = tool->create_target(*src);
194 const Tool &linker = toolchain.get_tool("LINK");
196 list<Target *> results;
199 const Tool &archiver = toolchain.get_tool("AR");
200 results.push_back(linker.create_target(objs, "shared"));
201 results.push_back(archiver.create_target(objs));
203 else if(type==MODULE)
204 results.push_back(linker.create_target(objs, "shared"));
206 results.push_back(linker.create_target(objs));
208 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
210 if(&pkg==builder.get_main_package() && deflt)
211 def_tgt->add_depend(*i);
213 world->add_depend(*i);
215 inst_list.push_back(*i);
219 Target *inst_tgt = builder.get_target("install");
220 const Tool © = toolchain.get_tool("CP");
221 for(list<Target *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
223 Target *inst = copy.create_target(**i, inst_loc);
224 inst_tgt->add_depend(inst);
228 PathList Component::collect_source_files() const
231 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
236 list<string> sfiles = list_files(path);
237 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
238 files.push_back(path / *j);
241 files.push_back(path);
248 Component::Loader::Loader(Component &c):
251 add("source", &Loader::source);
252 add("install", &Component::install);
253 add("build_info", &Loader::build_info);
254 add("require", &Loader::require);
255 add("default", &Component::deflt);
258 void Component::Loader::finish()
260 if(!inst_hdr.empty())
262 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
263 hdrcomp.sources = comp.sources;
264 hdrcomp.install = true;
265 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
269 void Component::Loader::source(const string &s)
271 comp.sources.push_back(s);
274 void Component::Loader::require(const string &n)
276 Package *req = comp.pkg.get_builder().get_package_manager().find_package(n);
278 comp.requires.push_back(req);
281 void Component::Loader::build_info()
283 load_sub(comp.build_info);