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