Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: tools/gn/command_ls.cc

Issue 937003002: Enhance GN introspection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "tools/gn/commands.h" 9 #include "tools/gn/commands.h"
10 #include "tools/gn/label_pattern.h" 10 #include "tools/gn/label_pattern.h"
11 #include "tools/gn/setup.h" 11 #include "tools/gn/setup.h"
12 #include "tools/gn/standard_out.h" 12 #include "tools/gn/standard_out.h"
13 #include "tools/gn/target.h" 13 #include "tools/gn/target.h"
14 14
15 namespace commands { 15 namespace commands {
16 16
17 const char kLs[] = "ls"; 17 const char kLs[] = "ls";
18 const char kLs_HelpShort[] = 18 const char kLs_HelpShort[] =
19 "ls: List matching targets."; 19 "ls: List matching targets.";
20 const char kLs_Help[] = 20 const char kLs_Help[] =
21 "gn ls <out_dir> [<label_pattern>] [--out] [--all-toolchains]\n" 21 "gn ls <out_dir> [<label_pattern>] [--out] [--all-toolchains]\n"
scottmg 2015/02/19 21:52:01 --as, --type, etc?
22 "\n" 22 "\n"
23 " Lists all targets matching the given pattern for the given builn\n" 23 " Lists all targets matching the given pattern for the given build\n"
24 " directory. By default, only targets in the default toolchain will\n" 24 " directory. By default, only targets in the default toolchain will\n"
25 " be matched unless a toolchain is explicitly supplied.\n" 25 " be matched unless a toolchain is explicitly supplied.\n"
26 "\n" 26 "\n"
27 " If the label pattern is unspecified, list all targets. The label\n" 27 " If the label pattern is unspecified, list all targets. The label\n"
28 " pattern is not a general regular expression (see\n" 28 " pattern is not a general regular expression (see\n"
29 " \"gn help label_pattern\"). If you need more complex expressions,\n" 29 " \"gn help label_pattern\"). If you need more complex expressions,\n"
30 " pipe the result through grep.\n" 30 " pipe the result through grep.\n"
31 "\n" 31 "\n"
32 " --out\n" 32 "Options\n"
33 " Lists the results as the files generated by the matching targets.\n" 33 "\n"
34 " These files will be relative to the build directory such that\n" 34 TARGET_PRINTING_MODE_COMMAND_LINE_HELP
35 " they can be specified on Ninja's command line as a file to build.\n"
36 "\n" 35 "\n"
37 " --all-toolchains\n" 36 " --all-toolchains\n"
38 " Matches all toolchains. If the label pattern does not specify an\n" 37 " Matches all toolchains. When set, if the label pattern does not\n"
39 " explicit toolchain, labels from all toolchains will be matched.\n" 38 " specify an explicit toolchain, labels from all toolchains will be\n"
39 " matched. When unset, only targets in the default toolchain will\n"
40 " be matched unless an explicit toolchain in the label is set.\n"
41 "\n"
42 TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
43 "\n"
44 TARGET_TYPE_FILTER_COMMAND_LINE_HELP
40 "\n" 45 "\n"
41 "Examples\n" 46 "Examples\n"
42 "\n" 47 "\n"
43 " gn ls out/Debug\n" 48 " gn ls out/Debug\n"
44 " Lists all targets in the default toolchain.\n" 49 " Lists all targets in the default toolchain.\n"
45 "\n" 50 "\n"
46 " gn ls out/Debug \"//base/*\"\n" 51 " gn ls out/Debug \"//base/*\"\n"
47 " Lists all targets in the directory base and all subdirectories.\n" 52 " Lists all targets in the directory base and all subdirectories.\n"
48 "\n" 53 "\n"
49 " gn ls out/Debug \"//base:*\"\n" 54 " gn ls out/Debug \"//base:*\"\n"
50 " Lists all targets defined in //base/BUILD.gn.\n" 55 " Lists all targets defined in //base/BUILD.gn.\n"
51 "\n" 56 "\n"
52 " gn ls out/Debug //base --out\n" 57 " gn ls out/Debug //base --as=output\n"
53 " Lists the build output file for //base:base\n" 58 " Lists the build output file for //base:base\n"
54 "\n" 59 "\n"
55 " gn ls out/Debug \"//base/*\" --out | xargs ninja -C out/Debug\n" 60 " gn ls out/Debug --type=executable\n"
61 " Lists all executables produced by the build.\n"
62 "\n"
63 " gn ls out/Debug \"//base/*\" --as=output | xargs ninja -C out/Debug\n"
56 " Builds all targets in //base and all subdirectories.\n" 64 " Builds all targets in //base and all subdirectories.\n"
57 "\n" 65 "\n"
58 " gn ls out/Debug //base --all-toolchains\n" 66 " gn ls out/Debug //base --all-toolchains\n"
59 " Lists all variants of the target //base:base (it may be referenced\n" 67 " Lists all variants of the target //base:base (it may be referenced\n"
60 " in multiple toolchains).\n"; 68 " in multiple toolchains).\n";
61 69
62 int RunLs(const std::vector<std::string>& args) { 70 int RunLs(const std::vector<std::string>& args) {
63 if (args.size() != 1 && args.size() != 2) { 71 if (args.size() == 0) {
64 Err(Location(), "You're holding it wrong.", 72 Err(Location(), "You're holding it wrong.",
65 "Usage: \"gn ls <build dir> [<label_pattern>]\"").PrintToStdout(); 73 "Usage: \"gn ls <build dir> [<label_pattern>]*\"").PrintToStdout();
66 return 1; 74 return 1;
67 } 75 }
68 76
69 Setup* setup = new Setup; 77 Setup* setup = new Setup;
70 if (!setup->DoSetup(args[0], false) || !setup->Run()) 78 if (!setup->DoSetup(args[0], false) || !setup->Run())
71 return 1; 79 return 1;
72 80
73 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); 81 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
74 bool all_toolchains = cmdline->HasSwitch("all-toolchains"); 82 bool all_toolchains = cmdline->HasSwitch("all-toolchains");
75 83
76 // Find matching targets.
77 std::vector<const Target*> matches; 84 std::vector<const Target*> matches;
78 if (args.size() == 2) { 85 if (args.size() > 1) {
79 // Given a pattern, match it. 86 // Some patterns or explicit labels were specified.
80 if (!ResolveTargetsFromCommandLinePattern(setup, args[1], all_toolchains, 87 std::vector<std::string> inputs(args.begin() + 1, args.end());
81 &matches)) 88
89 UniqueVector<const Target*> target_matches;
90 UniqueVector<const Config*> config_matches;
91 UniqueVector<const Toolchain*> toolchain_matches;
92 UniqueVector<SourceFile> file_matches;
93 if (!ResolveFromCommandLineInput(setup, inputs, all_toolchains,
94 &target_matches, &config_matches,
95 &toolchain_matches, &file_matches))
82 return 1; 96 return 1;
97 matches.insert(matches.begin(),
98 target_matches.begin(), target_matches.end());
83 } else if (all_toolchains) { 99 } else if (all_toolchains) {
84 // List all resolved targets. 100 // List all resolved targets.
85 matches = setup->builder()->GetAllResolvedTargets(); 101 matches = setup->builder()->GetAllResolvedTargets();
86 } else { 102 } else {
87 // List all resolved targets in the default toolchain. 103 // List all resolved targets in the default toolchain.
88 for (const auto& target : setup->builder()->GetAllResolvedTargets()) { 104 for (const auto& target : setup->builder()->GetAllResolvedTargets()) {
89 if (target->settings()->is_default()) 105 if (target->settings()->is_default())
90 matches.push_back(target); 106 matches.push_back(target);
91 } 107 }
92 } 108 }
93 109 FilterAndPrintTargets(false, &matches);
94 if (cmdline->HasSwitch("out")) {
95 // List results as build files.
96 for (const auto& match : matches) {
97 OutputString(match->dependency_output_file().value());
98 OutputString("\n");
99 }
100 } else {
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());
106
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));
112 OutputString("\n");
113 }
114 }
115
116 return 0; 110 return 0;
117 } 111 }
118 112
119 } // namespace commands 113 } // namespace commands
OLDNEW
« no previous file with comments | « tools/gn/command_desc.cc ('k') | tools/gn/command_refs.cc » ('j') | tools/gn/command_refs.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698