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>] [--default-toolchain] [--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" DEFAULT_TOOLCHAIN_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
RunLs(const std::vector<std::string> & args)59 int RunLs(const std::vector<std::string>& args) {
60 if (args.size() == 0) {
61 Err(Location(), "Unknown command format. See \"gn help ls\"",
62 "Usage: \"gn ls <build dir> [<label_pattern>]*\"")
63 .PrintToStdout();
64 return 1;
65 }
66
67 // Deliberately leaked to avoid expensive process teardown.
68 Setup* setup = new Setup;
69 if (!setup->DoSetup(args[0], false) || !setup->Run())
70 return 1;
71
72 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
73 bool default_toolchain_only = cmdline->HasSwitch(switches::kDefaultToolchain);
74
75 std::vector<const Target*> matches;
76 if (args.size() > 1) {
77 // Some patterns or explicit labels were specified.
78 std::vector<std::string> inputs(args.begin() + 1, args.end());
79
80 UniqueVector<const Target*> target_matches;
81 UniqueVector<const Config*> config_matches;
82 UniqueVector<const Toolchain*> toolchain_matches;
83 UniqueVector<SourceFile> file_matches;
84 if (!ResolveFromCommandLineInput(setup, inputs, default_toolchain_only,
85 &target_matches, &config_matches,
86 &toolchain_matches, &file_matches))
87 return 1;
88 matches.insert(matches.begin(), target_matches.begin(),
89 target_matches.end());
90 } else if (default_toolchain_only) {
91 // List all resolved targets in the default toolchain.
92 for (auto* target : setup->builder().GetAllResolvedTargets()) {
93 if (target->settings()->is_default())
94 matches.push_back(target);
95 }
96 } else {
97 // List all resolved targets.
98 matches = setup->builder().GetAllResolvedTargets();
99 }
100 FilterAndPrintTargets(false, &matches);
101 return 0;
102 }
103
104 } // namespace commands
105