3 This file is part of builder
4 Copyright © 2006-2010 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"
32 Component::Component(SourcePackage &p, Type t, const string &n):
42 void Component::create_build_info()
44 build_info.add(pkg.get_build_info());
46 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
47 build_info.add((*i)->get_exported_binfo());
49 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
50 *i = (pkg.get_source() / *i).str();
51 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
52 *i = (pkg.get_source() / *i).str();
54 if(pkg.get_library_mode()!=DYNAMIC)
56 // XXX This may pull in some unnecessary libpaths too. More thought required.
57 PackageList reqs = pkg.collect_requires();
58 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
60 const BuildInfo &ebi = (*i)->get_exported_binfo();
61 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
67 string strip = pkg.get_config().get_option("strip").value;
68 if(lexical_cast<bool>(strip))
69 build_info.ldflags.push_back("-s");
71 else if(type==LIBRARY)
73 build_info.cflags.push_back("-fPIC");
78 build_info.ldflags.push_back("-rdynamic");
79 build_info.libs.push_back("dl");
83 const PathList &host_src = module_host->get_sources();
84 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
85 build_info.incpath.push_back(i->str());
91 void Component::create_targets() const
93 Builder &builder = pkg.get_builder();
94 Target *world = builder.get_target("world");
95 Target *def_tgt = builder.get_target("default");
97 PathList files = collect_source_files();
98 list<FileTarget *> inst_list;
103 string tarname = name;
105 tarname = pkg.get_name()+"-"+pkg.get_version();
106 TarBall *result = new TarBall(builder, pkg, tarname);
110 const TargetMap &targets = builder.get_targets();
111 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
112 if(i->second->get_package()==&pkg && !i->second->is_buildable())
113 result->add_depend(i->second);
114 files.push_back(pkg.get_source()/"Build");
117 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
120 if(Target *tgt = builder.get_target(i->str()))
121 ft = dynamic_cast<FileTarget *>(tgt);
123 ft = new File(builder, *i);
124 result->add_depend(ft);
127 Target *tarbls_tgt = builder.get_target("tarballs");
128 tarbls_tgt->add_depend(result);
132 else if(type==INSTALL)
135 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
138 if(Target *tgt = builder.get_target(i->str()))
139 ft = dynamic_cast<FileTarget *>(tgt);
141 ft = new File(builder, pkg, *i);
142 inst_list.push_back(ft);
145 else if(type==DATAFILE)
148 if(Target *tgt = builder.get_target(files.front().str()))
149 source = dynamic_cast<File *>(tgt);
151 source = new File(builder, pkg, files.front());
152 ::DataFile *result = new ::DataFile(builder, *this, *source);
154 if(&pkg==builder.get_main_package() && deflt)
155 def_tgt->add_depend(result);
157 world->add_depend(result);
159 inst_list.push_back(result);
163 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
165 string ext = FS::extpart(FS::basename(*i));
168 FileTarget *hdr = dynamic_cast<FileTarget *>(builder.get_target(i->str()));
170 hdr = new Header(builder, *this, i->str());
172 // Install headers if requested
173 if(type==HEADERS && install)
174 inst_list.push_back(hdr);
179 if(type==PROGRAM || type==LIBRARY || type==MODULE)
181 list<ObjectFile *> objs;
182 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
184 string ext = FS::extpart(FS::basename(*i));
185 if((ext==".cpp" || ext==".cc" || ext==".c"))
187 SourceFile *src = new SourceFile(builder, *this, i->str());
188 ObjectFile *obj = new ObjectFile(builder, *this, *src);
193 list<FileTarget *> results;
196 results.push_back(new SharedLibrary(builder, *this, objs));
197 results.push_back(new StaticLibrary(builder, *this, objs));
200 results.push_back(new Executable(builder, *this, objs));
202 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
204 if(&pkg==builder.get_main_package() && deflt)
205 def_tgt->add_depend(*i);
207 world->add_depend(*i);
209 inst_list.push_back(*i);
213 Target *inst_tgt = builder.get_target("install");
214 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
216 Install *inst = new Install(builder, pkg, **i, inst_loc);
217 inst_tgt->add_depend(inst);
220 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(*i))
221 inst_tgt->add_depend(new Symlink(builder, pkg, *inst, shlib->get_name()));
225 PathList Component::collect_source_files() const
228 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
232 list<string> sfiles = list_files(*i);
233 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
234 files.push_back(*i / *j);
244 Component::Loader::Loader(Component &c):
247 add("source", &Loader::source);
248 add("install", &Component::install);
249 add("install_headers", &Loader::install_headers);
250 add("build_info", &Loader::build_info);
251 add("require", &Loader::require);
252 add("modular", &Loader::modular);
253 add("host", &Loader::host);
254 add("default", &Component::deflt);
257 void Component::Loader::finish()
259 if(!inst_hdr.empty())
261 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
262 hdrcomp.sources = comp.sources;
263 hdrcomp.install = true;
264 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
268 void Component::Loader::source(const string &s)
270 comp.sources.push_back(comp.pkg.get_source()/s);
273 void Component::Loader::require(const string &n)
275 Package *req = comp.pkg.get_builder().get_package(n);
277 comp.requires.push_back(req);
280 void Component::Loader::modular()
282 if(comp.type!=PROGRAM)
283 throw Exception("Only programs can be modular");
287 void Component::Loader::host(const string &n)
289 const ComponentList &comps = comp.pkg.get_components();
290 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
293 if(i->get_type()!=PROGRAM || !i->is_modular())
294 throw Exception("Module host must be a modular program");
295 comp.module_host = &*i;
299 throw KeyError("Unknown component", n);
302 void Component::Loader::install_headers(const string &p)
304 IO::print("%s: Note: install_headers is deprecated\n", get_source());
305 if(comp.type==HEADERS)
314 void Component::Loader::build_info()
316 load_sub(comp.build_info);