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");
70 else if(type==LIBRARY)
72 build_info.cflags.push_back("-fPIC");
77 build_info.ldflags.push_back("-rdynamic");
78 build_info.libs.push_back("dl");
82 const PathList &host_src = module_host->get_sources();
83 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
84 build_info.incpath.push_back(i->str());
90 void Component::create_targets() const
92 Builder &builder = pkg.get_builder();
93 Target *world = builder.get_target("world");
94 Target *def_tgt = builder.get_target("default");
96 PathList files = collect_source_files();
97 list<FileTarget *> inst_list;
102 string tarname = name;
104 tarname = pkg.get_name()+"-"+pkg.get_version();
105 TarBall *result = new TarBall(builder, pkg, tarname);
109 const TargetMap &targets = builder.get_targets();
110 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
111 if(i->second->get_package()==&pkg && !i->second->is_buildable())
112 result->add_depend(i->second);
113 files.push_back(pkg.get_source()/"Build");
116 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
119 if(Target *tgt = builder.get_target(i->str()))
120 ft = dynamic_cast<FileTarget *>(tgt);
122 ft = new File(builder, *i);
123 result->add_depend(ft);
126 Target *tarbls_tgt = builder.get_target("tarballs");
127 tarbls_tgt->add_depend(result);
131 else if(type==INSTALL)
134 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
137 if(Target *tgt = builder.get_target(i->str()))
138 ft = dynamic_cast<FileTarget *>(tgt);
140 ft = new File(builder, pkg, *i);
141 inst_list.push_back(ft);
144 else if(type==DATAFILE)
147 if(Target *tgt = builder.get_target(files.front().str()))
148 source = dynamic_cast<File *>(tgt);
150 source = new File(builder, pkg, files.front());
151 ::DataFile *result = new ::DataFile(builder, *this, *source);
153 if(&pkg==builder.get_main_package() && deflt)
154 def_tgt->add_depend(result);
156 world->add_depend(result);
158 inst_list.push_back(result);
162 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
164 string ext = FS::extpart(FS::basename(*i));
167 FileTarget *hdr = dynamic_cast<FileTarget *>(builder.get_target(i->str()));
169 hdr = new Header(builder, *this, i->str());
171 // Install headers if requested
172 if(type==HEADERS && install)
173 inst_list.push_back(hdr);
178 if(type==PROGRAM || type==LIBRARY || type==MODULE)
180 list<ObjectFile *> objs;
181 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
183 string ext = FS::extpart(FS::basename(*i));
184 if((ext==".cpp" || ext==".cc" || ext==".c"))
186 SourceFile *src = new SourceFile(builder, *this, i->str());
187 ObjectFile *obj = new ObjectFile(builder, *this, *src);
192 list<FileTarget *> results;
195 results.push_back(new SharedLibrary(builder, *this, objs));
196 results.push_back(new StaticLibrary(builder, *this, objs));
199 results.push_back(new Executable(builder, *this, objs));
201 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
203 if(&pkg==builder.get_main_package() && deflt)
204 def_tgt->add_depend(*i);
206 world->add_depend(*i);
208 inst_list.push_back(*i);
212 Target *inst_tgt = builder.get_target("install");
213 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
214 inst_tgt->add_depend(new Install(builder, pkg, **i, inst_loc));
217 PathList Component::collect_source_files() const
220 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
224 list<string> sfiles = list_files(*i);
225 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
226 files.push_back(*i / *j);
236 Component::Loader::Loader(Component &c):
239 add("source", &Loader::source);
240 add("install", &Component::install);
241 add("install_headers", &Loader::install_headers);
242 add("build_info", &Loader::build_info);
243 add("require", &Loader::require);
244 add("modular", &Loader::modular);
245 add("host", &Loader::host);
246 add("default", &Component::deflt);
249 void Component::Loader::finish()
251 if(!inst_hdr.empty())
253 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
254 hdrcomp.sources = comp.sources;
255 hdrcomp.install = true;
256 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
260 void Component::Loader::source(const string &s)
262 comp.sources.push_back(comp.pkg.get_source()/s);
265 void Component::Loader::require(const string &n)
267 Package *req = comp.pkg.get_builder().get_package(n);
269 comp.requires.push_back(req);
272 void Component::Loader::modular()
274 if(comp.type!=PROGRAM)
275 throw Exception("Only programs can be modular");
279 void Component::Loader::host(const string &n)
281 const ComponentList &comps = comp.pkg.get_components();
282 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
285 if(i->get_type()!=PROGRAM || !i->is_modular())
286 throw Exception("Module host must be a modular program");
287 comp.module_host = &*i;
291 throw KeyError("Unknown component", n);
294 void Component::Loader::install_headers(const string &p)
296 IO::print("%s: Note: install_headers is deprecated\n", get_source());
297 if(comp.type==HEADERS)
306 void Component::Loader::build_info()
308 load_sub(comp.build_info);