2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/lexicalcast.h>
10 #include "executable.h"
14 #include "objectfile.h"
15 #include "sharedlibrary.h"
16 #include "sourcepackage.h"
17 #include "staticlibrary.h"
24 Component::Component(SourcePackage &p, Type t, const string &n):
32 void Component::configure(const StringMap &opts, unsigned flag)
34 for(StringList::iterator i=sources.begin(); i!=sources.end(); ++i)
35 *i = (pkg.get_source()/pkg.expand_string(*i)).str();
37 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
38 (*i)->configure(opts, flag&2);
41 void Component::create_build_info()
43 const PackageList &pkg_reqs = pkg.get_requires();
44 PackageList direct_reqs = requires;
45 direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
47 PackageList all_reqs = direct_reqs;
48 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
50 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
51 build_info.add((*i)->get_exported_binfo());
54 const BuildInfo &ebi = (*i)->get_exported_binfo();
55 build_info.cflags.insert(build_info.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
56 build_info.incpath.insert(build_info.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
57 build_info.defines.insert(build_info.defines.end(), ebi.defines.begin(), ebi.defines.end());
60 const PackageList &reqs = (*i)->get_requires();
61 for(PackageList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
62 if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
63 all_reqs.push_back(*j);
66 build_info.add(pkg.get_build_info());
68 for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
69 *i = (pkg.get_source() / *i).str();
70 for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
71 *i = (pkg.get_source() / *i).str();
73 if(pkg.get_library_mode()!=DYNAMIC)
75 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
77 const BuildInfo &ebi = (*i)->get_exported_binfo();
78 build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
84 string strip = pkg.get_config().get_option("strip").value;
85 if(lexical_cast<bool>(strip))
86 build_info.ldflags.push_back("-s");
88 else if(type==LIBRARY)
90 build_info.cflags.push_back("-fPIC");
96 void Component::create_targets() const
98 Builder &builder = pkg.get_builder();
99 Target *world = builder.get_target("world");
100 Target *def_tgt = builder.get_target("default");
102 PathList files = collect_source_files();
103 list<FileTarget *> inst_list;
108 string tarname = name;
110 tarname = pkg.get_name()+"-"+pkg.get_version();
111 TarBall *result = new TarBall(builder, pkg, tarname);
115 const Builder::TargetMap &targets = builder.get_targets();
116 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
117 if(i->second->get_package()==&pkg && !i->second->is_buildable())
118 result->add_depend(i->second);
119 files.push_back(pkg.get_source()/"Build");
122 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
125 if(Target *tgt = builder.get_target_by_path(*i))
126 ft = dynamic_cast<FileTarget *>(tgt);
128 ft = new File(builder, *i);
129 result->add_depend(ft);
132 Target *tarbls_tgt = builder.get_target("tarballs");
133 tarbls_tgt->add_depend(result);
137 else if(type==INSTALL)
140 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
143 if(Target *tgt = builder.get_target_by_path(*i))
144 ft = dynamic_cast<FileTarget *>(tgt);
146 ft = new File(builder, pkg, *i);
147 inst_list.push_back(ft);
150 else if(type==DATAFILE)
153 if(Target *tgt = builder.get_target_by_path(files.front()))
154 source = dynamic_cast<File *>(tgt);
156 source = new File(builder, pkg, files.front());
157 ::DataFile *result = new ::DataFile(builder, *this, *source);
159 if(&pkg==builder.get_main_package() && deflt)
160 def_tgt->add_depend(result);
162 world->add_depend(result);
164 inst_list.push_back(result);
168 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
170 string ext = FS::extpart(FS::basename(*i));
173 FileTarget *hdr = builder.get_target_by_path(*i);
175 hdr = new Header(builder, *this, i->str());
177 // Install headers if requested
178 if(type==HEADERS && install)
179 inst_list.push_back(hdr);
184 if(type==PROGRAM || type==LIBRARY || type==MODULE)
186 list<ObjectFile *> objs;
187 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
189 string ext = FS::extpart(FS::basename(*i));
190 if((ext==".cpp" || ext==".cc" || ext==".c"))
192 SourceFile *src = new SourceFile(builder, *this, i->str());
193 ObjectFile *obj = new ObjectFile(builder, *this, *src);
198 list<FileTarget *> results;
201 results.push_back(new SharedLibrary(builder, *this, objs));
202 results.push_back(new StaticLibrary(builder, *this, objs));
205 results.push_back(new Executable(builder, *this, objs));
207 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
209 if(&pkg==builder.get_main_package() && deflt)
210 def_tgt->add_depend(*i);
212 world->add_depend(*i);
214 inst_list.push_back(*i);
218 Target *inst_tgt = builder.get_target("install");
219 for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
221 Install *inst = new Install(builder, pkg, **i, inst_loc);
222 inst_tgt->add_depend(inst);
226 PathList Component::collect_source_files() const
229 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
234 list<string> sfiles = list_files(path);
235 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
236 files.push_back(path / *j);
239 files.push_back(path);
246 Component::Loader::Loader(Component &c):
249 add("source", &Loader::source);
250 add("install", &Component::install);
251 add("install_headers", &Loader::install_headers);
252 add("build_info", &Loader::build_info);
253 add("require", &Loader::require);
254 add("modular", &Loader::modular);
255 add("host", &Loader::host);
256 add("default", &Component::deflt);
259 void Component::Loader::finish()
261 if(!inst_hdr.empty())
263 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
264 hdrcomp.sources = comp.sources;
265 hdrcomp.install = true;
266 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
270 void Component::Loader::source(const string &s)
272 comp.sources.push_back(s);
275 void Component::Loader::require(const string &n)
277 Package *req = comp.pkg.get_builder().get_package(n);
279 comp.requires.push_back(req);
282 void Component::Loader::modular()
284 IO::print("%s: Note: modular is deprecated\n", get_source());
285 comp.build_info.ldflags.push_back("-rdynamic");
286 comp.build_info.libs.push_back("dl");
289 void Component::Loader::host(const string &)
291 IO::print("%s: Note: host is deprecated\n", get_source());
294 void Component::Loader::install_headers(const string &p)
296 IO::print("%s: Note: install_headers is deprecated\n", get_source());
297 if(comp.type==HEADERS)
306 void Component::Loader::build_info()
308 load_sub(comp.build_info);