Skip to content

Commit

Permalink
cmd/dist, cmd/go: pass -arch for C compilation on Darwin
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cherrymui committed Jan 14, 2021
1 parent 84e8a06 commit eb33002
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/cmd/cgo/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/go/internal/work/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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":
Expand Down
24 changes: 24 additions & 0 deletions src/cmd/go/testdata/script/build_darwin_cc_arch.txt
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 9 additions & 2 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit eb33002

Please sign in to comment.