"flag"
        "fmt"
        "os"
+       "os/exec"
        "path/filepath"
        "strings"
 )
 
        setup()
 
+       checkCC()
        bootstrapBuildTools()
 
        // For the main bootstrap, building for host os/arch.
        }
 }
 
+// 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
 
 // 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,