Do not generate last_commit_position.h in debug builds.

Whenever a commit is created or squashed in git/jj, the commit hash of
HEAD changes, which previously updated last_commit_position.h during
gen.py and forced a rebuild of gn_main.cc and relinking of gn.

In debug mode, generate a dummy last_commit_position.h instead so that
squashes do not invalidate the build cache. Real commit positions
continue to be generated for release builds.

Change-Id: Ib9e2fac8c1ec74f04fb66f382629e1dc6a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/27280
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Matt Stark <msta@google.com>
diff --git a/build/gen.py b/build/gen.py
index 4f9cd32..5c49973 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -236,9 +236,13 @@
   out_dir = options.out_path
   if not os.path.isdir(out_dir):
     os.makedirs(out_dir)
-  if not options.no_last_commit_position:
-    GenerateLastCommitPosition(host,
-                               os.path.join(out_dir, 'last_commit_position.h'))
+  GenerateLastCommitPosition(
+    host,
+    os.path.join(out_dir, 'last_commit_position.h'),
+    # Don't generate last commit position for debug builds to ensure that `jj/git squash`
+    # does not invalidate the cache.
+    options.no_last_commit_position or options.debug,
+  )
   WriteGNNinja(os.path.join(out_dir, 'build.ninja'), platform, host, options, args_list)
   return 0
 
@@ -254,9 +258,9 @@
 
   return ret.returncode == 0 and "#define __GNUC__" in ret.stdout and not "#define __clang__" in ret.stdout
 
-def GenerateLastCommitPosition(host, header):
+def GenerateLastCommitPosition(host, header, generate_dummy):
   ROOT_TAG = 'initial-commit'
-  if os.path.isdir(os.path.join(REPO_ROOT, '.git')):
+  if not generate_dummy and os.path.isdir(os.path.join(REPO_ROOT, '.git')):
     describe_output = subprocess.check_output(
         ['git', 'describe', 'HEAD', '--abbrev=12', '--match', ROOT_TAG],
         shell=host.is_windows(), cwd=REPO_ROOT)