Match all toolchains by default.

GN was originally designed expecting that most users would only want to
interact with the default toolchain. As a result, most introspection
commands match only targets in the default toolchain by default.

But some builds have made greater use of the toolchain feature. For
example, in Fuchsia, allshared libraries and all of their dependencies
are compiled in a special "shared" toolchain with "position independent
code" enabled. Most users are not aware of this. So in such builds,
having introspection commands only match targets in the default
toolchain can be surprising and look broken.

This changes the default to match all toolchains for introspection
commands that can take labels. A new --default-toolchain flag is added
for the default behavior, and the previous --all-toolchains flag is
removed.

Change-Id: Ifa0dfa5b8c792c115443776f3b992d2d6b03bf02
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/7840
Reviewed-by: Scott Graham <scottmg@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
diff --git a/src/gn/command_check.cc b/src/gn/command_check.cc
index 7122d02..98a5e1c 100644
--- a/src/gn/command_check.cc
+++ b/src/gn/command_check.cc
@@ -70,10 +70,6 @@
 
 Command-specific switches
 
-  --force
-      Ignores specifications of "check_includes = false" and checks all
-      target's files that match the target label.
-
   --check-generated
       Generated files are normally not checked since they do not exist
       until after a build. With this flag, those generated files that
@@ -83,6 +79,12 @@
      Check system style includes (using <angle brackets>) in addition to
      "double quote" includes.
 
+)" DEFAULT_TOOLCHAIN_SWITCH_HELP
+    R"(
+  --force
+      Ignores specifications of "check_includes = false" and checks all
+      target's files that match the target label.
+
 What gets checked
 
   The .gn file may specify a list of targets to be checked in the list
@@ -190,6 +192,9 @@
   if (!setup->Run())
     return 1;
 
+  const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
+  bool default_toolchain_only = cmdline->HasSwitch(switches::kDefaultToolchain);
+
   std::vector<const Target*> all_targets =
       setup->builder().GetAllResolvedTargets();
 
@@ -202,9 +207,9 @@
     UniqueVector<const Config*> config_matches;
     UniqueVector<const Toolchain*> toolchain_matches;
     UniqueVector<SourceFile> file_matches;
-    if (!ResolveFromCommandLineInput(setup, inputs, false, &target_matches,
-                                     &config_matches, &toolchain_matches,
-                                     &file_matches))
+    if (!ResolveFromCommandLineInput(setup, inputs, default_toolchain_only,
+                                     &target_matches, &config_matches,
+                                     &toolchain_matches, &file_matches))
       return 1;
 
     if (target_matches.size() == 0) {
@@ -226,11 +231,10 @@
     }
   }
 
-  const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
   bool force = cmdline->HasSwitch("force");
   bool check_generated = cmdline->HasSwitch("check-generated");
-  bool check_system = setup->check_system_includes() ||
-                      cmdline->HasSwitch("check-system");
+  bool check_system =
+      setup->check_system_includes() || cmdline->HasSwitch("check-system");
 
   if (!CheckPublicHeaders(&setup->build_settings(), all_targets,
                           targets_to_check, force, check_generated,
@@ -254,12 +258,13 @@
 bool CheckPublicHeaders(const BuildSettings* build_settings,
                         const std::vector<const Target*>& all_targets,
                         const std::vector<const Target*>& to_check,
-                        bool force_check, bool check_generated,
+                        bool force_check,
+                        bool check_generated,
                         bool check_system) {
   ScopedTrace trace(TraceItem::TRACE_CHECK_HEADERS, "Check headers");
 
-  scoped_refptr<HeaderChecker> header_checker(
-      new HeaderChecker(build_settings, all_targets, check_generated, check_system));
+  scoped_refptr<HeaderChecker> header_checker(new HeaderChecker(
+      build_settings, all_targets, check_generated, check_system));
 
   std::vector<Err> header_errors;
   header_checker->Run(to_check, force_check, &header_errors);
diff --git a/src/gn/command_desc.cc b/src/gn/command_desc.cc
index 086d5f1..081ae83 100644
--- a/src/gn/command_desc.cc
+++ b/src/gn/command_desc.cc
@@ -519,9 +519,10 @@
       "--blame" to see the source of the dependency.
 
 Shared flags
+
 )"
 
-    ALL_TOOLCHAINS_SWITCH_HELP
+    DEFAULT_TOOLCHAIN_SWITCH_HELP
 
     R"(
   --format=json
@@ -557,6 +558,7 @@
   --all
       Collects all recursive dependencies and prints a sorted flat list. Also
       usable with --tree (see below).
+
 )"
 
     TARGET_PRINTING_MODE_COMMAND_LINE_HELP
@@ -572,11 +574,13 @@
 
       Tree output can not be used with the filtering or output flags: --as,
       --type, --testonly.
+
 )"
 
     TARGET_TYPE_FILTER_COMMAND_LINE_HELP
 
