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/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "tools/gn/build_settings.h"
15 #include "tools/gn/builder.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 BuildSettings
& build_settings() { return build_settings_
; }
58 Builder
* builder() { return builder_
.get(); }
59 LoaderImpl
* loader() { return loader_
.get(); }
61 // Name of the file in the root build directory that contains the build
63 static const char kBuildArgFileName
[];
67 CommonSetup(const CommonSetup
& other
);
69 // Performs the two sets of operations to run the generation before and after
70 // the message loop is run.
71 void RunPreMessageLoop();
72 bool RunPostMessageLoop();
74 BuildSettings build_settings_
;
75 scoped_refptr
<LoaderImpl
> loader_
;
76 scoped_refptr
<Builder
> builder_
;
78 SourceFile root_build_file_
;
80 bool check_for_bad_items_
;
81 bool check_for_unused_overrides_
;
82 bool check_public_headers_
;
85 CommonSetup
& operator=(const CommonSetup
& other
); // Disallow.
88 // Helper class to setup the build settings and environment for the various
90 class Setup
: public CommonSetup
{
95 // Configures the build for the current command line. On success returns
96 // true. On failure, prints the error and returns false.
98 // The parameter is the string the user specified for the build directory. We
99 // will try to interpret this as a SourceDir if possible, and will fail if is
101 bool DoSetup(const std::string
& build_dir
);
103 // Runs the load, returning true on success. On failure, prints the error
104 // and returns false. This includes both RunPreMessageLoop() and
105 // RunPostMessageLoop().
108 Scheduler
& scheduler() { return scheduler_
; }
110 virtual Scheduler
* GetScheduler() OVERRIDE
;
112 // Returns the file used to store the build arguments. Note that the path
114 SourceFile
GetBuildArgFile() const;
116 // Sets whether the build arguments should be filled during setup from the
117 // command line/build argument file. This will be true by default. The use
118 // case for setting it to false is when editing build arguments, we don't
119 // want to rely on them being valid.
120 void set_fill_arguments(bool fa
) { fill_arguments_
= fa
; }
123 // Fills build arguments. Returns true on success.
124 bool FillArguments(const base::CommandLine
& cmdline
);
126 // Fills the build arguments from the command line or from the build arg file.
127 bool FillArgsFromCommandLine(const std::string
& args
);
128 bool FillArgsFromFile();
130 // Given an already-loaded args_input_file_, parses and saves the resulting
131 // arguments. Backend for the different FillArgs variants.
132 bool FillArgsFromArgsInputFile();
134 // Writes the build arguments to the build arg file.
135 bool SaveArgsToFile();
137 // Fills the root directory into the settings. Returns true on success.
138 bool FillSourceDir(const base::CommandLine
& cmdline
);
140 // Fills the build directory given the value the user has specified.
141 // Must happen after FillSourceDir so we can resolve source-relative
143 bool FillBuildDir(const std::string
& build_dir
);
145 // Fills the python path portion of the command line. On failure, sets
146 // it to just "python".
147 void FillPythonPath();
150 bool RunConfigFile();
152 bool FillOtherConfig(const base::CommandLine
& cmdline
);
154 Scheduler scheduler_
;
156 // These empty settings and toolchain are used to interpret the command line
158 BuildSettings empty_build_settings_
;
159 Settings empty_settings_
;
160 Scope dotfile_scope_
;
162 // State for invoking the dotfile.
163 base::FilePath dotfile_name_
;
164 scoped_ptr
<InputFile
> dotfile_input_file_
;
165 std::vector
<Token
> dotfile_tokens_
;
166 scoped_ptr
<ParseNode
> dotfile_root_
;
168 // Set to true when we should populate the build arguments from the command
169 // line or build argument file. See setter above.
170 bool fill_arguments_
;
172 // State for invoking the command line args. We specifically want to keep
173 // this around for the entire run so that Values can blame to the command
174 // line when we issue errors about them.
175 scoped_ptr
<InputFile
> args_input_file_
;
176 std::vector
<Token
> args_tokens_
;
177 scoped_ptr
<ParseNode
> args_root_
;
179 DISALLOW_COPY_AND_ASSIGN(Setup
);
182 // A dependent setup allows one to do more than one build at a time. You would
183 // make a dependent setup which clones the state of the main one, make any
184 // necessary changes, and then run it.
186 // The way to run both at the same time is:
187 // dependent_setup.RunPreMessageLoop();
189 // dependent_setup.RunPostMessageLoop();
190 // so that the main setup executes the message loop, but both are run.
191 class DependentSetup
: public CommonSetup
{
193 // Note: this could be one function that takes a CommonSetup*, but then
194 // the compiler can get confused what to call, since it also matches the
195 // default copy constructor.
196 DependentSetup(Setup
* derive_from
);
197 DependentSetup(DependentSetup
* derive_from
);
198 virtual ~DependentSetup();
200 // These are the two parts of Run() in the regular setup, not including the
201 // call to actually run the message loop.
202 void RunPreMessageLoop();
203 bool RunPostMessageLoop();
205 virtual Scheduler
* GetScheduler() OVERRIDE
;
208 Scheduler
* scheduler_
;
211 #endif // TOOLS_GN_SETUP_H_