Skip to content

Commit 78b8e4f

Browse files
motiejusgopherbot
authored andcommitted
cmd/dist: support spaces and quotes in CC
As of CL 334732 `go build` can accept `$CC` with spaces and quotes, which lets us easily use `zig cc` as the C compiler, or easily pass extra compiler parameters: ``` CC="zig cc" go build <...> CC="clang-13 -v" go build <...> CC="zig cc -Wl,--print-gc-sections" go build <...> ``` However, the same does not apply for building go itself: ``` $ CC="zig cc" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) go tool dist: cannot invoke C compiler "zig cc": exec: "zig cc": executable file not found in $PATH Go needs a system C compiler for use with cgo. To set a C compiler, set CC=the-compiler. To disable cgo, set CGO_ENABLED=0. ``` With this change Go can be built directly with `zig cc` (the linker arg will disappear with CL 405414): ``` $ CC="zig cc -Wl,--no-gc-sections" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) Building Go toolchain1 using /usr/local/go. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1. Building Go toolchain2 using go_bootstrap and Go toolchain1. Building Go toolchain3 using go_bootstrap and Go toolchain2. Building packages and commands for linux/amd64. --- Installed Go for linux/amd64 in /home/motiejus/code/go Installed commands in /home/motiejus/code/go/bin $ ../bin/go version go version devel go1.19-811f1913a8 Thu May 19 09:44:49 2022 +0300 linux/amd64 ``` Fixes #52990 Change-Id: I66b3525d47db488d3c583c1aee3af78060fd5a38 GitHub-Last-Rev: ecc70d7 GitHub-Pull-Request: #52991 Reviewed-on: https://go-review.googlesource.com/c/go/+/407216 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Alex Rakoczy <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent e6d8b05 commit 78b8e4f

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/cmd/dist/build.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,15 +1630,21 @@ func checkCC() {
16301630
if !needCC() {
16311631
return
16321632
}
1633-
if output, err := exec.Command(defaultcc[""], "--help").CombinedOutput(); err != nil {
1633+
cc, err := quotedSplit(defaultcc[""])
1634+
if err != nil {
1635+
fatalf("split CC: %v", err)
1636+
}
1637+
var ccHelp = append(cc, "--help")
1638+
1639+
if output, err := exec.Command(ccHelp[0], ccHelp[1:]...).CombinedOutput(); err != nil {
16341640
outputHdr := ""
16351641
if len(output) > 0 {
16361642
outputHdr = "\nCommand output:\n\n"
16371643
}
16381644
fatalf("cannot invoke C compiler %q: %v\n\n"+
16391645
"Go needs a system C compiler for use with cgo.\n"+
16401646
"To set a C compiler, set CC=the-compiler.\n"+
1641-
"To disable cgo, set CGO_ENABLED=0.\n%s%s", defaultcc[""], err, outputHdr, output)
1647+
"To disable cgo, set CGO_ENABLED=0.\n%s%s", cc, err, outputHdr, output)
16421648
}
16431649
}
16441650

src/cmd/dist/quoted.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// quotedSplit is a verbatim copy from cmd/internal/quoted.go:Split and its
6+
// dependencies (isSpaceByte). Since this package is built using the host's
7+
// Go compiler, it cannot use `cmd/internal/...`. We also don't want to export
8+
// it to all Go users.
9+
//
10+
// Please keep those in sync.
11+
func quotedSplit(s string) ([]string, error) {
12+
// Split fields allowing '' or "" around elements.
13+
// Quotes further inside the string do not count.
14+
var f []string
15+
for len(s) > 0 {
16+
for len(s) > 0 && isSpaceByte(s[0]) {
17+
s = s[1:]
18+
}
19+
if len(s) == 0 {
20+
break
21+
}
22+
// Accepted quoted string. No unescaping inside.
23+
if s[0] == '"' || s[0] == '\'' {
24+
quote := s[0]
25+
s = s[1:]
26+
i := 0
27+
for i < len(s) && s[i] != quote {
28+
i++
29+
}
30+
if i >= len(s) {
31+
return nil, fmt.Errorf("unterminated %c string", quote)
32+
}
33+
f = append(f, s[:i])
34+
s = s[i+1:]
35+
continue
36+
}
37+
i := 0
38+
for i < len(s) && !isSpaceByte(s[i]) {
39+
i++
40+
}
41+
f = append(f, s[:i])
42+
s = s[i:]
43+
}
44+
return f, nil
45+
}
46+
47+
func isSpaceByte(c byte) bool {
48+
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
49+
}

src/cmd/internal/quoted/quoted.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func isSpaceByte(c byte) bool {
2020
// allowing single or double quotes around elements.
2121
// There is no unescaping or other processing within
2222
// quoted fields.
23+
//
24+
// Keep in sync with cmd/dist/quoted.go
2325
func Split(s string) ([]string, error) {
2426
// Split fields allowing '' or "" around elements.
2527
// Quotes further inside the string do not count.

0 commit comments

Comments
 (0)