]> git.tdb.fi Git - builder.git/blob - source/binarycomponent.cpp
Save caches before starting the build
[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_output_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::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                         list<Target *> templates;
68                         templates.push_back(gen->create_source(*this, *i));
69
70                         Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
71                         if(processing_unit!=Tool::ONE_FILE)
72                         {
73                                 FS::Path source_dir = FS::dirname(*i);
74                                 SourceList::iterator j = i;
75                                 for(++j; j!=source_filenames.end(); )
76                                 {
77                                         if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
78                                                 pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
79                                         {
80                                                 templates.push_back(gen->create_source(*this, *j));
81                                                 // Remove additional files so they won't get processed again
82                                                 source_filenames.erase(j++);
83                                         }
84                                         else
85                                                 ++j;
86                                 }
87                         }
88
89                         src = gen->create_target(templates);
90                         ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
91                 }
92
93                 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
94                 if(tool)
95                 {
96                         if(!src)
97                                 src = tool->create_source(*this, *i);
98                         if(!src)
99                                 continue;
100
101                         if(tool->accepts_suffix(ext))
102                         {
103                                 Target *obj = tool->create_target(*src);
104                                 objs.push_back(obj);
105                         }
106
107                         if(type==LIBRARY && install)
108                         {
109                                 if(dynamic_cast<FileTarget *>(src)->is_installable())
110                                         build_graph.add_installed_target(*src);
111
112                                 const Target::Dependencies &side_effects = src->get_side_effects();
113                                 for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
114                                         if(dynamic_cast<FileTarget *>(*j)->is_installable())
115                                                 build_graph.add_installed_target(**j);
116                         }
117                 }
118         }
119
120         Tool &linker = toolchain.get_tool("LINK");
121
122         list<Target *> results;
123         if(type==LIBRARY)
124         {
125                 Tool &archiver = toolchain.get_tool("AR");
126                 results.push_back(linker.create_target(objs, "shared"));
127                 results.push_back(archiver.create_target(objs));
128         }
129         else if(type==MODULE)
130                 results.push_back(linker.create_target(objs, "shared"));
131         else
132                 results.push_back(linker.create_target(objs));
133
134         for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
135         {
136                 build_graph.add_primary_target(**i);
137                 if(install)
138                         build_graph.add_installed_target(**i);
139         }
140 }
141
142 BinaryComponent::Loader::Loader(BinaryComponent &c):
143         DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
144 {
145         add("use", &Loader::use);
146 }
147
148 void BinaryComponent::Loader::use(const string &n)
149 {
150         const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
151         if(!comp || comp->type!=LIBRARY)
152                 throw logic_error(n+" is not a library");
153
154         obj.uses.push_back(comp);
155 }