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