]> git.tdb.fi Git - builder.git/blob - source/component.cpp
More reporting about reading and writing files and traversing directories
[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         pkg(p),
27         type(t),
28         name(n),
29         install(false),
30         deflt(true)
31 { }
32
33 void Component::configure(const StringMap &opts, unsigned flag)
34 {
35         for(StringList::iterator i=sources.begin(); i!=sources.end(); ++i)
36                 *i = (pkg.get_source()/pkg.expand_string(*i)).str();
37
38         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
39                 (*i)->configure(opts, flag&2);
40 }
41
42 void Component::create_build_info()
43 {
44         const PackageList &pkg_reqs = pkg.get_requires();
45         PackageList direct_reqs = requires;
46         direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
47
48         PackageList all_reqs = direct_reqs;
49         for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
50         {
51                 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
52                 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
53                         level = BuildInfo::DEPENDENCY;
54                 build_info.update_from((*i)->get_exported_binfo(), level);
55
56                 const PackageList &reqs = (*i)->get_requires();
57                 for(PackageList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
58                         if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
59                                 all_reqs.push_back(*j);
60         }
61
62         build_info.update_from(pkg.get_build_info());
63
64         for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
65                 *i = (pkg.get_source() / *i).str();
66         for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
67                 *i = (pkg.get_source() / *i).str();
68
69         if(type==LIBRARY || type==MODULE)
70                 if(build_info.libmode<BuildInfo::DYNAMIC)
71                         build_info.libmode = BuildInfo::DYNAMIC;
72
73         if(build_info.libmode<BuildInfo::DYNAMIC)
74         {
75                 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
76                 {
77                         const BuildInfo &ebi = (*i)->get_exported_binfo();
78                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
79                 }
80         }
81 }
82
83 void Component::create_targets() const
84 {
85         Builder &builder = pkg.get_builder();
86         const Toolchain &toolchain = builder.get_toolchain();
87         Target *world = builder.get_target("world");
88         Target *def_tgt = builder.get_target("default");
89
90         PathList source_filenames = collect_source_files();
91         list<Target *> inst_list;
92
93         string inst_loc;
94         if(type==TARBALL)
95         {
96                 //const Tool &tar = toolchain.get_tool("TAR");
97
98                 string tarname = name;
99                 if(name=="@src")
100                 {
101                         tarname = pkg.get_name()+"-"+pkg.get_version();
102                         source_filenames.push_back(pkg.get_source()/"Build");
103                 }
104
105                 list<Target *> files;
106                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
107                 {
108                         FileTarget *file = builder.get_vfs().get_target(*i);
109                         if(!file)
110                                 file = new File(builder, *i);
111                         files.push_back(file);
112                 }
113
114                 if(name=="@src")
115                 {
116                         const Builder::TargetMap &targets = builder.get_targets();
117                         for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
118                                 if(i->second->get_package()==&pkg && !i->second->is_buildable())
119                                         files.push_back(i->second);
120                 }
121
122                 /* XXX The source files don't have a package at the moment, so we can't
123                 create the tarball target until things get fixed up a bit */
124                 /*Target *result = tar.create_target(files, tarname);
125
126                 Target *tarballs_tgt = builder.get_target("tarballs");
127                 tarballs_tgt->add_depend(*result);*/
128
129                 return;
130         }
131         else if(type==INSTALL)
132         {
133                 inst_loc = name;
134                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
135                 {
136                         FileTarget *ft;
137                         if(Target *tgt = builder.get_vfs().get_target(*i))
138                                 ft = dynamic_cast<FileTarget *>(tgt);
139                         else
140                                 ft = new File(builder, pkg, *i);
141                         inst_list.push_back(ft);
142                 }
143         }
144         else if(type==DATAFILE)
145         {
146                 const Tool &dcomp = toolchain.get_tool("DATA");
147
148                 File *source;
149                 if(Target *tgt = builder.get_vfs().get_target(source_filenames.front()))
150                         source = dynamic_cast<File *>(tgt);
151                 else
152                         source = new File(builder, pkg, source_filenames.front());
153                 Target *result = dcomp.create_target(*source);
154
155                 if(&pkg==builder.get_main_package() && deflt)
156                         def_tgt->add_depend(*result);
157                 else
158                         world->add_depend(*result);
159                 if(install)
160                         inst_list.push_back(result);
161         }
162
163         if(type==PROGRAM || type==LIBRARY || type==MODULE)
164         {
165                 list<Target *> objs;
166                 for(PathList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
167                 {
168                         string ext = FS::extpart(FS::basename(*i));
169                         const Tool *tool = toolchain.get_tool_for_suffix(ext, true);
170                         if(tool)
171                         {
172                                 Target *src = tool->create_source(*this, *i);
173                                 if(tool->accepts_suffix(ext))
174                                 {
175                                         Target *obj = tool->create_target(*src);
176                                         objs.push_back(obj);
177                                 }
178
179                                 if(type==LIBRARY && install && !dynamic_cast<FileTarget *>(src)->get_install_location().empty())
180                                         inst_list.push_back(src);
181                         }
182                 }
183
184                 const Tool &linker = toolchain.get_tool("LINK");
185
186                 list<Target *> results;
187                 if(type==LIBRARY)
188                 {
189                         const Tool &archiver = toolchain.get_tool("AR");
190                         results.push_back(linker.create_target(objs, "shared"));
191                         results.push_back(archiver.create_target(objs));
192                 }
193                 else if(type==MODULE)
194                         results.push_back(linker.create_target(objs, "shared"));
195                 else
196                         results.push_back(linker.create_target(objs));
197
198                 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
199                 {
200                         if(&pkg==builder.get_main_package() && deflt)
201                                 def_tgt->add_depend(**i);
202                         else
203                                 world->add_depend(**i);
204                         if(install)
205                                 inst_list.push_back(*i);
206                 }
207         }
208
209         Target *inst_tgt = builder.get_target("install");
210         const Tool &copy = toolchain.get_tool("CP");
211         for(list<Target *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
212         {
213                 Target *inst = copy.create_target(**i, inst_loc);
214                 inst_tgt->add_depend(*inst);
215         }
216 }
217
218 PathList Component::collect_source_files() const
219 {
220         PathList files;
221         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
222         {
223                 FS::Path path(*i);
224                 if(FS::is_dir(path))
225                 {
226                         pkg.get_builder().get_logger().log("files", format("Traversing %s", path));
227                         list<string> sfiles = list_files(path);
228                         for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
229                                 files.push_back(path / *j);
230                 }
231                 else
232                         files.push_back(path);
233         }
234
235         return files;
236 }
237
238
239 Component::Loader::Loader(Component &c):
240         DataFile::ObjectLoader<Component>(c)
241 {
242         add("source",          &Loader::source);
243         add("install",         &Component::install);
244         add("install_map",     &Loader::install_map);
245         add("build_info",      &Loader::build_info);
246         add("require",         &Loader::require);
247         add("default",         &Component::deflt);
248 }
249
250 void Component::Loader::source(const string &s)
251 {
252         obj.sources.push_back(s);
253 }
254
255 void Component::Loader::require(const string &n)
256 {
257         Package *req = obj.pkg.get_builder().get_package_manager().find_package(n);
258         if(req)
259                 obj.requires.push_back(req);
260 }
261
262 void Component::Loader::build_info()
263 {
264         load_sub(obj.build_info);
265 }
266
267 void Component::Loader::install_map()
268 {
269         load_sub(obj.install_map, obj.pkg.get_source());
270 }