]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Split class Package into SourcePackage and BinaryPackage
[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/error.h>
9 #include <msp/path/utils.h>
10 #include <msp/strings/lexicalcast.h>
11 #include "builder.h"
12 #include "component.h"
13 #include "header.h"
14 #include "install.h"
15 #include "objectfile.h"
16 #include "sharedlibrary.h"
17 #include "sourcepackage.h"
18 #include "staticlibrary.h"
19 #include "target.h"
20
21 using namespace std;
22 using namespace Msp;
23
24 Component::Component(SourcePackage &p, Type t, const string &n):
25         pkg(p),
26         type(t),
27         name(n),
28         install(false),
29         module_host(0),
30         modular(false),
31         deflt(true)
32 { }
33
34 /**
35 Tries to resolve all references to packages.
36 */
37 void Component::resolve_refs()
38 {
39         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
40                 i->resolve();
41 }
42
43 /**
44 Prepares the build information for building.
45 */
46 void Component::create_build_info()
47 {
48         build_info.add(pkg.get_build_info());
49
50         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
51         {
52                 if(!i->get_package())
53                         continue;
54                 //i->get_package()->create_build_info();
55                 build_info.add(i->get_package()->get_exported_binfo());
56         }
57
58         if(type==PROGRAM)
59         {
60                 string strip=pkg.get_config().get_option("strip").value;
61                 if(lexical_cast<bool>(strip))
62                         build_info.ldflags.push_back("-s");
63         }
64
65         if(modular)
66         {
67                 build_info.ldflags.push_back("-rdynamic");
68                 build_info.libs.push_back("dl");
69         }
70         else if(module_host)
71         {
72                 const PathList &host_src=module_host->get_sources();
73                 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
74                         build_info.incpath.push_back(i->str());
75         }
76
77         build_info.unique();
78 }
79
80 /**
81 Creates all targets needed for building this component.
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<Target *> inst_tgts;
95         for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
96         {
97                 string basename=(*i)[-1];
98                 string ext=Path::splitext(basename).ext;
99                 if((ext==".cpp" || ext==".c") && build_exe)
100                 {
101                         SourceFile *src=new SourceFile(builder, this, i->str());
102
103                         // Compile sources
104                         ObjectFile *obj=new ObjectFile(builder, *this, *src);
105                         objs.push_back(obj);
106                 }
107                 else if(ext==".h")
108                 {
109                         Target *hdr=builder.get_target(i->str());
110                         if(!hdr)
111                                 hdr=new Header(builder, this, i->str());
112
113                         // Install headers if requested
114                         if(!install_headers.empty())
115                                 inst_tgts.push_back(hdr);
116                 }
117         }
118
119         if(build_exe)
120         {
121                 Executable    *exe=0;
122                 StaticLibrary *slib=0;
123                 if(type==LIBRARY)
124                 {
125                         exe=new SharedLibrary(builder, *this, objs);
126                         slib=new StaticLibrary(builder, *this, objs);
127                 }
128                 else
129                         exe=new Executable(builder, *this, objs);
130
131                 if(&pkg==builder.get_default_package() && deflt)
132                 {
133                         def_tgt->add_depend(exe);
134                         if(slib) def_tgt->add_depend(slib);
135                 }
136                 else
137                 {
138                         world->add_depend(exe);
139                         if(slib) world->add_depend(slib);
140                 }
141
142                 if(install)
143                 {
144                         inst_tgts.push_back(exe);
145                         if(slib)
146                                 inst_tgts.push_back(slib);
147                 }
148         }
149
150         Target *inst_tgt=builder.get_target("install");
151         for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
152                 inst_tgt->add_depend(new Install(builder, pkg, **i));
153 }
154
155 /**
156 Collects all files belonging to the component.
157 */
158 PathList Component::collect_source_files() const
159 {
160         PathList files;
161         for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
162         {
163                 struct stat st;
164                 stat(*i, st);
165                 if(S_ISDIR(st.st_mode))
166                 {
167                         list<string> sfiles=list_files(*i);
168                         for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
169                                 files.push_back(*i / *j);
170                 }
171                 else
172                         files.push_back(*i);
173         }
174
175         return files;
176 }
177
178
179 Component::Loader::Loader(Component &c):
180         comp(c)
181 {
182         add("source",          &Loader::source);
183         add("install",         &Component::install);
184         add("install_headers", &Component::install_headers);
185         add("build_info",      &Loader::build_info);
186         add("require",         &Loader::require);
187         add("modular",         &Loader::modular);
188         add("host",            &Loader::host);
189         add("default",         &Component::deflt);
190 }
191
192 void Component::Loader::source(const string &s)
193 {
194         comp.sources.push_back(comp.pkg.get_source()/s);
195 }
196
197 void Component::Loader::require(const string &n)
198 {
199         comp.requires.push_back(PackageRef(comp.pkg.get_builder(), n));
200 }
201
202 void Component::Loader::modular()
203 {
204         if(comp.type!=PROGRAM)
205                 throw Msp::Exception("Only programs can be modular");
206         comp.modular=true;
207 }
208
209 void Component::Loader::host(const string &n)
210 {
211         const ComponentList &comps=comp.pkg.get_components();
212         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
213                 if(i->get_name()==n)
214                 {
215                         if(i->get_type()!=PROGRAM || !i->get_modular())
216                                 throw Msp::Exception("Module host must be a modular program");
217                         comp.module_host=&*i;
218                         return;
219                 }
220
221         throw Msp::Exception("Unknown component");
222 }
223
224 void Component::Loader::build_info()
225 {
226         load_sub(comp.build_info);
227 }