jiri-profile-v23: Switch the default to Go 1.6

This also involves some formatting changes to files since apparently "go
fmt" has some changes between Go 1.5 and 1.6

Resolves vanadium/issues#1239

MultiPart: 1/2
Change-Id: I57858330578b5594640137559c37b32ad8e8ef77
diff --git a/jiri-profile-v23/android_profile/android.go b/jiri-profile-v23/android_profile/android.go
index 8ba48bb..80e514b 100644
--- a/jiri-profile-v23/android_profile/android.go
+++ b/jiri-profile-v23/android_profile/android.go
@@ -33,6 +33,7 @@
 	ndkExtract           func(seq runutil.Sequence, src, dst string) runutil.Sequence
 	ndkAPILevel          int
 	platformToolsVersion map[string]string
+	baseVersion          string // Version of the base profile that this requires
 }
 
 func ndkArch(goArch string) (string, error) {
@@ -71,21 +72,25 @@
 				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r9d-%s-%s.tar.bz2", ndkDownloadBaseURL, runtime.GOOS, arch),
 				ndkExtract:     tarExtract,
 				ndkAPILevel:    9,
+				baseVersion:    "4",
 			},
 			"4": &versionSpec{
 				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r10e-%s-%s.bin", ndkDownloadBaseURL, runtime.GOOS, arch),
 				ndkExtract:     selfExtract,
 				ndkAPILevel:    16,
+				baseVersion:    "4",
 			},
 			"5": &versionSpec{
 				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r10e-%s-%s.bin", ndkDownloadBaseURL, runtime.GOOS, arch),
 				ndkExtract:     selfExtract,
 				ndkAPILevel:    21,
+				baseVersion:    "4",
 			},
 			"7": &versionSpec{
 				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r10e-%s-%s.bin", ndkDownloadBaseURL, runtime.GOOS, arch),
 				ndkExtract:     selfExtract,
 				ndkAPILevel:    21,
+				baseVersion:    "4",
 			},
 			"8": &versionSpec{
 				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r10e-%s-%s.bin", ndkDownloadBaseURL, runtime.GOOS, arch),
@@ -95,8 +100,19 @@
 					"darwin": "sdk-repo-darwin-platform-tools-2219242",
 					"linux":  "sdk-repo-linux-platform-tools-2219198",
 				},
+				baseVersion: "4",
 			},
-		}, "8"),
+			"9": &versionSpec{
+				ndkDownloadURL: fmt.Sprintf("%s/android-ndk-r10e-%s-%s.bin", ndkDownloadBaseURL, runtime.GOOS, arch),
+				ndkExtract:     selfExtract,
+				ndkAPILevel:    21,
+				platformToolsVersion: map[string]string{
+					"darwin": "sdk-repo-darwin-platform-tools-2219242",
+					"linux":  "sdk-repo-linux-platform-tools-2219198",
+				},
+				baseVersion: "5",
+			},
+		}, "9"),
 	}
 	profilesmanager.Register(m)
 }
@@ -213,13 +229,10 @@
 	// So this is a good way to copy the arch/opsys and we just have to set
 	// the version.
 	baseTarget, err := profiles.NewTarget(target.String(), env)
+	baseTarget.SetVersion(m.spec.baseVersion)
 	if err != nil {
 		return nil, err
 	}
-	// We are setting version 4 to ensure that we get a newer version of Go that
-	// works on android.  It's not clear why the default version of the go
-	// profile is for an old version of go.
-	baseTarget.SetVersion("4")
 	if err := profilesmanager.EnsureProfileTargetIsInstalled(jirix, pdb, m.profileInstaller, "base", root, baseTarget); err != nil {
 		return nil, err
 	}
