3 This file is part of builder
4 Copyright © 2006-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/except.h>
9 #include <msp/fs/dir.h>
10 #include <msp/fs/stat.h>
11 #include <msp/fs/utils.h>
12 #include <msp/io/print.h>
13 #include <msp/strings/lexicalcast.h>
15 #include "component.h"
17 #include "executable.h"
21 #include "objectfile.h"
22 #include "sharedlibrary.h"
23 #include "sourcepackage.h"
24 #include "staticlibrary.h"
31 Component::Component(SourcePackage &p, Type t, const string &n):
41 void Component::create_build_info()
43 build_info.add(pkg.get_build_info());
45 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
46 build_info.add((*i)->get_exported_binfo());
48 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
49 *i=(pkg.get_source() / *i).str();
50 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
51 *i=(pkg.get_source() / *i).str();
53 if(pkg.get_library_mode()!=DYNAMIC)
55 // XXX This may pull in some unnecessary libpaths too. More thought required.
56 PackageList reqs=pkg.collect_requires();
57 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
59 const BuildInfo &ebi=(*i)->get_exported_binfo();
60 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
66 string strip=pkg.get_config().get_option("strip").value;
67 if(lexical_cast<bool>(strip))
68 build_info.ldflags.push_back("-s");
73 build_info.ldflags.push_back("-rdynamic");
74 build_info.libs.push_back("dl");
78 const PathList &host_src=module_host->get_sources();
79 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
80 build_info.incpath.push_back(i->str());
86 void Component::create_targets() const
88 Builder &builder=pkg.get_builder();
89 Target *world=builder.get_target("world");
90 Target *def_tgt=builder.get_target("default");
92 PathList files=collect_source_files();
93 list<FileTarget *> inst_list;
100 tarname=pkg.get_name()+"-"+pkg.get_version();
101 TarBall *result=new TarBall(builder, pkg, tarname);
105 const TargetMap &targets=builder.get_targets();
106 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
107 if(i->second->get_package()==&pkg && !i->second->is_buildable())
108 result->add_depend(i->second);
109 files.push_back(pkg.get_source()/"Build");
112 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
115 if(Target *tgt=builder.get_target(i->str()))
116 ft=dynamic_cast<FileTarget *>(tgt);
118 ft=new File(builder, *i);
119 result->add_depend(ft);
122 Target *tarbls_tgt=builder.get_target("tarballs");
123 tarbls_tgt->add_depend(result);
127 else if(type==INSTALL)
130 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
133 if(Target *tgt=builder.get_target(i->str()))
134 ft=dynamic_cast<FileTarget *>(tgt);
136 ft=new File(builder, pkg, *i);
137 inst_list.push_back(ft);
140 else if(type==DATAFILE)
143 if(Target *tgt=builder.get_target(files.front().str()))
144 source=dynamic_cast<File *>(tgt);
146 source=new File(builder, pkg, files.front());
147 ::DataFile *result=new ::DataFile(builder, *this, *source);
149 if(&pkg==builder.get_main_package() && deflt)
150 def_tgt->add_depend(result);
152 world->add_depend(result);
154 inst_list.push_back(result);
158 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
160 string ext=FS::extpart(FS::basename(*i));
163 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
165 hdr=new Header(builder, *this, i->str());
167 // Install headers if requested
168 if(type==HEADERS && install)
169 inst_list.push_back(hdr);
174 if(type==PROGRAM || type==LIBRARY || type==MODULE)
176 list<ObjectFile *> objs;
177 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
179 string ext=FS::extpart(FS::basename(*i));
180 if((ext==".cpp" || ext==".cc" || ext==".c"))
182 SourceFile *src=new SourceFile(builder, *this, i->str());
183 ObjectFile *obj=new ObjectFile(builder, *this, *src);
188 list<FileTarget *> results;
191 results.push_back(new SharedLibrary(builder, *this, objs));
192 results.push_back(new StaticLibrary(builder, *this, objs));
195 results.push_back(new Executable(builder, *this, objs));
197 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
199 if(&pkg==builder.get_main_package() && deflt)
200 def_tgt->add_depend(*i);
202 world->add_depend(*i);
204 inst_list.push_back(*i);
208 Target *inst_tgt=builder.get_target("install");
209 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
210 inst_tgt->add_depend(new Install(builder, pkg, **i, inst_loc));
213 PathList Component::collect_source_files() const
216 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
220 list<string> sfiles=list_files(*i);
221 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
222 files.push_back(*i / *j);
232 Component::Loader::Loader(Component &c):
235 add("source", &Loader::source);
236 add("install", &Component::install);
237 add("install_headers", &Loader::install_headers);
238 add("build_info", &Loader::build_info);
239 add("require", &Loader::require);
240 add("modular", &Loader::modular);
241 add("host", &Loader::host);
242 add("default", &Component::deflt);
245 void Component::Loader::finish()
247 if(!inst_hdr.empty())
249 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
250 hdrcomp.sources=comp.sources;
251 hdrcomp.install=true;
252 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
256 void Component::Loader::source(const string &s)
258 comp.sources.push_back(comp.pkg.get_source()/s);
261 void Component::Loader::require(const string &n)
263 Package *req=comp.pkg.get_builder().get_package(n);
265 comp.requires.push_back(req);
268 void Component::Loader::modular()
270 if(comp.type!=PROGRAM)
271 throw Exception("Only programs can be modular");
275 void Component::Loader::host(const string &n)
277 const ComponentList &comps=comp.pkg.get_components();
278 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
281 if(i->get_type()!=PROGRAM || !i->is_modular())
282 throw Exception("Module host must be a modular program");
283 comp.module_host=&*i;
287 throw KeyError("Unknown component", n);
290 void Component::Loader::install_headers(const string &p)
292 IO::print("%s: Note: install_headers is deprecated\n", get_source());
293 if(comp.type==HEADERS)
302 void Component::Loader::build_info()
304 load_sub(comp.build_info);