Always use commit_id.py to generate commit.h

commit_id_.py is capable of generating the default commit.h.
This makes it so we always take a single path in gn.

Remove the existing commit.h and generate it into the root
generation folder (not the id subfolder) because Android blueprints
can't handle generating into subfolders that don't exist.

Make the <angle_dir> argument capable of taking a filename or
directory name. This allows us to pass the .git/HEAD file which is
a gn input.  Android blueprints require all paths used as input
or output to a script are listed as inputs or outputs in the genrule.

BUG=angleproject:2344

Change-Id: I6600083fc400faf07808316c4a6244d6599df79a
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2074924
Commit-Queue: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 1e6ba59..aa17554 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -54,15 +54,6 @@
   }
 }
 
-angle_git_is_present = exec_script("src/commit_id.py",
-                                   [
-                                     "check",
-                                     rebase_path(".", root_build_dir),
-                                   ],
-                                   "value")
-
-angle_use_commit_id = angle_git_is_present == 1
-
 import("src/compiler.gni")
 import("src/libGLESv2.gni")
 
@@ -466,29 +457,27 @@
   visibility = [ ":commit_id" ]
 }
 
-commit_id_output_file = "$root_gen_dir/angle/id/commit.h"
-if (angle_use_commit_id) {
-  action("commit_id") {
-    script = "src/commit_id.py"
-    outputs = [ commit_id_output_file ]
+commit_id_output_file = "$root_gen_dir/angle/commit.h"
+action("commit_id") {
+  script = "src/commit_id.py"
+  outputs = [ commit_id_output_file ]
 
-    # commit id should depend on angle's HEAD revision
-    inputs = [ ".git/HEAD" ]
+  # commit id should depend on angle's HEAD revision
+  commit_id_git_dep = ".git/HEAD"
 
-    args = [
-      "gen",
-      rebase_path(".", root_build_dir),
-      rebase_path(commit_id_output_file, root_build_dir),
-    ]
-
-    public_configs = [ ":commit_id_config" ]
+  # Add git as a dependency if it is available.
+  angle_git_is_present =
+      exec_script("src/commit_id.py", [ "check" ], "value") == 1
+  if (angle_git_is_present) {
+    inputs = [ commit_id_git_dep ]
   }
-} else {
-  copy("commit_id") {
-    sources = [ "src/commit.h" ]
-    outputs = [ commit_id_output_file ]
-    public_configs = [ ":commit_id_config" ]
-  }
+
+  args = [
+    "gen",
+    rebase_path(commit_id_output_file, root_build_dir),
+  ]
+
+  public_configs = [ ":commit_id_config" ]
 }
 
 angle_source_set("angle_version") {
diff --git a/src/commit.h b/src/commit.h
deleted file mode 100644
index f21ff43..0000000
--- a/src/commit.h
+++ /dev/null
@@ -1,14 +0,0 @@
-//
-// Copyright 2014 The ANGLE Project Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-//
-// commit.h:
-//   This is a default commit hash header, when git is not available.
-//
-
-#define ANGLE_COMMIT_HASH "unknown hash"
-#define ANGLE_COMMIT_HASH_SIZE 12
-#define ANGLE_COMMIT_DATE "unknown date"
-
-#define ANGLE_DISABLE_PROGRAM_BINARY_LOAD
diff --git a/src/commit_id.py b/src/commit_id.py
index 1740750..e2d062a 100755
--- a/src/commit_id.py
+++ b/src/commit_id.py
@@ -11,40 +11,43 @@
 import os
 
 usage = """\
-Usage: commit_id.py check <angle_dir>                - check if git is present
-       commit_id.py gen <angle_dir> <file_to_write>  - generate commit.h"""
+Usage: commit_id.py check                - check if git is present
+       commit_id.py gen <file_to_write>  - generate commit.h"""
 
 
 def grab_output(command, cwd):
     return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip()
 
 
-if len(sys.argv) < 3:
+if len(sys.argv) < 2:
     sys.exit(usage)
 
 operation = sys.argv[1]
-cwd = sys.argv[2]
+cwd = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
 
 if operation == 'check':
-    index_path = os.path.join(cwd, '.git', 'index')
-    if os.path.exists(index_path):
+    try:
+        # Try a git command to verify the cwd is valid and we can use the 'gen command'
+        grab_output('git status', cwd)
         print("1")
-    else:
+    except:
         print("0")
     sys.exit(0)
 
-if len(sys.argv) < 4 or operation != 'gen':
+if len(sys.argv) < 3 or operation != 'gen':
     sys.exit(usage)
 
-output_file = sys.argv[3]
+output_file = sys.argv[2]
 commit_id_size = 12
+commit_id = 'unknown hash'
+commit_date = 'unknown date'
 
+additional_defines = []
 try:
     commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd)
     commit_date = grab_output('git show -s --format=%ci HEAD', cwd)
 except:
-    commit_id = 'invalid-hash'
-    commit_date = 'invalid-date'
+    additional_defines.append('#define ANGLE_DISABLE_PROGRAM_BINARY_LOAD')
 
 hfile = open(output_file, 'w')
 
@@ -52,4 +55,7 @@
 hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size)
 hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date)
 
+for additional_define in additional_defines:
+    hfile.write(additional_define + '\n')
+
 hfile.close()
diff --git a/src/common/version.h b/src/common/version.h
index 56d6be0..1aa31fd 100644
--- a/src/common/version.h
+++ b/src/common/version.h
@@ -7,7 +7,7 @@
 #ifndef COMMON_VERSION_H_
 #define COMMON_VERSION_H_
 
-#include "id/commit.h"
+#include "commit.h"
 
 #define ANGLE_MAJOR_VERSION 2
 #define ANGLE_MINOR_VERSION 1