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