]> git.tdb.fi Git - builder.git/blob - source/binarycomponent.cpp
Use auto type for iterator variables
[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(const Component *u: uses)
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(const FS::Path &s: sources)
26                         base = base.empty() ? s : FS::common_ancestor(base, s);
27                 for(const FS::Path &s: u->get_sources())
28                         base = FS::common_ancestor(base, s);
29                 build_info.incpath.push_back(base);
30                 build_info.libs.push_back(u->get_name());
31                 if(!u->get_install())
32                 {
33                         build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
34                         build_info.libpath.push_back(u->get_package().get_output_directory());
35                 }
36         }
37
38         if(type==LIBRARY || type==MODULE)
39                 if(build_info.libmode<BuildInfo::DYNAMIC)
40                         build_info.libmode = BuildInfo::DYNAMIC;
41 }
42
43 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
44 {
45         if(type==LIBRARY)
46                 binfo.libs.push_back(name);
47 }
48
49 void BinaryComponent::create_targets() const
50 {
51         Builder &builder = package.get_builder();
52         BuildGraph &build_graph = builder.get_build_graph();
53         const Toolchain &toolchain = builder.get_toolchain();
54         const Toolchain &pkg_tools = package.get_toolchain();
55
56         list<Target *> objs;
57         SourceList source_filenames = collect_source_files();
58         for(auto i=source_filenames.begin(); i!=source_filenames.end(); ++i)
59         {
60                 string ext = FS::extpart(FS::basename(*i));
61                 Target *src = 0;
62
63                 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
64                 if(gen)
65                 {
66                         list<Target *> templates;
67                         templates.push_back(gen->create_source(*this, *i));
68
69                         Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
70                         if(processing_unit!=Tool::ONE_FILE)
71                         {
72                                 FS::Path source_dir = FS::dirname(*i);
73                                 auto j = i;
74                                 for(++j; j!=source_filenames.end(); )
75                                 {
76                                         if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
77                                                 pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
78                                         {
79                                                 templates.push_back(gen->create_source(*this, *j));
80                                                 // Remove additional files so they won't get processed again
81                                                 source_filenames.erase(j++);
82                                         }
83                                         else
84                                                 ++j;
85                                 }
86                         }
87
88                         src = gen->create_target(templates);
89                         ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
90                 }
91
92                 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
93                 if(tool)
94                 {
95                         if(!src)
96                                 src = tool->create_source(*this, *i);
97                         if(!src)
98                                 continue;
99
100                         if(tool->accepts_suffix(ext))
101                         {
102                                 Target *obj = tool->create_target(*src);
103                                 objs.push_back(obj);
104                         }
105
106                         if(type==LIBRARY && install)
107                         {
108                                 if(dynamic_cast<FileTarget *>(src)->is_installable())
109                                         build_graph.add_installed_target(*src);
110
111                                 for(Target *s: src->get_side_effects())
112                                         if(dynamic_cast<FileTarget *>(s)->is_installable())
113                                                 build_graph.add_installed_target(*s);
114                         }
115                 }
116         }
117
118         Tool &linker = toolchain.get_tool("LINK");
119
120         list<Target *> results;
121         if(type==LIBRARY)
122         {
123                 Tool &archiver = toolchain.get_tool("AR");
124                 results.push_back(linker.create_target(objs, "shared"));
125                 results.push_back(archiver.create_target(objs));
126         }
127         else if(type==MODULE)
128                 results.push_back(linker.create_target(objs, "shared"));
129         else
130                 results.push_back(linker.create_target(objs));
131
132         for(Target *r: results)
133         {
134                 build_graph.add_primary_target(*r);
135                 if(install)
136                         build_graph.add_installed_target(*r);
137         }
138 }
139
140 BinaryComponent::Loader::Loader(BinaryComponent &c):
141         DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
142 {
143         add("use", &Loader::use);
144 }
145
146 void BinaryComponent::Loader::use(const string &n)
147 {
148         const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
149         if(!comp || comp->type!=LIBRARY)
150                 throw logic_error(n+" is not a library");
151
152         obj.uses.push_back(comp);
153 }