| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 base::StringPiece line(&data[previous_line_offset], | 95 base::StringPiece line(&data[previous_line_offset], |
| 96 line_off - previous_line_offset + 1); | 96 line_off - previous_line_offset + 1); |
| 97 if (!DoesLineBeginWithComment(line)) | 97 if (!DoesLineBeginWithComment(line)) |
| 98 break; | 98 break; |
| 99 | 99 |
| 100 comment->insert(0, StripHashFromLine(line) + "\n"); | 100 comment->insert(0, StripHashFromLine(line) + "\n"); |
| 101 line_off = previous_line_offset; | 101 line_off = previous_line_offset; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 void PrintArgHelp(const base::StringPiece& name, const Value& value) { | 105 // Prints the value and origin for a default value. Default values always list |
| 106 OutputString(name.as_string(), DECORATION_YELLOW); | 106 // an origin and if there is no origin, print a message about it being |
| 107 OutputString(" Default = " + value.ToString(true) + "\n"); | 107 // internally set. Overrides can't be internally set so the location handling |
| 108 | 108 // is a bit different. |
| 109 // |
| 110 // The default value also contains the docstring. |
| 111 void PrintDefaultValueInfo(base::StringPiece name, const Value& value) { |
| 112 OutputString(value.ToString(true) + "\n"); |
| 109 if (value.origin()) { | 113 if (value.origin()) { |
| 110 std::string location, comment; | 114 std::string location, comment; |
| 111 GetContextForValue(value, &location, &comment); | 115 GetContextForValue(value, &location, &comment); |
| 112 OutputString(" " + location + "\n" + comment); | 116 OutputString(" From " + location + "\n"); |
| 117 if (!comment.empty()) |
| 118 OutputString("\n" + comment); |
| 113 } else { | 119 } else { |
| 114 OutputString(" (Internally set; try `gn help " + name.as_string() + | 120 OutputString(" (Internally set; try `gn help " + name.as_string() + |
| 115 "`.)\n"); | 121 "`.)\n"); |
| 116 } | 122 } |
| 117 } | 123 } |
| 118 | 124 |
| 125 // Override value is null if there is no override. |
| 126 void PrintArgHelp(const base::StringPiece& name, |
| 127 const Args::ValueWithOverride& val) { |
| 128 OutputString(name.as_string(), DECORATION_YELLOW); |
| 129 OutputString("\n"); |
| 130 |
| 131 if (val.has_override) { |
| 132 // Override present, print both it and the default. |
| 133 OutputString(" Current value = " + val.override_value.ToString(true) + |
| 134 "\n"); |
| 135 if (val.override_value.origin()) { |
| 136 std::string location, comment; |
| 137 GetContextForValue(val.override_value, &location, &comment); |
| 138 OutputString(" From " + location + "\n"); |
| 139 } |
| 140 OutputString(" Overridden from the default = "); |
| 141 PrintDefaultValueInfo(name, val.default_value); |
| 142 } else { |
| 143 // No override. |
| 144 OutputString(" Current value (from the default) = "); |
| 145 PrintDefaultValueInfo(name, val.default_value); |
| 146 } |
| 147 } |
| 148 |
| 119 int ListArgs(const std::string& build_dir) { | 149 int ListArgs(const std::string& build_dir) { |
| 120 Setup* setup = new Setup; | 150 Setup* setup = new Setup; |
| 121 if (!setup->DoSetup(build_dir, false) || !setup->Run()) | 151 if (!setup->DoSetup(build_dir, false) || !setup->Run()) |
| 122 return 1; | 152 return 1; |
| 123 | 153 |
| 124 Scope::KeyValueMap build_args; | 154 Args::ValueWithOverrideMap args = |
| 125 setup->build_settings().build_args().MergeDeclaredArguments(&build_args); | 155 setup->build_settings().build_args().GetAllArguments(); |
| 126 | |
| 127 // Find all of the arguments we care about. Use a regular map so they're | |
| 128 // sorted nicely when we write them out. | |
| 129 std::map<base::StringPiece, Value> sorted_args; | |
| 130 std::string list_value = | 156 std::string list_value = |
| 131 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kSwitchList); | 157 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kSwitchList); |
| 132 if (list_value.empty()) { | 158 if (!list_value.empty()) { |
| 133 // List all values. | |
| 134 for (const auto& arg : build_args) | |
| 135 sorted_args.insert(arg); | |
| 136 } else { | |
| 137 // List just the one specified as the parameter to --list. | 159 // List just the one specified as the parameter to --list. |
| 138 Scope::KeyValueMap::const_iterator found_arg = build_args.find(list_value); | 160 auto found = args.find(list_value); |
| 139 if (found_arg == build_args.end()) { | 161 if (found == args.end()) { |
| 140 Err(Location(), "Unknown build argument.", | 162 Err(Location(), "Unknown build argument.", |
| 141 "You asked for \"" + list_value + "\" which I didn't find in any " | 163 "You asked for \"" + list_value + "\" which I didn't find in any " |
| 142 "build file\nassociated with this build.").PrintToStdout(); | 164 "build file\nassociated with this build.").PrintToStdout(); |
| 143 return 1; | 165 return 1; |
| 144 } | 166 } |
| 145 sorted_args.insert(*found_arg); | 167 |
| 168 // Delete everything from the map except the one requested. |
| 169 Args::ValueWithOverrideMap::value_type preserved = *found; |
| 170 args.clear(); |
| 171 args.insert(preserved); |
| 146 } | 172 } |
| 147 | 173 |
| 148 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchShort)) { | 174 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchShort)) { |
| 149 // Short key=value output. | 175 // Short <key>=<current_value> output. |
| 150 for (const auto& arg : sorted_args) { | 176 for (const auto& arg : args) { |
| 151 OutputString(arg.first.as_string()); | 177 OutputString(arg.first.as_string()); |
| 152 OutputString(" = "); | 178 OutputString(" = "); |
| 153 OutputString(arg.second.ToString(true)); | 179 if (arg.second.has_override) |
| 180 OutputString(arg.second.override_value.ToString(true)); |
| 181 else |
| 182 OutputString(arg.second.default_value.ToString(true)); |
| 154 OutputString("\n"); | 183 OutputString("\n"); |
| 155 } | 184 } |
| 156 return 0; | 185 return 0; |
| 157 } | 186 } |
| 158 | 187 |
| 159 // Long output. | 188 // Long output. |
| 160 for (const auto& arg : sorted_args) { | 189 for (const auto& arg : args) { |
| 161 PrintArgHelp(arg.first, arg.second); | 190 PrintArgHelp(arg.first, arg.second); |
| 162 OutputString("\n"); | 191 OutputString("\n"); |
| 163 } | 192 } |
| 164 | 193 |
| 165 return 0; | 194 return 0; |
| 166 } | 195 } |
| 167 | 196 |
| 168 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
| 169 | 198 |
| 170 bool RunEditor(const base::FilePath& file_to_edit) { | 199 bool RunEditor(const base::FilePath& file_to_edit) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 192 "\"...\n"); | 221 "\"...\n"); |
| 193 ::WaitForSingleObject(info.hProcess, INFINITE); | 222 ::WaitForSingleObject(info.hProcess, INFINITE); |
| 194 ::CloseHandle(info.hProcess); | 223 ::CloseHandle(info.hProcess); |
| 195 } | 224 } |
| 196 return true; | 225 return true; |
| 197 } | 226 } |
| 198 | 227 |
| 199 #else // POSIX | 228 #else // POSIX |
| 200 | 229 |
| 201 bool RunEditor(const base::FilePath& file_to_edit) { | 230 bool RunEditor(const base::FilePath& file_to_edit) { |
| 202 const char* editor_ptr = getenv("VISUAL"); | 231 const char* editor_ptr = getenv("GN_EDITOR"); |
| 203 if (!editor_ptr) | 232 if (!editor_ptr) |
| 204 editor_ptr = getenv("GN_EDITOR"); | 233 editor_ptr = getenv("VISUAL"); |
| 205 if (!editor_ptr) | 234 if (!editor_ptr) |
| 206 editor_ptr = getenv("EDITOR"); | 235 editor_ptr = getenv("EDITOR"); |
| 207 if (!editor_ptr) | 236 if (!editor_ptr) |
| 208 editor_ptr = "vi"; | 237 editor_ptr = "vi"; |
| 209 | 238 |
| 210 std::string cmd(editor_ptr); | 239 std::string cmd(editor_ptr); |
| 211 cmd.append(" \""); | 240 cmd.append(" \""); |
| 212 | 241 |
| 213 // Its impossible to do this properly since we don't know the user's shell, | 242 // Its impossible to do this properly since we don't know the user's shell, |
| 214 // but quoting and escaping internal quotes should handle 99.999% of all | 243 // but quoting and escaping internal quotes should handle 99.999% of all |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 extern const char kArgs[] = "args"; | 306 extern const char kArgs[] = "args"; |
| 278 extern const char kArgs_HelpShort[] = | 307 extern const char kArgs_HelpShort[] = |
| 279 "args: Display or configure arguments declared by the build."; | 308 "args: Display or configure arguments declared by the build."; |
| 280 extern const char kArgs_Help[] = | 309 extern const char kArgs_Help[] = |
| 281 R"(gn args <out_dir> [--list] [--short] [--args] | 310 R"(gn args <out_dir> [--list] [--short] [--args] |
| 282 | 311 |
| 283 See also "gn help buildargs" for a more high-level overview of how | 312 See also "gn help buildargs" for a more high-level overview of how |
| 284 build arguments work. | 313 build arguments work. |
| 285 | 314 |
| 286 Usage | 315 Usage |
| 316 |
| 287 gn args <out_dir> | 317 gn args <out_dir> |
| 288 Open the arguments for the given build directory in an editor (as | 318 Open the arguments for the given build directory in an editor. If the |
| 289 specified by the EDITOR environment variable). If the given build | 319 given build directory doesn't exist, it will be created and an empty args |
| 290 directory doesn't exist, it will be created and an empty args file will | 320 file will be opened in the editor. You would type something like this |
| 291 be opened in the editor. You would type something like this into that | 321 into that file: |
| 292 file: | |
| 293 enable_doom_melon=false | 322 enable_doom_melon=false |
| 294 os="android" | 323 os="android" |
| 295 | 324 |
| 325 To find your editor on Posix, GN will search the environment variables in |
| 326 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command |
| 327 associated with .txt files. |
| 328 |
| 296 Note: you can edit the build args manually by editing the file "args.gn" | 329 Note: you can edit the build args manually by editing the file "args.gn" |
| 297 in the build directory and then running "gn gen <out_dir>". | 330 in the build directory and then running "gn gen <out_dir>". |
| 298 | 331 |
| 299 gn args <out_dir> --list[=<exact_arg>] [--short] | 332 gn args <out_dir> --list[=<exact_arg>] [--short] |
| 300 Lists all build arguments available in the current configuration, or, if | 333 Lists all build arguments available in the current configuration, or, if |
| 301 an exact_arg is specified for the list flag, just that one build | 334 an exact_arg is specified for the list flag, just that one build |
| 302 argument. | 335 argument. |
| 303 | 336 |
| 304 The output will list the declaration location, default value, and comment | 337 The output will list the declaration location, current value for the |
| 305 preceeding the declaration. If --short is specified, only the names and | 338 build, default value (if different than the current value), and comment |
| 306 values will be printed. | 339 preceeding the declaration. |
| 307 | 340 |
| 308 If the out_dir is specified, the build configuration will be taken from | 341 If --short is specified, only the names and current values will be |
| 309 that build directory. The reason this is needed is that the definition of | 342 printed. |
| 310 some arguments is dependent on the build configuration, so setting some | |
| 311 values might add, remove, or change the default values for other | |
| 312 arguments. Specifying your exact configuration allows the proper | |
| 313 arguments to be displayed. | |
| 314 | |
| 315 Instead of specifying the out_dir, you can also use the command-line flag | |
| 316 to specify the build configuration: | |
| 317 --args=<exact list of args to use> | |
| 318 | 343 |
| 319 Examples | 344 Examples |
| 320 | 345 |
| 321 gn args out/Debug | 346 gn args out/Debug |
| 322 Opens an editor with the args for out/Debug. | 347 Opens an editor with the args for out/Debug. |
| 323 | 348 |
| 324 gn args out/Debug --list --short | 349 gn args out/Debug --list --short |
| 325 Prints all arguments with their default values for the out/Debug | 350 Prints all arguments with their default values for the out/Debug |
| 326 build. | 351 build. |
| 327 | 352 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 343 "Or see \"gn help args\" for more variants.").PrintToStdout(); | 368 "Or see \"gn help args\" for more variants.").PrintToStdout(); |
| 344 return 1; | 369 return 1; |
| 345 } | 370 } |
| 346 | 371 |
| 347 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchList)) | 372 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchList)) |
| 348 return ListArgs(args[0]); | 373 return ListArgs(args[0]); |
| 349 return EditArgsFile(args[0]); | 374 return EditArgsFile(args[0]); |
| 350 } | 375 } |
| 351 | 376 |
| 352 } // namespace commands | 377 } // namespace commands |
| OLD | NEW |