BUILD.gn file for submodules

When submodules are enabled, we read the .git file to find the new .git
location.

Bug: b/253329946
Change-Id: I373e47870b2bd4ecb6638149cefad1bd782635c9
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/mesa/+/818627
Reviewed-by: Craig Stout <cstout@google.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 167dd1a..4783437 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -20,6 +20,7 @@
 # IN THE SOFTWARE.
 
 import("../mesa.gni")
+import("//build/checkout.gni")
 
 config("common_config") {
   mesa_version = read_file("../VERSION", "trim string")
@@ -132,6 +133,19 @@
   include_dirs = [ "$target_gen_dir" ]
 }
 
+if (submodules) {
+  mesa_git_link_path_list =
+    read_file("../.git", "list lines")
+  mesa_git_link_path = mesa_git_link_path_list[0]
+  mesa_relative_git_dir_split = string_split(mesa_git_link_path)
+  mesa_relative_git_dir = mesa_relative_git_dir_split[1]
+  mesa_git_head =
+    "../${mesa_relative_git_dir}/HEAD"
+} else {
+  mesa_git_head = "../.git/HEAD"
+}
+
+
 mesa_python_action("sha1") {
   output_name = "git_sha1.h"
   outputs = [ "$target_gen_dir/$output_name" ]
@@ -143,7 +157,7 @@
   ]
 
   # Ensure that this action reruns when git HEAD moves
-  inputs = [ "../.git/HEAD" ]
+  inputs = [ mesa_git_head ]
 
   public_configs = [ ":sha1_config" ]
 }