Use relative GOPATH for microfactory golang builds.

Bug: b/336890676
Change-Id: I15ff839986d6c4997d5abccaa2ae8a9703d6cfb6
diff --git a/microfactory/microfactory.go b/microfactory/microfactory.go
index faa0d73..4024dff 100644
--- a/microfactory/microfactory.go
+++ b/microfactory/microfactory.go
@@ -68,8 +68,25 @@
 	goToolDir = filepath.Join(runtime.GOROOT(), "pkg", "tool", runtime.GOOS+"_"+runtime.GOARCH)
 	goVersion = findGoVersion()
 	isGo18    = strings.Contains(goVersion, "go1.8")
+	relGoRoot = runtime.GOROOT()
 )
 
+func init() {
+	// make the GoRoot relative
+	if filepath.IsAbs(relGoRoot) {
+		pwd, err := os.Getwd()
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "failed to get the current directory: %s\n", err)
+			return
+		}
+		relGoRoot, err = filepath.Rel(pwd, relGoRoot)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "failed to get the GOROOT relative path: %s\n", err)
+			return
+		}
+	}
+}
+
 func findGoVersion() string {
 	if version, err := ioutil.ReadFile(filepath.Join(runtime.GOROOT(), "VERSION")); err == nil {
 		return string(version)
@@ -401,6 +418,9 @@
 		"-o", p.output,
 		"-p", p.Name,
 		"-complete", "-pack", "-nolocalimports")
+	cmd.Env = []string{
+		"GOROOT=" + relGoRoot,
+	}
 	if !isGo18 && !config.Race {
 		cmd.Args = append(cmd.Args, "-c", fmt.Sprintf("%d", runtime.NumCPU()))
 	}
@@ -538,6 +558,9 @@
 	}
 	cmd.Args = append(cmd.Args, p.output)
 	cmd.Stdin = nil
+	cmd.Env = []string{
+		"GOROOT=" + relGoRoot,
+	}
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
 	if config.Verbose {
@@ -605,6 +628,9 @@
 	}
 
 	cmd := exec.Command(mybin, os.Args[1:]...)
+	cmd.Env = []string{
+		"GOROOT=" + relGoRoot,
+	}
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr