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"
16 #include "executable.h"
20 #include "objectfile.h"
21 #include "sharedlibrary.h"
22 #include "sourcepackage.h"
23 #include "staticlibrary.h"
30 Component::Component(SourcePackage &p, Type t, const string &n):
40 void Component::create_build_info()
42 build_info.add(pkg.get_build_info());
44 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
45 build_info.add((*i)->get_exported_binfo());
47 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
48 *i=(pkg.get_source() / *i).str();
49 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
50 *i=(pkg.get_source() / *i).str();
52 if(pkg.get_library_mode()!=DYNAMIC)
54 // XXX This may pull in some unnecessary libpaths too. More thought required.
55 PackageList reqs=pkg.collect_requires();
56 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
58 const BuildInfo &ebi=(*i)->get_exported_binfo();
59 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
65 string strip=pkg.get_config().get_option("strip").value;
66 if(lexical_cast<bool>(strip))
67 build_info.ldflags.push_back("-s");
72 build_info.ldflags.push_back("-rdynamic");
73 build_info.libs.push_back("dl");
77 const PathList &host_src=module_host->get_sources();
78 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
79 build_info.incpath.push_back(i->str());
85 void Component::create_targets() const
87 Builder &builder=pkg.get_builder();
88 Target *world=builder.get_target("world");
89 Target *def_tgt=builder.get_target("default");
91 PathList files=collect_source_files();
97 tarname=pkg.get_name()+"-"+pkg.get_version();
98 TarBall *result=new TarBall(builder, pkg, tarname);
102 const TargetMap &targets=builder.get_targets();
103 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
104 if(i->second->get_package()==&pkg && !i->second->get_buildable())
105 result->add_depend(i->second);
106 files.push_back(pkg.get_source()/"Build");
109 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
112 if(Target *tgt=builder.get_target(i->str()))
113 ft=dynamic_cast<FileTarget *>(tgt);
115 ft=new File(builder, *i);
116 result->add_depend(ft);
119 Target *tarbls_tgt=builder.get_target("tarballs");
120 tarbls_tgt->add_depend(result);
125 list<FileTarget *> inst_list;
126 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
128 string ext=FS::extpart(FS::basename(*i));
131 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
133 hdr=new Header(builder, this, i->str());
135 // Install headers if requested
136 if(type==HEADERS && install)
137 inst_list.push_back(hdr);
141 if(type==PROGRAM || type==LIBRARY || type==MODULE)
143 list<ObjectFile *> objs;
144 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
146 string ext=FS::extpart(FS::basename(*i));
147 if((ext==".cpp" || ext==".cc" || ext==".c"))
149 SourceFile *src=new SourceFile(builder, this, i->str());
150 ObjectFile *obj=new ObjectFile(builder, *this, *src);
155 list<FileTarget *> results;
158 results.push_back(new SharedLibrary(builder, *this, objs));
159 results.push_back(new StaticLibrary(builder, *this, objs));
162 results.push_back(new Executable(builder, *this, objs));
164 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
166 if(&pkg==builder.get_main_package() && deflt)
167 def_tgt->add_depend(*i);
169 world->add_depend(*i);
171 inst_list.push_back(*i);
175 Target *inst_tgt=builder.get_target("install");
176 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
177 inst_tgt->add_depend(new Install(builder, pkg, **i));
180 PathList Component::collect_source_files() const
183 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
187 list<string> sfiles=list_files(*i);
188 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
189 files.push_back(*i / *j);
199 Component::Loader::Loader(Component &c):
202 add("source", &Loader::source);
203 add("install", &Component::install);
204 add("install_headers", &Loader::install_headers);
205 add("build_info", &Loader::build_info);
206 add("require", &Loader::require);
207 add("modular", &Loader::modular);
208 add("host", &Loader::host);
209 add("default", &Component::deflt);
212 void Component::Loader::finish()
214 if(!inst_hdr.empty())
216 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
217 hdrcomp.sources=comp.sources;
218 hdrcomp.install=true;
219 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
223 void Component::Loader::source(const string &s)
225 comp.sources.push_back(comp.pkg.get_source()/s);
228 void Component::Loader::require(const string &n)
230 Package *req=comp.pkg.get_builder().get_package(n);
232 comp.requires.push_back(req);
235 void Component::Loader::modular()
237 if(comp.type!=PROGRAM)
238 throw Msp::Exception("Only programs can be modular");
242 void Component::Loader::host(const string &n)
244 const ComponentList &comps=comp.pkg.get_components();
245 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
248 if(i->get_type()!=PROGRAM || !i->get_modular())
249 throw Msp::Exception("Module host must be a modular program");
250 comp.module_host=&*i;
254 throw Msp::Exception("Unknown component");
257 void Component::Loader::install_headers(const string &p)
259 IO::print("%s: Note: install_headers is deprecated\n", get_source());
260 if(comp.type==HEADERS)
269 void Component::Loader::build_info()
271 load_sub(comp.build_info);