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"
16 #include "executable.h"
20 #include "objectfile.h"
21 #include "sharedlibrary.h"
22 #include "sourcepackage.h"
23 #include "staticlibrary.h"
30 Component::Component(SourcePackage &p, Type t, const string &n):
40 void Component::create_build_info()
42 build_info.add(pkg.get_build_info());
44 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
45 build_info.add((*i)->get_exported_binfo());
47 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
48 *i=(pkg.get_source() / *i).str();
49 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
50 *i=(pkg.get_source() / *i).str();
52 if(pkg.get_library_mode()!=DYNAMIC)
54 // XXX This may pull in some unnecessary libpaths too. More thought required.
55 PackageList reqs=pkg.collect_requires();
56 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
58 const BuildInfo &ebi=(*i)->get_exported_binfo();
59 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
65 string strip=pkg.get_config().get_option("strip").value;
66 if(lexical_cast<bool>(strip))
67 build_info.ldflags.push_back("-s");
72 build_info.ldflags.push_back("-rdynamic");
73 build_info.libs.push_back("dl");
77 const PathList &host_src=module_host->get_sources();
78 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
79 build_info.incpath.push_back(i->str());
85 void Component::create_targets() const
87 Builder &builder=pkg.get_builder();
88 Target *world=builder.get_target("world");
89 Target *def_tgt=builder.get_target("default");
91 PathList files=collect_source_files();
92 list<FileTarget *> inst_list;
99 tarname=pkg.get_name()+"-"+pkg.get_version();
100 TarBall *result=new TarBall(builder, pkg, tarname);
104 const TargetMap &targets=builder.get_targets();
105 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
106 if(i->second->get_package()==&pkg && !i->second->is_buildable())
107 result->add_depend(i->second);
108 files.push_back(pkg.get_source()/"Build");
111 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
114 if(Target *tgt=builder.get_target(i->str()))
115 ft=dynamic_cast<FileTarget *>(tgt);
117 ft=new File(builder, *i);
118 result->add_depend(ft);
121 Target *tarbls_tgt=builder.get_target("tarballs");
122 tarbls_tgt->add_depend(result);
126 else if(type==INSTALL)
129 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
132 if(Target *tgt=builder.get_target(i->str()))
133 ft=dynamic_cast<FileTarget *>(tgt);
135 ft=new File(builder, pkg, *i);
136 inst_list.push_back(ft);
141 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
143 string ext=FS::extpart(FS::basename(*i));
146 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
148 hdr=new Header(builder, *this, i->str());
150 // Install headers if requested
151 if(type==HEADERS && install)
152 inst_list.push_back(hdr);
157 if(type==PROGRAM || type==LIBRARY || type==MODULE)
159 list<ObjectFile *> objs;
160 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
162 string ext=FS::extpart(FS::basename(*i));
163 if((ext==".cpp" || ext==".cc" || ext==".c"))
165 SourceFile *src=new SourceFile(builder, *this, i->str());
166 ObjectFile *obj=new ObjectFile(builder, *this, *src);
171 list<FileTarget *> results;
174 results.push_back(new SharedLibrary(builder, *this, objs));
175 results.push_back(new StaticLibrary(builder, *this, objs));
178 results.push_back(new Executable(builder, *this, objs));
180 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
182 if(&pkg==builder.get_main_package() && deflt)
183 def_tgt->add_depend(*i);
185 world->add_depend(*i);
187 inst_list.push_back(*i);
191 Target *inst_tgt=builder.get_target("install");
192 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
193 inst_tgt->add_depend(new Install(builder, pkg, **i, inst_loc));
196 PathList Component::collect_source_files() const
199 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
203 list<string> sfiles=list_files(*i);
204 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
205 files.push_back(*i / *j);
215 Component::Loader::Loader(Component &c):
218 add("source", &Loader::source);
219 add("install", &Component::install);
220 add("install_headers", &Loader::install_headers);
221 add("build_info", &Loader::build_info);
222 add("require", &Loader::require);
223 add("modular", &Loader::modular);
224 add("host", &Loader::host);
225 add("default", &Component::deflt);
228 void Component::Loader::finish()
230 if(!inst_hdr.empty())
232 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
233 hdrcomp.sources=comp.sources;
234 hdrcomp.install=true;
235 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
239 void Component::Loader::source(const string &s)
241 comp.sources.push_back(comp.pkg.get_source()/s);
244 void Component::Loader::require(const string &n)
246 Package *req=comp.pkg.get_builder().get_package(n);
248 comp.requires.push_back(req);
251 void Component::Loader::modular()
253 if(comp.type!=PROGRAM)
254 throw Exception("Only programs can be modular");
258 void Component::Loader::host(const string &n)
260 const ComponentList &comps=comp.pkg.get_components();
261 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
264 if(i->get_type()!=PROGRAM || !i->is_modular())
265 throw Exception("Module host must be a modular program");
266 comp.module_host=&*i;
270 throw KeyError("Unknown component", n);
273 void Component::Loader::install_headers(const string &p)
275 IO::print("%s: Note: install_headers is deprecated\n", get_source());
276 if(comp.type==HEADERS)
285 void Component::Loader::build_info()
287 load_sub(comp.build_info);