• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_GN_SETUP_H_
6 #define TOOLS_GN_SETUP_H_
7 
8 #include <vector>
9 
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"
22 
23 class InputFile;
24 class ParseNode;
25 
26 namespace base {
27 class CommandLine;
28 }
29 
30 extern const char kDotfile_Help[];
31 
32 // Base class for code shared between Setup and DependentSetup.
33 class CommonSetup {
34  public:
35   virtual ~CommonSetup();
36 
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;
40 
41   // When true (the default), Run() will check for unresolved dependencies and
42   // cycles upon completion. When false, such errors will be ignored.
set_check_for_bad_items(bool s)43   void set_check_for_bad_items(bool s) { check_for_bad_items_ = s; }
44 
45   // When true (the default), RunPostMessageLoop will check for overrides that
46   // were specified but not used. When false, such errors will be ignored.
set_check_for_unused_overrides(bool s)47   void set_check_for_unused_overrides(bool s) {
48     check_for_unused_overrides_ = s;
49   }
50 
51   // After a successful run, setting this will additionally cause the public
52   // headers to be checked. Defaults to false.
set_check_public_headers(bool s)53   void set_check_public_headers(bool s) {
54     check_public_headers_ = s;
55   }
56 
build_settings()57   BuildSettings& build_settings() { return build_settings_; }
builder()58   Builder* builder() { return builder_.get(); }
loader()59   LoaderImpl* loader() { return loader_.get(); }
60 
61   // Name of the file in the root build directory that contains the build
62   // arguements.
63   static const char kBuildArgFileName[];
64 
65  protected:
66   CommonSetup();
67   CommonSetup(const CommonSetup& other);
68 
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();
73 
74   BuildSettings build_settings_;
75   scoped_refptr<LoaderImpl> loader_;
76   scoped_refptr<Builder> builder_;
77 
78   SourceFile root_build_file_;
79 
80   bool check_for_bad_items_;
81   bool check_for_unused_overrides_;
82   bool check_public_headers_;
83 
84  private:
85   CommonSetup& operator=(const CommonSetup& other);  // Disallow.
86 };
87 
88 // Helper class to setup the build settings and environment for the various
89 // commands to run.
90 class Setup : public CommonSetup {
91  public:
92   Setup();
93   virtual ~Setup();
94 
95   // Configures the build for the current command line. On success returns
96   // true. On failure, prints the error and returns false.
97   //
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
100   // is malformed.
101   bool DoSetup(const std::string& build_dir);
102 
103   // Runs the load, returning true on success. On failure, prints the error
104   // and returns false. This includes both RunPreMessageLoop() and
105   // RunPostMessageLoop().
106   bool Run();
107 
scheduler()108   Scheduler& scheduler() { return scheduler_; }
109 
110   virtual Scheduler* GetScheduler() OVERRIDE;
111 
112   // Returns the file used to store the build arguments. Note that the path
113   // might not exist.
114   SourceFile GetBuildArgFile() const;
115 
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.
set_fill_arguments(bool fa)120   void set_fill_arguments(bool fa) { fill_arguments_ = fa; }
121 
122  private:
123   // Fills build arguments. Returns true on success.
124   bool FillArguments(const base::CommandLine& cmdline);
125 
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();
129 
130   // Given an already-loaded args_input_file_, parses and saves the resulting
131   // arguments. Backend for the different FillArgs variants.
132   bool FillArgsFromArgsInputFile();
133 
134   // Writes the build arguments to the build arg file.
135   bool SaveArgsToFile();
136 
137   // Fills the root directory into the settings. Returns true on success.
138   bool FillSourceDir(const base::CommandLine& cmdline);
139 
140   // Fills the build directory given the value the user has specified.
141   // Must happen after FillSourceDir so we can resolve source-relative
142   // paths.
143   bool FillBuildDir(const std::string& build_dir);
144 
145   // Fills the python path portion of the command line. On failure, sets
146   // it to just "python".
147   void FillPythonPath();
148 
149   // Run config file.
150   bool RunConfigFile();
151 
152   bool FillOtherConfig(const base::CommandLine& cmdline);
153 
154   Scheduler scheduler_;
155 
156   // These empty settings and toolchain are used to interpret the command line
157   // and dot file.
158   BuildSettings empty_build_settings_;
159   Settings empty_settings_;
160   Scope dotfile_scope_;
161 
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_;
167 
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_;
171 
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_;
178 
179   DISALLOW_COPY_AND_ASSIGN(Setup);
180 };
181 
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.
185 //
186 // The way to run both at the same time is:
187 //   dependent_setup.RunPreMessageLoop();
188 //   main_setup.Run();
189 //   dependent_setup.RunPostMessageLoop();
190 // so that the main setup executes the message loop, but both are run.
191 class DependentSetup : public CommonSetup {
192  public:
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();
199 
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();
204 
205   virtual Scheduler* GetScheduler() OVERRIDE;
206 
207  private:
208   Scheduler* scheduler_;
209 };
210 
211 #endif  // TOOLS_GN_SETUP_H_
212