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 <build dir> [<label_pattern>] [--out] [--all-toolchains]\n"
23 " Lists all targets matching the given pattern for the given builn\n"
24 " directory. By default, only targets in the default toolchain will\n"
25 " be matched unless a toolchain is explicitly supplied.\n"
27 " If the label pattern is unspecified, list all targets. The label\n"
28 " pattern is not a general regular expression (see\n"
29 " \"gn help label_pattern\"). If you need more complex expressions,\n"
30 " pipe the result through grep.\n"
33 " Lists the results as the files generated by the matching targets.\n"
34 " These files will be relative to the build directory such that\n"
35 " they can be specified on Ninja's command line as a file to build.\n"
38 " Matches all toolchains. If the label pattern does not specify an\n"
39 " explicit toolchain, labels from all toolchains will be matched.\n"
44 " Lists all targets in the default toolchain.\n"
46 " gn ls out/Debug \"//base/*\"\n"
47 " Lists all targets in the directory base and all subdirectories.\n"
49 " gn ls out/Debug \"//base:*\"\n"
50 " Lists all targets defined in //base/BUILD.gn.\n"
52 " gn ls out/Debug //base --out\n"
53 " Lists the build output file for //base:base\n"
55 " gn ls out/Debug \"//base/*\" --out | xargs ninja -C out/Debug\n"
56 " Builds all targets in //base and all subdirectories.\n"
58 " gn ls out/Debug //base --all-toolchains\n"
59 " Lists all variants of the target //base:base (it may be referenced\n"
60 " in multiple toolchains).\n";
62 int RunLs(const std::vector<std::string>& args) {
63 if (args.size() != 1 && args.size() != 2) {
64 Err(Location(), "You're holding it wrong.",
65 "Usage: \"gn ls <build dir> [<label_pattern>]\"").PrintToStdout();
69 Setup* setup = new Setup;
70 if (!setup->DoSetup(args[0], false) || !setup->Run())
73 const CommandLine* cmdline = CommandLine::ForCurrentProcess();
74 bool all_toolchains = cmdline->HasSwitch("all-toolchains");
76 // Find matching targets.
77 std::vector<const Target*> matches;
78 if (args.size() == 2) {
79 // Given a pattern, match it.
80 if (!ResolveTargetsFromCommandLinePattern(setup, args[1], all_toolchains,
83 } else if (all_toolchains) {
84 // List all resolved targets.
85 matches = setup->builder()->GetAllResolvedTargets();
87 // List all resolved targets in the default toolchain.
88 for (const auto& target : setup->builder()->GetAllResolvedTargets()) {
89 if (target->settings()->is_default())
90 matches.push_back(target);
94 if (cmdline->HasSwitch("out")) {
95 // List results as build files.
96 for (const auto& match : matches) {
97 OutputString(match->dependency_output_file().value());
101 // List results as sorted labels.
102 std::vector<Label> sorted_matches;
103 for (const auto& match : matches)
104 sorted_matches.push_back(match->label());
105 std::sort(sorted_matches.begin(), sorted_matches.end());
107 Label default_tc_label = setup->loader()->default_toolchain_label();
108 for (const auto& match : sorted_matches) {
109 // Print toolchain only for ones not in the default toolchain.
110 OutputString(match.GetUserVisibleName(
111 match.GetToolchainLabel() != default_tc_label));
119 } // namespace commands