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"
19 #include "objectfile.h"
20 #include "sharedlibrary.h"
21 #include "sourcepackage.h"
22 #include "staticlibrary.h"
28 Component::Component(SourcePackage &p, Type t, const string &n):
38 void Component::create_build_info()
40 build_info.add(pkg.get_build_info());
42 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
43 build_info.add((*i)->get_exported_binfo());
45 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
46 *i=(pkg.get_source() / *i).str();
47 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
48 *i=(pkg.get_source() / *i).str();
50 if(pkg.get_library_mode()!=DYNAMIC)
52 // XXX This may pull in some unnecessary libpaths too. More thought required.
53 PackageList reqs=pkg.collect_requires();
54 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
56 const BuildInfo &ebi=(*i)->get_exported_binfo();
57 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
63 string strip=pkg.get_config().get_option("strip").value;
64 if(lexical_cast<bool>(strip))
65 build_info.ldflags.push_back("-s");
70 build_info.ldflags.push_back("-rdynamic");
71 build_info.libs.push_back("dl");
75 const PathList &host_src=module_host->get_sources();
76 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
77 build_info.incpath.push_back(i->str());
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<FileTarget *> inst_tgts;
95 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
97 string ext=FS::extpart(FS::basename(*i));
98 if((ext==".cpp" || ext==".c") && build_exe)
100 SourceFile *src=new SourceFile(builder, this, i->str());
103 ObjectFile *obj=new ObjectFile(builder, *this, *src);
108 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
110 hdr=new Header(builder, this, i->str());
112 // Install headers if requested
113 if(type==HEADERS && install)
114 inst_tgts.push_back(hdr);
121 StaticLibrary *slib=0;
124 bin=new SharedLibrary(builder, *this, objs);
125 slib=new StaticLibrary(builder, *this, objs);
128 bin=new Executable(builder, *this, objs);
130 if(&pkg==builder.get_main_package() && deflt)
132 def_tgt->add_depend(bin);
133 if(slib) def_tgt->add_depend(slib);
137 world->add_depend(bin);
138 if(slib) world->add_depend(slib);
143 inst_tgts.push_back(bin);
145 inst_tgts.push_back(slib);
149 Target *inst_tgt=builder.get_target("install");
150 for(list<FileTarget *>::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
151 inst_tgt->add_depend(new Install(builder, pkg, **i));
154 PathList Component::collect_source_files() const
157 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
161 list<string> sfiles=list_files(*i);
162 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
163 files.push_back(*i / *j);
173 Component::Loader::Loader(Component &c):
176 add("source", &Loader::source);
177 add("install", &Component::install);
178 add("install_headers", &Loader::install_headers);
179 add("build_info", &Loader::build_info);
180 add("require", &Loader::require);
181 add("modular", &Loader::modular);
182 add("host", &Loader::host);
183 add("default", &Component::deflt);
186 void Component::Loader::finish()
188 if(!inst_hdr.empty())
190 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
191 hdrcomp.sources=comp.sources;
192 hdrcomp.install=true;
193 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
197 void Component::Loader::source(const string &s)
199 comp.sources.push_back(comp.pkg.get_source()/s);
202 void Component::Loader::require(const string &n)
204 Package *req=comp.pkg.get_builder().get_package(n);
206 comp.requires.push_back(req);
209 void Component::Loader::modular()
211 if(comp.type!=PROGRAM)
212 throw Msp::Exception("Only programs can be modular");
216 void Component::Loader::host(const string &n)
218 const ComponentList &comps=comp.pkg.get_components();
219 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
222 if(i->get_type()!=PROGRAM || !i->get_modular())
223 throw Msp::Exception("Module host must be a modular program");
224 comp.module_host=&*i;
228 throw Msp::Exception("Unknown component");
231 void Component::Loader::install_headers(const string &p)
233 IO::print("%s: Note: install_headers is deprecated\n", get_source());
234 if(comp.type==HEADERS)
243 void Component::Loader::build_info()
245 load_sub(comp.build_info);