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, \
42 INSERT_COMMAND(Format
)
52 const Target
* ResolveTargetFromCommandLineString(
54 const std::string
& label_string
) {
55 // Need to resolve the label after we know the default toolchain.
56 Label default_toolchain
= setup
->loader()->default_toolchain_label();
57 Value
arg_value(NULL
, label_string
);
59 Label label
= Label::Resolve(SourceDirForCurrentDirectory(
60 setup
->build_settings().root_path()),
61 default_toolchain
, arg_value
, &err
);
62 if (err
.has_error()) {
67 const Item
* item
= setup
->builder()->GetItem(label
);
69 Err(Location(), "Label not found.",
70 label
.GetUserVisibleName(false) + " not found.").PrintToStdout();
74 const Target
* target
= item
->AsTarget();
76 Err(Location(), "Not a target.",
77 "The \"" + label
.GetUserVisibleName(false) + "\" thing\n"
78 "is not a target. Somebody should probably implement this command for "
79 "other\nitem types.");
86 bool ResolveTargetsFromCommandLinePattern(
88 const std::string
& label_pattern
,
90 std::vector
<const Target
*>* matches
) {
91 Value
pattern_value(NULL
, label_pattern
);
94 LabelPattern pattern
= LabelPattern::GetPattern(
95 SourceDirForCurrentDirectory(setup
->build_settings().root_path()),
98 if (err
.has_error()) {
103 if (!all_toolchains
) {
104 // By default a pattern with an empty toolchain will match all toolchains.
105 // IF the caller wants to default to the main toolchain only, set it
107 if (pattern
.toolchain().is_null()) {
108 // No explicit toolchain set.
109 pattern
.set_toolchain(setup
->loader()->default_toolchain_label());
113 std::vector
<const Target
*> all_targets
=
114 setup
->builder()->GetAllResolvedTargets();
116 for (size_t i
= 0; i
< all_targets
.size(); i
++) {
117 if (pattern
.Matches(all_targets
[i
]->label()))
118 matches
->push_back(all_targets
[i
]);
123 } // namespace commands