Skip to content

Commit e14339d

Browse files
cmd/go: don't crash on unknown GOARCH unless we actually care
For example, "GOARCH=sparc go build -compiler=gccgo" should not crash merely because the architecture character for sparc is not known. Change-Id: I18912c7f5d90ef8f586592235ec9d6e5053e4bef Reviewed-on: https://go-review.googlesource.com/7695 Reviewed-by: Russ Cox <[email protected]>
1 parent db96e68 commit e14339d

2 files changed

Lines changed: 36 additions & 25 deletions

File tree

src/cmd/go/build.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,10 @@ func runInstall(cmd *Command, args []string) {
384384
var (
385385
goarch string
386386
goos string
387-
archChar string
388387
exeSuffix string
388+
389+
archCharVal string
390+
archCharErr error
389391
)
390392

391393
func init() {
@@ -394,16 +396,16 @@ func init() {
394396
if goos == "windows" {
395397
exeSuffix = ".exe"
396398
}
397-
var err error
398-
archChar, err = build.ArchChar(goarch)
399-
if err != nil {
400-
if _, isgc := buildToolchain.(gcToolchain); isgc {
401-
fatalf("%s", err)
402-
}
403-
// archChar is only required for gcToolchain, if we're using
404-
// another toolchain leave it blank.
405-
archChar = ""
399+
archCharVal, archCharErr = build.ArchChar(goarch)
400+
}
401+
402+
// archChar returns the architecture character. This is only needed
403+
// for the gc toolchain, so only fail if we actually need it.
404+
func archChar() string {
405+
if archCharErr != nil {
406+
fatalf("%s", archCharErr)
406407
}
408+
return archCharVal
407409
}
408410

409411
// A builder holds global state about a build.
@@ -839,7 +841,7 @@ func (b *builder) build(a *action) (err error) {
839841
fmt.Fprintf(os.Stderr, "%s\n", a.p.ImportPath)
840842
}
841843

842-
if a.p.Standard && a.p.ImportPath == "runtime" && buildContext.Compiler == "gc" &&
844+
if a.p.Standard && a.p.ImportPath == "runtime" && buildContext.Compiler == "gc" && archChar() != "" &&
843845
(!hasString(a.p.GoFiles, "zgoos_"+buildContext.GOOS+".go") ||
844846
!hasString(a.p.GoFiles, "zgoarch_"+buildContext.GOARCH+".go")) {
845847
return fmt.Errorf("%s/%s must be bootstrapped using make%v", buildContext.GOOS, buildContext.GOARCH, defaultSuffix())
@@ -1002,9 +1004,11 @@ func (b *builder) build(a *action) (err error) {
10021004
}
10031005
}
10041006

1005-
objExt := archChar
1007+
var objExt string
10061008
if _, ok := buildToolchain.(gccgoToolchain); ok {
10071009
objExt = "o"
1010+
} else {
1011+
objExt = archChar()
10081012
}
10091013

10101014
for _, file := range cfiles {
@@ -1637,18 +1641,18 @@ func (noToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error
16371641
type gcToolchain struct{}
16381642

16391643
func (gcToolchain) compiler() string {
1640-
return tool(archChar + "g")
1644+
return tool(archChar() + "g")
16411645
}
16421646

16431647
func (gcToolchain) linker() string {
1644-
return tool(archChar + "l")
1648+
return tool(archChar() + "l")
16451649
}
16461650

16471651
func (gcToolchain) gc(b *builder, p *Package, archive, obj string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, output []byte, err error) {
16481652
if archive != "" {
16491653
ofile = archive
16501654
} else {
1651-
out := "_go_." + archChar
1655+
out := "_go_." + archChar()
16521656
ofile = obj + out
16531657
}
16541658

@@ -1677,7 +1681,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, asmhdr bool,
16771681
gcargs = append(gcargs, "-installsuffix", buildContext.InstallSuffix)
16781682
}
16791683

1680-
args := []interface{}{buildToolExec, tool(archChar + "g"), "-o", ofile, "-trimpath", b.work, buildGcflags, gcargs, "-D", p.localPrefix, importArgs}
1684+
args := []interface{}{buildToolExec, tool(archChar() + "g"), "-o", ofile, "-trimpath", b.work, buildGcflags, gcargs, "-D", p.localPrefix, importArgs}
16811685
if ofile == archive {
16821686
args = append(args, "-pack")
16831687
}
@@ -1706,7 +1710,7 @@ func (gcToolchain) asm(b *builder, p *Package, obj, ofile, sfile string) error {
17061710
return err
17071711
}
17081712
if verifyAsm && goarch != "arm64" {
1709-
if err := toolVerify(b, p, "old"+archChar+"a", ofile, args); err != nil {
1713+
if err := toolVerify(b, p, "old"+archChar()+"a", ofile, args); err != nil {
17101714
return err
17111715
}
17121716
}
@@ -1882,7 +1886,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
18821886
}
18831887
}
18841888
ldflags = append(ldflags, buildLdflags...)
1885-
return b.run(".", p.ImportPath, nil, buildToolExec, tool(archChar+"l"), "-o", out, importArgs, ldflags, mainpkg)
1889+
return b.run(".", p.ImportPath, nil, buildToolExec, tool(archChar()+"l"), "-o", out, importArgs, ldflags, mainpkg)
18861890
}
18871891

18881892
func (gcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
@@ -2175,12 +2179,12 @@ func (b *builder) ccompilerCmd(envvar, defcmd, objdir string) []string {
21752179

21762180
// gccArchArgs returns arguments to pass to gcc based on the architecture.
21772181
func (b *builder) gccArchArgs() []string {
2178-
switch archChar {
2179-
case "8":
2182+
switch goarch {
2183+
case "386":
21802184
return []string{"-m32"}
2181-
case "6":
2185+
case "amd64", "amd64p32":
21822186
return []string{"-m64"}
2183-
case "5":
2187+
case "arm":
21842188
return []string{"-marm"} // not thumb
21852189
}
21862190
return nil
@@ -2245,7 +2249,12 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
22452249
cgoflags := []string{}
22462250
// TODO: make cgo not depend on $GOARCH?
22472251

2248-
objExt := archChar
2252+
var objExt string
2253+
if _, ok := buildToolchain.(gccgoToolchain); ok {
2254+
objExt = "o"
2255+
} else {
2256+
objExt = archChar()
2257+
}
22492258

22502259
if p.Standard && p.ImportPath == "runtime/cgo" {
22512260
cgoflags = append(cgoflags, "-import_runtime_cgo=false")
@@ -2269,7 +2278,6 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
22692278
if pkgpath := gccgoPkgpath(p); pkgpath != "" {
22702279
cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
22712280
}
2272-
objExt = "o"
22732281
}
22742282
if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, cgoflags, "--", cgoCPPFLAGS, cgoexeCFLAGS, cgofiles); err != nil {
22752283
return nil, nil, err

src/cmd/go/env.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func mkEnv() []envVar {
3636
env := []envVar{
3737
{"GOARCH", goarch},
3838
{"GOBIN", gobin},
39-
{"GOCHAR", archChar},
4039
{"GOEXE", exeSuffix},
4140
{"GOHOSTARCH", runtime.GOARCH},
4241
{"GOHOSTOS", runtime.GOOS},
@@ -50,6 +49,10 @@ func mkEnv() []envVar {
5049
{"TERM", "dumb"},
5150
}
5251

52+
if archCharErr == nil {
53+
env = append(env, envVar{"GOCHAR", archChar()})
54+
}
55+
5356
if goos != "plan9" {
5457
cmd := b.gccCmd(".")
5558
env = append(env, envVar{"CC", cmd[0]})

0 commit comments

Comments
 (0)