cmd/dist: diagnose lack of gcc earlier in build

Fixes #10731.

Change-Id: I105629b03fd3323c0a2ca5b6b0fd35ee92b7fd2e
Reviewed-on: https://go-review.googlesource.com/12146
Reviewed-by: Rob Pike <r@golang.org>
diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index f557238..0616be8 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -9,6 +9,7 @@
 	"flag"
 	"fmt"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"strings"
 )
@@ -1022,6 +1023,7 @@
 
 	setup()
 
+	checkCC()
 	bootstrapBuildTools()
 
 	// For the main bootstrap, building for host os/arch.
@@ -1067,6 +1069,57 @@
 	}
 }
 
+// Copied from go/build/build.go.
+// Cannot use go/build directly because cmd/dist for a new release
+// builds against an old release's go/build, which may be out of sync.
+var cgoEnabled = map[string]bool{
+	"darwin/386":      true,
+	"darwin/amd64":    true,
+	"darwin/arm":      true,
+	"darwin/arm64":    true,
+	"dragonfly/amd64": true,
+	"freebsd/386":     true,
+	"freebsd/amd64":   true,
+	"linux/386":       true,
+	"linux/amd64":     true,
+	"linux/arm":       true,
+	"linux/arm64":     true,
+	"linux/ppc64le":   true,
+	"android/386":     true,
+	"android/amd64":   true,
+	"android/arm":     true,
+	"netbsd/386":      true,
+	"netbsd/amd64":    true,
+	"netbsd/arm":      true,
+	"openbsd/386":     true,
+	"openbsd/amd64":   true,
+	"solaris/amd64":   true,
+	"windows/386":     true,
+	"windows/amd64":   true,
+}
+
+func needCC() bool {
+	switch os.Getenv("CGO_ENABLED") {
+	case "1":
+		return true
+	case "0":
+		return false
+	}
+	return cgoEnabled[gohostos+"/"+gohostarch]
+}
+
+func checkCC() {
+	if !needCC() {
+		return
+	}
+	if _, err := exec.Command(defaultcc, "--help").Output(); err != nil {
+		fatal("cannot invoke C compiler %q: %v\n\n"+
+			"Go needs a system C compiler for use with cgo.\n"+
+			"To set a C compiler, export CC=the-compiler.\n"+
+			"To disable cgo, export CGO_ENABLED=0.\n", defaultcc, err)
+	}
+}
+
 func defaulttarg() string {
 	// xgetwd might return a path with symlinks fully resolved, and if
 	// there happens to be symlinks in goroot, then the hasprefix test
diff --git a/src/go/build/build.go b/src/go/build/build.go
index bd84c57..6496414 100644
--- a/src/go/build/build.go
+++ b/src/go/build/build.go
@@ -256,6 +256,7 @@
 // if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
 var Default Context = defaultContext()
 
+// Also known to cmd/dist/build.go.
 var cgoEnabled = map[string]bool{
 	"darwin/386":      true,
 	"darwin/amd64":    true,