]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: support spaces and quotes in CC
authorMotiejus Jakštys <motiejus@jakstys.lt>
Fri, 20 May 2022 07:32:48 +0000 (07:32 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 26 May 2022 20:14:15 +0000 (20:14 +0000)
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: ecc70d722406f0c7d0c1930c872db392e80e7cf5
GitHub-Pull-Request: golang/go#52991
Reviewed-on: https://go-review.googlesource.com/c/go/+/407216
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Alex Rakoczy <alex@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/dist/build.go
src/cmd/dist/quoted.go [new file with mode: 0644]
src/cmd/internal/quoted/quoted.go

index 0a7af2b2d19af9a766ef765d95558dec206a9896..7c44c4a60528c15987d3d16e08b22d6e6eaa5475 100644 (file)
@@ -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 (file)
index 0000000..e87b8a3
--- /dev/null
@@ -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'
+}
index e7575dfc669c5e0601b08c9837449423942b45e6..b3d3c400ec7234755d455b464069aebad6ff29da 100644 (file)
@@ -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.