• 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 "base/macros.h"
13 #include "gn/build_settings.h"
14 #include "gn/builder.h"
15 #include "gn/label_pattern.h"
16 #include "gn/loader.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   // Runs the load, returning true on success. On failure, prints the error
61   // and returns false. This includes both RunPreMessageLoop() and
62   // RunPostMessageLoop().
63   //
64   // cmdline is the gn invocation command, with flags like --root and --dotfile.
65   // If no explicit cmdline is passed, base::CommandLine::ForCurrentProcess()
66   // is used.
67   bool Run();
68   bool Run(const base::CommandLine& cmdline);
69 
scheduler()70   Scheduler& scheduler() { return scheduler_; }
71 
72   // Returns the file used to store the build arguments. Note that the path
73   // might not exist.
74   SourceFile GetBuildArgFile() const;
75 
76   // Sets whether the build arguments should be filled during setup from the
77   // command line/build argument file. This will be true by default. The use
78   // case for setting it to false is when editing build arguments, we don't
79   // want to rely on them being valid.
set_fill_arguments(bool fa)80   void set_fill_arguments(bool fa) { fill_arguments_ = fa; }
81 
82   // After a successful run, setting this will additionally cause the public
83   // headers to be checked. Defaults to false.
set_check_public_headers(bool s)84   void set_check_public_headers(bool s) { check_public_headers_ = s; }
85 
86   // After a successful run, setting this will additionally cause system style
87   // includes to be checked. Defaults to false.
set_check_system_includes(bool s)88   void set_check_system_includes(bool s) { check_system_includes_ = s; }
89 
check_system_includes()90   bool check_system_includes() const { return check_system_includes_; }
91 
92   // Before DoSetup, setting this will generate an empty args.gn if
93   // it does not exist and set up correct dependencies for it.
set_gen_empty_args(bool ge)94   void set_gen_empty_args(bool ge) { gen_empty_args_ = ge; }
95 
96   // Read from the .gn file, these are the targets to check. If the .gn file
97   // does not specify anything, this will be null. If the .gn file specifies
98   // the empty list, this will be non-null but empty.
check_patterns()99   const std::vector<LabelPattern>* check_patterns() const {
100     return check_patterns_.get();
101   }
102 
build_settings()103   BuildSettings& build_settings() { return build_settings_; }
builder()104   Builder& builder() { return builder_; }
loader()105   LoaderImpl* loader() { return loader_.get(); }
106 
GetDotFile()107   const SourceFile& GetDotFile() const { return dotfile_input_file_->name(); }
108 
109   // Name of the file in the root build directory that contains the build
110   // arguments.
111   static const char kBuildArgFileName[];
112 
113  private:
114   // Performs the two sets of operations to run the generation before and after
115   // the message loop is run.
116   void RunPreMessageLoop();
117   bool RunPostMessageLoop(const base::CommandLine& cmdline);
118 
119   // Fills build arguments. Returns true on success.
120   bool FillArguments(const base::CommandLine& cmdline);
121 
122   // Fills the build arguments from the command line or from the build arg file.
123   bool FillArgsFromCommandLine(const std::string& args);
124   bool FillArgsFromFile();
125 
126   // Given an already-loaded args_input_file_, parses and saves the resulting
127   // arguments. Backend for the different FillArgs variants.
128   bool FillArgsFromArgsInputFile();
129 
130   // Writes the build arguments to the build arg file.
131   bool SaveArgsToFile();
132 
133   // Fills the root directory into the settings. Returns true on success.
134   bool FillSourceDir(const base::CommandLine& cmdline);
135 
136   // Fills the build directory given the value the user has specified.
137   // Must happen after FillSourceDir so we can resolve source-relative
138   // paths. If require_exists is false, it will fail if the dir doesn't exist.
139   bool FillBuildDir(const std::string& build_dir, bool require_exists);
140 
141   // Fills the python path portion of the command line. On failure, sets
142   // it to just "python".
143   bool FillPythonPath(const base::CommandLine& cmdline);
144 
145   // Run config file.
146   bool RunConfigFile();
147 
148   bool FillOtherConfig(const base::CommandLine& cmdline);
149 
150   BuildSettings build_settings_;
151   scoped_refptr<LoaderImpl> loader_;
152   Builder builder_;
153 
154   SourceFile root_build_file_;
155 
156   bool check_public_headers_ = false;
157   bool check_system_includes_ = false;
158 
159   // See getter for info.
160   std::unique_ptr<std::vector<LabelPattern>> check_patterns_;
161 
162   Scheduler scheduler_;
163 
164   // These settings and toolchain are used to interpret the command line and
165   // dot file.
166   Settings dotfile_settings_;
167   Scope dotfile_scope_;
168 
169   // State for invoking the dotfile.
170   base::FilePath dotfile_name_;
171   std::unique_ptr<InputFile> dotfile_input_file_;
172   std::vector<Token> dotfile_tokens_;
173   std::unique_ptr<ParseNode> dotfile_root_;
174 
175   // Default overrides, specified in the dotfile.
176   // Owned by the Value (if it exists) in the dotfile_scope_.
177   const Scope* default_args_ = nullptr;
178 
179   // Set to true when we should populate the build arguments from the command
180   // line or build argument file. See setter above.
181   bool fill_arguments_ = true;
182 
183   // Generate an empty args.gn file if it does not exists.
184   bool gen_empty_args_ = false;
185 
186   // State for invoking the command line args. We specifically want to keep
187   // this around for the entire run so that Values can blame to the command
188   // line when we issue errors about them.
189   std::unique_ptr<InputFile> args_input_file_;
190   std::vector<Token> args_tokens_;
191   std::unique_ptr<ParseNode> args_root_;
192 
193   DISALLOW_COPY_AND_ASSIGN(Setup);
194 };
195 
196 #endif  // TOOLS_GN_SETUP_H_
197