@@ -292,12 +305,12 @@
 		androidPlatformToolsZipFile := filepath.Join(tmpDir, "platform-tools.zip")
 		return jirix.NewSeq().
 			Call(func() error {
-			url := platformToolsBaseURL + "/" + suffix + ".zip"
-			return profilesutil.Fetch(jirix, androidPlatformToolsZipFile, url)
-		}, "fetch android platform tools").
+				url := platformToolsBaseURL + "/" + suffix + ".zip"
+				return profilesutil.Fetch(jirix, androidPlatformToolsZipFile, url)
+			}, "fetch android platform tools").
 			Call(func() error {
-			return profilesutil.Unzip(jirix, androidPlatformToolsZipFile, tmpDir)
-		}, "unzip android platform tools").
+				return profilesutil.Unzip(jirix, androidPlatformToolsZipFile, tmpDir)
+			}, "unzip android platform tools").
 			MkdirAll(filepath.Dir(outDir), profilesutil.DefaultDirPerm).
 			Rename(filepath.Join(tmpDir, "platform-tools"), outDir).
 			Done()
diff --git a/jiri-profile-v23/base_profile/base.go b/jiri-profile-v23/base_profile/base.go
index 65140da..71d7c0a 100644
--- a/jiri-profile-v23/base_profile/base.go
+++ b/jiri-profile-v23/base_profile/base.go
@@ -43,7 +43,11 @@
 					{"go", "1.5.2.1:56093743"},
 					{"syncbase", ""}},
 				},
-			}, "1"),
+				"5": &versionSpec{[]struct{ name, version string }{
+					{"go", "1.6"},
+					{"syncbase", ""}},
+				},
+			}, "5"),
 	}
 	profilesmanager.Register(m)
 }
@@ -113,7 +117,13 @@
 		os = "darwin"
 	}
 	base.Set("GOOS", os)
-	profilesreader.MergeEnv(profilesreader.ProfileMergePolicies(), base, profileEnvs...)
+	// Slight modifications to ProfileMergePolicies: Want the values from
+	// the "go" profile we depend on to prevail.
+	mp := profilesreader.ProfileMergePolicies()
+	mp["GOROOT"] = profilesreader.UseLast
+	mp["GOROOT_BOOTSTRAP"] = profilesreader.IgnoreBaseUseLast
+	mp["CGO_ENABLED"] = profilesreader.IgnoreBaseUseLast
+	profilesreader.MergeEnv(mp, base, profileEnvs...)
 	target.Env.Vars = base.ToSlice()
 	pdb.InstallProfile(m.profileInstaller, m.profileName, string(root))
 	return pdb.AddProfileTarget(m.profileInstaller, m.profileName, target)
diff --git a/jiri-profile-v23/dart_profile/dart.go b/jiri-profile-v23/dart_profile/dart.go
index 823dd41..874612a 100644
--- a/jiri-profile-v23/dart_profile/dart.go
+++ b/jiri-profile-v23/dart_profile/dart.go
@@ -111,8 +111,8 @@
 		sdkZipFile := filepath.Join(tmpDir, "dart-sdk.zip")
 		return jirix.NewSeq().
 			Call(func() error {
-			return profilesutil.Fetch(jirix, sdkZipFile, sdkUrl)
-		}, "fetch dart sdk").
+				return profilesutil.Fetch(jirix, sdkZipFile, sdkUrl)
+			}, "fetch dart sdk").
 			Call(func() error { return profilesutil.Unzip(jirix, sdkZipFile, tmpDir) }, "unzip dart sdk").
 			MkdirAll(filepath.Dir(outDir), profilesutil.DefaultDirPerm).
 			Rename(filepath.Join(tmpDir, "dart-sdk"), outDir).
diff --git a/jiri-profile-v23/go_profile/darwin_xcompile.go b/jiri-profile-v23/go_profile/darwin_xcompile.go
index 5c29a91..05dd3c3 100644
--- a/jiri-profile-v23/go_profile/darwin_xcompile.go
+++ b/jiri-profile-v23/go_profile/darwin_xcompile.go
@@ -110,7 +110,7 @@
 			Pushd(binutilsSrc).
 			MkdirAll(absBinutilsBin, profilesutil.DefaultDirPerm).
 			Run("./configure", "--target="+targetABI, "--program-prefix=",
-			"--prefix="+absBinutilsBin, "--with-sysroot=yes").
+				"--prefix="+absBinutilsBin, "--with-sysroot=yes").
 			Run("make", "-j8").
 			Last("make", "install")
 	}
