2 #include <msp/core/algorithm.h>
3 #include <msp/core/maputils.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/lexicalcast.h>
7 #include <msp/strings/utils.h>
8 #include "android/androidapplicationcomponent.h"
9 #include "binarycomponent.h"
10 #include "binarypackage.h"
12 #include "builtin/compilecommandsjson.h"
13 #include "datafile/datapackcomponent.h"
15 #include "installcomponent.h"
16 #include "builtin/pkgconfigfile.h"
18 #include "sourcearchivecomponent.h"
19 #include "sourcegenerator.h"
20 #include "sourcepackage.h"
22 #include "builtin/vcxprojectfile.h"
23 #include "builtin/vssolutionfile.h"
28 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
30 source_dir(FS::dirname(f)),
36 build_file = builder.get_vfs().get_target(f);
38 build_file = new File(builder, *this, f);
39 source_archive = new SourceArchiveComponent(*this);
40 components.push_back(source_archive);
43 SourcePackage::~SourcePackage()
45 for(Component *c: components)
49 FS::Path SourcePackage::get_temp_directory() const
51 string subdir = builder.get_current_arch().get_name();
55 subdir += build_type->get_name();
58 const FS::Path &temp = builder.get_temp_directory();
59 if(temp.is_absolute())
60 return temp/name/subdir;
62 return source_dir/temp/subdir;
65 FS::Path SourcePackage::get_output_directory() const
67 const Architecture &arch = builder.get_current_arch();
71 return source_dir/arch.get_name();
74 const Component &SourcePackage::get_component(const string &n) const
76 auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
77 if(i!=components.end())
82 bool SourcePackage::match_feature(const string &feat, const string *comp) const
84 string value = config.get_option("with_"+feat).value;
88 return lexical_cast<bool>(value);
91 void SourcePackage::set_build_type(const BuildType &t)
96 void SourcePackage::do_prepare()
98 BuildInfo final_build_info;
101 final_build_info.update_from(build_type->get_build_info());
103 final_build_info.update_from(build_info);
104 build_info = final_build_info;
106 build_info.incpath.push_back((builder.get_prefix()/"include").str());
107 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
109 for(const Feature &f: features)
111 string ident = "WITH_"+toupper(f.name);
112 string value = config.get_option("with_"+f.name).value;
114 if(f.choices.empty())
116 if(!lexical_cast<bool>(value))
121 build_info.defines[ident] = value;
123 export_binfo.defines[ident] = value;
126 for(Component *c: components)
129 c->create_build_info();
131 c->update_exported_build_info(export_binfo);
136 for(Component *c: components)
139 const Architecture &arch = builder.get_native_arch();
140 if(!export_binfo.libs.empty())
142 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
143 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
145 if(arch.get_system()=="linux")
147 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
148 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
152 export_binfo.standards = build_info.standards;
154 if(arch.get_system()=="windows")
156 new VcxProjectFile(builder, *this);
157 new VsSolutionFile(builder, *this);
160 new CompileCommandsJson(builder, *this);
162 builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
165 void SourcePackage::save_caches()
172 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
173 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
174 FeatureConditional(p, p.name),
177 add("android_application", &Loader::component<AndroidApplicationComponent>);
178 add("build_info", &Loader::build_info);
179 add("datapack", &Loader::component<DataPackComponent>);
180 add("description", &SourcePackage::description);
181 add("feature", &Loader::feature);
182 add("generate", &Loader::generate);
183 add("install", &Loader::component<InstallComponent>);
184 add("interface_version", &Loader::interface_version);
185 add("library", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
186 add("module", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
187 add("program", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
188 add("source_archive", &Loader::source_archive);
189 add("source_tarball", &Loader::source_archive);
190 add("tarball", &Loader::tarball);
191 add("version", &Loader::version);
194 void SourcePackage::Loader::finish()
196 /* Make sure the source tarball is last in the list so targets from all
197 other components wil be created first */
198 auto i = find(obj.components, obj.source_archive);
199 if(i!=obj.components.end())
201 obj.components.erase(i);
202 obj.components.push_back(obj.source_archive);
206 void SourcePackage::Loader::feature(const string &n, const string &d)
209 feat.description = d;
211 obj.features.push_back(feat);
213 const Config::Option &opt = obj.config.add_option(feat);
216 auto i = options->find(opt.name);
217 if(i!=options->end())
218 obj.config.set_option(opt.name, i->second);
222 template<typename C, typename... Args>
223 void SourcePackage::Loader::component(Args... args, const string &n)
225 if(any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; }))
227 C *comp = new C(obj, n, args...);
229 obj.components.push_back(comp);
232 void SourcePackage::Loader::build_info()
234 load_sub(obj.build_info);
237 void SourcePackage::Loader::generate(const string &tag)
239 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
241 obj.local_tools.add_tool(gen);
244 void SourcePackage::Loader::interface_version(const string &v)
246 obj.interface_version = v;
247 if(obj.version.empty())
251 void SourcePackage::Loader::source_archive()
253 load_sub(*obj.source_archive);
256 void SourcePackage::Loader::tarball(const string &)
258 IO::print("%s: Deprecated tarball component ignored\n", get_source());
261 void SourcePackage::Loader::version(const string &v)
265 string::size_type i = 0;
266 for(unsigned dots=0; i<obj.version.size(); ++i)
267 if(obj.version[i]=='.' && ++dots>=2)
269 obj.interface_version = obj.version.substr(0, i);