]> git.tdb.fi Git - builder.git/blob - source/builder.h
Rewrite the architecture system
[builder.git] / source / builder.h
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef BUILDER_H_
9 #define BUILDER_H_
10
11 #include <list>
12 #include <map>
13 #include <string>
14 #include <msp/core/application.h>
15 #include <msp/datafile/loader.h>
16 #include <msp/fs/path.h>
17 #include "architecture.h"
18 #include "config.h"
19 #include "misc.h"
20 #include "problem.h"
21 #include "target.h"
22
23 class Analyzer;
24 class Config;
25 class Package;
26 class SourcePackage;
27
28 /**
29 The main application class.  Controls and owns everything.  Rules the world.
30 */
31 class Builder: public Msp::Application
32 {
33 private:
34         class Loader: public Msp::DataFile::Loader
35         {
36         private:
37                 Builder &bld;
38                 Msp::FS::Path src;
39
40         public:
41                 Loader(Builder &, const Msp::FS::Path &);
42         private:
43                 void binpkg(const std::string &);
44                 void cross_prefix(const std::string &, const std::string &);
45                 void profile(const std::string &);
46                 void package(const std::string &);
47         };
48
49         class ProfileLoader: public Msp::DataFile::Loader
50         {
51         private:
52                 StringMap &profile;
53
54         public:
55                 ProfileLoader(StringMap &);
56         private:
57                 void option(const std::string &, const std::string &);
58         };
59
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
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 Msp::Application::RegApp<Builder> reg;
102         static std::string usagemsg;
103         static std::string helpmsg;
104
105 public:
106         Builder(int, char **);
107         ~Builder();
108
109         int main();
110         unsigned get_verbose() const { return verbose; }
111         bool get_dry_run() const { return dry_run; }
112         bool get_build_all() const { return build_all; }
113
114         /** Gets a package by name, possibly creating it.  Returns 0 if the package
115         could not be located. */
116         Package *get_package(const std::string &);
117
118         SourcePackage *get_main_package() const { return main_pkg; }
119
120         std::string run_pkgconfig(const std::string &, const std::string &);
121
122         /** Looks up a target by name.  Returns 0 if no such target exists. */
123         Target *get_target(const std::string &) const;
124
125         const TargetMap &get_targets() const { return targets; }
126
127         /** Tries to locate a header based on location of including file and include
128         path.  Considers known targets as well as existing files.  If a matching
129         target is not found but a file exists, a new SystemHeader target will be
130         created and returned. */
131         Target *get_header(const std::string &, const Msp::FS::Path &, const StringList &);
132
133         /** Tries to locate a library in a library path.  The library name should be
134         the same as would be given to the linker with -l, i.e. without the "lib"
135         prefix or extension.  Considers known targets as well as existing files.  If
136         a matching target is not found but a file exists, a new SystemLibrary target
137         will be created and returned. */
138         Target *get_library(const std::string &, const StringList &, LibMode);
139
140         const Msp::FS::Path &get_cwd() const { return cwd; }
141         const Architecture &get_current_arch() const { return *current_arch; }
142         const Architecture &get_native_arch() const { return native_arch; }
143         const Msp::FS::Path &get_prefix() const { return prefix; }
144         const StringList &get_warnings() const { return warnings; }
145         void apply_profile_template(Config &, const std::string &) const;
146
147         /** Adds a target to both the target map and the new target queue.  Called
148         from Target constructor. */
149         void add_target(Target *);
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