1 #include <msp/core/error.h>
2 #include <msp/path/utils.h>
7 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "staticlibrary.h"
16 Component::Component(Package &p, Type t, const string &n):
27 Tries to resolve all references to packages.
29 void Component::resolve_refs()
31 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
36 Prepares the build information for building.
38 void Component::create_build_info()
40 build_info.add(pkg.get_build_info());
42 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
46 //i->get_package()->create_build_info();
47 build_info.add(i->get_package()->get_exported_binfo());
52 build_info.ldflags.push_back("-rdynamic");
53 build_info.libs.push_back("dl");
57 const PathList &host_src=module_host->get_sources();
58 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
59 build_info.incpath.push_back(i->str());
66 Creates all targets needed for building this component.
68 void Component::create_targets() const
70 Builder &builder=pkg.get_builder();
71 Target *world=builder.get_target("world");
72 Target *def_tgt=builder.get_target("default");
74 PathList files=collect_source_files();
76 bool build_exe=(type!=Component::HEADERS);
78 list<ObjectFile *> objs;
79 list<Target *> inst_tgts;
80 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
82 string basename=(*i)[-1];
83 string ext=Path::splitext(basename).ext;
84 if((ext==".cpp" || ext==".c") && build_exe)
86 SourceFile *src=new SourceFile(builder, this, i->str());
89 ObjectFile *obj=new ObjectFile(builder, *this, *src);
94 Target *hdr=builder.get_target(i->str());
96 hdr=new Header(builder, this, i->str());
98 // Install headers if requested
99 if(!install_headers.empty())
100 inst_tgts.push_back(hdr);
107 StaticLibrary *slib=0;
108 if(type==Component::LIBRARY)
110 exe=new SharedLibrary(builder, *this, objs);
111 slib=new StaticLibrary(builder, *this, objs);
114 exe=new Executable(builder, *this, objs);
116 if(&pkg==builder.get_default_package() && deflt)
118 def_tgt->add_depend(exe);
119 if(slib) def_tgt->add_depend(slib);
123 world->add_depend(exe);
124 if(slib) world->add_depend(slib);
129 inst_tgts.push_back(exe);
131 inst_tgts.push_back(slib);
135 Target *inst_tgt=builder.get_target("install");
136 for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
137 inst_tgt->add_depend(new Install(builder, pkg, **i));
141 Collects all files belonging to the component.
143 PathList Component::collect_source_files() const
146 for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
150 if(S_ISDIR(st.st_mode))
152 list<string> sfiles=list_files(*i);
153 for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
154 files.push_back(*i / *j);
164 Component::Loader::Loader(Component &c):
167 add("source", &Loader::source);
168 add("install", &Component::install);
169 add("install_headers", &Component::install_headers);
170 add("build_info", &Loader::build_info);
171 add("require", &Loader::require);
172 add("modular", &Loader::modular);
173 add("host", &Loader::host);
174 add("default", &Component::deflt);
177 void Component::Loader::source(const string &s)
179 comp.sources.push_back(comp.pkg.get_source()/s);
182 void Component::Loader::require(const string &n)
184 comp.requires.push_back(PackageRef(comp.pkg.get_builder(), n));
187 void Component::Loader::modular()
189 if(comp.type!=PROGRAM)
190 throw Msp::Exception("Only programs can be modular");
194 void Component::Loader::host(const string &n)
196 const ComponentList &comps=comp.pkg.get_components();
197 for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
200 if(i->get_type()!=PROGRAM || !i->get_modular())
201 throw Msp::Exception("Module host must be a modular program");
202 comp.module_host=&*i;
206 throw Msp::Exception("Unknown component");
209 void Component::Loader::build_info()
211 load_sub(comp.build_info);