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"
16 CommandInfo::CommandInfo()
22 CommandInfo::CommandInfo(const char* in_help_short
,
24 CommandRunner in_runner
)
25 : help_short(in_help_short
),
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, \
51 const Target
* ResolveTargetFromCommandLineString(
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
);
58 Label label
= Label::Resolve(SourceDirForCurrentDirectory(
59 setup
->build_settings().root_path()),
60 default_toolchain
, arg_value
, &err
);
61 if (err
.has_error()) {
66 const Item
* item
= setup
->builder()->GetItem(label
);
68 Err(Location(), "Label not found.",
69 label
.GetUserVisibleName(false) + " not found.").PrintToStdout();
73 const Target
* target
= item
->AsTarget();
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.");
85 bool ResolveTargetsFromCommandLinePattern(
87 const std::string
& label_pattern
,
89 std::vector
<const Target
*>* matches
) {
90 Value
pattern_value(NULL
, label_pattern
);
93 LabelPattern pattern
= LabelPattern::GetPattern(
94 SourceDirForCurrentDirectory(setup
->build_settings().root_path()),
97 if (err
.has_error()) {
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
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
]);
122 } // namespace commands