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(); }
63 CommonSetup(const CommonSetup
& other
);
65 // Performs the two sets of operations to run the generation before and after
66 // the message loop is run.
67 void RunPreMessageLoop();
68 bool RunPostMessageLoop();
70 BuildSettings build_settings_
;
71 scoped_refptr
<LoaderImpl
> loader_
;
72 scoped_refptr
<Builder
> builder_
;
74 SourceFile root_build_file_
;
76 bool check_for_bad_items_
;
77 bool check_for_unused_overrides_
;
78 bool check_public_headers_
;
81 CommonSetup
& operator=(const CommonSetup
& other
); // Disallow.
84 // Helper class to setup the build settings and environment for the various
86 class Setup
: public CommonSetup
{
91 // Configures the build for the current command line. On success returns
92 // true. On failure, prints the error and returns false.
94 // The parameter is the string the user specified for the build directory. We
95 // will try to interpret this as a SourceDir if possible, and will fail if is
97 bool DoSetup(const std::string
& build_dir
);
99 // Runs the load, returning true on success. On failure, prints the error
100 // and returns false. This includes both RunPreMessageLoop() and
101 // RunPostMessageLoop().
104 Scheduler
& scheduler() { return scheduler_
; }
106 virtual Scheduler
* GetScheduler() OVERRIDE
;
109 // Fills build arguments. Returns true on success.
110 bool FillArguments(const base::CommandLine
& cmdline
);
112 // Fills the root directory into the settings. Returns true on success.
113 bool FillSourceDir(const base::CommandLine
& cmdline
);
115 // Fills the build directory given the value the user has specified.
116 // Must happen after FillSourceDir so we can resolve source-relative
118 bool FillBuildDir(const std::string
& build_dir
);
120 // Fills the python path portion of the command line. On failure, sets
121 // it to just "python".
122 void FillPythonPath();
125 bool RunConfigFile();
127 bool FillOtherConfig(const base::CommandLine
& cmdline
);
129 Scheduler scheduler_
;
131 // These empty settings and toolchain are used to interpret the command line
133 BuildSettings empty_build_settings_
;
134 Settings empty_settings_
;
135 Scope dotfile_scope_
;
137 // State for invoking the dotfile.
138 base::FilePath dotfile_name_
;
139 scoped_ptr
<InputFile
> dotfile_input_file_
;
140 std::vector
<Token
> dotfile_tokens_
;
141 scoped_ptr
<ParseNode
> dotfile_root_
;
143 // State for invoking the command line args. We specifically want to keep
144 // this around for the entire run so that Values can blame to the command
145 // line when we issue errors about them.
146 scoped_ptr
<InputFile
> args_input_file_
;
147 std::vector
<Token
> args_tokens_
;
148 scoped_ptr
<ParseNode
> args_root_
;
150 DISALLOW_COPY_AND_ASSIGN(Setup
);
153 // A dependent setup allows one to do more than one build at a time. You would
154 // make a dependent setup which clones the state of the main one, make any
155 // necessary changes, and then run it.
157 // The way to run both at the same time is:
158 // dependent_setup.RunPreMessageLoop();
160 // dependent_setup.RunPostMessageLoop();
161 // so that the main setup executes the message loop, but both are run.
162 class DependentSetup
: public CommonSetup
{
164 // Note: this could be one function that takes a CommonSetup*, but then
165 // the compiler can get confused what to call, since it also matches the
166 // default copy constructor.
167 DependentSetup(Setup
* derive_from
);
168 DependentSetup(DependentSetup
* derive_from
);
169 virtual ~DependentSetup();
171 // These are the two parts of Run() in the regular setup, not including the
172 // call to actually run the message loop.
173 void RunPreMessageLoop();
174 bool RunPostMessageLoop();
176 virtual Scheduler
* GetScheduler() OVERRIDE
;
179 Scheduler
* scheduler_
;
182 #endif // TOOLS_GN_SETUP_H_