3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/error.h>
9 #include <msp/path/utils.h>
10 #include <msp/strings/lexicalcast.h>
12 #include "component.h"
15 #include "objectfile.h"
16 #include "sharedlibrary.h"
17 #include "sourcepackage.h"
18 #include "staticlibrary.h"
24 Component::Component(SourcePackage &p, Type t, const string &n):
35 Prepares the build information for building.
37 void Component::create_build_info()
39 build_info.add(pkg.get_build_info());
41 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
42 build_info.add((*i)->get_exported_binfo());
46 string strip=pkg.get_config().get_option("strip").value;
47 if(lexical_cast<bool>(strip))
48 build_info.ldflags.push_back("-s");
53 build_info.ldflags.push_back("-rdynamic");
54 build_info.libs.push_back("dl");
58 const PathList &host_src=module_host->get_sources();
59 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
60 build_info.incpath.push_back(i->str());
67 Creates all targets needed for building this component.
69 void Component::create_targets() const
71 Builder &builder=pkg.get_builder();
72 Target *world=builder.get_target("world");
73 Target *def_tgt=builder.get_target("default");
75 PathList files=collect_source_files();
77 bool build_exe=(type!=HEADERS);
79 list<ObjectFile *> objs;
80 list<Target *> inst_tgts;
81 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
83 string basename=(*i)[-1];
84 string ext=Path::splitext(basename).ext;
85 if((ext==".cpp" || ext==".c") && build_exe)
87 SourceFile *src=new SourceFile(builder, this, i->str());
90 ObjectFile *obj=new ObjectFile(builder, *this, *src);
95 Target *hdr=builder.get_target(i->str());
97 hdr=new Header(builder, this, i->str());
99 // Install headers if requested
100 if(!install_headers.empty())
101 inst_tgts.push_back(hdr);
108 StaticLibrary *slib=0;
111 exe=new SharedLibrary(builder, *this, objs);
112 slib=new StaticLibrary(builder, *this, objs);
115 exe=new Executable(builder, *this, objs);
117 if(&pkg==builder.get_default_package() && deflt)
119 def_tgt->add_depend(exe);
120 if(slib) def_tgt->add_depend(slib);
124 world->add_depend(exe);
125 if(slib) world->add_depend(slib);
130 inst_tgts.push_back(exe);
132 inst_tgts.push_back(slib);
136 Target *inst_tgt=builder.get_target("install");
137 for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
138 inst_tgt->add_depend(new Install(builder, pkg, **i));
142 Collects all files belonging to the component.
144 PathList Component::collect_source_files() const
147 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
151 if(S_ISDIR(st.st_mode))
153 list<string> sfiles=list_files(*i);
154 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
155 files.push_back(*i / *j);
165 Component::Loader::Loader(Component &c):
168 add("source", &Loader::source);
169 add("install", &Component::install);
170 add("install_headers", &Component::install_headers);
171 add("build_info", &Loader::build_info);
172 add("require", &Loader::require);
173 add("modular", &Loader::modular);
174 add("host", &Loader::host);
175 add("default", &Component::deflt);
178 void Component::Loader::source(const string &s)
180 comp.sources.push_back(comp.pkg.get_source()/s);
183 void Component::Loader::require(const string &n)
185 Package *req=comp.pkg.get_builder().get_package(n);
187 comp.requires.push_back(req);
190 void Component::Loader::modular()
192 if(comp.type!=PROGRAM)
193 throw Msp::Exception("Only programs can be modular");
197 void Component::Loader::host(const string &n)
199 const ComponentList &comps=comp.pkg.get_components();
200 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
203 if(i->get_type()!=PROGRAM || !i->get_modular())
204 throw Msp::Exception("Module host must be a modular program");
205 comp.module_host=&*i;
209 throw Msp::Exception("Unknown component");
212 void Component::Loader::build_info()
214 load_sub(comp.build_info);