diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index 0a7af2b2d1..7c44c4a605 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -1630,7 +1630,13 @@ func checkCC() { if !needCC() { return } - if output, err := exec.Command(defaultcc[""], "--help").CombinedOutput(); err != nil { + cc, err := quotedSplit(defaultcc[""]) + if err != nil { + fatalf("split CC: %v", err) + } + var ccHelp = append(cc, "--help") + + if output, err := exec.Command(ccHelp[0], ccHelp[1:]...).CombinedOutput(); err != nil { outputHdr := "" if len(output) > 0 { outputHdr = "\nCommand output:\n\n" @@ -1638,7 +1644,7 @@ func checkCC() { fatalf("cannot invoke C compiler %q: %v\n\n"+ "Go needs a system C compiler for use with cgo.\n"+ "To set a C compiler, set CC=the-compiler.\n"+ - "To disable cgo, set CGO_ENABLED=0.\n%s%s", defaultcc[""], err, outputHdr, output) + "To disable cgo, set CGO_ENABLED=0.\n%s%s", cc, err, outputHdr, output) } } diff --git a/src/cmd/dist/quoted.go b/src/cmd/dist/quoted.go new file mode 100644 index 0000000000..e87b8a3965 --- /dev/null +++ b/src/cmd/dist/quoted.go @@ -0,0 +1,49 @@ +package main + +import "fmt" + +// quotedSplit is a verbatim copy from cmd/internal/quoted.go:Split and its +// dependencies (isSpaceByte). Since this package is built using the host's +// Go compiler, it cannot use `cmd/internal/...`. We also don't want to export +// it to all Go users. +// +// Please keep those in sync. +func quotedSplit(s string) ([]string, error) { + // Split fields allowing '' or "" around elements. + // Quotes further inside the string do not count. + var f []string + for len(s) > 0 { + for len(s) > 0 && isSpaceByte(s[0]) { + s = s[1:] + } + if len(s) == 0 { + break + } + // Accepted quoted string. No unescaping inside. + if s[0] == '"' || s[0] == '\'' { + quote := s[0] + s = s[1:] + i := 0 + for i < len(s) && s[i] != quote { + i++ + } + if i >= len(s) { + return nil, fmt.Errorf("unterminated %c string", quote) + } + f = append(f, s[:i]) + s = s[i+1:] + continue + } + i := 0 + for i < len(s) && !isSpaceByte(s[i]) { + i++ + } + f = append(f, s[:i]) + s = s[i:] + } + return f, nil +} + +func isSpaceByte(c byte) bool { + return c == ' ' || c == '\t' || c == '\n' || c == '\r' +} diff --git a/src/cmd/internal/quoted/quoted.go b/src/cmd/internal/quoted/quoted.go index e7575dfc66..b3d3c400ec 100644 --- a/src/cmd/internal/quoted/quoted.go +++ b/src/cmd/internal/quoted/quoted.go @@ -20,6 +20,8 @@ func isSpaceByte(c byte) bool { // allowing single or double quotes around elements. // There is no unescaping or other processing within // quoted fields. +// +// Keep in sync with cmd/dist/quoted.go func Split(s string) ([]string, error) { // Split fields allowing '' or "" around elements. // Quotes further inside the string do not count.