[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / gn / command_ls.cc
blobed4aaca2496a593a7d187ee56e8722d84ecee514
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.
5 #include <algorithm>
6 #include <set>
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"
15 namespace commands {
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"
23 "\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"
27 "\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"
32 "\n"
33 "Options\n"
34 "\n"
35 TARGET_PRINTING_MODE_COMMAND_LINE_HELP
36 "\n"
37 " --all-toolchains\n"
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"
42 "\n"
43 TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
44 "\n"
45 TARGET_TYPE_FILTER_COMMAND_LINE_HELP
46 "\n"
47 "Examples\n"
48 "\n"
49 " gn ls out/Debug\n"
50 " Lists all targets in the default toolchain.\n"
51 "\n"
52 " gn ls out/Debug \"//base/*\"\n"
53 " Lists all targets in the directory base and all subdirectories.\n"
54 "\n"
55 " gn ls out/Debug \"//base:*\"\n"
56 " Lists all targets defined in //base/BUILD.gn.\n"
57 "\n"
58 " gn ls out/Debug //base --as=output\n"
59 " Lists the build output file for //base:base\n"
60 "\n"
61 " gn ls out/Debug --type=executable\n"
62 " Lists all executables produced by the build.\n"
63 "\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"
66 "\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();
75 return 1;
78 Setup* setup = new Setup;
79 if (!setup->DoSetup(args[0], false) || !setup->Run())
80 return 1;
82 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
83 bool all_toolchains = cmdline->HasSwitch("all-toolchains");
85 std::vector<const Target*> matches;
86 if (args.size() > 1) {
87 // Some patterns or explicit labels were specified.
88 std::vector<std::string> inputs(args.begin() + 1, args.end());
90 UniqueVector<const Target*> target_matches;
91 UniqueVector<const Config*> config_matches;
92 UniqueVector<const Toolchain*> toolchain_matches;
93 UniqueVector<SourceFile> file_matches;
94 if (!ResolveFromCommandLineInput(setup, inputs, all_toolchains,
95 &target_matches, &config_matches,
96 &toolchain_matches, &file_matches))
97 return 1;
98 matches.insert(matches.begin(),
99 target_matches.begin(), target_matches.end());
100 } else if (all_toolchains) {
101 // List all resolved targets.
102 matches = setup->builder()->GetAllResolvedTargets();
103 } else {
104 // List all resolved targets in the default toolchain.
105 for (const auto& target : setup->builder()->GetAllResolvedTargets()) {
106 if (target->settings()->is_default())
107 matches.push_back(target);
110 FilterAndPrintTargets(false, &matches);
111 return 0;
114 } // namespace commands