]> git.tdb.fi Git - builder.git/blob - source/lib/sourcepackage.h
d458c9094208c9c86ff4755bb34d9bb47aa6d3b7
[builder.git] / source / lib / sourcepackage.h
1 #ifndef SOURCEPACKAGE_H_
2 #define SOURCEPACKAGE_H_
3
4 #include <algorithm>
5 #include <stdexcept>
6 #include <string>
7 #include <msp/core/typeregistry.h>
8 #include "buildinfo.h"
9 #include "cache.h"
10 #include "component.h"
11 #include "conditionalloader.h"
12 #include "config.h"
13 #include "feature.h"
14 #include "package.h"
15 #include "toolchain.h"
16
17 class BuildType;
18 class FileTarget;
19 class SourceArchiveComponent;
20
21 /**
22 A package that can be built by Builder.
23 */
24 class SourcePackage: public Package
25 {
26 public:
27         class Loader: public Msp::DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>, public FeatureConditional
28         {
29                 friend class SourcePackage;
30
31         private:
32                 template<typename T>
33                 struct AddComponent
34                 {
35                         void operator()(const std::string &n, Loader &l) const { l.add(n, &Loader::component<T>); }
36                 };
37
38                 const Config::InputOptions *options;
39
40         public:
41                 Loader(SourcePackage &, const Config::InputOptions *);
42         private:
43                 void finish() override;
44
45                 void feature(const std::string &, const std::string &);
46                 template<typename C, typename... Args>
47                 void component(Args..., const std::string &);
48                 void build_info();
49                 void generate(const std::string &);
50                 void interface_version(const std::string &);
51                 void source_archive();
52                 void tarball(const std::string &);
53                 void version(const std::string &);
54         };
55
56         using ComponentRegistry = Msp::TypeRegistry<Loader::AddComponent, Loader &>;
57
58 private:
59         std::string version;
60         std::string interface_version;
61         std::string description;
62
63         FileTarget *build_file;
64         Msp::FS::Path source_dir;
65         const BuildType *build_type = 0;
66         Toolchain local_tools;
67         std::vector<Feature> features;
68         BuildInfo build_info;
69         std::vector<Component *> components;
70         SourceArchiveComponent *source_archive;
71         Config config;
72         mutable Cache cache;
73
74 public:
75         SourcePackage(Builder &, const std::string &, const Msp::FS::Path &);
76         ~SourcePackage();
77
78         const std::string &get_version() const { return version; }
79         const std::string &get_interface_version() const { return interface_version; }
80         const std::string &get_description() const { return description; }
81
82         FileTarget &get_build_file() const { return *build_file; }
83         const Msp::FS::Path &get_source_directory() const { return source_dir; }
84         Msp::FS::Path get_temp_directory() const;
85         Msp::FS::Path get_output_directory() const;
86
87         const Toolchain &get_toolchain() const { return local_tools; }
88         const Component &get_component(const std::string &) const;
89         const Config &get_config() const { return config; }
90         bool match_feature(const std::string &, const std::string *) const;
91         void set_build_type(const BuildType &);
92         const BuildInfo &get_build_info() const { return build_info; }
93 private:
94         void do_prepare() override;
95
96 public:
97         Cache &get_cache() const { return cache; }
98 private:
99         void save_caches() override;
100 };
101
102
103 template<typename C, typename... Args>
104 void SourcePackage::Loader::component(Args... args, const std::string &n)
105 {
106         if(std::any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; }))
107                 throw Msp::key_error(n);
108         C *comp = new C(obj, n, args...);
109         load_sub(*comp);
110         obj.components.push_back(comp);
111 }
112
113 #endif