-    R"(Note
+    R"(
+Note
 
   This command will show the full name of directories and source files, but
   when directories and source paths are written to the build file, they will be
@@ -623,7 +627,7 @@
   target_list.push_back(args[1]);
 
   if (!ResolveFromCommandLineInput(
-          setup, target_list, cmdline->HasSwitch(switches::kAllToolchains),
+          setup, target_list, cmdline->HasSwitch(switches::kDefaultToolchain),
           &target_matches, &config_matches, &toolchain_matches, &file_matches))
     return 1;
 
diff --git a/src/gn/command_ls.cc b/src/gn/command_ls.cc
index e95730f..cfd7b0f 100644
--- a/src/gn/command_ls.cc
+++ b/src/gn/command_ls.cc
@@ -18,7 +18,7 @@
 const char kLs[] = "ls";
 const char kLs_HelpShort[] = "ls: List matching targets.";
 const char kLs_Help[] =
-    R"(gn ls <out_dir> [<label_pattern>] [--all-toolchains] [--as=...]
+    R"(gn ls <out_dir> [<label_pattern>] [--default-toolchain] [--as=...]
       [--type=...] [--testonly=...]
 
   Lists all targets matching the given pattern for the given build directory.
@@ -31,7 +31,7 @@
 
 Options
 
-)" TARGET_PRINTING_MODE_COMMAND_LINE_HELP "\n" ALL_TOOLCHAINS_SWITCH_HELP
+)" TARGET_PRINTING_MODE_COMMAND_LINE_HELP "\n" DEFAULT_TOOLCHAIN_SWITCH_HELP
     "\n" TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
     "\n" TARGET_TYPE_FILTER_COMMAND_LINE_HELP
     R"(
@@ -54,10 +54,6 @@
 
   gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
       Builds all targets in //base and all subdirectories.
