Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / gn / command_ls.cc
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 "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"
14
15 namespace commands {
16
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"
22     "\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"
26     "\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"
31     "\n"
32     "  --out\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"
36     "\n"
37     "  --all-toolchains\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"
40     "\n"
41     "Examples\n"
42     "\n"
43     "  gn ls out/Debug\n"
44     "      Lists all targets in the default toolchain.\n"
45     "\n"
46     "  gn ls out/Debug \"//base/*\"\n"
47     "      Lists all targets in the directory base and all subdirectories.\n"
48     "\n"
49     "  gn ls out/Debug \"//base:*\"\n"
50     "      Lists all targets defined in //base/BUILD.gn.\n"
51     "\n"
52     "  gn ls out/Debug //base --out\n"
53     "      Lists the build output file for //base:base\n"
54     "\n"
55     "  gn ls out/Debug \"//base/*\" --out | xargs ninja -C out/Debug\n"
56     "      Builds all targets in //base and all subdirectories.\n"
57     "\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";
61
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();
66     return 1;
67   }
68
69   Setup* setup = new Setup;
70   if (!setup->DoSetup(args[0], false) || !setup->Run())
71     return 1;
72
73   const CommandLine* cmdline = CommandLine::ForCurrentProcess();
74   bool all_toolchains = cmdline->HasSwitch("all-toolchains");
75
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,
81                                               &matches))
82       return 1;
83   } else if (all_toolchains) {
84     // List all resolved targets.
85     matches = setup->builder()->GetAllResolvedTargets();
86   } else {
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);
91     }
92   }
93
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;
117 }
118
119 }  // namespace commands