1 // Copyright 2014 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.
8 #include "base/command_line.h"
9 #include "tools/gn/commands.h"
10 #include "tools/gn/label_pattern.h"
11 #include "tools/gn/setup.h"
12 #include "tools/gn/standard_out.h"
13 #include "tools/gn/target.h"
17 const char kLs
[] = "ls";
18 const char kLs_HelpShort
[] =
19 "ls: List matching targets.";
20 const char kLs_Help
[] =
21 "gn ls <out_dir> [<label_pattern>] [--all-toolchains] [--as=...]\n"
22 " [--type=...] [--testonly=...]\n"
24 " Lists all targets matching the given pattern for the given build\n"
25 " directory. By default, only targets in the default toolchain will\n"
26 " be matched unless a toolchain is explicitly supplied.\n"
28 " If the label pattern is unspecified, list all targets. The label\n"
29 " pattern is not a general regular expression (see\n"
30 " \"gn help label_pattern\"). If you need more complex expressions,\n"
31 " pipe the result through grep.\n"
35 TARGET_PRINTING_MODE_COMMAND_LINE_HELP
38 " Matches all toolchains. When set, if the label pattern does not\n"
39 " specify an explicit toolchain, labels from all toolchains will be\n"
40 " matched. When unset, only targets in the default toolchain will\n"
41 " be matched unless an explicit toolchain in the label is set.\n"
43 TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
45 TARGET_TYPE_FILTER_COMMAND_LINE_HELP
50 " Lists all targets in the default toolchain.\n"
52 " gn ls out/Debug \"//base/*\"\n"
53 " Lists all targets in the directory base and all subdirectories.\n"
55 " gn ls out/Debug \"//base:*\"\n"
56 " Lists all targets defined in //base/BUILD.gn.\n"
58 " gn ls out/Debug //base --as=output\n"
59 " Lists the build output file for //base:base\n"
61 " gn ls out/Debug --type=executable\n"
62 " Lists all executables produced by the build.\n"
64 " gn ls out/Debug \"//base/*\" --as=output | xargs ninja -C out/Debug\n"
65 " Builds all targets in //base and all subdirectories.\n"
67 " gn ls out/Debug //base --all-toolchains\n"
68 " Lists all variants of the target //base:base (it may be referenced\n"
69 " in multiple toolchains).\n";
71 int RunLs(const std::vector
<std::string
>& args
) {
72 if (args
.size() == 0) {
73 Err(Location(), "You're holding it wrong.",
74 "Usage: \"gn ls <build dir> [<label_pattern>]*\"").PrintToStdout();
78 Setup
* setup
= new Setup
;
79 setup
->build_settings().set_check_for_bad_items(false);
80 if (!setup
->DoSetup(args
[0], false) || !setup
->Run())
83 const base::CommandLine
* cmdline
= base::CommandLine::ForCurrentProcess();
84 bool all_toolchains
= cmdline
->HasSwitch("all-toolchains");
86 std::vector
<const Target
*> matches
;
87 if (args
.size() > 1) {
88 // Some patterns or explicit labels were specified.
89 std::vector
<std::string
> inputs(args
.begin() + 1, args
.end());
91 UniqueVector
<const Target
*> target_matches
;
92 UniqueVector
<const Config
*> config_matches
;
93 UniqueVector
<const Toolchain
*> toolchain_matches
;
94 UniqueVector
<SourceFile
> file_matches
;
95 if (!ResolveFromCommandLineInput(setup
, inputs
, all_toolchains
,
96 &target_matches
, &config_matches
,
97 &toolchain_matches
, &file_matches
))
99 matches
.insert(matches
.begin(),
100 target_matches
.begin(), target_matches
.end());
101 } else if (all_toolchains
) {
102 // List all resolved targets.
103 matches
= setup
->builder()->GetAllResolvedTargets();
105 // List all resolved targets in the default toolchain.
106 for (const auto& target
: setup
->builder()->GetAllResolvedTargets()) {
107 if (target
->settings()->is_default())
108 matches
.push_back(target
);
111 FilterAndPrintTargets(false, &matches
);
115 } // namespace commands