[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 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 | |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
tfarina | f51a763e | 2015-08-10 00:55:38 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 13 | #include "tools/gn/build_settings.h" |
[email protected] | 26542b0 | 2013-11-08 23:25:04 | [diff] [blame] | 14 | #include "tools/gn/builder.h" |
brettw | 2bafab4 | 2014-11-27 18:36:17 | [diff] [blame] | 15 | #include "tools/gn/label_pattern.h" |
[email protected] | 26542b0 | 2013-11-08 23:25:04 | [diff] [blame] | 16 | #include "tools/gn/loader.h" |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 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 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 23 | class InputFile; |
| 24 | class ParseNode; |
| 25 | |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 26 | namespace base { |
| 27 | class CommandLine; |
| 28 | } |
| 29 | |
[email protected] | ceb1841 | 2013-07-31 19:17:58 | [diff] [blame] | 30 | extern const char kDotfile_Help[]; |
| 31 | |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 32 | // Helper class to setup the build settings and environment for the various |
| 33 | // commands to run. |
| 34 | class Setup { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 35 | public: |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 36 | Setup(); |
| 37 | ~Setup(); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 38 | |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 39 | // 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] | a17a783 | 2013-12-13 17:25:10 | [diff] [blame] | 70 | |
[email protected] | 126d8b5 | 2014-04-07 22:17:35 | [diff] [blame] | 71 | // 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 | |
brettw | 2bafab4 | 2014-11-27 18:36:17 | [diff] [blame] | 77 | // 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] | e3730f81 | 2013-10-16 16:46:14 | [diff] [blame] | 84 | BuildSettings& build_settings() { return build_settings_; } |
brettw | 8293c35 | 2016-07-26 20:38:37 | [diff] [blame] | 85 | Builder& builder() { return builder_; } |
[email protected] | 26542b0 | 2013-11-08 23:25:04 | [diff] [blame] | 86 | LoaderImpl* loader() { return loader_.get(); } |
[email protected] | e3730f81 | 2013-10-16 16:46:14 | [diff] [blame] | 87 | |
[email protected] | d5645f1 | 2014-05-03 04:32:19 | [diff] [blame] | 88 | // Name of the file in the root build directory that contains the build |
| 89 | // arguements. |
| 90 | static const char kBuildArgFileName[]; |
| 91 | |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 92 | private: |
[email protected] | e3730f81 | 2013-10-16 16:46:14 | [diff] [blame] | 93 | // 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] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 98 | // Fills build arguments. Returns true on success. |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 99 | bool FillArguments(const base::CommandLine& cmdline); |
[email protected] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 100 | |
[email protected] | d5645f1 | 2014-05-03 04:32:19 | [diff] [blame] | 101 | // 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] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 112 | // Fills the root directory into the settings. Returns true on success. |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 113 | bool FillSourceDir(const base::CommandLine& cmdline); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 114 | |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 115 | // Fills the build directory given the value the user has specified. |
| 116 | // Must happen after FillSourceDir so we can resolve source-relative |
brettw | 4dd3ef0 | 2014-09-10 01:45:09 | [diff] [blame] | 117 | // 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] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 119 | |
[email protected] | c9f0540 | 2013-09-23 23:12:58 | [diff] [blame] | 120 | // Fills the python path portion of the command line. On failure, sets |
| 121 | // it to just "python". |
phosek | a104997e | 2017-06-21 03:36:01 | [diff] [blame] | 122 | bool FillPythonPath(const base::CommandLine& cmdline); |
[email protected] | c9f0540 | 2013-09-23 23:12:58 | [diff] [blame] | 123 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 124 | // Run config file. |
| 125 | bool RunConfigFile(); |
| 126 | |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 127 | bool FillOtherConfig(const base::CommandLine& cmdline); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 128 | |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 129 | BuildSettings build_settings_; |
| 130 | scoped_refptr<LoaderImpl> loader_; |
brettw | 8293c35 | 2016-07-26 20:38:37 | [diff] [blame] | 131 | Builder builder_; |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 132 | |
| 133 | SourceFile root_build_file_; |
| 134 | |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 135 | bool check_public_headers_; |
| 136 | |
| 137 | // See getter for info. |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 138 | std::unique_ptr<std::vector<LabelPattern>> check_patterns_; |
brettw | 4d30586 | 2015-03-06 22:38:20 | [diff] [blame] | 139 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 140 | Scheduler scheduler_; |
| 141 | |
brettw | c2f498dd | 2015-04-27 21:31:03 | [diff] [blame] | 142 | // These settings and toolchain are used to interpret the command line and |
| 143 | // dot file. |
| 144 | Settings dotfile_settings_; |
[email protected] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 145 | Scope dotfile_scope_; |
| 146 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 147 | // State for invoking the dotfile. |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 148 | base::FilePath dotfile_name_; |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 149 | std::unique_ptr<InputFile> dotfile_input_file_; |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 150 | std::vector<Token> dotfile_tokens_; |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 151 | std::unique_ptr<ParseNode> dotfile_root_; |
[email protected] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 152 | |
tim | adbf156 | 2017-01-21 03:06:37 | [diff] [blame] | 153 | // 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] | d5645f1 | 2014-05-03 04:32:19 | [diff] [blame] | 157 | // 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] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 161 | // 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. |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 164 | std::unique_ptr<InputFile> args_input_file_; |
[email protected] | 60749e1c | 2013-08-19 21:11:05 | [diff] [blame] | 165 | std::vector<Token> args_tokens_; |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 166 | std::unique_ptr<ParseNode> args_root_; |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 167 | |
| 168 | DISALLOW_COPY_AND_ASSIGN(Setup); |
| 169 | }; |
| 170 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 171 | #endif // TOOLS_GN_SETUP_H_ |