Add a target in GN that generates a last_change.h file for the SVN revision.

This is GN-only so the GYP build currently sets this to 0.

This adds the root generated file directory to all targets, so you can do #include "foo/bar.h" and get a generated file "bar.h" generated in directory "foo". This basically matches the internal Google build, and I use it here.

BUG=
R=viettrungluu@chromium.org

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

Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 9af0b83e7ce94d9e5c61623ab76634f97374c517
diff --git a/tools/gn/BUILD.gn b/tools/gn/BUILD.gn
index 6195ff3..c5cc1cb 100644
--- a/tools/gn/BUILD.gn
+++ b/tools/gn/BUILD.gn
@@ -4,6 +4,10 @@
 
 gyp_file = "gn.gyp"
 
+# Not defined when doing a pure GYP build, this lets the code key off of
+# GN-specific features (the last change target).
+defines = [ "GN_BUILD" ]
+
 static_library("gn_lib") {
   sources = [
     "args.cc",
@@ -151,9 +155,11 @@
     "variables.cc",
     "variables.h",
   ]
+
   deps = [
     "//base",
     "//base/third_party/dynamic_annotations",
+    "//build/util:last_change",
   ]
 }
 
@@ -161,6 +167,7 @@
   sources = [
     "gn_main.cc",
   ]
+
   deps = [
     ":gn_lib",
   ]
diff --git a/tools/gn/command_help.cc b/tools/gn/command_help.cc
index 954db12..8f8611e 100644
--- a/tools/gn/command_help.cc
+++ b/tools/gn/command_help.cc
@@ -51,6 +51,8 @@
       "--tracelog: Writes a Chrome-compatible trace log to the given file.");
   PrintShortHelp(
       "-v: Verbose mode, print lots of logging.");
+  PrintShortHelp(
+      "--version: Print the GN binary's version and exit.");
 
   // Functions.
   OutputString("\nBuildfile functions (type \"gn help <function>\" for more "
diff --git a/tools/gn/gn_main.cc b/tools/gn/gn_main.cc
index 9c86ab5..bfd5010 100644
--- a/tools/gn/gn_main.cc
+++ b/tools/gn/gn_main.cc
@@ -8,6 +8,15 @@
 #include "tools/gn/commands.h"
 #include "tools/gn/err.h"
 #include "tools/gn/location.h"
+#include "tools/gn/standard_out.h"
+
+// Only the GN-generated build makes this header for now.
+// TODO(brettw) consider adding this if we need it in GYP.
+#if defined(GN_BUILD)
+#include "build/util/last_change.h"
+#else
+#define LAST_CHANGE "UNKNOWN"
+#endif
 
 namespace {
 
@@ -39,6 +48,10 @@
   if (cmdline.HasSwitch("help")) {
     // Make "--help" default to help command.
     command = commands::kHelp;
+  } else if (cmdline.HasSwitch("version")) {
+    // Make "--version" print the version and exit.
+    OutputString(std::string(LAST_CHANGE) + "\n");
+    exit(0);
   } else if (args.empty()) {
     command = commands::kGen;
   } else {
diff --git a/tools/gn/secondary/build/config/compiler/BUILD.gn b/tools/gn/secondary/build/config/compiler/BUILD.gn
index ef9d61f..20ffbb5 100644
--- a/tools/gn/secondary/build/config/compiler/BUILD.gn
+++ b/tools/gn/secondary/build/config/compiler/BUILD.gn
@@ -4,7 +4,7 @@
 
 # Base compiler configuration.
 config("compiler") {
-  include_dirs = [ "//" ]
+  include_dirs = [ "//", root_gen_dir ]
   if (is_win) {
     cflags = [
       "/Gy",  # Enable function-level linking.
diff --git a/tools/gn/secondary/build/util/BUILD.gn b/tools/gn/secondary/build/util/BUILD.gn
new file mode 100644
index 0000000..d43cc49
--- /dev/null
+++ b/tools/gn/secondary/build/util/BUILD.gn
@@ -0,0 +1,35 @@
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.

+# Use of this source code is governed by a BSD-style license that can be

+# found in the LICENSE file.

+

+# This target generates a "last_change.h" header file in the generated files

+# directory that contains a define of the last revision of the source tree

+# of the form:

+#  #define LAST_CHANGE "123456"

+#

+# The version is a string rather than an integer for extra flexibility (for

+# example, we may require git hashes in the future).

+#

+# All you nede to do is depend on this target, and then from your source code:

+#  #include "build/util/last_change.h"

+custom("last_change") {

+  script = "//build/util/lastchange.py"

+

+  # This script must be run before targets depending on us.

+  hard_dep = true

+

+  # Rerun the script any time this file changes.

+  source_prereqs = [ "//build/util/LASTCHANGE" ]

+

+  output_header = "$target_gen_dir/last_change.h"

+  outputs = [ output_header ]

+

+  build_relative_src = rebase_path("//", ".", root_build_dir)

+  build_relative_outputs = rebase_path(output_header, ".", root_build_dir)

+

+  args = [

+    "--source-dir=$build_relative_src",

+    "--header=$build_relative_outputs",

+    "--version-macro=LAST_CHANGE",

+  ]

+}