Support host build in the Go template

Change-Id: Idb8d5c5b6b8025040696d970c9e91a8f020c8d08
diff --git a/gobuild.py b/go/build.py
similarity index 80%
rename from gobuild.py
rename to go/build.py
index b51fa28..fe3c33d 100755
--- a/gobuild.py
+++ b/go/build.py
@@ -19,8 +19,10 @@
                         required=True)
     parser.add_argument('--depfile', help='The path to the depfile',
                         required=True)
-    parser.add_argument('--current-cpu', help='Target arch, x64 or arm64.',
-                        required=True)
+    parser.add_argument('--current-cpu', help='Target architecture.',
+                        choices=['x64', 'arm64'], required=True)
+    parser.add_argument('--current-os', help='Target operating system.',
+                        choices=['fuchsia', 'linux', 'mac', 'win'], required=True)
     parser.add_argument('--go-tool', help='The go tool to use for builds')
     parser.add_argument('--is-test', help='True if the target is a go test',
                         default=False)
@@ -28,20 +30,23 @@
     parser.add_argument('package', help='The package name')
     args = parser.parse_args()
 
-    if args.current_cpu == "x64":
-        goarch = "amd64"
-    elif args.current_cpu == "arm64":
-        goarch = "arm64"
-    else:
-        print("unknown current_cpu: ", args.current_cpu)
-        return 1
-
+    goarch = {
+        'x64': 'amd64',
+        'arm64': 'arm64',
+    }[args.current_cpu]
+    goos = {
+        'fuchsia': 'fuchsia',
+        'linux': 'linux',
+        'mac': 'darwin',
+        'win': 'windows',
+    }[args.current_os]
     gopath = args.root_out_dir
 
-    os.environ['CGO_ENABLED'] = '1'
-    os.environ['GOPATH'] = gopath + ":" + os.path.join(args.root_out_dir, "gen/go")
-    os.environ['GOOS'] = 'fuchsia'
+    if args.current_os == 'fuchsia':
+        os.environ['CGO_ENABLED'] = '1'
     os.environ['GOARCH'] = goarch
+    os.environ['GOOS'] = goos
+    os.environ['GOPATH'] = gopath + ":" + os.path.join(args.root_out_dir, "gen/go")
     if 'GOBIN' in os.environ:
         del os.environ['GOBIN']
     if 'GOROOT' in os.environ:
diff --git a/go/go_binary.gni b/go/go_binary.gni
index 1e8e921..8549975 100644
--- a/go/go_binary.gni
+++ b/go/go_binary.gni
@@ -20,7 +20,7 @@
 
     outputs = [ out_path ]
 
-    script = rebase_path("//.") + "/build/gobuild.py"
+    script = rebase_path("//build/go/build.py")
     depfile_path = "${out_path}.d"
     depfile = depfile_path
     console = true # only run one gobuild.py at once
@@ -29,11 +29,13 @@
       "--fuchsia-root",
       rebase_path("//."),
       "--root-out-dir",
-      rebase_path("$root_out_dir"),
+      rebase_path(root_out_dir),
       "--depfile",
       rebase_path(depfile_path),
       "--current-cpu",
       current_cpu,
+      "--current-os",
+      current_os,
       "--go-tool",
       rebase_path("//buildtools/go"),
       "--binname",
diff --git a/go/go_test.gni b/go/go_test.gni
index ab46d0c..e84475a 100644
--- a/go/go_test.gni
+++ b/go/go_test.gni
@@ -17,7 +17,7 @@
 
     outputs = [ out_path ]
 
-    script = rebase_path("//.") + "/build/gobuild.py"
+    script = rebase_path("//build/go/build.py")
     depfile_path = "${out_path}.d"
     depfile = depfile_path
     console = true # only run one gobuild.py at once
@@ -31,6 +31,8 @@
       rebase_path(depfile_path),
       "--current-cpu",
       current_cpu,
+      "--current-os",
+      current_os,
       "--is-test=true",
       "--binname",
       target_name,