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();
53 string strip=pkg.get_config().get_option("strip").value;
54 if(lexical_cast<bool>(strip))
55 build_info.ldflags.push_back("-s");
60 build_info.ldflags.push_back("-rdynamic");
61 build_info.libs.push_back("dl");
65 const PathList &host_src=module_host->get_sources();
66 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
67 build_info.incpath.push_back(i->str());
74 Creates all targets needed for building this component.
76 void Component::create_targets() const
78 Builder &builder=pkg.get_builder();
79 Target *world=builder.get_target("world");
80 Target *def_tgt=builder.get_target("default");
82 PathList files=collect_source_files();
84 bool build_exe=(type!=HEADERS);
86 list<ObjectFile *> objs;
87 list<Target *> inst_tgts;
88 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
90 string ext=FS::extpart(FS::basename(*i));
91 if((ext==".cpp" || ext==".c") && build_exe)
93 SourceFile *src=new SourceFile(builder, this, i->str());
96 ObjectFile *obj=new ObjectFile(builder, *this, *src);
101 Target *hdr=builder.get_target(i->str());
103 hdr=new Header(builder, this, i->str());
105 // Install headers if requested
106 if(!install_headers.empty())
107 inst_tgts.push_back(hdr);
114 StaticLibrary *slib=0;
117 exe=new SharedLibrary(builder, *this, objs);
118 slib=new StaticLibrary(builder, *this, objs);
121 exe=new Executable(builder, *this, objs);
123 if(&pkg==builder.get_main_package() && deflt)
125 def_tgt->add_depend(exe);
126 if(slib) def_tgt->add_depend(slib);
130 world->add_depend(exe);
131 if(slib) world->add_depend(slib);
136 inst_tgts.push_back(exe);
138 inst_tgts.push_back(slib);
142 Target *inst_tgt=builder.get_target("install");
143 for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
144 inst_tgt->add_depend(new Install(builder, pkg, **i));
148 Collects all files belonging to the component.
150 PathList Component::collect_source_files() const
153 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
157 list<string> sfiles=list_files(*i);
158 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
159 files.push_back(*i / *j);
169 Component::Loader::Loader(Component &c):
172 add("source", &Loader::source);
173 add("install", &Component::install);
174 add("install_headers", &Component::install_headers);
175 add("build_info", &Loader::build_info);
176 add("require", &Loader::require);
177 add("modular", &Loader::modular);
178 add("host", &Loader::host);
179 add("default", &Component::deflt);
182 void Component::Loader::source(const string &s)
184 comp.sources.push_back(comp.pkg.get_source()/s);
187 void Component::Loader::require(const string &n)
189 Package *req=comp.pkg.get_builder().get_package(n);
191 comp.requires.push_back(req);
194 void Component::Loader::modular()
196 if(comp.type!=PROGRAM)
197 throw Msp::Exception("Only programs can be modular");
201 void Component::Loader::host(const string &n)
203 const ComponentList &comps=comp.pkg.get_components();
204 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
207 if(i->get_type()!=PROGRAM || !i->get_modular())
208 throw Msp::Exception("Module host must be a modular program");
209 comp.module_host=&*i;
213 throw Msp::Exception("Unknown component");
216 void Component::Loader::build_info()
218 load_sub(comp.build_info);