3 This file is part of builder
4 Copyright © 2006-2007 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/strings/lexicalcast.h>
14 #include "component.h"
17 #include "objectfile.h"
18 #include "sharedlibrary.h"
19 #include "sourcepackage.h"
20 #include "staticlibrary.h"
26 Component::Component(SourcePackage &p, Type t, const string &n):
37 Prepares the build information for building.
39 void Component::create_build_info()
41 build_info.add(pkg.get_build_info());
43 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
44 build_info.add((*i)->get_exported_binfo());
46 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
47 *i=(pkg.get_source() / *i).str();
48 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
49 *i=(pkg.get_source() / *i).str();
51 if(pkg.get_library_mode()!=DYNAMIC)
53 // XXX This may pull in some unnecessary libpaths too. More thought required.
54 PackageList reqs=pkg.collect_requires();
55 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
57 const BuildInfo &ebi=(*i)->get_exported_binfo();
58 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
64 string strip=pkg.get_config().get_option("strip").value;
65 if(lexical_cast<bool>(strip))
66 build_info.ldflags.push_back("-s");
71 build_info.ldflags.push_back("-rdynamic");
72 build_info.libs.push_back("dl");
76 const PathList &host_src=module_host->get_sources();
77 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
78 build_info.incpath.push_back(i->str());
85 Creates all targets needed for building this component.
87 void Component::create_targets() const
89 Builder &builder=pkg.get_builder();
90 Target *world=builder.get_target("world");
91 Target *def_tgt=builder.get_target("default");
93 PathList files=collect_source_files();
95 bool build_exe=(type!=HEADERS);
97 list<ObjectFile *> objs;
98 list<Target *> inst_tgts;
99 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
101 string ext=FS::extpart(FS::basename(*i));
102 if((ext==".cpp" || ext==".c") && build_exe)
104 SourceFile *src=new SourceFile(builder, this, i->str());
107 ObjectFile *obj=new ObjectFile(builder, *this, *src);
112 Target *hdr=builder.get_target(i->str());
114 hdr=new Header(builder, this, i->str());
116 // Install headers if requested
117 if(!install_headers.empty())
118 inst_tgts.push_back(hdr);
125 StaticLibrary *slib=0;
128 exe=new SharedLibrary(builder, *this, objs);
129 slib=new StaticLibrary(builder, *this, objs);
132 exe=new Executable(builder, *this, objs);
134 if(&pkg==builder.get_main_package() && deflt)
136 def_tgt->add_depend(exe);
137 if(slib) def_tgt->add_depend(slib);
141 world->add_depend(exe);
142 if(slib) world->add_depend(slib);
147 inst_tgts.push_back(exe);
149 inst_tgts.push_back(slib);
153 Target *inst_tgt=builder.get_target("install");
154 for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
155 inst_tgt->add_depend(new Install(builder, pkg, **i));
159 Collects all files belonging to the component.
161 PathList Component::collect_source_files() const
164 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
168 list<string> sfiles=list_files(*i);
169 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
170 files.push_back(*i / *j);
180 Component::Loader::Loader(Component &c):
183 add("source", &Loader::source);
184 add("install", &Component::install);
185 add("install_headers", &Component::install_headers);
186 add("build_info", &Loader::build_info);
187 add("require", &Loader::require);
188 add("modular", &Loader::modular);
189 add("host", &Loader::host);
190 add("default", &Component::deflt);
193 void Component::Loader::source(const string &s)
195 comp.sources.push_back(comp.pkg.get_source()/s);
198 void Component::Loader::require(const string &n)
200 Package *req=comp.pkg.get_builder().get_package(n);
202 comp.requires.push_back(req);
205 void Component::Loader::modular()
207 if(comp.type!=PROGRAM)
208 throw Msp::Exception("Only programs can be modular");
212 void Component::Loader::host(const string &n)
214 const ComponentList &comps=comp.pkg.get_components();
215 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
218 if(i->get_type()!=PROGRAM || !i->get_modular())
219 throw Msp::Exception("Module host must be a modular program");
220 comp.module_host=&*i;
224 throw Msp::Exception("Unknown component");
227 void Component::Loader::build_info()
229 load_sub(comp.build_info);