]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Support building modular programs
[builder.git] / source / component.cpp
1 #include <msp/error.h>
2 #include "component.h"
3 #include "package.h"
4
5 using namespace std;
6
7 #include <iostream>
8
9 Component::Component(Package &p, Type t, const string &n):
10         pkg(p),
11         type(t),
12         name(n),
13         install(false),
14         module_host(0),
15         modular(false)
16 { }
17
18 /**
19 Tries to resolve all references to packages.
20 */
21 void Component::resolve_refs()
22 {
23         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
24                 i->resolve();
25 }
26
27 /**
28 Prepares the build information for building.
29 */
30 void Component::create_build_info()
31 {
32         build_info.add(pkg.get_build_info());
33
34         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
35         {
36                 if(!i->get_package())
37                         continue;
38                 //i->get_package()->create_build_info();
39                 build_info.add(i->get_package()->get_exported_binfo());
40         }
41
42         if(modular)
43         {
44                 build_info.ldflags.push_back("-rdynamic");
45                 build_info.libs.push_back("dl");
46         }
47         else if(module_host)
48         {
49                 const PathList &host_src=module_host->get_sources();
50                 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
51                         build_info.incpath.push_back(i->str());
52         }
53
54         build_info.unique();
55 }
56
57 Component::Loader::Loader(Component &c):
58         comp(c)
59 {
60         add("source",          &Loader::source);
61         add("install",         &Component::install);
62         add("install_headers", &Component::install_headers);
63         add("build_info",      &Loader::build_info);
64         add("require",         &Loader::require);
65         add("modular",         &Loader::modular);
66         add("host",            &Loader::host);
67 }
68
69 void Component::Loader::source(const string &s)
70 {
71         comp.sources.push_back(comp.pkg.get_source()/s);
72 }
73
74 void Component::Loader::require(const string &n)
75 {
76         comp.requires.push_back(PackageRef(comp.pkg.get_builder(), n));
77 }
78
79 void Component::Loader::modular()
80 {
81         if(comp.type!=PROGRAM)
82                 throw Msp::Exception("Only programs can be modular");
83         comp.modular=true;
84 }
85
86 void Component::Loader::host(const string &n)
87 {
88         const ComponentList &comps=comp.pkg.get_components();
89         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
90                 if(i->get_name()==n)
91                 {
92                         if(i->get_type()!=PROGRAM || !i->get_modular())
93                                 throw Msp::Exception("Module host must be a modular program");
94                         comp.module_host=&*i;
95                         return;
96                 }
97
98         throw Msp::Exception("Unknown component");
99 }
100
101 void Component::Loader::build_info()
102 {
103         load_sub(comp.build_info);
104 }