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