Use $root:default as a "default" rule

When the root build file is changed in .gn file, we should check
$root:default rather than //:default when looking for "default" rule.

Review-Url: https://codereview.chromium.org/2824153002
Cr-Original-Commit-Position: refs/heads/master@{#465758}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: ddb3babce43a642e5f4e985e2a69c741057aad3c
diff --git a/tools/gn/build_settings.cc b/tools/gn/build_settings.cc
index c60002d..4cf2b86 100644
--- a/tools/gn/build_settings.cc
+++ b/tools/gn/build_settings.cc
@@ -25,6 +25,10 @@
 BuildSettings::~BuildSettings() {
 }
 
+void BuildSettings::SetRootTargetLabel(const Label& r) {
+  root_target_label_ = r;
+}
+
 void BuildSettings::SetRootPath(const base::FilePath& r) {
   DCHECK(r.value()[r.value().size() - 1] != base::FilePath::kSeparators[0]);
   root_path_ = r.NormalizePathSeparatorsTo('/');
diff --git a/tools/gn/build_settings.h b/tools/gn/build_settings.h
index 3f9f45b..3f41ae3 100644
--- a/tools/gn/build_settings.h
+++ b/tools/gn/build_settings.h
@@ -14,6 +14,7 @@
 #include "base/files/file_path.h"
 #include "base/macros.h"
 #include "tools/gn/args.h"
+#include "tools/gn/label.h"
 #include "tools/gn/scope.h"
 #include "tools/gn/source_dir.h"
 #include "tools/gn/source_file.h"
@@ -31,6 +32,10 @@
   BuildSettings(const BuildSettings& other);
   ~BuildSettings();
 
+  // Root target label.
+  const Label& root_target_label() const { return root_target_label_; }
+  void SetRootTargetLabel(const Label& r);
+
   // Absolute path of the source root on the local system. Everything is
   // relative to this. Does not end in a [back]slash.
   const base::FilePath& root_path() const { return root_path_; }
@@ -104,6 +109,7 @@
   }
 
  private:
+  Label root_target_label_;
   base::FilePath root_path_;
   std::string root_path_utf8_;
   base::FilePath secondary_source_path_;
diff --git a/tools/gn/docs/reference.md b/tools/gn/docs/reference.md
index 9c318b8..4b88f55 100644
--- a/tools/gn/docs/reference.md
+++ b/tools/gn/docs/reference.md
@@ -5861,8 +5861,9 @@
   All generated targets (see "gn help execution") will be added to an implicit
   build rule called "all" so "ninja all" will always compile everything. The
   default rule will be used by Ninja if no specific target is specified (just
-  typing "ninja"). If there is a target named "//:default" it will be the
-  default build rule, otherwise the implicit "all" rule will be used.
+  typing "ninja"). If there is a target named "default" in the root build file,
+  it will be the default build rule, otherwise the implicit "all" rule will be
+  used.
 ```
 
 #### **Phony rules**
diff --git a/tools/gn/ninja_build_writer.cc b/tools/gn/ninja_build_writer.cc
index 2f2336c..f3e9aaf 100644
--- a/tools/gn/ninja_build_writer.cc
+++ b/tools/gn/ninja_build_writer.cc
@@ -349,8 +349,9 @@
   All generated targets (see "gn help execution") will be added to an implicit
   build rule called "all" so "ninja all" will always compile everything. The
   default rule will be used by Ninja if no specific target is specified (just
-  typing "ninja"). If there is a target named "//:default" it will be the
-  default build rule, otherwise the implicit "all" rule will be used.
+  typing "ninja"). If there is a target named "default" in the root build file,
+  it will be the default build rule, otherwise the implicit "all" rule will be
+  used.
 
 Phony rules
 
@@ -396,7 +397,7 @@
   written_rules.insert("all");
 
   // Set if we encounter a target named "//:default".
-  bool default_target_exists = false;
+  const Target* default_target = nullptr;
 
   // Targets in the root build file.
   std::vector<const Target*> toplevel_targets;
@@ -418,8 +419,9 @@
     const Label& label = target->label();
     const std::string& short_name = label.name();
 
-    if (label.dir().value() == "//" && label.name() == "default")
-      default_target_exists = true;
+    if (label.dir() == build_settings_->root_target_label().dir() &&
+        short_name == "default")
+      default_target = target;
 
     // Count the number of targets with the given short name.
     Counts& short_names_counts = short_names[short_name];
@@ -537,10 +539,18 @@
   }
   out_ << std::endl;
 
-  if (default_target_exists)
-    out_ << "\ndefault default" << std::endl;
-  else if (!default_toolchain_targets_.empty())
+  if (default_target) {
+    // Use the short name when available
+    if (written_rules.find("default") != written_rules.end()) {
+      out_ << "\ndefault default" << std::endl;
+    } else {
+      out_ << "\ndefault ";
+      path_output_.WriteFile(out_, default_target->dependency_output_file());
+      out_ << std::endl;
+    }
+  } else if (!default_toolchain_targets_.empty()) {
     out_ << "\ndefault all" << std::endl;
+  }
 
   return true;
 }
diff --git a/tools/gn/setup.cc b/tools/gn/setup.cc
index 2c2ba38..22f4fce 100644
--- a/tools/gn/setup.cc
+++ b/tools/gn/setup.cc
@@ -698,6 +698,7 @@
 bool Setup::FillOtherConfig(const base::CommandLine& cmdline) {
   Err err;
   SourceDir current_dir("//");
+  Label root_target_label(current_dir, "");
 
   // Secondary source path, read from the config file if present.
   // Read from the config file if present.
@@ -720,8 +721,7 @@
       return false;
     }
 
-    Label root_target_label =
-        Label::Resolve(current_dir, Label(), *root_value, &err);
+    root_target_label = Label::Resolve(current_dir, Label(), *root_value, &err);
     if (err.has_error()) {
       err.PrintToStdout();
       return false;
@@ -729,6 +729,7 @@
 
     root_build_file_ = Loader::BuildFileForLabel(root_target_label);
   }
+  build_settings_.SetRootTargetLabel(root_target_label);
 
   // Build config file.
   const Value* build_config_value =