]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Un-abbreviate some function and variable names
[builder.git] / source / component.cpp
1 #include <algorithm>
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 "builder.h"
8 #include "component.h"
9 #include "csourcefile.h"
10 #include "datafile.h"
11 #include "executable.h"
12 #include "file.h"
13 #include "objectfile.h"
14 #include "sharedlibrary.h"
15 #include "sourcepackage.h"
16 #include "staticlibrary.h"
17 #include "tarball.h"
18 #include "target.h"
19 #include "tool.h"
20 #include "toolchain.h"
21
22 using namespace std;
23 using namespace Msp;
24
25 Component::Component(SourcePackage &p, Type t, const string &n):
26         package(p),
27         type(t),
28         name(n),
29         install(false),
30         deflt(true)
31 { }
32
33 void Component::prepare()
34 {
35         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
36                 (*i)->prepare();
37 }
38
39 void Component::create_build_info()
40 {
41         const PackageList &pkg_reqs = package.get_required_packages();
42         PackageList direct_reqs = requires;
43         direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
44
45         PackageList all_reqs = direct_reqs;
46         for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
47         {
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_build_info(), level);
52
53                 const PackageList &reqs = (*i)->get_required_packages();
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);
57         }
58
59         build_info.update_from(package.get_build_info());
60
61         for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
62                 *i = (package.get_source_directory() / *i).str();
63         for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
64                 *i = (package.get_source_directory() / *i).str();
65
66         if(type==LIBRARY || type==MODULE)
67                 if(build_info.libmode<BuildInfo::DYNAMIC)
68                         build_info.libmode = BuildInfo::DYNAMIC;
69
70         if(build_info.libmode<BuildInfo::DYNAMIC)
71         {
72                 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
73                 {
74                         const BuildInfo &ebi = (*i)->get_exported_build_info();
75                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
76                 }
77         }
78 }
79
80 void Component::create_targets() const
81 {
82         Builder &builder = package.get_builder();
83         const Toolchain &toolchain = builder.get_toolchain();
84         Target *world = builder.get_target("world");
85         Target *def_tgt = builder.get_target("default");
86
87         PathList source_filenames = collect_source_files();
88         list<Target *> inst_list;
89
90         string inst_loc;
91         if(type==TARBALL)
92         {
93                 //const Tool &tar = toolchain.get_tool("TAR");
94
95                 string tarname = name;
96                 if(name=="@src")
97                 {
98                         tarname = package.get_name()+"-"+package.get_version();
99                         source_filenames.push_back(package.get_source_directory()/"Build");
100                 }
101
102                 list<Target *> files;
103                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
104                 {
105                         FileTarget *file = builder.get_vfs().get_target(*i);
106                         if(!file)
107                                 file = new File(builder, *i);
108                         files.push_back(file);
109                 }
110
111                 if(name=="@src")
112                 {
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()==&package && !i->second->is_buildable())
116                                         files.push_back(i->second);
117                 }
118
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);
122
123                 Target *tarballs_tgt = builder.get_target("tarballs");
124                 tarballs_tgt->add_dependency(*result);*/
125
126                 return;
127         }
128         else if(type==INSTALL)
129         {
130                 inst_loc = name;
131                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
132                 {
133                         FileTarget *ft;
134                         if(Target *tgt = builder.get_vfs().get_target(*i))
135                                 ft = dynamic_cast<FileTarget *>(tgt);
136                         else
137                                 ft = new File(builder, package, *i);
138                         inst_list.push_back(ft);
139                 }
140         }
141         else if(type==DATAFILE)
142         {
143                 const Tool &dcomp = toolchain.get_tool("DATA");
144
145                 File *source;
146                 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
147                         source = dynamic_cast<File *>(tgt);
148                 else
149                         source = new File(builder, package, source_filenames.front());
150                 Target *result = dcomp.create_target(*source);
151
152                 if(&package==builder.get_main_package() && deflt)
153                         def_tgt->add_dependency(*result);
154                 else
155                         world->add_dependency(*result);
156                 if(install)
157                         inst_list.push_back(result);
158         }
159
160         if(type==PROGRAM || type==LIBRARY || type==MODULE)
161         {
162                 list<Target *> objs;
163                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
164                 {
165                         string ext = FS::extpart(FS::basename(*i));
166                         const Tool *tool = toolchain.get_tool_for_suffix(ext, true);
167                         if(tool)
168                         {
169                                 Target *src = tool->create_source(*this, *i);
170                                 if(tool->accepts_suffix(ext))
171                                 {
172                                         Target *obj = tool->create_target(*src);
173                                         objs.push_back(obj);
174                                 }
175
176                                 if(type==LIBRARY && install && !dynamic_cast<FileTarget *>(src)->get_install_location().empty())
177                                         inst_list.push_back(src);
178                         }
179                 }
180
181                 const Tool &linker = toolchain.get_tool("LINK");
182
183                 list<Target *> results;
184                 if(type==LIBRARY)
185                 {
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));
189                 }
190                 else if(type==MODULE)
191                         results.push_back(linker.create_target(objs, "shared"));
192                 else
193                         results.push_back(linker.create_target(objs));
194
195                 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
196                 {
197                         if(&package==builder.get_main_package() && deflt)
198                                 def_tgt->add_dependency(**i);
199                         else
200                                 world->add_dependency(**i);
201                         if(install)
202                                 inst_list.push_back(*i);
203                 }
204         }
205
206         Target *inst_tgt = builder.get_target("install");
207         const Tool &copy = toolchain.get_tool("CP");
208         for(list<Target *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
209         {
210                 Target *inst = copy.create_target(**i, inst_loc);
211                 inst_tgt->add_dependency(*inst);
212         }
213 }
214
215 PathList Component::collect_source_files() const
216 {
217         PathList files;
218         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
219         {
220                 FS::Path path(*i);
221                 if(FS::is_dir(path))
222                 {
223                         package.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);
227                 }
228                 else
229                         files.push_back(path);
230         }
231
232         return files;
233 }
234
235
236 Component::Loader::Loader(Component &c):
237         DataFile::ObjectLoader<Component>(c)
238 {
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);
245 }
246
247 void Component::Loader::source(const string &s)
248 {
249         obj.sources.push_back((obj.package.get_source_directory()/s).str());
250 }
251
252 void Component::Loader::require(const string &n)
253 {
254         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
255         if(req)
256                 obj.requires.push_back(req);
257 }
258
259 void Component::Loader::build_info()
260 {
261         load_sub(obj.build_info);
262 }
263
264 void Component::Loader::install_map()
265 {
266         load_sub(obj.install_map, obj.package.get_source_directory());
267 }