]> git.tdb.fi Git - builder.git/blob - source/lib/builder.h
Add visibility decorations to the library and 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();
58         };
59
60         std::vector<LoadedPlugin> plugins;
61         PackageManager package_manager;
62         SourcePackage::ComponentRegistry component_registry;
63
64         Architecture native_arch;
65         Architecture *current_arch = 0;
66         std::map<std::string, BuildType> build_types;
67         BuildType *build_type = 0;
68         Toolchain toolchain;
69         VirtualFileSystem vfs;
70         BuildGraph build_graph;
71         Logger default_logger;
72         const Logger *logger;
73
74         bool auto_prefix = true;
75         Msp::FS::Path prefix;
76         Msp::FS::Path tempdir = "temp";
77
78         Loader *top_loader = 0;
79
80 public:
81         Builder();
82         ~Builder();
83
84         void load_plugins();
85         PackageManager &get_package_manager() { return package_manager; }
86         SourcePackage::ComponentRegistry &get_component_registry() { return component_registry; }
87
88         template<typename F>
89         void call_plugins(F) const;
90
91         void set_architecture(const std::string &);
92         const Architecture &get_current_arch() const { return *current_arch; }
93         const Architecture &get_native_arch() const { return native_arch; }
94         void set_build_type(const std::string &);
95         std::vector<std::string> get_build_types() const;
96         const BuildType &get_build_type() const;
97         BuildGraph &get_build_graph() { return build_graph; }
98         void set_prefix(const Msp::FS::Path &);
99         void set_temp_directory(const Msp::FS::Path &);
100         const Msp::FS::Path &get_prefix() const { return prefix; }
101         const Msp::FS::Path &get_temp_directory() const { return tempdir; }
102
103 private:
104         void update_auto_prefix();
105
106 public:
107         void add_default_tools();
108         const Toolchain &get_toolchain() const { return toolchain; }
109         VirtualFileSystem &get_vfs() { return vfs; }
110         void set_logger(const Logger *);
111         const Logger &get_logger() const { return *logger; }
112
113         std::vector<std::string> collect_problems() const;
114 private:
115         void collect_broken_packages(const Package &, std::vector<const Package *> &) const;
116
117 public:
118         /** Loads a build file.  If opts is not null, it is used to configure any
119         packages loaded from this file.  If all is true, external packages are also
120         configured. */
121         void load_build_file(const Msp::FS::Path &, const Config::InputOptions *opts = 0, bool all = false);
122
123         /** Saves package configuration and dependency caches. */
124         void save_caches();
125
126         /** Builds the goal targets.  The build graph must be prepared first. */
127         int build(unsigned jobs = 1, bool dry_run = false, bool show_progress = false);
128
129         /** Cleans buildable targets.  If all is true, cleans all packages.
130         Otherwise cleans only the default package. */
131         int clean(bool all = false, bool dry_run = false);
132
133         int do_create_makefile();
134 };
135
136 template<typename F>
137 void Builder::call_plugins(F func) const
138 {
139         for(const LoadedPlugin &p: plugins)
140                 func(*p.plugin);
141 }
142
143 #endif