]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Make sure we have libpaths for all static libs
[builder.git] / source / component.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
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/strings/lexicalcast.h>
13 #include "builder.h"
14 #include "component.h"
15 #include "header.h"
16 #include "install.h"
17 #include "objectfile.h"
18 #include "sharedlibrary.h"
19 #include "sourcepackage.h"
20 #include "staticlibrary.h"
21 #include "target.h"
22
23 using namespace std;
24 using namespace Msp;
25
26 Component::Component(SourcePackage &p, Type t, const string &n):
27         pkg(p),
28         type(t),
29         name(n),
30         install(false),
31         module_host(0),
32         modular(false),
33         deflt(true)
34 { }
35
36 /**
37 Prepares the build information for building.
38 */
39 void Component::create_build_info()
40 {
41         build_info.add(pkg.get_build_info());
42
43         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
44                 build_info.add((*i)->get_exported_binfo());
45
46         for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
47                 *i=(pkg.get_source() / *i).str();
48         for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
49                 *i=(pkg.get_source() / *i).str();
50
51         if(pkg.get_library_mode()!=DYNAMIC)
52         {
53                 // XXX This may pull in some unnecessary libpaths too.  More thought required.
54                 PackageList reqs=pkg.collect_requires();
55                 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
56                 {
57                         const BuildInfo &ebi=(*i)->get_exported_binfo();
58                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
59                 }
60         }
61
62         if(type==PROGRAM)
63         {
64                 string strip=pkg.get_config().get_option("strip").value;
65                 if(lexical_cast<bool>(strip))
66                         build_info.ldflags.push_back("-s");
67         }
68
69         if(modular)
70         {
71                 build_info.ldflags.push_back("-rdynamic");
72                 build_info.libs.push_back("dl");
73         }
74         else if(module_host)
75         {
76                 const PathList &host_src=module_host->get_sources();
77                 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
78                         build_info.incpath.push_back(i->str());
79         }
80
81         build_info.unique();
82 }
83
84 /**
85 Creates all targets needed for building this component.
86 */
87 void Component::create_targets() const
88 {
89         Builder &builder=pkg.get_builder();
90         Target *world=builder.get_target("world");
91         Target *def_tgt=builder.get_target("default");
92
93         PathList files=collect_source_files();
94
95         bool build_exe=(type!=HEADERS);
96
97         list<ObjectFile *> objs;
98         list<Target *> inst_tgts;
99         for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
100         {
101                 string ext=FS::extpart(FS::basename(*i));
102                 if((ext==".cpp" || ext==".c") && build_exe)
103                 {
104                         SourceFile *src=new SourceFile(builder, this, i->str());
105
106                         // Compile sources
107                         ObjectFile *obj=new ObjectFile(builder, *this, *src);
108                         objs.push_back(obj);
109                 }
110                 else if(ext==".h")
111                 {
112                         Target *hdr=builder.get_target(i->str());
113                         if(!hdr)
114                                 hdr=new Header(builder, this, i->str());
115
116                         // Install headers if requested
117                         if(!install_headers.empty())
118                                 inst_tgts.push_back(hdr);
119                 }
120         }
121
122         if(build_exe)
123         {
124                 Executable    *exe=0;
125                 StaticLibrary *slib=0;
126                 if(type==LIBRARY)
127                 {
128                         exe=new SharedLibrary(builder, *this, objs);
129                         slib=new StaticLibrary(builder, *this, objs);
130                 }
131                 else
132                         exe=new Executable(builder, *this, objs);
133
134                 if(&pkg==builder.get_main_package() && deflt)
135                 {
136                         def_tgt->add_depend(exe);
137                         if(slib) def_tgt->add_depend(slib);
138                 }
139                 else
140                 {
141                         world->add_depend(exe);
142                         if(slib) world->add_depend(slib);
143                 }
144
145                 if(install)
146                 {
147                         inst_tgts.push_back(exe);
148                         if(slib)
149                                 inst_tgts.push_back(slib);
150                 }
151         }
152
153         Target *inst_tgt=builder.get_target("install");
154         for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
155                 inst_tgt->add_depend(new Install(builder, pkg, **i));
156 }
157
158 /**
159 Collects all files belonging to the component.
160 */
161 PathList Component::collect_source_files() const
162 {
163         PathList files;
164         for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
165         {
166                 if(FS::is_dir(*i))
167                 {
168                         list<string> sfiles=list_files(*i);
169                         for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
170                                 files.push_back(*i / *j);
171                 }
172                 else
173                         files.push_back(*i);
174         }
175
176         return files;
177 }
178
179
180 Component::Loader::Loader(Component &c):
181         comp(c)
182 {
183         add("source",          &Loader::source);
184         add("install",         &Component::install);
185         add("install_headers", &Component::install_headers);
186         add("build_info",      &Loader::build_info);
187         add("require",         &Loader::require);
188         add("modular",         &Loader::modular);
189         add("host",            &Loader::host);
190         add("default",         &Component::deflt);
191 }
192
193 void Component::Loader::source(const string &s)
194 {
195         comp.sources.push_back(comp.pkg.get_source()/s);
196 }
197
198 void Component::Loader::require(const string &n)
199 {
200         Package *req=comp.pkg.get_builder().get_package(n);
201         if(req)
202                 comp.requires.push_back(req);
203 }
204
205 void Component::Loader::modular()
206 {
207         if(comp.type!=PROGRAM)
208                 throw Msp::Exception("Only programs can be modular");
209         comp.modular=true;
210 }
211
212 void Component::Loader::host(const string &n)
213 {
214         const ComponentList &comps=comp.pkg.get_components();
215         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
216                 if(i->get_name()==n)
217                 {
218                         if(i->get_type()!=PROGRAM || !i->get_modular())
219                                 throw Msp::Exception("Module host must be a modular program");
220                         comp.module_host=&*i;
221                         return;
222                 }
223
224         throw Msp::Exception("Unknown component");
225 }
226
227 void Component::Loader::build_info()
228 {
229         load_sub(comp.build_info);
230 }