blob: 68a8f0b35e53501e004097d822899136ae4c366a [file] [log] [blame]
[email protected]96ea63d2013-07-30 10:17:071// 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
dchenga500b692016-04-08 19:55:428#include <memory>
[email protected]96ea63d2013-07-30 10:17:079#include <vector>
10
[email protected]96ea63d2013-07-30 10:17:0711#include "base/files/file_path.h"
tfarinaf51a763e2015-08-10 00:55:3812#include "base/macros.h"
[email protected]96ea63d2013-07-30 10:17:0713#include "tools/gn/build_settings.h"
[email protected]26542b02013-11-08 23:25:0414#include "tools/gn/builder.h"
brettw2bafab42014-11-27 18:36:1715#include "tools/gn/label_pattern.h"
[email protected]26542b02013-11-08 23:25:0416#include "tools/gn/loader.h"
[email protected]96ea63d2013-07-30 10:17:0717#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
[email protected]96ea63d2013-07-30 10:17:0723class InputFile;
24class ParseNode;
25
[email protected]2f3b1cc2014-03-17 23:07:1526namespace base {
27class CommandLine;
28}
29
[email protected]ceb18412013-07-31 19:17:5830extern const char kDotfile_Help[];
31
brettw4d305862015-03-06 22:38:2032// Helper class to setup the build settings and environment for the various
33// commands to run.
34class Setup {
[email protected]96ea63d2013-07-30 10:17:0735 public:
brettw4d305862015-03-06 22:38:2036 Setup();
37 ~Setup();
[email protected]96ea63d2013-07-30 10:17:0738
brettw4d305862015-03-06 22:38:2039 // Configures the build for the current command line. On success returns
40 // true. On failure, prints the error and returns false.
41 //
42 // The parameter is the string the user specified for the build directory. We
43 // will try to interpret this as a SourceDir if possible, and will fail if is
44 // is malformed.
45 //
46 // With force_create = false, setup will fail if the build directory doesn't
47 // alreay exist with an args file in it. With force_create set to true, the
48 // directory will be created if necessary. Commands explicitly doing
49 // generation should set this to true to create it, but querying commands
50 // should set it to false to prevent creating oddly-named directories in case
51 // the user omits the build directory argument (which is easy to do).
52 bool DoSetup(const std::string& build_dir, bool force_create);
53
54 // Runs the load, returning true on success. On failure, prints the error
55 // and returns false. This includes both RunPreMessageLoop() and
56 // RunPostMessageLoop().
57 bool Run();
58
59 Scheduler& scheduler() { return scheduler_; }
60
61 // Returns the file used to store the build arguments. Note that the path
62 // might not exist.
63 SourceFile GetBuildArgFile() const;
64
65 // Sets whether the build arguments should be filled during setup from the
66 // command line/build argument file. This will be true by default. The use
67 // case for setting it to false is when editing build arguments, we don't
68 // want to rely on them being valid.
69 void set_fill_arguments(bool fa) { fill_arguments_ = fa; }
[email protected]a17a7832013-12-13 17:25:1070
[email protected]126d8b52014-04-07 22:17:3571 // After a successful run, setting this will additionally cause the public
72 // headers to be checked. Defaults to false.
73 void set_check_public_headers(bool s) {
74 check_public_headers_ = s;
75 }
76
brettw2bafab42014-11-27 18:36:1777 // Read from the .gn file, these are the targets to check. If the .gn file
78 // does not specify anything, this will be null. If the .gn file specifies
79 // the empty list, this will be non-null but empty.
80 const std::vector<LabelPattern>* check_patterns() const {
81 return check_patterns_.get();
82 }
83
[email protected]e3730f812013-10-16 16:46:1484 BuildSettings& build_settings() { return build_settings_; }
brettw8293c352016-07-26 20:38:3785 Builder& builder() { return builder_; }
[email protected]26542b02013-11-08 23:25:0486 LoaderImpl* loader() { return loader_.get(); }
[email protected]e3730f812013-10-16 16:46:1487
[email protected]d5645f12014-05-03 04:32:1988 // Name of the file in the root build directory that contains the build
89 // arguements.
90 static const char kBuildArgFileName[];
91
brettw4d305862015-03-06 22:38:2092 private:
[email protected]e3730f812013-10-16 16:46:1493 // Performs the two sets of operations to run the generation before and after
94 // the message loop is run.
95 void RunPreMessageLoop();
96 bool RunPostMessageLoop();
97
[email protected]60749e1c2013-08-19 21:11:0598 // Fills build arguments. Returns true on success.
[email protected]2f3b1cc2014-03-17 23:07:1599 bool FillArguments(const base::CommandLine& cmdline);
[email protected]60749e1c2013-08-19 21:11:05100
[email protected]d5645f12014-05-03 04:32:19101 // Fills the build arguments from the command line or from the build arg file.
102 bool FillArgsFromCommandLine(const std::string& args);
103 bool FillArgsFromFile();
104
105 // Given an already-loaded args_input_file_, parses and saves the resulting
106 // arguments. Backend for the different FillArgs variants.
107 bool FillArgsFromArgsInputFile();
108
109 // Writes the build arguments to the build arg file.
110 bool SaveArgsToFile();
111
[email protected]96ea63d2013-07-30 10:17:07112 // Fills the root directory into the settings. Returns true on success.
[email protected]2f3b1cc2014-03-17 23:07:15113 bool FillSourceDir(const base::CommandLine& cmdline);
[email protected]96ea63d2013-07-30 10:17:07114
[email protected]a623db1872014-02-19 19:11:17115 // Fills the build directory given the value the user has specified.
116 // Must happen after FillSourceDir so we can resolve source-relative
brettw4dd3ef02014-09-10 01:45:09117 // paths. If require_exists is false, it will fail if the dir doesn't exist.
118 bool FillBuildDir(const std::string& build_dir, bool require_exists);
[email protected]a623db1872014-02-19 19:11:17119
[email protected]c9f05402013-09-23 23:12:58120 // Fills the python path portion of the command line. On failure, sets
121 // it to just "python".
phoseka104997e2017-06-21 03:36:01122 bool FillPythonPath(const base::CommandLine& cmdline);
[email protected]c9f05402013-09-23 23:12:58123
[email protected]96ea63d2013-07-30 10:17:07124 // Run config file.
125 bool RunConfigFile();
126
[email protected]2f3b1cc2014-03-17 23:07:15127 bool FillOtherConfig(const base::CommandLine& cmdline);
[email protected]96ea63d2013-07-30 10:17:07128
brettw4d305862015-03-06 22:38:20129 BuildSettings build_settings_;
130 scoped_refptr<LoaderImpl> loader_;
brettw8293c352016-07-26 20:38:37131 Builder builder_;
brettw4d305862015-03-06 22:38:20132
133 SourceFile root_build_file_;
134
brettw4d305862015-03-06 22:38:20135 bool check_public_headers_;
136
137 // See getter for info.
dchenga500b692016-04-08 19:55:42138 std::unique_ptr<std::vector<LabelPattern>> check_patterns_;
brettw4d305862015-03-06 22:38:20139
[email protected]96ea63d2013-07-30 10:17:07140 Scheduler scheduler_;
141
brettwc2f498dd2015-04-27 21:31:03142 // These settings and toolchain are used to interpret the command line and
143 // dot file.
144 Settings dotfile_settings_;
[email protected]60749e1c2013-08-19 21:11:05145 Scope dotfile_scope_;
146
[email protected]96ea63d2013-07-30 10:17:07147 // State for invoking the dotfile.
[email protected]96ea63d2013-07-30 10:17:07148 base::FilePath dotfile_name_;
dchenga500b692016-04-08 19:55:42149 std::unique_ptr<InputFile> dotfile_input_file_;
[email protected]96ea63d2013-07-30 10:17:07150 std::vector<Token> dotfile_tokens_;
dchenga500b692016-04-08 19:55:42151 std::unique_ptr<ParseNode> dotfile_root_;
[email protected]60749e1c2013-08-19 21:11:05152
timadbf1562017-01-21 03:06:37153 // Default overrides, specified in the dotfile.
154 // Owned by the Value (if it exists) in the dotfile_scope_.
155 const Scope* default_args_;
156
[email protected]d5645f12014-05-03 04:32:19157 // Set to true when we should populate the build arguments from the command
158 // line or build argument file. See setter above.
159 bool fill_arguments_;
160
[email protected]60749e1c2013-08-19 21:11:05161 // State for invoking the command line args. We specifically want to keep
162 // this around for the entire run so that Values can blame to the command
163 // line when we issue errors about them.
dchenga500b692016-04-08 19:55:42164 std::unique_ptr<InputFile> args_input_file_;
[email protected]60749e1c2013-08-19 21:11:05165 std::vector<Token> args_tokens_;
dchenga500b692016-04-08 19:55:42166 std::unique_ptr<ParseNode> args_root_;
[email protected]96ea63d2013-07-30 10:17:07167
168 DISALLOW_COPY_AND_ASSIGN(Setup);
169};
170
[email protected]96ea63d2013-07-30 10:17:07171#endif // TOOLS_GN_SETUP_H_