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(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
39 void Component::create_build_info()
41 const PackageList &pkg_reqs = pkg.get_requires();
42 PackageList direct_reqs = requires;
43 direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
45 PackageList all_reqs = direct_reqs;
46 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
48 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
49 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
50 level = BuildInfo::DEPENDENCY;
51 build_info.update_from((*i)->get_exported_binfo(), level);
53 const PackageList &reqs = (*i)->get_requires();
54 for(PackageList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
55 if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
56 all_reqs.push_back(*j);
59 build_info.update_from(pkg.get_build_info());
61 for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
62 *i = (pkg.get_source() / *i).str();
63 for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
64 *i = (pkg.get_source() / *i).str();
66 if(type==LIBRARY || type==MODULE)
67 if(build_info.libmode<BuildInfo::DYNAMIC)
68 build_info.libmode = BuildInfo::DYNAMIC;
70 if(build_info.libmode<BuildInfo::DYNAMIC)
72 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
74 const BuildInfo &ebi = (*i)->get_exported_binfo();
75 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
80 void Component::create_targets() const
82 Builder &builder = pkg.get_builder();
83 const Toolchain &toolchain = builder.get_toolchain();
84 Target *world = builder.get_target("world");
85 Target *def_tgt = builder.get_target("default");
87 PathList source_filenames = collect_source_files();
88 list<Target *> inst_list;
93 //const Tool &tar = toolchain.get_tool("TAR");
95 string tarname = name;
98 tarname = pkg.get_name()+"-"+pkg.get_version();
99 source_filenames.push_back(pkg.get_source()/"Build");
102 list<Target *> files;
103 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
105 FileTarget *file = builder.get_vfs().get_target(*i);
107 file = new File(builder, *i);
108 files.push_back(file);
113 const Builder::TargetMap &targets = builder.get_targets();
114 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
115 if(i->second->get_package()==&pkg && !i->second->is_buildable())
116 files.push_back(i->second);
119 /* XXX The source files don't have a package at the moment, so we can't
120 create the tarball target until things get fixed up a bit */
121 /*Target *result = tar.create_target(files, tarname);
123 Target *tarballs_tgt = builder.get_target("tarballs");
124 tarballs_tgt->add_depend(*result);*/
128 else if(type==INSTALL)
131 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
134 if(Target *tgt = builder.get_vfs().get_target(*i))
135 ft = dynamic_cast<FileTarget *>(tgt);
137 ft = new File(builder, pkg, *i);
138 inst_list.push_back(ft);
141 else if(type==DATAFILE)
143 const Tool &dcomp = toolchain.get_tool("DATA");
146 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
147 source = dynamic_cast<File *>(tgt);
149 source = new File(builder, pkg, source_filenames.front());
150 Target *result = dcomp.create_target(*source);
152 if(&pkg==builder.get_main_package() && deflt)
153 def_tgt->add_depend(*result);
155 world->add_depend(*result);
157 inst_list.push_back(result);
160 if(type==PROGRAM || type==LIBRARY || type==MODULE)
163 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
165 string ext = FS::extpart(FS::basename(*i));
166 const Tool *tool = toolchain.get_tool_for_suffix(ext, true);
169 Target *src = tool->create_source(*this, *i);
170 if(tool->accepts_suffix(ext))
172 Target *obj = tool->create_target(*src);
176 if(type==LIBRARY && install && !dynamic_cast<FileTarget *>(src)->get_install_location().empty())
177 inst_list.push_back(src);
181 const Tool &linker = toolchain.get_tool("LINK");
183 list<Target *> results;
186 const Tool &archiver = toolchain.get_tool("AR");
187 results.push_back(linker.create_target(objs, "shared"));
188 results.push_back(archiver.create_target(objs));
190 else if(type==MODULE)
191 results.push_back(linker.create_target(objs, "shared"));
193 results.push_back(linker.create_target(objs));
195 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
197 if(&pkg==builder.get_main_package() && deflt)
198 def_tgt->add_depend(**i);
200 world->add_depend(**i);
202 inst_list.push_back(*i);
206 Target *inst_tgt = builder.get_target("install");
207 const Tool © = toolchain.get_tool("CP");
208 for(list<Target *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
210 Target *inst = copy.create_target(**i, inst_loc);
211 inst_tgt->add_depend(*inst);
215 PathList Component::collect_source_files() const
218 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
223 pkg.get_builder().get_logger().log("files", format("Traversing %s", path));
224 list<string> sfiles = list_files(path);
225 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
226 files.push_back(path / *j);
229 files.push_back(path);
236 Component::Loader::Loader(Component &c):
237 DataFile::ObjectLoader<Component>(c)
239 add("source", &Loader::source);
240 add("install", &Component::install);
241 add("install_map", &Loader::install_map);
242 add("build_info", &Loader::build_info);
243 add("require", &Loader::require);
244 add("default", &Component::deflt);
247 void Component::Loader::source(const string &s)
249 obj.sources.push_back((obj.pkg.get_source()/s).str());
252 void Component::Loader::require(const string &n)
254 Package *req = obj.pkg.get_builder().get_package_manager().find_package(n);
256 obj.requires.push_back(req);
259 void Component::Loader::build_info()
261 load_sub(obj.build_info);
264 void Component::Loader::install_map()
266 load_sub(obj.install_map, obj.pkg.get_source());