-
-  gn ls out/Debug //base --all-toolchains
-      Lists all variants of the target //base:base (it may be referenced
-      in multiple toolchains).
 )";
 
 int RunLs(const std::vector<std::string>& args) {
@@ -74,7 +70,7 @@
     return 1;
 
   const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
-  bool all_toolchains = cmdline->HasSwitch(switches::kAllToolchains);
+  bool default_toolchain_only = cmdline->HasSwitch(switches::kDefaultToolchain);
 
   std::vector<const Target*> matches;
   if (args.size() > 1) {
@@ -85,21 +81,21 @@
     UniqueVector<const Config*> config_matches;
     UniqueVector<const Toolchain*> toolchain_matches;
     UniqueVector<SourceFile> file_matches;
-    if (!ResolveFromCommandLineInput(setup, inputs, all_toolchains,
+    if (!ResolveFromCommandLineInput(setup, inputs, default_toolchain_only,
                                      &target_matches, &config_matches,
                                      &toolchain_matches, &file_matches))
       return 1;
     matches.insert(matches.begin(), target_matches.begin(),
                    target_matches.end());
-  } else if (all_toolchains) {
-    // List all resolved targets.
-    matches = setup->builder().GetAllResolvedTargets();
-  } else {
+  } else if (default_toolchain_only) {
     // List all resolved targets in the default toolchain.
     for (auto* target : setup->builder().GetAllResolvedTargets()) {
       if (target->settings()->is_default())
         matches.push_back(target);
     }
+  } else {
+    // List all resolved targets.
+    matches = setup->builder().GetAllResolvedTargets();
   }
   FilterAndPrintTargets(false, &matches);
   return 0;
diff --git a/src/gn/command_outputs.cc b/src/gn/command_outputs.cc
index 376405b..9360ead 100644
--- a/src/gn/command_outputs.cc
+++ b/src/gn/command_outputs.cc
@@ -87,7 +87,7 @@
   UniqueVector<const Config*> config_matches;
   UniqueVector<const Toolchain*> toolchain_matches;
   UniqueVector<SourceFile> file_matches;
-  if (!ResolveFromCommandLineInput(setup, inputs, true, &target_matches,
+  if (!ResolveFromCommandLineInput(setup, inputs, false, &target_matches,
                                    &config_matches, &toolchain_matches,
                                    &file_matches))
     return 1;
@@ -106,7 +106,7 @@
       setup->builder().GetAllResolvedTargets();
   for (const SourceFile& file : file_matches) {
     std::vector<TargetContainingFile> targets;
-    GetTargetsContainingFile(setup, all_targets, file, true, &targets);
+    GetTargetsContainingFile(setup, all_targets, file, false, &targets);
     if (targets.empty()) {
       Err(Location(), base::StringPrintf("No targets reference the file '%s'.",
                                          file.value().c_str()))
diff --git a/src/gn/command_refs.cc b/src/gn/command_refs.cc
index 4f9ba05..c30d4c4 100644
--- a/src/gn/command_refs.cc
+++ b/src/gn/command_refs.cc
@@ -144,11 +144,11 @@
 void GetTargetsReferencingConfig(Setup* setup,
                                  const std::vector<const Target*>& all_targets,
                                  const Config* config,
-                                 bool all_toolchains,
+                                 bool default_toolchain_only,
                                  UniqueVector<const Target*>* matches) {
   Label default_toolchain = setup->loader()->default_toolchain_label();
   for (auto* target : all_targets) {
-    if (!all_toolchains) {
+    if (default_toolchain_only) {
       // Only check targets in the default toolchain.
       if (target->label().GetToolchainLabel() != default_toolchain)
         continue;
@@ -238,7 +238,7 @@
     R"(gn refs
 
   gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)*
-          [--all] [--all-toolchains] [--as=...] [--testonly=...] [--type=...]
+          [--all] [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
 
   Finds reverse dependencies (which targets reference something). The input is
   a list containing:
@@ -272,15 +272,17 @@
       directly or indirectly on that file.
 
       When used with --tree, turns off eliding to show a complete tree.
+
 )"
 
-    ALL_TOOLCHAINS_SWITCH_HELP "\n" TARGET_PRINTING_MODE_COMMAND_LINE_HELP
+    TARGET_PRINTING_MODE_COMMAND_LINE_HELP "\n" DEFAULT_TOOLCHAIN_SWITCH_HELP
 
     R"(
   -q
      Quiet. If nothing matches, don't print any output. Without this option, if
      there are no matches there will be an informational message printed which
      might interfere with scripts processing the output.
+
 )"
 
     TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP
@@ -292,6 +294,7 @@
 
       Tree output can not be used with the filtering or output flags: --as,
       --type, --testonly.
+
 )"
 
     TARGET_TYPE_FILTER_COMMAND_LINE_HELP
@@ -350,7 +353,7 @@
   const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
   bool tree = cmdline->HasSwitch("tree");
   bool all = cmdline->HasSwitch("all");
-  bool all_toolchains = cmdline->HasSwitch(switches::kAllToolchains);
+  bool default_toolchain_only = cmdline->HasSwitch(switches::kDefaultToolchain);
 
   // Deliberately leaked to avoid expensive process teardown.
   Setup* setup = new Setup;
@@ -386,7 +389,7 @@
   UniqueVector<const Config*> config_matches;
   UniqueVector<const Toolchain*> toolchain_matches;
   UniqueVector<SourceFile> file_matches;
-  if (!ResolveFromCommandLineInput(setup, inputs, all_toolchains,
+  if (!ResolveFromCommandLineInput(setup, inputs, default_toolchain_only,
                                    &target_matches, &config_matches,
                                    &toolchain_matches, &file_matches))
     return 1;
@@ -401,7 +404,7 @@
   UniqueVector<const Target*> explicit_target_matches;
   for (const auto& file : file_matches) {
     std::vector<TargetContainingFile> target_containing;
-    GetTargetsContainingFile(setup, all_targets, file, all_toolchains,
+    GetTargetsContainingFile(setup, all_targets, file, default_toolchain_only,
                              &target_containing);
 
     // Extract just the Target*.
@@ -409,7 +412,8 @@
       explicit_target_matches.push_back(pair.first);
   }
   for (auto* config : config_matches) {
-    GetTargetsReferencingConfig(setup, all_targets, config, all_toolchains,
+    GetTargetsReferencingConfig(setup, all_targets, config,
+                                default_toolchain_only,
                                 &explicit_target_matches);
   }
 
diff --git a/src/gn/commands.cc b/src/gn/commands.cc
index 1941cba..431f8d2 100644
--- a/src/gn/commands.cc
+++ b/src/gn/commands.cc
@@ -31,11 +31,12 @@
 // returns false. If the pattern is valid, fills the vector (which might be
 // empty if there are no matches) and returns true.
 //
-// If all_toolchains is false, a pattern with an unspecified toolchain will
-// match the default toolchain only. If true, all toolchains will be matched.
+// If default_toolchain_only is true, a pattern with an unspecified toolchain
+// will match the default toolchain only. If true, all toolchains will be
+// matched.
 bool ResolveTargetsFromCommandLinePattern(Setup* setup,
                                           const std::string& label_pattern,
-                                          bool all_toolchains,
+                                          bool default_toolchain_only,
                                           std::vector<const Target*>* matches) {
   Value pattern_value(nullptr, label_pattern);
 
@@ -48,7 +49,7 @@
     return false;
   }
 
-  if (!all_toolchains) {
+  if (default_toolchain_only) {
     // By default a pattern with an empty toolchain will match all toolchains.
     // If the caller wants to default to the main toolchain only, set it
     // explicitly.
@@ -70,7 +71,7 @@
     Setup* setup,
     const SourceDir& current_dir,
     const std::string& input,
-    bool all_toolchains,
+    bool default_toolchain_only,
     UniqueVector<const Target*>* target_matches,
     UniqueVector<const Config*>* config_matches,
     UniqueVector<const Toolchain*>* toolchain_matches,
@@ -80,8 +81,8 @@
     // future to allow the user to specify which types of things they want to
     // match, but it should probably only match targets by default.
     std::vector<const Target*> target_match_vector;
-    if (!ResolveTargetsFromCommandLinePattern(setup, input, all_toolchains,
-                                              &target_match_vector))
+    if (!ResolveTargetsFromCommandLinePattern(
+            setup, input, default_toolchain_only, &target_match_vector))
       return false;
     for (const Target* target : target_match_vector)
       target_matches->push_back(target);
@@ -485,7 +486,7 @@
 bool ResolveFromCommandLineInput(
     Setup* setup,
     const std::vector<std::string>& input,
-    bool all_toolchains,
+    bool default_toolchain_only,
     UniqueVector<const Target*>* target_matches,
     UniqueVector<const Config*>* config_matches,
     UniqueVector<const Toolchain*>* toolchain_matches,
@@ -499,9 +500,9 @@
   SourceDir cur_dir =
       SourceDirForCurrentDirectory(setup->build_settings().root_path());
   for (const auto& cur : input) {
-    if (!ResolveStringFromCommandLineInput(setup, cur_dir, cur, all_toolchains,
-                                           target_matches, config_matches,
-                                           toolchain_matches, file_matches))
+    if (!ResolveStringFromCommandLineInput(
+            setup, cur_dir, cur, default_toolchain_only, target_matches,
+            config_matches, toolchain_matches, file_matches))
       return false;
   }
   return true;
@@ -608,11 +609,11 @@
 void GetTargetsContainingFile(Setup* setup,
                               const std::vector<const Target*>& all_targets,
                               const SourceFile& file,
-                              bool all_toolchains,
+                              bool default_toolchain_only,
                               std::vector<TargetContainingFile>* matches) {
   Label default_toolchain = setup->loader()->default_toolchain_label();
   for (auto* target : all_targets) {
-    if (!all_toolchains) {
+    if (default_toolchain_only) {
       // Only check targets in the default toolchain.
       if (target->label().GetToolchainLabel() != default_toolchain)
         continue;
diff --git a/src/gn/commands.h b/src/gn/commands.h
index a3e6e42..41b99bc 100644
--- a/src/gn/commands.h
+++ b/src/gn/commands.h
@@ -132,7 +132,7 @@
 bool ResolveFromCommandLineInput(
     Setup* setup,
     const std::vector<std::string>& input,
-    bool all_toolchains,
+    bool default_toolchain_only,
     UniqueVector<const Target*>* target_matches,
     UniqueVector<const Config*>* config_matches,
     UniqueVector<const Toolchain*>* toolchain_matches,
@@ -226,7 +226,7 @@
 void GetTargetsContainingFile(Setup* setup,
                               const std::vector<const Target*>& all_targets,
                               const SourceFile& file,
-                              bool all_toolchains,
+                              bool default_toolchain_only,
                               std::vector<TargetContainingFile>* matches);
 
 // Extra help from command_check.cc
diff --git a/src/gn/switches.cc b/src/gn/switches.cc
index dea73c6..e7b3a81 100644
--- a/src/gn/switches.cc
+++ b/src/gn/switches.cc
@@ -273,7 +273,7 @@
 // immediately if this switch is used.
 const char kVersion_Help[] = "";
 
-const char kAllToolchains[] = "all-toolchains";
+const char kDefaultToolchain[] = "default-toolchain";
 
 // -----------------------------------------------------------------------------
 
diff --git a/src/gn/switches.h b/src/gn/switches.h
index 900104a..69c47b6 100644
--- a/src/gn/switches.h
+++ b/src/gn/switches.h
@@ -101,16 +101,15 @@
 // This switch is used by several commands. It is here so it can be shared,
 // but it's documented in the individual commands it applies to rather than
 // globally.
-extern const char kAllToolchains[];
-#define ALL_TOOLCHAINS_SWITCH_HELP                                             \
-  "  --all-toolchains\n"                                                       \
-  "      Normally only inputs in the default toolchain will be included.\n"    \
-  "      This switch will turn on matching all toolchains.\n"                  \
-  "\n"                                                                         \
-  "      For example, a file is in a target might be compiled twice:\n"        \
-  "      once in the default toolchain and once in a secondary one. Without\n" \
-  "      this flag, only the default toolchain one will be matched by\n"       \
-  "      wildcards. With this flag, both will be matched.\n"
+extern const char kDefaultToolchain[];
+#define DEFAULT_TOOLCHAIN_SWITCH_HELP                                         \
+  "  --default-toolchain\n"                                                   \
+  "      Normally wildcard targets are matched in all toolchains. This\n"     \
+  "      switch makes wildcard labels with no explicit toolchain reference\n" \
+  "      only match targets in the default toolchain.\n"                      \
+  "\n"                                                                        \
+  "      Non-wildcard inputs with no explicit toolchain specification will\n" \
+  "      always match only a target in the default toolchain if one exists.\n"
 
 }  // namespace switches