| 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 "tools/gn/commands.h" | 5 #include "tools/gn/commands.h" |
| 6 #include "tools/gn/filesystem_utils.h" | 6 #include "tools/gn/filesystem_utils.h" |
| 7 #include "tools/gn/item.h" | 7 #include "tools/gn/item.h" |
| 8 #include "tools/gn/label.h" | 8 #include "tools/gn/label.h" |
| 9 #include "tools/gn/label_pattern.h" |
| 9 #include "tools/gn/setup.h" | 10 #include "tools/gn/setup.h" |
| 10 #include "tools/gn/standard_out.h" | 11 #include "tools/gn/standard_out.h" |
| 11 #include "tools/gn/target.h" | 12 #include "tools/gn/target.h" |
| 12 | 13 |
| 13 namespace commands { | 14 namespace commands { |
| 14 | 15 |
| 15 CommandInfo::CommandInfo() | 16 CommandInfo::CommandInfo() |
| 16 : help_short(NULL), | 17 : help_short(NULL), |
| 17 help(NULL), | 18 help(NULL), |
| 18 runner(NULL) { | 19 runner(NULL) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 32 #define INSERT_COMMAND(cmd) \ | 33 #define INSERT_COMMAND(cmd) \ |
| 33 info_map[k##cmd] = CommandInfo(k##cmd##_HelpShort, \ | 34 info_map[k##cmd] = CommandInfo(k##cmd##_HelpShort, \ |
| 34 k##cmd##_Help, \ | 35 k##cmd##_Help, \ |
| 35 &Run##cmd); | 36 &Run##cmd); |
| 36 | 37 |
| 37 INSERT_COMMAND(Args) | 38 INSERT_COMMAND(Args) |
| 38 INSERT_COMMAND(Check) | 39 INSERT_COMMAND(Check) |
| 39 INSERT_COMMAND(Desc) | 40 INSERT_COMMAND(Desc) |
| 40 INSERT_COMMAND(Gen) | 41 INSERT_COMMAND(Gen) |
| 41 INSERT_COMMAND(Help) | 42 INSERT_COMMAND(Help) |
| 43 INSERT_COMMAND(Ls) |
| 42 INSERT_COMMAND(Refs) | 44 INSERT_COMMAND(Refs) |
| 43 | 45 |
| 44 #undef INSERT_COMMAND | 46 #undef INSERT_COMMAND |
| 45 } | 47 } |
| 46 return info_map; | 48 return info_map; |
| 47 } | 49 } |
| 48 | 50 |
| 49 const Target* GetTargetForDesc(const std::vector<std::string>& args) { | 51 const Target* ResolveTargetFromCommandLineString( |
| 50 // Deliberately leaked to avoid expensive process teardown. | 52 Setup* setup, |
| 51 Setup* setup = new Setup; | 53 const std::string& label_string) { |
| 52 // TODO(brettw) bug 343726: Use a temporary directory instead of this | |
| 53 // default one to avoid messing up any build that's in there. | |
| 54 if (!setup->DoSetup("//out/Default/")) | |
| 55 return NULL; | |
| 56 | |
| 57 // Do the actual load. This will also write out the target ninja files. | |
| 58 if (!setup->Run()) | |
| 59 return NULL; | |
| 60 | |
| 61 // Need to resolve the label after we know the default toolchain. | 54 // Need to resolve the label after we know the default toolchain. |
| 62 Label default_toolchain = setup->loader()->default_toolchain_label(); | 55 Label default_toolchain = setup->loader()->default_toolchain_label(); |
| 63 Value arg_value(NULL, args[0]); | 56 Value arg_value(NULL, label_string); |
| 64 Err err; | 57 Err err; |
| 65 Label label = Label::Resolve(SourceDirForCurrentDirectory( | 58 Label label = Label::Resolve(SourceDirForCurrentDirectory( |
| 66 setup->build_settings().root_path()), | 59 setup->build_settings().root_path()), |
| 67 default_toolchain, arg_value, &err); | 60 default_toolchain, arg_value, &err); |
| 68 if (err.has_error()) { | 61 if (err.has_error()) { |
| 69 err.PrintToStdout(); | 62 err.PrintToStdout(); |
| 70 return NULL; | 63 return NULL; |
| 71 } | 64 } |
| 72 | 65 |
| 73 const Item* item = setup->builder()->GetItem(label); | 66 const Item* item = setup->builder()->GetItem(label); |
| 74 if (!item) { | 67 if (!item) { |
| 75 Err(Location(), "Label not found.", | 68 Err(Location(), "Label not found.", |
| 76 label.GetUserVisibleName(false) + " not found.").PrintToStdout(); | 69 label.GetUserVisibleName(false) + " not found.").PrintToStdout(); |
| 77 return NULL; | 70 return NULL; |
| 78 } | 71 } |
| 79 | 72 |
| 80 const Target* target = item->AsTarget(); | 73 const Target* target = item->AsTarget(); |
| 81 if (!target) { | 74 if (!target) { |
| 82 Err(Location(), "Not a target.", | 75 Err(Location(), "Not a target.", |
| 83 "The \"" + label.GetUserVisibleName(false) + "\" thing\n" | 76 "The \"" + label.GetUserVisibleName(false) + "\" thing\n" |
| 84 "is not a target. Somebody should probably implement this command for " | 77 "is not a target. Somebody should probably implement this command for " |
| 85 "other\nitem types."); | 78 "other\nitem types."); |
| 86 return NULL; | 79 return NULL; |
| 87 } | 80 } |
| 88 | 81 |
| 89 return target; | 82 return target; |
| 90 } | 83 } |
| 91 | 84 |
| 85 bool ResolveTargetsFromCommandLinePattern( |
| 86 Setup* setup, |
| 87 const std::string& label_pattern, |
| 88 bool all_toolchains, |
| 89 std::vector<const Target*>* matches) { |
| 90 Value pattern_value(NULL, label_pattern); |
| 91 |
| 92 Err err; |
| 93 LabelPattern pattern = LabelPattern::GetPattern( |
| 94 SourceDirForCurrentDirectory(setup->build_settings().root_path()), |
| 95 pattern_value, |
| 96 &err); |
| 97 if (err.has_error()) { |
| 98 err.PrintToStdout(); |
| 99 return false; |
| 100 } |
| 101 |
| 102 if (!all_toolchains) { |
| 103 // By default a pattern with an empty toolchain will match all toolchains. |
| 104 // IF the caller wants to default to the main toolchain only, set it |
| 105 // explicitly. |
| 106 if (pattern.toolchain().is_null()) { |
| 107 // No explicit toolchain set. |
| 108 pattern.set_toolchain(setup->loader()->default_toolchain_label()); |
| 109 } |
| 110 } |
| 111 |
| 112 std::vector<const Target*> all_targets = |
| 113 setup->builder()->GetAllResolvedTargets(); |
| 114 |
| 115 for (size_t i = 0; i < all_targets.size(); i++) { |
| 116 if (pattern.Matches(all_targets[i]->label())) |
| 117 matches->push_back(all_targets[i]); |
| 118 } |
| 119 return true; |
| 120 } |
| 121 |
| 92 } // namespace commands | 122 } // namespace commands |
| OLD | NEW |