]> git.tdb.fi Git - builder.git/blob - source/builder.h
Big rewrite for a more tool-centric approach
[builder.git] / source / builder.h
1 #ifndef BUILDER_H_
2 #define BUILDER_H_
3
4 #include <list>
5 #include <map>
6 #include <string>
7 #include <msp/core/application.h>
8 #include <msp/datafile/loader.h>
9 #include <msp/fs/path.h>
10 #include "architecture.h"
11 #include "config.h"
12 #include "misc.h"
13 #include "problem.h"
14 #include "target.h"
15 #include "toolchain.h"
16
17 class Analyzer;
18 class Config;
19 class FileTarget;
20 class Package;
21 class SourcePackage;
22 class VirtualTarget;
23
24 /**
25 The main application class.  Controls and owns everything.  Rules the world.
26 */
27 class Builder: public Msp::RegisteredApplication<Builder>
28 {
29 private:
30         class Loader: public Msp::DataFile::Loader
31         {
32         private:
33                 Builder &bld;
34                 Msp::FS::Path src;
35
36         public:
37                 Loader(Builder &, const Msp::FS::Path &);
38         private:
39                 void binpkg(const std::string &);
40                 void cross_prefix(const std::string &, const std::string &);
41                 void profile(const std::string &);
42                 void package(const std::string &);
43         };
44
45         class ProfileLoader: public Msp::DataFile::Loader
46         {
47         private:
48                 StringMap &profile;
49
50         public:
51                 ProfileLoader(StringMap &);
52         private:
53                 void option(const std::string &, const std::string &);
54         };
55
56 public:
57         typedef std::map<std::string, Target *> TargetMap;
58
59 private:
60         typedef std::list<Package *> PackageList;
61         typedef std::map<std::string, Package *> PackageMap;
62         typedef std::map<std::string, StringMap> ProfileTemplateMap;
63
64         StringList cmdline_targets;
65         StringMap cmdline_options;
66         Msp::FS::Path cwd;
67
68         PackageMap packages;
69         SourcePackage *main_pkg;
70         PathList pkg_path;
71         PathList pkg_dirs;
72         bool no_externals;
73
74         TargetMap targets;
75         TargetMap targets_by_path;
76         TargetList new_tgts;
77         TargetMap includes;
78         TargetMap libraries;
79
80         Architecture native_arch;
81         Architecture *current_arch;
82         StringMap cross_prefixes;
83         ProfileTemplateMap profile_tmpl;
84         Toolchain toolchain;
85
86         ProblemList problems;
87         Analyzer *analyzer;
88         bool build;
89         unsigned clean;
90         bool dry_run;
91         bool help;
92         unsigned verbose;
93         bool show_progress;
94         std::string build_file;
95         unsigned jobs;
96         StringList what_if;
97         bool conf_all;
98         bool conf_only;
99         bool build_all;
100         bool create_makefile;
101         Msp::FS::Path prefix;
102         StringList warnings;
103
104         static std::string usagemsg;
105         static std::string helpmsg;
106
107 public:
108         Builder(int, char **);
109         ~Builder();
110
111         int main();
112         unsigned get_verbose() const { return verbose; }
113         bool get_dry_run() const { return dry_run; }
114         bool get_build_all() const { return build_all; }
115
116         /** Gets a package by name, possibly creating it.  Returns 0 if the package
117         could not be located. */
118         Package *get_package(const std::string &);
119
120         SourcePackage *get_main_package() const { return main_pkg; }
121
122         std::string run_pkgconfig(const std::string &, const std::string &);
123
124         /** Looks up a target by name.  Returns 0 if no such target exists. */
125         Target *get_target(const std::string &) const;
126
127         FileTarget *get_target_by_path(const Msp::FS::Path &) const;
128
129         const TargetMap &get_targets() const { return targets; }
130
131         /** Tries to locate a header based on location of including file and include
132         path.  Considers known targets as well as existing files.  If a matching
133         target is not found but a file exists, a new SystemHeader target will be
134         created and returned. */
135         Target *get_header(const std::string &, const Msp::FS::Path &, const StringList &);
136
137         /** Tries to locate a library in a library path.  The library name should be
138         the same as would be given to the linker with -l, i.e. without the "lib"
139         prefix or extension.  Considers known targets as well as existing files.  If
140         a matching target is not found but a file exists, a new SystemLibrary target
141         will be created and returned. */
142         Target *get_library(const std::string &, const StringList &, LibMode);
143
144         const Msp::FS::Path &get_cwd() const { return cwd; }
145         const Architecture &get_current_arch() const { return *current_arch; }
146         const Architecture &get_native_arch() const { return native_arch; }
147         const Msp::FS::Path &get_prefix() const { return prefix; }
148         const StringList &get_warnings() const { return warnings; }
149         void apply_profile_template(Config &, const std::string &) const;
150
151         const Toolchain &get_toolchain() const { return toolchain; }
152
153         /** Adds a target to both the target map and the new target queue.  Called
154         from Target constructor. */
155         void add_target(Target *);
156         void register_path(const Msp::FS::Path &, FileTarget *);
157
158         void problem(const std::string &, const std::string &);
159
160         static void usage(const char *, const char *, bool);
161
162 private:
163         /** Determines the source directory of a package.  Pkg-config is consulted
164         first, and if it fails, the package path is searched for matches. */
165         Msp::FS::Path get_package_location(const std::string &);
166
167         /** Loads a build file.  Returns 0 on success or -1 if the file could not be
168         opened. */
169         int load_build_file(const Msp::FS::Path &);
170
171         /** Creates targets for all packages and prepares them for building.
172         Returns 0 if everything went ok, -1 if something bad happened and a build
173         shouldn't be attempted. */
174         int create_targets();
175
176         /**
177         Check if a header exists, either as a target or a file.  Returns an existing
178         target of one was found, or a new SystemHeader target if there was no known
179         target but the file exists.
180         */
181         Target *get_header(const Msp::FS::Path &);
182
183         Target *get_library(const std::string &, const Msp::FS::Path &, LibMode);
184
185         /** Supervises the build process, starting new actions when slots become
186         available. */
187         int do_build();
188
189         /** Cleans buildable targets.  If clean is 1, cleans only the default
190         package.  If clean is 2 or greater, cleans all buildable packages.
191         */
192         int do_clean();
193
194         int do_create_makefile();
195
196         /** Prints out information about the default package. */
197         void package_help();
198 };
199
200 #endif