@@ -125,8 +125,8 @@
 		return jirix.NewSeq().MkdirAll(absClangBuildSrc, profilesutil.DefaultDirPerm).
 			Pushd(absClangBuildSrc).
 			Run("cmake", "-GUnix Makefiles", "-DCMAKE_INSTALL_PREFIX="+inst.Abs(jirix),
-			"-DLLVM_TARGETS_TO_BUILD=ARM",
-			"-DLLVM_EXTERNAL_CLANG_SOURCE_DIR="+absClangSrc, absLLVMSrc).
+				"-DLLVM_TARGETS_TO_BUILD=ARM",
+				"-DLLVM_EXTERNAL_CLANG_SOURCE_DIR="+absClangSrc, absLLVMSrc).
 			Run("make", "-j8").
 			Last("make", "install")
 	}
diff --git a/jiri-profile-v23/go_profile/go.go b/jiri-profile-v23/go_profile/go.go
index 6ff3ad7..bed4eb2 100644
--- a/jiri-profile-v23/go_profile/go.go
+++ b/jiri-profile-v23/go_profile/go.go
@@ -21,7 +21,6 @@
 	"v.io/jiri/gitutil"
 	"v.io/jiri/profiles"
 	"v.io/jiri/profiles/profilesmanager"
-	"v.io/jiri/profiles/profilesreader"
 	"v.io/jiri/profiles/profilesutil"
 	"v.io/jiri/project"
 	"v.io/x/lib/envvar"
@@ -156,7 +155,7 @@
 				"560937434d5f2857bb69e0a6881a38201a197a8d", nil},
 			"1.6": &versionSpec{
 				"e805bf39458915365924228dc53969ce04e32813", nil},
-		}, "1.5.1"), // TODO(ashankar): Change to 1.6 once all code has been updated to pass tests with it
+		}, "1.6"),
 	}
 	profilesmanager.Register(m)
 }
@@ -255,6 +254,8 @@
 	if release := newGoRelease(target.Version()); release != nil &&
 		len(m.spec.patchFiles) == 0 &&
 		!env.Contains("GO_FLAGS") {
+		// Not using a bootstrapped Go, delete any references from env.
+		env.Delete("GOROOT_BOOTSTRAP")
 		goInstDir = m.goRoot.Join("shared").Join(target.Version())
 		fn := func() error { return release.install(jirix, goInstDir.Abs(jirix)) }
 		if err := profilesutil.AtomicAction(jirix, fn, goInstDir.Abs(jirix), "Install a release version of the Go toolchain"); err != nil {
@@ -289,9 +290,7 @@
 			return err
 		}
 	}
-	// Merge our target environment and GOROOT
-	goEnv := []string{"GOROOT=" + goInstDir.Symbolic()}
-	profilesreader.MergeEnv(profilesreader.ProfileMergePolicies(), env, goEnv)
+	env.Set("GOROOT", goInstDir.Symbolic())
 	target.Env.Vars = env.ToSlice()
 	target.InstallationDir = string(goInstDir)
 
@@ -563,6 +562,12 @@
 		"CC_FOR_TARGET=" + filepath.Join(jirix.Root, "release/swift/clang/clangwrap.sh"),
 		"CXX_FOR_TARGET=" + filepath.Join(jirix.Root, "release/swift/clang/clangwrap++.sh"),
 		"GOOS=darwin",
+		"GOHOSTARCH=amd64",
+		"GOHOSTOS=darwin",
+		// We need to explicitly pass the ios build tag to the make.bash script for go so that it won't compile
+		// crypto code that's meant for the mac. It'll work on the device because those files have a build tag
+		// of !arm64, but the simulator will fail because those specific APIs don't exist on iOS.
+		"GO_FLAGS=-tags ios",
 	}
 
 	// 32-bit arm is always armv7 in Apple-land
@@ -576,14 +581,6 @@
 	// Submitted to golang, currently marked for 1.7: https://go-review.googlesource.com/#/c/19206/
 	patchPath := filepath.Join(jirix.Root, "release/go/src/v.io/x/devtools/jiri-v23-profile/go/macho_linker.patch")
 	m.spec.patchFiles = append(m.spec.patchFiles, patchPath)
