]> git.tdb.fi Git - builder.git/blob - source/binarycomponent.cpp
Use qualified name in base class initializer
[builder.git] / source / binarycomponent.cpp
1 #include <msp/fs/utils.h>
2 #include "binarycomponent.h"
3 #include "builder.h"
4 #include "filetarget.h"
5 #include "sourcepackage.h"
6 #include "tool.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 BinaryComponent::BinaryComponent(SourcePackage &p, const string &n, Type t):
12         Component(p, n),
13         type(t)
14 { }
15
16 void BinaryComponent::create_build_info()
17 {
18         Component::create_build_info();
19
20         for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
21         {
22                 /* Select an include path that contains all the sources for this and the
23                 used component.  This should produce a sensible result in most cases. */
24                 FS::Path base;
25                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
26                         base = base.empty() ? *j : FS::common_ancestor(base, *j);
27                 const SourceList &use_sources = (*i)->get_sources();
28                 for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
29                         base = FS::common_ancestor(base, *j);
30                 build_info.incpath.push_back(base);
31                 build_info.libs.push_back((*i)->get_name());
32                 if(!(*i)->get_install())
33                 {
34                         build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
35                         build_info.libpath.push_back((*i)->get_package().get_source_directory());
36                 }
37         }
38
39         if(type==LIBRARY || type==MODULE)
40                 if(build_info.libmode<BuildInfo::DYNAMIC)
41                         build_info.libmode = BuildInfo::DYNAMIC;
42 }
43
44 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
45 {
46         if(type==LIBRARY)
47                 binfo.libs.push_back(name);
48 }
49
50 void BinaryComponent::create_targets() const
51 {
52         Builder &builder = package.get_builder();
53         BuildGraph &build_graph = builder.get_build_graph();
54         const Toolchain &toolchain = builder.get_toolchain();
55         const Toolchain &pkg_tools = package.get_toolchain();
56
57         list<Target *> objs;
58         SourceList source_filenames = collect_source_files();
59         for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
60         {
61                 string ext = FS::extpart(FS::basename(*i));
62                 Target *src = 0;
63
64                 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
65                 if(gen)
66                 {
67                         Target *tmpl = gen->create_source(*this, *i);
68                         if(tmpl)
69                         {
70                                 src = gen->create_target(*tmpl);
71                                 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
72                         }
73                 }
74
75                 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
76                 if(tool)
77                 {
78                         if(!src)
79                                 src = tool->create_source(*this, *i);
80                         if(!src)
81                                 continue;
82
83                         if(tool->accepts_suffix(ext))
84                         {
85                                 Target *obj = tool->create_target(*src);
86                                 objs.push_back(obj);
87                         }
88
89                         if(type==LIBRARY && install)
90                         {
91                                 if(dynamic_cast<FileTarget *>(src)->is_installable())
92                                         build_graph.add_installed_target(*src);
93
94                                 const Target::Dependencies &side_effects = src->get_side_effects();
95                                 for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
96                                         if(dynamic_cast<FileTarget *>(*j)->is_installable())
97                                                 build_graph.add_installed_target(**j);
98                         }
99                 }
100         }
101
102         Tool &linker = toolchain.get_tool("LINK");
103
104         list<Target *> results;
105         if(type==LIBRARY)
106         {
107                 Tool &archiver = toolchain.get_tool("AR");
108                 results.push_back(linker.create_target(objs, "shared"));
109                 results.push_back(archiver.create_target(objs));
110         }
111         else if(type==MODULE)
112                 results.push_back(linker.create_target(objs, "shared"));
113         else
114                 results.push_back(linker.create_target(objs));
115
116         for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
117         {
118                 build_graph.add_primary_target(**i);
119                 if(install)
120                         build_graph.add_installed_target(**i);
121         }
122 }
123
124 BinaryComponent::Loader::Loader(BinaryComponent &c):
125         DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
126 {
127         add("use", &Loader::use);
128 }
129
130 void BinaryComponent::Loader::use(const string &n)
131 {
132         const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
133         if(!comp || comp->type!=LIBRARY)
134                 throw logic_error(n+" is not a library");
135
136         obj.uses.push_back(comp);
137 }