1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_SETUP_H_
6 #define TOOLS_GN_SETUP_H_
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "tools/gn/build_settings.h"
14 #include "tools/gn/builder.h"
15 #include "tools/gn/loader.h"
16 #include "tools/gn/scheduler.h"
17 #include "tools/gn/scope.h"
18 #include "tools/gn/settings.h"
19 #include "tools/gn/token.h"
20 #include "tools/gn/toolchain.h"
29 extern const char kDotfile_Help
[];
31 // Base class for code shared between Setup and DependentSetup.
34 virtual ~CommonSetup();
36 // Returns the scheduler. This is virtual since only the main setup has a
37 // scheduler and the derived ones just store pointers.
38 virtual Scheduler
* GetScheduler() = 0;
40 // When true (the default), Run() will check for unresolved dependencies and
41 // cycles upon completion. When false, such errors will be ignored.
42 void set_check_for_bad_items(bool s
) { check_for_bad_items_
= s
; }
44 // When true (the default), RunPostMessageLoop will check for overrides that
45 // were specified but not used. When false, such errors will be ignored.
46 void set_check_for_unused_overrides(bool s
) {
47 check_for_unused_overrides_
= s
;
50 // After a successful run, setting this will additionally cause the public
51 // headers to be checked. Defaults to false.
52 void set_check_public_headers(bool s
) {
53 check_public_headers_
= s
;
56 BuildSettings
& build_settings() { return build_settings_
; }
57 Builder
* builder() { return builder_
.get(); }
58 LoaderImpl
* loader() { return loader_
.get(); }
60 // Name of the file in the root build directory that contains the build
62 static const char kBuildArgFileName
[];
66 CommonSetup(const CommonSetup
& other
);
68 // Performs the two sets of operations to run the generation before and after
69 // the message loop is run.
70 void RunPreMessageLoop();
71 bool RunPostMessageLoop();
73 BuildSettings build_settings_
;
74 scoped_refptr
<LoaderImpl
> loader_
;
75 scoped_refptr
<Builder
> builder_
;
77 SourceFile root_build_file_
;
79 bool check_for_bad_items_
;
80 bool check_for_unused_overrides_
;
81 bool check_public_headers_
;
84 CommonSetup
& operator=(const CommonSetup
& other
); // Disallow.
87 // Helper class to setup the build settings and environment for the various
89 class Setup
: public CommonSetup
{
94 // Configures the build for the current command line. On success returns
95 // true. On failure, prints the error and returns false.
97 // The parameter is the string the user specified for the build directory. We
98 // will try to interpret this as a SourceDir if possible, and will fail if is
101 // With force_create = false, setup will fail if the build directory doesn't
102 // alreay exist with an args file in it. With force_create set to true, the
103 // directory will be created if necessary. Commands explicitly doing
104 // generation should set this to true to create it, but querying commands
105 // should set it to false to prevent creating oddly-named directories in case
106 // the user omits the build directory argument (which is easy to do).
107 bool DoSetup(const std::string
& build_dir
, bool force_create
);
109 // Runs the load, returning true on success. On failure, prints the error
110 // and returns false. This includes both RunPreMessageLoop() and
111 // RunPostMessageLoop().
114 Scheduler
& scheduler() { return scheduler_
; }
116 Scheduler
* GetScheduler() override
;
118 // Returns the file used to store the build arguments. Note that the path
120 SourceFile
GetBuildArgFile() const;
122 // Sets whether the build arguments should be filled during setup from the
123 // command line/build argument file. This will be true by default. The use
124 // case for setting it to false is when editing build arguments, we don't
125 // want to rely on them being valid.
126 void set_fill_arguments(bool fa
) { fill_arguments_
= fa
; }
129 // Fills build arguments. Returns true on success.
130 bool FillArguments(const base::CommandLine
& cmdline
);
132 // Fills the build arguments from the command line or from the build arg file.
133 bool FillArgsFromCommandLine(const std::string
& args
);
134 bool FillArgsFromFile();
136 // Given an already-loaded args_input_file_, parses and saves the resulting
137 // arguments. Backend for the different FillArgs variants.
138 bool FillArgsFromArgsInputFile();
140 // Writes the build arguments to the build arg file.
141 bool SaveArgsToFile();
143 // Fills the root directory into the settings. Returns true on success.
144 bool FillSourceDir(const base::CommandLine
& cmdline
);
146 // Fills the build directory given the value the user has specified.
147 // Must happen after FillSourceDir so we can resolve source-relative
148 // paths. If require_exists is false, it will fail if the dir doesn't exist.
149 bool FillBuildDir(const std::string
& build_dir
, bool require_exists
);
151 // Fills the python path portion of the command line. On failure, sets
152 // it to just "python".
153 void FillPythonPath();
156 bool RunConfigFile();
158 bool FillOtherConfig(const base::CommandLine
& cmdline
);
160 Scheduler scheduler_
;
162 // These empty settings and toolchain are used to interpret the command line
164 BuildSettings empty_build_settings_
;
165 Settings empty_settings_
;
166 Scope dotfile_scope_
;
168 // State for invoking the dotfile.
169 base::FilePath dotfile_name_
;
170 scoped_ptr
<InputFile
> dotfile_input_file_
;
171 std::vector
<Token
> dotfile_tokens_
;
172 scoped_ptr
<ParseNode
> dotfile_root_
;
174 // Set to true when we should populate the build arguments from the command
175 // line or build argument file. See setter above.
176 bool fill_arguments_
;
178 // State for invoking the command line args. We specifically want to keep
179 // this around for the entire run so that Values can blame to the command
180 // line when we issue errors about them.
181 scoped_ptr
<InputFile
> args_input_file_
;
182 std::vector
<Token
> args_tokens_
;
183 scoped_ptr
<ParseNode
> args_root_
;
185 DISALLOW_COPY_AND_ASSIGN(Setup
);
188 // A dependent setup allows one to do more than one build at a time. You would
189 // make a dependent setup which clones the state of the main one, make any
190 // necessary changes, and then run it.
192 // The way to run both at the same time is:
193 // dependent_setup.RunPreMessageLoop();
195 // dependent_setup.RunPostMessageLoop();
196 // so that the main setup executes the message loop, but both are run.
197 class DependentSetup
: public CommonSetup
{
199 // Note: this could be one function that takes a CommonSetup*, but then
200 // the compiler can get confused what to call, since it also matches the
201 // default copy constructor.
202 DependentSetup(Setup
* derive_from
);
203 DependentSetup(DependentSetup
* derive_from
);
204 ~DependentSetup() override
;
206 // These are the two parts of Run() in the regular setup, not including the
207 // call to actually run the message loop.
208 void RunPreMessageLoop();
209 bool RunPostMessageLoop();
211 Scheduler
* GetScheduler() override
;
214 Scheduler
* scheduler_
;
217 #endif // TOOLS_GN_SETUP_H_