]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Deprecate the install_headers statement
[builder.git] / source / component.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <iostream>
9 #include <msp/core/except.h>
10 #include <msp/fs/dir.h>
11 #include <msp/fs/stat.h>
12 #include <msp/fs/utils.h>
13 #include <msp/strings/lexicalcast.h>
14 #include "builder.h"
15 #include "component.h"
16 #include "executable.h"
17 #include "header.h"
18 #include "install.h"
19 #include "objectfile.h"
20 #include "sharedlibrary.h"
21 #include "sourcepackage.h"
22 #include "staticlibrary.h"
23 #include "target.h"
24
25 using namespace std;
26 using namespace Msp;
27
28 Component::Component(SourcePackage &p, Type t, const string &n):
29         pkg(p),
30         type(t),
31         name(n),
32         install(false),
33         module_host(0),
34         modular(false),
35         deflt(true)
36 { }
37
38 void Component::create_build_info()
39 {
40         build_info.add(pkg.get_build_info());
41
42         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
43                 build_info.add((*i)->get_exported_binfo());
44
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();
49
50         if(pkg.get_library_mode()!=DYNAMIC)
51         {
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)
55                 {
56                         const BuildInfo &ebi=(*i)->get_exported_binfo();
57                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
58                 }
59         }
60
61         if(type==PROGRAM)
62         {
63                 string strip=pkg.get_config().get_option("strip").value;
64                 if(lexical_cast<bool>(strip))
65                         build_info.ldflags.push_back("-s");
66         }
67
68         if(modular)
69         {
70                 build_info.ldflags.push_back("-rdynamic");
71                 build_info.libs.push_back("dl");
72         }
73         else if(module_host)
74         {
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());
78         }
79
80         build_info.unique();
81 }
82
83 void Component::create_targets() const
84 {
85         Builder &builder=pkg.get_builder();
86         Target *world=builder.get_target("world");
87         Target *def_tgt=builder.get_target("default");
88
89         PathList files=collect_source_files();
90
91         bool build_exe=(type!=HEADERS);
92
93         list<ObjectFile *> objs;
94         list<FileTarget *> inst_tgts;
95         for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
96         {
97                 string ext=FS::extpart(FS::basename(*i));
98                 if((ext==".cpp" || ext==".c") && build_exe)
99                 {
100                         SourceFile *src=new SourceFile(builder, this, i->str());
101
102                         // Compile sources
103                         ObjectFile *obj=new ObjectFile(builder, *this, *src);
104                         objs.push_back(obj);
105                 }
106                 else if(ext==".h")
107                 {
108                         FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
109                         if(!hdr)
110                                 hdr=new Header(builder, this, i->str());
111
112                         // Install headers if requested
113                         if(type==HEADERS && install)
114                                 inst_tgts.push_back(hdr);
115                 }
116         }
117
118         if(build_exe)
119         {
120                 Binary *bin=0;
121                 StaticLibrary *slib=0;
122                 if(type==LIBRARY)
123                 {
124                         bin=new SharedLibrary(builder, *this, objs);
125                         slib=new StaticLibrary(builder, *this, objs);
126                 }
127                 else
128                         bin=new Executable(builder, *this, objs);
129
130                 if(&pkg==builder.get_main_package() && deflt)
131                 {
132                         def_tgt->add_depend(bin);
133                         if(slib) def_tgt->add_depend(slib);
134                 }
135                 else
136                 {
137                         world->add_depend(bin);
138                         if(slib) world->add_depend(slib);
139                 }
140
141                 if(install)
142                 {
143                         inst_tgts.push_back(bin);
144                         if(slib)
145                                 inst_tgts.push_back(slib);
146                 }
147         }
148
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));
152 }
153
154 PathList Component::collect_source_files() const
155 {
156         PathList files;
157         for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
158         {
159                 if(FS::is_dir(*i))
160                 {
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);
164                 }
165                 else
166                         files.push_back(*i);
167         }
168
169         return files;
170 }
171
172
173 Component::Loader::Loader(Component &c):
174         comp(c)
175 {
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);
184 }
185
186 void Component::Loader::finish()
187 {
188         if(!inst_hdr.empty())
189         {
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);
194         }
195 }
196
197 void Component::Loader::source(const string &s)
198 {
199         comp.sources.push_back(comp.pkg.get_source()/s);
200 }
201
202 void Component::Loader::require(const string &n)
203 {
204         Package *req=comp.pkg.get_builder().get_package(n);
205         if(req)
206                 comp.requires.push_back(req);
207 }
208
209 void Component::Loader::modular()
210 {
211         if(comp.type!=PROGRAM)
212                 throw Msp::Exception("Only programs can be modular");
213         comp.modular=true;
214 }
215
216 void Component::Loader::host(const string &n)
217 {
218         const ComponentList &comps=comp.pkg.get_components();
219         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
220                 if(i->get_name()==n)
221                 {
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;
225                         return;
226                 }
227
228         throw Msp::Exception("Unknown component");
229 }
230
231 void Component::Loader::install_headers(const string &p)
232 {
233         cout<<get_source()<<": Note: install_headers is deprecated\n";
234         if(comp.type==HEADERS)
235         {
236                 comp.name=p;
237                 comp.install=true;
238         }
239         else
240                 inst_hdr=p;
241 }
242
243 void Component::Loader::build_info()
244 {
245         load_sub(comp.build_info);
246 }