func buildBuildGcsfuse()

in tools/util/build_gcsfuse.go [91:150]


func buildBuildGcsfuse(dst string) (err error) {
	// Figure out where we can find the source for build_gcsfuse.
	var srcDir string
	{
		var pkg *build.Package
		pkg, err = build.Import(
			"github.com/googlecloudplatform/gcsfuse/v2/tools/build_gcsfuse",
			"",
			build.FindOnly)

		if err != nil {
			err = fmt.Errorf("build.Import: %w", err)
			return
		}

		srcDir = pkg.Dir
	}

	// Create a directory to become GOPATH for our build below.
	gopath, err := os.MkdirTemp("", "build_gcsfuse_gopath")
	if err != nil {
		err = fmt.Errorf("TempDir: %w", err)
		return
	}
	defer os.RemoveAll(gopath)

	// Create a directory to become GOCACHE for our build below.
	var gocache string
	gocache, err = os.MkdirTemp("", "build_gcsfuse_gocache")
	if err != nil {
		err = fmt.Errorf("TempDir: %w", err)
		return
	}
	defer os.RemoveAll(gocache)

	// Build within that directory with no GOPATH -- it should have no external
	// dependencies besides the standard library.
	{
		cmd := exec.Command(
			"go", "build",
			"-o", dst,
		)

		cmd.Dir = srcDir
		cmd.Env = []string{
			fmt.Sprintf("GOROOT=%s", runtime.GOROOT()),
			fmt.Sprintf("GOPATH=%s", gopath),
			fmt.Sprintf("GOCACHE=%s", gocache),
		}

		var output []byte
		output, err = cmd.CombinedOutput()
		if err != nil {
			err = fmt.Errorf("go build build_gcsfuse: %w\nOutput:\n%s", err, output)
			return
		}
	}

	return
}