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 Tries to resolve all references to packages.
37 void Component::resolve_refs()
39 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
44 Prepares the build information for building.
46 void Component::create_build_info()
48 build_info.add(pkg.get_build_info());
50 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
54 //i->get_package()->create_build_info();
55 build_info.add(i->get_package()->get_exported_binfo());
60 string strip=pkg.get_config().get_option("strip").value;
61 if(lexical_cast<bool>(strip))
62 build_info.ldflags.push_back("-s");
67 build_info.ldflags.push_back("-rdynamic");
68 build_info.libs.push_back("dl");
72 const PathList &host_src=module_host->get_sources();
73 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
74 build_info.incpath.push_back(i->str());
81 Creates all targets needed for building this component.
83 void Component::create_targets() const
85 Builder &builder=pkg.get_builder();
86 Target *world=builder.get_target("world");
87 Target *def_tgt=builder.get_target("default");
89 PathList files=collect_source_files();
91 bool build_exe=(type!=HEADERS);
93 list<ObjectFile *> objs;
94 list<Target *> inst_tgts;
95 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
97 string basename=(*i)[-1];
98 string ext=Path::splitext(basename).ext;
99 if((ext==".cpp" || ext==".c") && build_exe)
101 SourceFile *src=new SourceFile(builder, this, i->str());
104 ObjectFile *obj=new ObjectFile(builder, *this, *src);
109 Target *hdr=builder.get_target(i->str());
111 hdr=new Header(builder, this, i->str());
113 // Install headers if requested
114 if(!install_headers.empty())
115 inst_tgts.push_back(hdr);
122 StaticLibrary *slib=0;
125 exe=new SharedLibrary(builder, *this, objs);
126 slib=new StaticLibrary(builder, *this, objs);
129 exe=new Executable(builder, *this, objs);
131 if(&pkg==builder.get_default_package() && deflt)
133 def_tgt->add_depend(exe);
134 if(slib) def_tgt->add_depend(slib);
138 world->add_depend(exe);
139 if(slib) world->add_depend(slib);
144 inst_tgts.push_back(exe);
146 inst_tgts.push_back(slib);
150 Target *inst_tgt=builder.get_target("install");
151 for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
152 inst_tgt->add_depend(new Install(builder, pkg, **i));
156 Collects all files belonging to the component.
158 PathList Component::collect_source_files() const
161 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
165 if(S_ISDIR(st.st_mode))
167 list<string> sfiles=list_files(*i);
168 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
169 files.push_back(*i / *j);
179 Component::Loader::Loader(Component &c):
182 add("source", &Loader::source);
183 add("install", &Component::install);
184 add("install_headers", &Component::install_headers);
185 add("build_info", &Loader::build_info);
186 add("require", &Loader::require);
187 add("modular", &Loader::modular);
188 add("host", &Loader::host);
189 add("default", &Component::deflt);
192 void Component::Loader::source(const string &s)
194 comp.sources.push_back(comp.pkg.get_source()/s);
197 void Component::Loader::require(const string &n)
199 comp.requires.push_back(PackageRef(comp.pkg.get_builder(), n));
202 void Component::Loader::modular()
204 if(comp.type!=PROGRAM)
205 throw Msp::Exception("Only programs can be modular");
209 void Component::Loader::host(const string &n)
211 const ComponentList &comps=comp.pkg.get_components();
212 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
215 if(i->get_type()!=PROGRAM || !i->get_modular())
216 throw Msp::Exception("Module host must be a modular program");
217 comp.module_host=&*i;
221 throw Msp::Exception("Unknown component");
224 void Component::Loader::build_info()
226 load_sub(comp.build_info);