Instrumental test for BookmarksBridge. It currently tests these functionalities:...
[chromium-blink-merge.git] / tools / gn / commands.cc
blob9598d49e7cd21551b558e5db2e5af7cf913e98f4
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.
5 #include "tools/gn/commands.h"
6 #include "tools/gn/filesystem_utils.h"
7 #include "tools/gn/item.h"
8 #include "tools/gn/label.h"
9 #include "tools/gn/label_pattern.h"
10 #include "tools/gn/setup.h"
11 #include "tools/gn/standard_out.h"
12 #include "tools/gn/target.h"
14 namespace commands {
16 CommandInfo::CommandInfo()
17 : help_short(NULL),
18 help(NULL),
19 runner(NULL) {
22 CommandInfo::CommandInfo(const char* in_help_short,
23 const char* in_help,
24 CommandRunner in_runner)
25 : help_short(in_help_short),
26 help(in_help),
27 runner(in_runner) {
30 const CommandInfoMap& GetCommands() {
31 static CommandInfoMap info_map;
32 if (info_map.empty()) {
33 #define INSERT_COMMAND(cmd) \
34 info_map[k##cmd] = CommandInfo(k##cmd##_HelpShort, \
35 k##cmd##_Help, \
36 &Run##cmd);
38 INSERT_COMMAND(Args)
39 INSERT_COMMAND(Check)
40 INSERT_COMMAND(Desc)
41 INSERT_COMMAND(Gen)
42 INSERT_COMMAND(Help)
43 INSERT_COMMAND(Ls)
44 INSERT_COMMAND(Refs)
46 #undef INSERT_COMMAND
48 return info_map;
51 const Target* ResolveTargetFromCommandLineString(
52 Setup* setup,
53 const std::string& label_string) {
54 // Need to resolve the label after we know the default toolchain.
55 Label default_toolchain = setup->loader()->default_toolchain_label();
56 Value arg_value(NULL, label_string);
57 Err err;
58 Label label = Label::Resolve(SourceDirForCurrentDirectory(
59 setup->build_settings().root_path()),
60 default_toolchain, arg_value, &err);
61 if (err.has_error()) {
62 err.PrintToStdout();
63 return NULL;
66 const Item* item = setup->builder()->GetItem(label);
67 if (!item) {
68 Err(Location(), "Label not found.",
69 label.GetUserVisibleName(false) + " not found.").PrintToStdout();
70 return NULL;
73 const Target* target = item->AsTarget();
74 if (!target) {
75 Err(Location(), "Not a target.",
76 "The \"" + label.GetUserVisibleName(false) + "\" thing\n"
77 "is not a target. Somebody should probably implement this command for "
78 "other\nitem types.");
79 return NULL;
82 return target;
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);
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;
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());
112 std::vector<const Target*> all_targets =
113 setup->builder()->GetAllResolvedTargets();
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]);
119 return true;
122 } // namespace commands