]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist, cmd/go: pass -arch for C compilation on Darwin
authorCherry Zhang <cherryyz@google.com>
Thu, 14 Jan 2021 17:29:16 +0000 (12:29 -0500)
committerCherry Zhang <cherryyz@google.com>
Thu, 14 Jan 2021 21:55:29 +0000 (21:55 +0000)
On Apple Silicon Mac, the C compiler has an annoying default
target selection, depending on the ancestor processes'
architecture. In particular, if the shell or IDE is x86, when
running "go build" even with a native ARM64 Go toolchain, the C
compiler defaults to x86, causing build failures. We pass "-arch"
flag explicitly to avoid this situation.

Fixes #43692.
Fixes #43476.
Updates golang/vscode-go#1087.

Change-Id: I80b6a116a114e11e273c6886e377a1cc969fa3f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/283812
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/cgo/gcc.go
src/cmd/go/internal/work/exec.go
src/cmd/go/testdata/script/build_darwin_cc_arch.txt [new file with mode: 0644]
src/cmd/link/internal/ld/lib.go

index 111a309eb5c026c78c99808c7ae8bfe25ee7b2a0..b5e28e325459c0ac54122e97ceeeb74de54133c2 100644 (file)
@@ -1549,7 +1549,14 @@ func (p *Package) gccBaseCmd() []string {
 func (p *Package) gccMachine() []string {
        switch goarch {
        case "amd64":
+               if goos == "darwin" {
+                       return []string{"-arch", "x86_64", "-m64"}
+               }
                return []string{"-m64"}
+       case "arm64":
+               if goos == "darwin" {
+                       return []string{"-arch", "arm64"}
+               }
        case "386":
                return []string{"-m32"}
        case "arm":
index feb2299d40132bfc196142902c0d6dbd271673a9..af8b78e6618e78576ec521b19d02c267dae0da01 100644 (file)
@@ -2435,7 +2435,7 @@ func (b *Builder) fcExe() []string {
 func (b *Builder) compilerExe(envValue string, def string) []string {
        compiler := strings.Fields(envValue)
        if len(compiler) == 0 {
-               compiler = []string{def}
+               compiler = strings.Fields(def)
        }
        return compiler
 }
@@ -2581,7 +2581,14 @@ func (b *Builder) gccArchArgs() []string {
        case "386":
                return []string{"-m32"}
        case "amd64":
+               if cfg.Goos == "darwin" {
+                       return []string{"-arch", "x86_64", "-m64"}
+               }
                return []string{"-m64"}
+       case "arm64":
+               if cfg.Goos == "darwin" {
+                       return []string{"-arch", "arm64"}
+               }
        case "arm":
                return []string{"-marm"} // not thumb
        case "s390x":
diff --git a/src/cmd/go/testdata/script/build_darwin_cc_arch.txt b/src/cmd/go/testdata/script/build_darwin_cc_arch.txt
new file mode 100644 (file)
index 0000000..2b81b4c
--- /dev/null
@@ -0,0 +1,24 @@
+# Test that we pass -arch flag to C compiler on Darwin (issue 43692).
+
+[!darwin] skip
+[!cgo] skip
+
+# clear CC, in case user sets it
+env CC=
+
+env CGO_ENABLED=1
+
+env GOARCH=amd64
+go build -n -x c.go
+stderr 'clang.*-arch x86_64'
+
+env GOARCH=arm64
+go build -n -x c.go
+stderr 'clang.*-arch arm64'
+
+-- c.go --
+package main
+
+import "C"
+
+func main() {}
index bf95745d8d9d706de00678ef64925311b5dfc412..dd5e8ab2c5cf29fe2d552adecc8f9c59f516c792 100644 (file)
@@ -1749,12 +1749,19 @@ func hostlinkArchArgs(arch *sys.Arch) []string {
        switch arch.Family {
        case sys.I386:
                return []string{"-m32"}
-       case sys.AMD64, sys.S390X:
+       case sys.AMD64:
+               if objabi.GOOS == "darwin" {
+                       return []string{"-arch", "x86_64", "-m64"}
+               }
+               return []string{"-m64"}
+       case sys.S390X:
                return []string{"-m64"}
        case sys.ARM:
                return []string{"-marm"}
        case sys.ARM64:
-               // nothing needed
+               if objabi.GOOS == "darwin" {
+                       return []string{"-arch", "arm64"}
+               }
        case sys.MIPS64:
                return []string{"-mabi=64"}
        case sys.MIPS: