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 117 // This is a combination of the export_compile_commands list in the dotfile, 118 // and any additions specified on the command-line. export_compile_commands()119 const std::vector<LabelPattern>& export_compile_commands() const { 120 return export_compile_commands_; 121 } 122 build_settings()123 BuildSettings& build_settings() { return build_settings_; } builder()124 Builder& builder() { return builder_; } loader()125 LoaderImpl* loader() { return loader_.get(); } 126 GetDotFile()127 const SourceFile& GetDotFile() const { return dotfile_input_file_->name(); } 128 129 // Name of the file in the root build directory that contains the build 130 // arguments. 131 static const char kBuildArgFileName[]; 132 133 private: 134 // Performs the two sets of operations to run the generation before and after 135 // the message loop is run. 136 void RunPreMessageLoop(); 137 bool RunPostMessageLoop(const base::CommandLine& cmdline); 138 139 // Fills build arguments. Returns true on success. 140 bool FillArguments(const base::CommandLine& cmdline, Err* err); 141 142 bool FillOhosComponentsInfo(const std::string& build_dir, Err* err); 143 144 // Fills the build arguments from the command line or from the build arg file. 145 bool FillArgsFromCommandLine(const std::string& args, Err* err); 146 bool FillArgsFromFile(Err* err); 147 148 // Given an already-loaded args_input_file_, parses and saves the resulting 149 // arguments. Backend for the different FillArgs variants. 150 bool FillArgsFromArgsInputFile(Err* err); 151 152 // Writes the build arguments to the build arg file. 153 bool SaveArgsToFile(); 154 155 // Fills the root directory into the settings. Returns true on success, or 156 // |err| filled out. 157 bool FillSourceDir(const base::CommandLine& cmdline, Err* err); 158 159 // Fills the build directory given the value the user has specified. 160 // Must happen after FillSourceDir so we can resolve source-relative 161 // paths. If require_exists is false, it will fail if the dir doesn't exist. 162 bool FillBuildDir(const std::string& build_dir, 163 bool require_exists, 164 Err* err); 165 166 // Fills the python path portion of the command line. On failure, sets 167 // it to just "python". 168 bool FillPythonPath(const base::CommandLine& cmdline, Err* err); 169 170 // Run config file. 171 bool RunConfigFile(Err* err); 172 173 bool FillOtherConfig(const base::CommandLine& cmdline, Err* err); 174 175 BuildSettings build_settings_; 176 OhosComponents ohos_components_; 177 178 scoped_refptr<LoaderImpl> loader_; 179 Builder builder_; 180 181 SourceFile root_build_file_; 182 183 bool check_public_headers_ = false; 184 bool check_system_includes_ = false; 185 186 // See getter for info. 187 std::unique_ptr<std::vector<LabelPattern>> check_patterns_; 188 std::unique_ptr<std::vector<LabelPattern>> no_check_patterns_; 189 190 Scheduler scheduler_; 191 192 // These settings and toolchain are used to interpret the command line and 193 // dot file. 194 Settings dotfile_settings_; 195 Scope dotfile_scope_; 196 197 // State for invoking the dotfile. 198 base::FilePath dotfile_name_; 199 std::unique_ptr<InputFile> dotfile_input_file_; 200 std::vector<Token> dotfile_tokens_; 201 std::unique_ptr<ParseNode> dotfile_root_; 202 203 // Default overrides, specified in the dotfile. 204 // Owned by the Value (if it exists) in the dotfile_scope_. 205 const Scope* default_args_ = nullptr; 206 207 // Set to true when we should populate the build arguments from the command 208 // line or build argument file. See setter above. 209 bool fill_arguments_ = true; 210 211 // Generate an empty args.gn file if it does not exists. 212 bool gen_empty_args_ = false; 213 214 // State for invoking the command line args. We specifically want to keep 215 // this around for the entire run so that Values can blame to the command 216 // line when we issue errors about them. 217 std::unique_ptr<InputFile> args_input_file_; 218 std::vector<Token> args_tokens_; 219 std::unique_ptr<ParseNode> args_root_; 220 221 std::vector<LabelPattern> export_compile_commands_; 222 223 Setup(const Setup&) = delete; 224 Setup& operator=(const Setup&) = delete; 225 }; 226 227 #endif // TOOLS_GN_SETUP_H_ 228