]> git.tdb.fi Git - builder.git/blob - source/builder.h
Make the name of a FileTarget be its basename instead of full path
[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
75         TargetMap targets;
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
85         ProblemList problems;
86         Analyzer *analyzer;
87         bool build;
88         unsigned clean;
89         bool dry_run;
90         bool help;
91         unsigned verbose;
92         bool show_progress;
93         std::string build_file;
94         unsigned jobs;
95         StringList what_if;
96         bool conf_all;
97         bool conf_only;
98         bool build_all;
99         bool create_makefile;
100         Msp::FS::Path prefix;
101         StringList warnings;
102
103         static Msp::Application::RegApp<Builder> reg;
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         const TargetMap &get_targets() const { return targets; }
128
129         /** Tries to locate a header based on location of including file and include
130         path.  Considers known targets as well as existing files.  If a matching
131         target is not found but a file exists, a new SystemHeader target will be
132         created and returned. */
133         Target *get_header(const std::string &, const Msp::FS::Path &, const StringList &);
134
135         /** Tries to locate a library in a library path.  The library name should be
136         the same as would be given to the linker with -l, i.e. without the "lib"
137         prefix or extension.  Considers known targets as well as existing files.  If
138         a matching target is not found but a file exists, a new SystemLibrary target
139         will be created and returned. */
140         Target *get_library(const std::string &, const StringList &, LibMode);
141
142         const Msp::FS::Path &get_cwd() const { return cwd; }
143         const Architecture &get_current_arch() const { return *current_arch; }
144         const Architecture &get_native_arch() const { return native_arch; }
145         const Msp::FS::Path &get_prefix() const { return prefix; }
146         const StringList &get_warnings() const { return warnings; }
147         void apply_profile_template(Config &, const std::string &) const;
148
149         /** Adds a target to both the target map and the new target queue.  Called
150         from Target constructor. */
151         void add_target(FileTarget *);
152         void add_target(VirtualTarget *);
153
154         void problem(const std::string &, const std::string &);
155
156         static void usage(const char *, const char *, bool);
157
158 private:
159         /** Determines the source directory of a package.  Pkg-config is consulted
160         first, and if it fails, the package path is searched for matches. */
161         Msp::FS::Path get_package_location(const std::string &);
162
163         /** Loads a build file.  Returns 0 on success or -1 if the file could not be
164         opened. */
165         int load_build_file(const Msp::FS::Path &);
166
167         /** Creates targets for all packages and prepares them for building.
168         Returns 0 if everything went ok, -1 if something bad happened and a build
169         shouldn't be attempted. */
170         int create_targets();
171
172         /**
173         Check if a header exists, either as a target or a file.  Returns an existing
174         target of one was found, or a new SystemHeader target if there was no known
175         target but the file exists.
176         */
177         Target *get_header(const Msp::FS::Path &);
178
179         Target *get_library(const std::string &, const Msp::FS::Path &, LibMode);
180
181         /** Supervises the build process, starting new actions when slots become
182         available. */
183         int do_build();
184
185         /** Cleans buildable targets.  If clean is 1, cleans only the default
186         package.  If clean is 2 or greater, cleans all buildable packages.
187         */
188         int do_clean();
189
190         int do_create_makefile();
191
192         /** Prints out information about the default package. */
193         void package_help();
194 };
195
196 #endif