mirror of
https://github.com/golang/go.git
synced 2025-11-10 21:51:05 +00:00
cmd/dist, cmd/go: pass -arch for C compilation on Darwin
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>
This commit is contained in:
parent
84e8a06f62
commit
eb330020dc
4 changed files with 48 additions and 3 deletions
|
|
@ -1549,7 +1549,14 @@ func (p *Package) gccBaseCmd() []string {
|
||||||
func (p *Package) gccMachine() []string {
|
func (p *Package) gccMachine() []string {
|
||||||
switch goarch {
|
switch goarch {
|
||||||
case "amd64":
|
case "amd64":
|
||||||
|
if goos == "darwin" {
|
||||||
|
return []string{"-arch", "x86_64", "-m64"}
|
||||||
|
}
|
||||||
return []string{"-m64"}
|
return []string{"-m64"}
|
||||||
|
case "arm64":
|
||||||
|
if goos == "darwin" {
|
||||||
|
return []string{"-arch", "arm64"}
|
||||||
|
}
|
||||||
case "386":
|
case "386":
|
||||||
return []string{"-m32"}
|
return []string{"-m32"}
|
||||||
case "arm":
|
case "arm":
|
||||||
|
|
|
||||||
|
|
@ -2435,7 +2435,7 @@ func (b *Builder) fcExe() []string {
|
||||||
func (b *Builder) compilerExe(envValue string, def string) []string {
|
func (b *Builder) compilerExe(envValue string, def string) []string {
|
||||||
compiler := strings.Fields(envValue)
|
compiler := strings.Fields(envValue)
|
||||||
if len(compiler) == 0 {
|
if len(compiler) == 0 {
|
||||||
compiler = []string{def}
|
compiler = strings.Fields(def)
|
||||||
}
|
}
|
||||||
return compiler
|
return compiler
|
||||||
}
|
}
|
||||||
|
|
@ -2581,7 +2581,14 @@ func (b *Builder) gccArchArgs() []string {
|
||||||
case "386":
|
case "386":
|
||||||
return []string{"-m32"}
|
return []string{"-m32"}
|
||||||
case "amd64":
|
case "amd64":
|
||||||
|
if cfg.Goos == "darwin" {
|
||||||
|
return []string{"-arch", "x86_64", "-m64"}
|
||||||
|
}
|
||||||
return []string{"-m64"}
|
return []string{"-m64"}
|
||||||
|
case "arm64":
|
||||||
|
if cfg.Goos == "darwin" {
|
||||||
|
return []string{"-arch", "arm64"}
|
||||||
|
}
|
||||||
case "arm":
|
case "arm":
|
||||||
return []string{"-marm"} // not thumb
|
return []string{"-marm"} // not thumb
|
||||||
case "s390x":
|
case "s390x":
|
||||||
|
|
|
||||||
24
src/cmd/go/testdata/script/build_darwin_cc_arch.txt
vendored
Normal file
24
src/cmd/go/testdata/script/build_darwin_cc_arch.txt
vendored
Normal file
|
|
@ -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() {}
|
||||||
|
|
@ -1749,12 +1749,19 @@ func hostlinkArchArgs(arch *sys.Arch) []string {
|
||||||
switch arch.Family {
|
switch arch.Family {
|
||||||
case sys.I386:
|
case sys.I386:
|
||||||
return []string{"-m32"}
|
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"}
|
return []string{"-m64"}
|
||||||
case sys.ARM:
|
case sys.ARM:
|
||||||
return []string{"-marm"}
|
return []string{"-marm"}
|
||||||
case sys.ARM64:
|
case sys.ARM64:
|
||||||
// nothing needed
|
if objabi.GOOS == "darwin" {
|
||||||
|
return []string{"-arch", "arm64"}
|
||||||
|
}
|
||||||
case sys.MIPS64:
|
case sys.MIPS64:
|
||||||
return []string{"-mabi=64"}
|
return []string{"-mabi=64"}
|
||||||
case sys.MIPS:
|
case sys.MIPS:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue