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