Auto re-gen for gn cmake generator.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3028

Change-Id: I73c6ce9e852c280bbb2017c30717809b4cb44046
Reviewed-on: https://skia-review.googlesource.com/3028
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
diff --git a/gn/gn_to_cmake.py b/gn/gn_to_cmake.py
index 30a039a..86906f5 100644
--- a/gn/gn_to_cmake.py
+++ b/gn/gn_to_cmake.py
@@ -15,6 +15,8 @@
 
 gn gen out/config --ide=json
 python gn/gn_to_cmake.py out/config/project.json
+
+The first is recommended, as it will auto-update.
 """
 
 
@@ -606,14 +608,49 @@
   out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+')
   out.write('# Generated by gn_to_cmake.py.\n')
   out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
+  out.write('cmake_policy(VERSION 2.8.8)\n\n')
+
+  # Update the gn generated ninja build.
+  # If a build file has changed, this will update CMakeLists.ext if
+  # gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
+  # style was used to create this config.
+  out.write('execute_process(COMMAND ninja -C "')
+  out.write(CMakeStringEscape(project.build_path))
+  out.write('" build.ninja)\n')
+
+  out.write('include(CMakeLists.ext)\n')
+  out.close()
+
+  out = open(posixpath.join(project.build_path, 'CMakeLists.ext'), 'w+')
+  out.write('# Generated by gn_to_cmake.py.\n')
+  out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
   out.write('cmake_policy(VERSION 2.8.8)\n')
 
   # The following appears to be as-yet undocumented.
   # http://public.kitware.com/Bug/view.php?id=8392
-  out.write('enable_language(ASM)\n')
+  out.write('enable_language(ASM)\n\n')
   # ASM-ATT does not support .S files.
   # output.write('enable_language(ASM-ATT)\n')
 
+  # Current issues with automatic re-generation:
+  # The gn generated build.ninja target uses build.ninja.d
+  #   but build.ninja.d does not contain the ide or gn.
+  # Currently the ide is not run if the project.json file is not changed
+  #   but the ide needs to be run anyway if it has itself changed.
+  #   This can be worked around by deleting the project.json file.
+  out.write('file(READ "')
+  gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d')
+  out.write(CMakeStringEscape(gn_deps_file))
+  out.write('" "gn_deps_string" OFFSET ')
+  out.write(str(len('build.ninja: ')))
+  out.write(')\n')
+  # One would think this would need to worry about escaped spaces
+  # but gn doesn't escape spaces here (it generates invalid .d files).
+  out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n')
+  out.write('foreach("gn_dep" ${gn_deps})\n')
+  out.write('  configure_file(${gn_dep} "CMakeLists.devnull" COPYONLY)\n')
+  out.write('endforeach("gn_dep")\n')
+
   for target_name in project.targets.keys():
     out.write('\n')
     WriteTarget(out, Target(target_name, project), project)