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.
4
5 #include <algorithm>
6 #include <set>
7
8 #include "base/command_line.h"
9 #include "gn/commands.h"
10 #include "gn/label_pattern.h"
11 #include "gn/setup.h"
12 #include "gn/standard_out.h"
13 #include "gn/switches.h"
14 #include "gn/target.h"
15
16 namespace commands {
17
18 const char kLs[] = "ls";
19 const char kLs_HelpShort[] = "ls: List matching targets.";
20 const char kLs_Help[] =
21 R"(gn ls <out_dir> [<label_pattern>] [--all-toolchains] [--as=...]
22 [--type=...] [--testonly=...]
23
24 Lists all targets matching the given pattern for the given build directory.
25 By default, only targets in the default toolchain will be matched unless a
26 toolchain is explicitly supplied.
27
28 If the label pattern is unspecified, list all targets. The label pattern is
29 not a general regular expression (see "gn help label_pattern"). If you need
30 more complex expressions, pipe the result through grep.
31
32 Options
33
34 )" TARGET_PRINTING_MODE_COMMAND_LINE_HELP "\n" ALL_TOOLCHAINS_SWITCH_HELP
35 "\n" TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
36 "\n" TARGET_TYPE_FILTER_COMMAND_LINE_HELP
37 R"(
38 Examples
39
40 gn ls out/Debug
41 Lists all targets in the default toolchain.
42
43 gn ls out/Debug "//base/*"
44 Lists all targets in the directory base and all subdirectories.
45
46 gn ls out/Debug "//base:*"
47 Lists all targets defined in //base/BUILD.gn.
48
49 gn ls out/Debug //base --as=output
50 Lists the build output file for //base:base
51
52 gn ls out/Debug --type=executable
53 Lists all executables produced by the build.
54
55 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
56 Builds all targets in //base and all subdirectories.
57
58 gn ls out/Debug //base --all-toolchains
59 Lists all variants of the target //base:base (it may be referenced
60 in multiple toolchains).
61 )";
62
RunLs(const std::vector<std::string> & args)63 int RunLs(const std::vector<std::string>& args) {
64 if (args.size() == 0) {
65 Err(Location(), "You're holding it wrong.",
66 "Usage: \"gn ls <build dir> [<label_pattern>]*\"")
67 .PrintToStdout();
68 return 1;
69 }
70
71 // Deliberately leaked to avoid expensive process teardown.
72 Setup* setup = new Setup;
73 if (!setup->DoSetup(args[0], false) || !setup->Run())
74 return 1;
75
76 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
77 bool all_toolchains = cmdline->HasSwitch(switches::kAllToolchains);
78
79 std::vector<const Target*> matches;
80 if (args.size() > 1) {
81 // Some patterns or explicit labels were specified.
82 std::vector<std::string> inputs(args.begin() + 1, args.end());
83
84 UniqueVector<const Target*> target_matches;
85 UniqueVector<const Config*> config_matches;
86 UniqueVector<const Toolchain*> toolchain_matches;
87 UniqueVector<SourceFile> file_matches;
88 if (!ResolveFromCommandLineInput(setup, inputs, all_toolchains,
89 &target_matches, &config_matches,
90 &toolchain_matches, &file_matches))
91 return 1;
92 matches.insert(matches.begin(), target_matches.begin(),
93 target_matches.end());
94 } else if (all_toolchains) {
95 // List all resolved targets.
96 matches = setup->builder().GetAllResolvedTargets();
97 } else {
98 // List all resolved targets in the default toolchain.
99 for (auto* target : setup->builder().GetAllResolvedTargets()) {
100 if (target->settings()->is_default())
101 matches.push_back(target);
102 }
103 }
104 FilterAndPrintTargets(false, &matches);
105 return 0;
106 }
107
108 } // namespace commands
109