-	// Additional to influence make.bash used when building our own toolchain
-	vars = append(vars,
-		"GOHOSTARCH=amd64",
-		"GOHOSTOS=darwin",
-		// We need to explicitly pass the ios build tag to the make.bash script for go so that it won't compile
-		// crypto code that's meant for the mac. It'll work on the device because those files have a build tag
-		// of !arm64, but the simulator will fail because those specific APIs don't exist on iOS.
-		"GO_FLAGS=-tags ios")
 	return "", vars, nil
 }
 
diff --git a/jiri-profile-v23/mojo_profile/mojo.go b/jiri-profile-v23/mojo_profile/mojo.go
index 7473fdf..6a1c603 100644
--- a/jiri-profile-v23/mojo_profile/mojo.go
+++ b/jiri-profile-v23/mojo_profile/mojo.go
@@ -421,8 +421,8 @@
 		androidPlatformToolsZipFile := filepath.Join(tmpDir, "platform-tools.zip")
 		return jirix.NewSeq().
 			Call(func() error {
-			return profilesutil.Fetch(jirix, androidPlatformToolsZipFile, androidPlatformToolsUrl(m.spec.androidPlatformToolsVersion))
-		}, "fetch android platform tools").
+				return profilesutil.Fetch(jirix, androidPlatformToolsZipFile, androidPlatformToolsUrl(m.spec.androidPlatformToolsVersion))
+			}, "fetch android platform tools").
 			Call(func() error { return profilesutil.Unzip(jirix, androidPlatformToolsZipFile, tmpDir) }, "unzip android platform tools").
 			MkdirAll(filepath.Dir(outDir), profilesutil.DefaultDirPerm).
 			Rename(filepath.Join(tmpDir, "platform-tools"), outDir).
@@ -617,8 +617,8 @@
 		outFile := filepath.Join(outDir, "libsystem_thunks.a")
 		return jirix.NewSeq().MkdirAll(outDir, profilesutil.DefaultDirPerm).
 			Call(func() error {
-			return profilesutil.Fetch(jirix, outFile, mojoSystemThunksUrl(m.platform, m.buildVersion))
-		}, "fetch mojo system thunks").Done()
+				return profilesutil.Fetch(jirix, outFile, mojoSystemThunksUrl(m.platform, m.buildVersion))
+			}, "fetch mojo system thunks").Done()
 	}
 	return profilesutil.AtomicAction(jirix, fn, outDir, "Download Mojo system thunks")
 }
diff --git a/oncall/collect.go b/oncall/collect.go
index 586210c..b780f54 100644
--- a/oncall/collect.go
+++ b/oncall/collect.go
@@ -453,9 +453,9 @@
 		Capture(&stdoutBuf, &stderrBuf).
 		Timeout(debugCommandTimeout).
 		Last(debug,
-		"--timeout", debugRPCTimeout.String(),
-		"--v23.namespace.root", namespaceRoot,
-		"--v23.credentials", credentialsFlag, "stats", "read", fmt.Sprintf("%s/build.[TPUM]*", buildInfoEndpointPrefix)); err != nil {
+			"--timeout", debugRPCTimeout.String(),
+			"--v23.namespace.root", namespaceRoot,
+			"--v23.credentials", credentialsFlag, "stats", "read", fmt.Sprintf("%s/build.[TPUM]*", buildInfoEndpointPrefix)); err != nil {
 		return fmt.Errorf("debug command failed: %v\nSTDERR:\n%s\nSTDOUT:\n%s\nEND\n", err, stderrBuf.String(), stdoutBuf.String())
 	}
 
diff --git a/vcloud/vcloud.go b/vcloud/vcloud.go
index 9bc7f99..d259ab2 100644
--- a/vcloud/vcloud.go
+++ b/vcloud/vcloud.go
@@ -309,11 +309,11 @@
 	var stdouterr bytes.Buffer
 	err := ctx.NewSeq().Read(nil).Capture(&stdouterr, &stdouterr).
 		Last("gcloud", "compute", "ssh",
-		addUser(user, n.Name),
-		"--project", *flagProject,
-		"--zone", n.Zone,
-		"--command", quoteForCommand(cmdline),
-	)
+			addUser(user, n.Name),
+			"--project", *flagProject,
+			"--zone", n.Zone,
+			"--command", quoteForCommand(cmdline),
+		)
 	return runResult{node: n, out: stdouterr.String(), err: err}
 }