]> git.tdb.fi Git - builder.git/blob - source/lib/builder.h
Make it possible to use built-in plugins
[builder.git] / source / lib / builder.h
1 #ifndef BUILDER_H_
2 #define BUILDER_H_
3
4 #include <map>
5 #include <string>
6 #include <msp/core/module.h>
7 #include <msp/datafile/loader.h>
8 #include <msp/fs/path.h>
9 #include "architecture.h"
10 #include "buildgraph.h"
11 #include "buildtype.h"
12 #include "config.h"
13 #include "libbuilder_api.h"
14 #include "logger.h"
15 #include "packagemanager.h"
16 #include "sourcepackage.h"
17 #include "target.h"
18 #include "toolchain.h"
19 #include "virtualfilesystem.h"
20
21 class Package;
22 class Plugin;
23
24 /**
25 This class ties everything else together.  It also contains code for loading
26 build files and supervising the build process.
27 */
28 class LIBBUILDER_API Builder
29 {
30 private:
31         class Loader: public Msp::DataFile::ObjectLoader<Builder>
32         {
33         private:
34                 const Config::InputOptions *options;
35                 bool conf_all;
36
37         public:
38                 Loader(Builder &, const Config::InputOptions * = 0, bool = false);
39                 ~Loader();
40
41         private:
42                 void architecture(const std::string &);
43                 void binpkg(const std::string &);
44                 void build_type(const std::string &);
45                 void package(const std::string &);
46         };
47
48 private:
49         struct LoadedPlugin
50         {
51                 Msp::FS::Path path;
52                 Msp::Module *module = nullptr;
53                 Plugin *plugin = nullptr;
54
55                 LoadedPlugin() = default;
56                 LoadedPlugin(LoadedPlugin &&);
57                 LoadedPlugin &operator=(LoadedPlugin &&);
58                 ~LoadedPlugin();
59         };
60
61         std::vector<LoadedPlugin> plugins;
62         PackageManager package_manager;
63         SourcePackage::ComponentRegistry component_registry;
64
65         Architecture native_arch;
66         Architecture *current_arch = 0;
67         std::map<std::string, BuildType> build_types;
68         BuildType *build_type = 0;
69         Toolchain toolchain;
70         VirtualFileSystem vfs;
71         BuildGraph build_graph;
72         Logger default_logger;
73         const Logger *logger;
74
75         bool auto_prefix = true;
76         Msp::FS::Path prefix;
77         Msp::FS::Path tempdir = "temp";
78
79         Loader *top_loader = 0;
80
81 public:
82         Builder();
83         ~Builder();
84
85         void load_plugins();
86
87         template<typename... T>
88         void load_plugins();
89
90 private:
91         void add_plugins(std::vector<LoadedPlugin> &);
92
93 public:
94         PackageManager &get_package_manager() { return package_manager; }
95         SourcePackage::ComponentRegistry &get_component_registry() { return component_registry; }
96
97         template<typename F>
98         void call_plugins(F) const;
99
100         void set_architecture(const std::string &);
101         const Architecture &get_current_arch() const { return *current_arch; }
102         const Architecture &get_native_arch() const { return native_arch; }
103         void set_build_type(const std::string &);
104         std::vector<std::string> get_build_types() const;
105         const BuildType &get_build_type() const;
106         BuildGraph &get_build_graph() { return build_graph; }
107         void set_prefix(const Msp::FS::Path &);
108         void set_temp_directory(const Msp::FS::Path &);
109         const Msp::FS::Path &get_prefix() const { return prefix; }
110         const Msp::FS::Path &get_temp_directory() const { return tempdir; }
111
112 private:
113         void update_auto_prefix();
114
115 public:
116         void add_default_tools();
117         const Toolchain &get_toolchain() const { return toolchain; }
118         VirtualFileSystem &get_vfs() { return vfs; }
119         void set_logger(const Logger *);
120         const Logger &get_logger() const { return *logger; }
121
122         std::vector<std::string> collect_problems() const;
123 private:
124         void collect_broken_packages(const Package &, std::vector<const Package *> &) const;
125
126 public:
127         /** Loads a build file.  If opts is not null, it is used to configure any
128         packages loaded from this file.  If all is true, external packages are also
129         configured. */
130         void load_build_file(const Msp::FS::Path &, const Config::InputOptions *opts = 0, bool all = false);
131
132         /** Saves package configuration and dependency caches. */
133         void save_caches();
134
135         /** Builds the goal targets.  The build graph must be prepared first. */
136         int build(unsigned jobs = 1, bool dry_run = false, bool show_progress = false);
137
138         /** Cleans buildable targets.  If all is true, cleans all packages.
139         Otherwise cleans only the default package. */
140         int clean(bool all = false, bool dry_run = false);
141
142         int do_create_makefile();
143 };
144
145 template<typename... T>
146 void Builder::load_plugins()
147 {
148         Plugin *raw_plugins[] = { new T(*this)... };
149         std::vector<LoadedPlugin> pending_plugins;
150         for(Plugin *p: raw_plugins)
151         {
152                 LoadedPlugin plugin;
153                 plugin.plugin = p;
154                 pending_plugins.emplace_back(std::move(plugin));
155         }
156         add_plugins(pending_plugins);
157 }
158
159 template<typename F>
160 void Builder::call_plugins(F func) const
161 {
162         for(const LoadedPlugin &p: plugins)
163                 func(*p.plugin);
164 }
165
166 #endif