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/label_pattern.h"
16 #include "tools/gn/loader.h"
17 #include "tools/gn/scheduler.h"
18 #include "tools/gn/scope.h"
19 #include "tools/gn/settings.h"
20 #include "tools/gn/token.h"
21 #include "tools/gn/toolchain.h"
30 extern const char kDotfile_Help
[];
32 // Base class for code shared between Setup and DependentSetup.
35 virtual ~CommonSetup();
37 // Returns the scheduler. This is virtual since only the main setup has a
38 // scheduler and the derived ones just store pointers.
39 virtual Scheduler
* GetScheduler() = 0;
41 // When true (the default), Run() will check for unresolved dependencies and
42 // cycles upon completion. When false, such errors will be ignored.
43 void set_check_for_bad_items(bool s
) { check_for_bad_items_
= s
; }
45 // When true (the default), RunPostMessageLoop will check for overrides that
46 // were specified but not used. When false, such errors will be ignored.
47 void set_check_for_unused_overrides(bool s
) {
48 check_for_unused_overrides_
= s
;
51 // After a successful run, setting this will additionally cause the public
52 // headers to be checked. Defaults to false.
53 void set_check_public_headers(bool s
) {
54 check_public_headers_
= s
;
57 // Read from the .gn file, these are the targets to check. If the .gn file
58 // does not specify anything, this will be null. If the .gn file specifies
59 // the empty list, this will be non-null but empty.
60 const std::vector
<LabelPattern
>* check_patterns() const {
61 return check_patterns_
.get();
64 BuildSettings
& build_settings() { return build_settings_
; }
65 Builder
* builder() { return builder_
.get(); }
66 LoaderImpl
* loader() { return loader_
.get(); }
68 // Name of the file in the root build directory that contains the build
70 static const char kBuildArgFileName
[];
74 CommonSetup(const CommonSetup
& other
);
76 // Performs the two sets of operations to run the generation before and after
77 // the message loop is run.
78 void RunPreMessageLoop();
79 bool RunPostMessageLoop();
81 BuildSettings build_settings_
;
82 scoped_refptr
<LoaderImpl
> loader_
;
83 scoped_refptr
<Builder
> builder_
;
85 SourceFile root_build_file_
;
87 bool check_for_bad_items_
;
88 bool check_for_unused_overrides_
;
89 bool check_public_headers_
;
91 // See getter for info.
92 scoped_ptr
<std::vector
<LabelPattern
> > check_patterns_
;
95 CommonSetup
& operator=(const CommonSetup
& other
); // Disallow.
98 // Helper class to setup the build settings and environment for the various
100 class Setup
: public CommonSetup
{
105 // Configures the build for the current command line. On success returns
106 // true. On failure, prints the error and returns false.
108 // The parameter is the string the user specified for the build directory. We
109 // will try to interpret this as a SourceDir if possible, and will fail if is
112 // With force_create = false, setup will fail if the build directory doesn't
113 // alreay exist with an args file in it. With force_create set to true, the
114 // directory will be created if necessary. Commands explicitly doing
115 // generation should set this to true to create it, but querying commands
116 // should set it to false to prevent creating oddly-named directories in case
117 // the user omits the build directory argument (which is easy to do).
118 bool DoSetup(const std::string
& build_dir
, bool force_create
);
120 // Runs the load, returning true on success. On failure, prints the error
121 // and returns false. This includes both RunPreMessageLoop() and
122 // RunPostMessageLoop().
125 Scheduler
& scheduler() { return scheduler_
; }
127 Scheduler
* GetScheduler() override
;
129 // Returns the file used to store the build arguments. Note that the path
131 SourceFile
GetBuildArgFile() const;
133 // Sets whether the build arguments should be filled during setup from the
134 // command line/build argument file. This will be true by default. The use
135 // case for setting it to false is when editing build arguments, we don't
136 // want to rely on them being valid.
137 void set_fill_arguments(bool fa
) { fill_arguments_
= fa
; }
140 // Fills build arguments. Returns true on success.
141 bool FillArguments(const base::CommandLine
& cmdline
);
143 // Fills the build arguments from the command line or from the build arg file.
144 bool FillArgsFromCommandLine(const std::string
& args
);
145 bool FillArgsFromFile();
147 // Given an already-loaded args_input_file_, parses and saves the resulting
148 // arguments. Backend for the different FillArgs variants.
149 bool FillArgsFromArgsInputFile();
151 // Writes the build arguments to the build arg file.
152 bool SaveArgsToFile();
154 // Fills the root directory into the settings. Returns true on success.
155 bool FillSourceDir(const base::CommandLine
& cmdline
);
157 // Fills the build directory given the value the user has specified.
158 // Must happen after FillSourceDir so we can resolve source-relative
159 // paths. If require_exists is false, it will fail if the dir doesn't exist.
160 bool FillBuildDir(const std::string
& build_dir
, bool require_exists
);
162 // Fills the python path portion of the command line. On failure, sets
163 // it to just "python".
164 void FillPythonPath();
167 bool RunConfigFile();
169 bool FillOtherConfig(const base::CommandLine
& cmdline
);
171 Scheduler scheduler_
;
173 // These empty settings and toolchain are used to interpret the command line
175 BuildSettings empty_build_settings_
;
176 Settings empty_settings_
;
177 Scope dotfile_scope_
;
179 // State for invoking the dotfile.
180 base::FilePath dotfile_name_
;
181 scoped_ptr
<InputFile
> dotfile_input_file_
;
182 std::vector
<Token
> dotfile_tokens_
;
183 scoped_ptr
<ParseNode
> dotfile_root_
;
185 // Set to true when we should populate the build arguments from the command
186 // line or build argument file. See setter above.
187 bool fill_arguments_
;
189 // State for invoking the command line args. We specifically want to keep
190 // this around for the entire run so that Values can blame to the command
191 // line when we issue errors about them.
192 scoped_ptr
<InputFile
> args_input_file_
;
193 std::vector
<Token
> args_tokens_
;
194 scoped_ptr
<ParseNode
> args_root_
;
196 DISALLOW_COPY_AND_ASSIGN(Setup
);
199 // A dependent setup allows one to do more than one build at a time. You would
200 // make a dependent setup which clones the state of the main one, make any
201 // necessary changes, and then run it.
203 // The way to run both at the same time is:
204 // dependent_setup.RunPreMessageLoop();
206 // dependent_setup.RunPostMessageLoop();
207 // so that the main setup executes the message loop, but both are run.
208 class DependentSetup
: public CommonSetup
{
210 // Note: this could be one function that takes a CommonSetup*, but then
211 // the compiler can get confused what to call, since it also matches the
212 // default copy constructor.
213 DependentSetup(Setup
* derive_from
);
214 DependentSetup(DependentSetup
* derive_from
);
215 ~DependentSetup() override
;
217 // These are the two parts of Run() in the regular setup, not including the
218 // call to actually run the message loop.
219 void RunPreMessageLoop();
220 bool RunPostMessageLoop();
222 Scheduler
* GetScheduler() override
;
225 Scheduler
* scheduler_
;
228 #endif // TOOLS_GN_SETUP_H_