• 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 <memory>
9 #include <vector>
10 
11 #include "base/files/file_path.h"
12 #include "gn/build_settings.h"
13 #include "gn/builder.h"
14 #include "gn/label_pattern.h"
15 #include "gn/loader.h"
16 #include "gn/scheduler.h"
17 #include "gn/scope.h"
18 #include "gn/settings.h"
19 #include "gn/token.h"
20 #include "gn/toolchain.h"
21 
22 class InputFile;
23 class ParseNode;
24 
25 namespace base {
26 class CommandLine;
27 }
28 
29 extern const char kDotfile_Help[];
30 
31 // Helper class to set up the build settings and environment for the various
32 // commands to run.
33 class Setup {
34  public:
35   Setup();
36 
37   // Configures the build for the current command line. On success returns
38   // true. On failure, prints the error and returns false.
39   //
40   // The parameter is the string the user specified for the build directory. We
41   // will try to interpret this as a SourceDir if possible, and will fail if is
42   // is malformed.
43   //
44   // With force_create = false, setup will fail if the build directory doesn't
45   // already exist with an args file in it. With force_create set to true, the
46   // directory will be created if necessary. Commands explicitly doing
47   // generation should set this to true to create it, but querying commands
48   // should set it to false to prevent creating oddly-named directories in case
49   // the user omits the build directory argument (which is easy to do).
50   //
51   // cmdline is the gn invocation command, with flags like --root and --dotfile.
52   // If no explicit cmdline is passed, base::CommandLine::ForCurrentProcess()
53   // is used.
54   bool DoSetup(const std::string& build_dir, bool force_create);
55   bool DoSetup(const std::string& build_dir,
56                bool force_create,
57                const base::CommandLine& cmdline);
58 
59   // Same as DoSetup() but used for tests to capture error output.
60   bool DoSetupWithErr(const std::string& build_dir,
61                       bool force_create,
62                       const base::CommandLine& cmdline,
63                       Err* err);
64 
65   // Runs the load, returning true on success. On failure, prints the error
66   // and returns false. This includes both RunPreMessageLoop() and
67   // RunPostMessageLoop().
68   //
69   // cmdline is the gn invocation command, with flags like --root and --dotfile.
70   // If no explicit cmdline is passed, base::CommandLine::ForCurrentProcess()
71   // is used.
72   bool Run();
73   bool Run(const base::CommandLine& cmdline);
74 
scheduler()75   Scheduler& scheduler() { return scheduler_; }
76 
77   // Returns the file used to store the build arguments. Note that the path
78   // might not exist.
79   SourceFile GetBuildArgFile() const;
80 
81   // Sets whether the build arguments should be filled during setup from the
82   // command line/build argument file. This will be true by default. The use
83   // case for setting it to false is when editing build arguments, we don't
84   // want to rely on them being valid.
set_fill_arguments(bool fa)85   void set_fill_arguments(bool fa) { fill_arguments_ = fa; }
86 
87   // After a successful run, setting this will additionally cause the public
88   // headers to be checked. Defaults to false.
set_check_public_headers(bool s)89   void set_check_public_headers(bool s) { check_public_headers_ = s; }
90 
91   // After a successful run, setting this will additionally cause system style
92   // includes to be checked. Defaults to false.
set_check_system_includes(bool s)93   void set_check_system_includes(bool s) { check_system_includes_ = s; }
94 
check_system_includes()95   bool check_system_includes() const { return check_system_includes_; }
96 
97   // Before DoSetup, setting this will generate an empty args.gn if
98   // it does not exist and set up correct dependencies for it.
set_gen_empty_args(bool ge)99   void set_gen_empty_args(bool ge) { gen_empty_args_ = ge; }
100 
101   // Read from the .gn file, these are the targets to check. If the .gn file
102   // does not specify anything, this will be null. If the .gn file specifies
103   // the empty list, this will be non-null but empty.
check_patterns()104   const std::vector<LabelPattern>* check_patterns() const {
105     return check_patterns_.get();
106   }
107 
108   // Read from the .gn file, these are the targets *not* to check. If the .gn
109   // file does not specify anything, this will be null. If the .gn file
110   // specifies the empty list, this will be non-null but empty. At least one of
111   // check_patterns() and no_check_patterns() will be null.
no_check_patterns()112   const std::vector<LabelPattern>* no_check_patterns() const {
113     return no_check_patterns_.get();
114   }
115 
build_settings()116   BuildSettings& build_settings() { return build_settings_; }
builder()117   Builder& builder() { return builder_; }
loader()118   LoaderImpl* loader() { return loader_.get(); }
119 
GetDotFile()120   const SourceFile& GetDotFile() const { return dotfile_input_file_->name(); }
121 
122   // Name of the file in the root build directory that contains the build
123   // arguments.
124   static const char kBuildArgFileName[];
125 
126  private:
127   // Performs the two sets of operations to run the generation before and after
128   // the message loop is run.
129   void RunPreMessageLoop();
130   bool RunPostMessageLoop(const base::CommandLine& cmdline);
131 
132   // Fills build arguments. Returns true on success.
133   bool FillArguments(const base::CommandLine& cmdline, Err* err);
134 
135   // Fills the build arguments from the command line or from the build arg file.
136   bool FillArgsFromCommandLine(const std::string& args, Err* err);
137   bool FillArgsFromFile(Err* err);
138 
139   // Given an already-loaded args_input_file_, parses and saves the resulting
140   // arguments. Backend for the different FillArgs variants.
141   bool FillArgsFromArgsInputFile(Err* err);
142 
143   // Writes the build arguments to the build arg file.
144   bool SaveArgsToFile();
145 
146   // Fills the root directory into the settings. Returns true on success, or
147   // |err| filled out.
148   bool FillSourceDir(const base::CommandLine& cmdline, Err* err);
149 
150   // Fills the build directory given the value the user has specified.
151   // Must happen after FillSourceDir so we can resolve source-relative
152   // paths. If require_exists is false, it will fail if the dir doesn't exist.
153   bool FillBuildDir(const std::string& build_dir,
154                     bool require_exists,
155                     Err* err);
156 
157   // Fills the python path portion of the command line. On failure, sets
158   // it to just "python".
159   bool FillPythonPath(const base::CommandLine& cmdline, Err* err);
160 
161   // Run config file.
162   bool RunConfigFile(Err* err);
163 
164   bool FillOtherConfig(const base::CommandLine& cmdline, Err* err);
165 
166   BuildSettings build_settings_;
167   scoped_refptr<LoaderImpl> loader_;
168   Builder builder_;
169 
170   SourceFile root_build_file_;
171 
172   bool check_public_headers_ = false;
173   bool check_system_includes_ = false;
174 
175   // See getter for info.
176   std::unique_ptr<std::vector<LabelPattern>> check_patterns_;
177   std::unique_ptr<std::vector<LabelPattern>> no_check_patterns_;
178 
179   Scheduler scheduler_;
180 
181   // These settings and toolchain are used to interpret the command line and
182   // dot file.
183   Settings dotfile_settings_;
184   Scope dotfile_scope_;
185 
186   // State for invoking the dotfile.
187   base::FilePath dotfile_name_;
188   std::unique_ptr<InputFile> dotfile_input_file_;
189   std::vector<Token> dotfile_tokens_;
190   std::unique_ptr<ParseNode> dotfile_root_;
191 
192   // Default overrides, specified in the dotfile.
193   // Owned by the Value (if it exists) in the dotfile_scope_.
194   const Scope* default_args_ = nullptr;
195 
196   // Set to true when we should populate the build arguments from the command
197   // line or build argument file. See setter above.
198   bool fill_arguments_ = true;
199 
200   // Generate an empty args.gn file if it does not exists.
201   bool gen_empty_args_ = false;
202 
203   // State for invoking the command line args. We specifically want to keep
204   // this around for the entire run so that Values can blame to the command
205   // line when we issue errors about them.
206   std::unique_ptr<InputFile> args_input_file_;
207   std::vector<Token> args_tokens_;
208   std::unique_ptr<ParseNode> args_root_;
209 
210   Setup(const Setup&) = delete;
211   Setup& operator=(const Setup&) = delete;
212 };
213 
214 #endif  // TOOLS_GN_SETUP_H_
215