Support spaces in Mac GN build output names.

We can't use object paths with spaces in them due to a mismatch between Ninja and the Mac LD's filelist parameter (explained in more detail in a comment added in the mac toolchain GN file). In order to support output_names with spaces, the object file directory must be based on something else.

Adds a new substitution "{{target_label}}" to the tool that expands to the target name after the colon, not overridden by the output_name. This makes more sense from a directory structure perspective, and label names won't have spaces (they can but don't, and with this change won't work on Mac).

Also adds quoting for Mac tool commands so spaces will work in names.

I made the change in the Mac toolchain definition for testing, but it is currently commented out pending the binary roll. After the binary roll, we should uncomment this and update the other toolchains to match.

BUG=546894

Review URL: https://codereview.chromium.org/1436563003

Cr-Original-Commit-Position: refs/heads/master@{#358792}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 782b1bc8562e9adbb1ea7d98929d6709ff7290b9
diff --git a/tools/gn/function_toolchain.cc b/tools/gn/function_toolchain.cc
index 7068370..ed4bbc3 100644
--- a/tools/gn/function_toolchain.cc
+++ b/tools/gn/function_toolchain.cc
@@ -595,6 +595,13 @@
     "        omitted from the label for targets in the default toolchain, and\n"
     "        will be included for targets in other toolchains.\n"
     "\n"
+    "    {{label_name}}\n"
+    "        The short name of the label of the target. This is the part\n"
+    "        after the colon. For \"//foo/bar:baz\" this will be \"baz\".\n"
+    "        Unlike {{target_output_name}}, this is not affected by the\n"
+    "        \"output_prefix\" in the tool or the \"output_name\" set\n"
+    "        on the target.\n"
+    "\n"
     "    {{output}}\n"
     "        The relative path and name of the output(s) of the current\n"
     "        build step. If there is more than one output, this will expand\n"
@@ -612,6 +619,7 @@
     "        The short name of the current target with no path information,\n"
     "        or the value of the \"output_name\" variable if one is specified\n"
     "        in the target. This will include the \"output_prefix\" if any.\n"
+    "        See also {{label_name}}.\n"
     "        Example: \"libfoo\" for the target named \"foo\" and an\n"
     "        output prefix for the linker tool of \"lib\".\n"
     "\n"
diff --git a/tools/gn/ninja_target_writer.cc b/tools/gn/ninja_target_writer.cc
index cfc207c..7641307 100644
--- a/tools/gn/ninja_target_writer.cc
+++ b/tools/gn/ninja_target_writer.cc
@@ -103,6 +103,12 @@
     written_anything = true;
   }
 
+  // Target label name
+  if (bits.used[SUBSTITUTION_LABEL_NAME]) {
+    WriteEscapedSubstitution(SUBSTITUTION_LABEL_NAME);
+    written_anything = true;
+  }
+
   // Root gen dir.
   if (bits.used[SUBSTITUTION_ROOT_GEN_DIR]) {
     WriteEscapedSubstitution(SUBSTITUTION_ROOT_GEN_DIR);
diff --git a/tools/gn/substitution_type.cc b/tools/gn/substitution_type.cc
index f67df26..c185ae1 100644
--- a/tools/gn/substitution_type.cc
+++ b/tools/gn/substitution_type.cc
@@ -22,6 +22,7 @@
   "{{source_out_dir}}",  // SUBSTITUTION_SOURCE_OUT_DIR
 
   "{{label}}",  // SUBSTITUTION_LABEL
+  "{{label_name}}",  // SUBSTITUTION_LABEL_NAME
   "{{root_gen_dir}}",  // SUBSTITUTION_ROOT_GEN_DIR
   "{{root_out_dir}}",  // SUBSTITUTION_ROOT_OUT_DIR
   "{{target_gen_dir}}",  // SUBSTITUTION_TARGET_GEN_DIR
@@ -59,6 +60,7 @@
     "source_out_dir",            // SUBSTITUTION_SOURCE_OUT_DIR
 
     "label",               // SUBSTITUTION_LABEL
+    "label_name",          // SUBSTITUTION_LABEL_NAME
     "root_gen_dir",        // SUBSTITUTION_ROOT_GEN_DIR
     "root_out_dir",        // SUBSTITUTION_ROOT_OUT_DIR
     "target_gen_dir",      // SUBSTITUTION_TARGET_GEN_DIR
@@ -124,6 +126,7 @@
   return type == SUBSTITUTION_LITERAL ||
          type == SUBSTITUTION_OUTPUT ||
          type == SUBSTITUTION_LABEL ||
+         type == SUBSTITUTION_LABEL_NAME ||
          type == SUBSTITUTION_ROOT_GEN_DIR ||
          type == SUBSTITUTION_ROOT_OUT_DIR ||
          type == SUBSTITUTION_TARGET_GEN_DIR ||
diff --git a/tools/gn/substitution_type.h b/tools/gn/substitution_type.h
index 42040c9..7d7ea12 100644
--- a/tools/gn/substitution_type.h
+++ b/tools/gn/substitution_type.h
@@ -34,6 +34,7 @@
   // Valid for all compiler and linker tools. These depend on the target and
   // no not vary on a per-file basis.
   SUBSTITUTION_LABEL,  // {{label}}
+  SUBSTITUTION_LABEL_NAME,  // {{label_name}}
   SUBSTITUTION_ROOT_GEN_DIR,  // {{root_gen_dir}}
   SUBSTITUTION_ROOT_OUT_DIR,  // {{root_out_dir}}
   SUBSTITUTION_TARGET_GEN_DIR,  // {{target_gen_dir}}
diff --git a/tools/gn/substitution_writer.cc b/tools/gn/substitution_writer.cc
index a642e47..0351eb1 100644
--- a/tools/gn/substitution_writer.cc
+++ b/tools/gn/substitution_writer.cc
@@ -422,6 +422,9 @@
       *result = target->label().GetUserVisibleName(
           !target->settings()->is_default());
       break;
+    case SUBSTITUTION_LABEL_NAME:
+      *result = target->label().name();
+      break;
     case SUBSTITUTION_ROOT_GEN_DIR:
       SetDirOrDotWithNoSlash(
           GetToolchainGenDirAsOutputFile(target->settings()).value(),
diff --git a/tools/gn/substitution_writer_unittest.cc b/tools/gn/substitution_writer_unittest.cc
index 6ae28ec..5abd619 100644
--- a/tools/gn/substitution_writer_unittest.cc
+++ b/tools/gn/substitution_writer_unittest.cc
@@ -193,6 +193,10 @@
   EXPECT_EQ("//foo/bar:baz", result);
 
   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
+      &target, SUBSTITUTION_LABEL_NAME, &result));
+  EXPECT_EQ("baz", result);
+
+  EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
       &target, SUBSTITUTION_ROOT_GEN_DIR, &result));
   EXPECT_EQ("gen", result);