]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Route InstalledFile target creation through Builder
[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(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
36                 (*i)->prepare();
37 }
38
39 void Component::create_build_info()
40 {
41         BuildInfo final_build_info;
42
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());
46
47         Package::Requirements all_reqs = direct_reqs;
48         for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
49         {
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);
54
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);
59         }
60
61         final_build_info.update_from(package.get_build_info());
62
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();
67
68         final_build_info.update_from(build_info);
69         build_info = final_build_info;
70
71         for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
72         {
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. */
75                 FS::Path base;
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);
82         }
83
84         if(type==LIBRARY || type==MODULE)
85                 if(build_info.libmode<BuildInfo::DYNAMIC)
86                         build_info.libmode = BuildInfo::DYNAMIC;
87
88         if(build_info.libmode<BuildInfo::DYNAMIC)
89         {
90                 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
91                 {
92                         const BuildInfo &ebi = (*i)->get_exported_build_info();
93                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
94                 }
95         }
96 }
97
98 void Component::create_targets() const
99 {
100         Builder &builder = package.get_builder();
101         const Toolchain &toolchain = builder.get_toolchain();
102
103         SourceList source_filenames = collect_source_files();
104
105         string inst_loc;
106         if(type==TARBALL)
107         {
108                 const Tool &tar = toolchain.get_tool("TAR");
109
110                 list<Target *> files;
111                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
112                 {
113                         FileTarget *file = builder.get_vfs().get_target(*i);
114                         if(!file)
115                                 file = new File(builder, package, *i);
116                         files.push_back(file);
117                 }
118
119                 string tarname = name;
120                 if(name=="@src")
121                 {
122                         tarname = package.get_name()+"-"+package.get_version();
123                         files.insert(files.begin(), &package.get_build_file());
124
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);
130                 }
131
132                 Target *result = tar.create_target(files, tarname);
133
134                 builder.get_target("tarballs")->add_dependency(*result);
135
136                 return;
137         }
138         else if(type==INSTALL)
139         {
140                 Target *inst = builder.get_target("install");
141                 const Tool &copy = toolchain.get_tool("CP");
142                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
143                 {
144                         FileTarget *ft;
145                         if(Target *tgt = builder.get_vfs().get_target(*i))
146                                 ft = dynamic_cast<FileTarget *>(tgt);
147                         else
148                                 ft = new File(builder, package, *i);
149                         inst->add_dependency(*copy.create_target(*ft, name));
150                 }
151         }
152         else if(type==DATAFILE)
153         {
154                 const Tool &dcomp = toolchain.get_tool("DATA");
155
156                 File *source;
157                 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
158                         source = dynamic_cast<File *>(tgt);
159                 else
160                         source = new File(builder, package, source_filenames.front());
161                 Target *result = dcomp.create_target(*source);
162
163                 builder.add_primary_target(*result);
164                 if(install)
165                         builder.add_installed_target(*result);
166         }
167
168         if(type==PROGRAM || type==LIBRARY || type==MODULE)
169         {
170                 list<Target *> objs;
171                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
172                 {
173                         string ext = FS::extpart(FS::basename(*i));
174                         const Tool *tool = toolchain.get_tool_for_suffix(ext, true);
175                         if(tool)
176                         {
177                                 Target *src = tool->create_source(*this, *i);
178                                 if(!src)
179                                         continue;
180
181                                 if(tool->accepts_suffix(ext))
182                                 {
183                                         Target *obj = tool->create_target(*src);
184                                         objs.push_back(obj);
185                                 }
186
187                                 if(type==LIBRARY && install && dynamic_cast<FileTarget *>(src)->is_installable())
188                                         builder.add_installed_target(*src);
189                         }
190                 }
191
192                 const Tool &linker = toolchain.get_tool("LINK");
193
194                 list<Target *> results;
195                 if(type==LIBRARY)
196                 {
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));
200                 }
201                 else if(type==MODULE)
202                         results.push_back(linker.create_target(objs, "shared"));
203                 else
204                         results.push_back(linker.create_target(objs));
205
206                 const Target::Dependencies &world_deps = builder.get_target("world")->get_dependencies();
207                 for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
208                 {
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. */
212                         bool found = false;
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))
215                                 {
216                                         results.front()->add_dependency(**j);
217                                         found = true;
218                                         break;
219                                 }
220                         if(!found)
221                                 builder.problem(package.get_name(), format("Can't find static library %s for component %s", (*i)->get_name(), name));
222                 }
223
224                 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
225                 {
226                         builder.add_primary_target(**i);
227                         if(install)
228                                 builder.add_installed_target(**i);
229                 }
230         }
231 }
232
233 Component::SourceList Component::collect_source_files() const
234 {
235         SourceList files;
236         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
237         {
238                 FS::Path path(*i);
239                 if(FS::is_dir(path))
240                 {
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);
245                 }
246                 else
247                         files.push_back(path);
248         }
249
250         return files;
251 }
252
253
254 Component::Loader::Loader(Component &c):
255         DataFile::ObjectLoader<Component>(c)
256 {
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);
264 }
265
266 void Component::Loader::source(const string &s)
267 {
268         obj.sources.push_back((obj.package.get_source_directory()/s).str());
269 }
270
271 void Component::Loader::require(const string &n)
272 {
273         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
274         if(req)
275                 obj.requires.push_back(req);
276 }
277
278 void Component::Loader::build_info()
279 {
280         load_sub(obj.build_info);
281 }
282
283 void Component::Loader::install_map()
284 {
285         load_sub(obj.install_map, obj.package.get_source_directory());
286 }
287
288 void Component::Loader::use(const string &n)
289 {
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)
293                 {
294                         obj.uses.push_back(&*i);
295                         return;
296                 }
297         throw invalid_argument("Component::Loader::use");
298 }