Skip to content

Commit d22f287

Browse files
Bryan C. Millsgopherbot
authored andcommitted
cmd/dist: refactor generated cgo-support logic
During bootstrapping, cmd/dist writes a file indicating which GOOS/GOARCH combinations are valid, and which support cgo-enabled builds. That information previously went into the go/build package, but today it fits in more naturally in the internal/platform package (which already has a number of functions indicating feature support for GOOS/GOARCH combinations). Moreover, as of CL 450739 the cmd/go logic for determining whether to use cgo is somewhat more nuanced than the go/build logic: cmd/go checks for the presence of a C compiler, whereas go/build does not (mostly because it determines its configuration at package-init time, and checking $PATH for a C compiler is somewhat expensive). To simplify this situation, this change: - consolidates the “cgo supported” check in internal/platform (alongside many other platform-support checks) instead of making it a one-off in go/build, - and updates a couple of tests to use testenv.HasCGO instead of build.Default.CgoEnabled to decide whether to test a cgo-specific behavior. For #58884. For #59500. Change-Id: I0bb2502dba4545a3d98c9e915727382ce536a0f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/483695 Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
1 parent 0d699b6 commit d22f287

File tree

15 files changed

+55
-49
lines changed

15 files changed

+55
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _testmain.go
3838
/src/go/build/zcgo.go
3939
/src/go/doc/headscan
4040
/src/internal/buildcfg/zbootstrap.go
41+
/src/internal/platform/zosarch.go
4142
/src/runtime/internal/sys/zversion.go
4243
/src/unicode/maketables
4344
/src/time/tzdata/zzipdata.go

src/cmd/dist/build.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,16 @@ var deptab = []struct {
626626
}{
627627
{"cmd/go/internal/cfg", []string{
628628
"zdefaultcc.go",
629+
}},
630+
{"go/build", []string{
631+
"zcgo.go",
632+
}},
633+
{"internal/platform", []string{
629634
"zosarch.go",
630635
}},
631636
{"runtime/internal/sys", []string{
632637
"zversion.go",
633638
}},
634-
{"go/build", []string{
635-
"zcgo.go",
636-
}},
637639
{"time/tzdata", []string{
638640
"zzipdata.go",
639641
}},
@@ -650,10 +652,10 @@ var gentab = []struct {
650652
nameprefix string
651653
gen func(string, string)
652654
}{
655+
{"zcgo.go", mkzcgo},
653656
{"zdefaultcc.go", mkzdefaultcc},
654657
{"zosarch.go", mkzosarch},
655658
{"zversion.go", mkzversion},
656-
{"zcgo.go", mkzcgo},
657659
{"zzipdata.go", mktzdata},
658660

659661
// not generated anymore, but delete the file if we see it
@@ -1196,6 +1198,7 @@ var cleanlist = []string{
11961198
"runtime/internal/sys",
11971199
"cmd/cgo",
11981200
"cmd/go/internal/cfg",
1201+
"internal/platform",
11991202
"go/build",
12001203
}
12011204

src/cmd/dist/buildgo.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,55 +91,19 @@ func defaultCCFunc(name string, defaultcc map[string]string) string {
9191
return buf.String()
9292
}
9393

94-
// mkzosarch writes zosarch.go for cmd/go.
95-
func mkzosarch(dir, file string) {
96-
// sort for deterministic zosarch.go file
97-
var list []string
98-
for plat := range cgoEnabled {
99-
list = append(list, plat)
100-
}
101-
sort.Strings(list)
102-
103-
var buf strings.Builder
104-
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n\n")
105-
fmt.Fprintf(&buf, "package cfg\n\n")
106-
fmt.Fprintf(&buf, "var OSArchSupportsCgo = map[string]bool{\n")
107-
for _, plat := range list {
108-
fmt.Fprintf(&buf, "\t%s: %v,\n", quote(plat), cgoEnabled[plat])
109-
}
110-
fmt.Fprintf(&buf, "}\n")
111-
112-
writefile(buf.String(), file, writeSkipSame)
113-
}
114-
11594
// mkzcgo writes zcgo.go for the go/build package:
11695
//
11796
// package build
118-
// var cgoEnabled = map[string]bool{}
97+
// const defaultCGO_ENABLED = <CGO_ENABLED>
11998
//
12099
// It is invoked to write go/build/zcgo.go.
121100
func mkzcgo(dir, file string) {
122-
// sort for deterministic zcgo.go file
123-
var list []string
124-
for plat, hasCgo := range cgoEnabled {
125-
if hasCgo {
126-
list = append(list, plat)
127-
}
128-
}
129-
sort.Strings(list)
130-
131101
var buf strings.Builder
132102
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
133103
fmt.Fprintln(&buf)
134104
fmt.Fprintf(&buf, "package build\n")
135105
fmt.Fprintln(&buf)
136106
fmt.Fprintf(&buf, "const defaultCGO_ENABLED = %s\n", quote(os.Getenv("CGO_ENABLED")))
137-
fmt.Fprintln(&buf)
138-
fmt.Fprintf(&buf, "var cgoEnabled = map[string]bool{\n")
139-
for _, plat := range list {
140-
fmt.Fprintf(&buf, "\t%s: true,\n", quote(plat))
141-
}
142-
fmt.Fprintf(&buf, "}\n")
143107

144108
writefile(buf.String(), file, writeSkipSame)
145109
}

src/cmd/dist/buildruntime.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"fmt"
9+
"sort"
910
"strings"
1011
)
1112

@@ -82,3 +83,26 @@ func mkobjabi(file string) {
8283

8384
writefile(buf.String(), file, writeSkipSame)
8485
}
86+
87+
// mkzosarch writes zosarch.go for internal/platform.
88+
func mkzosarch(dir, file string) {
89+
// sort for deterministic file contents.
90+
var list []string
91+
for plat := range cgoEnabled {
92+
list = append(list, plat)
93+
}
94+
sort.Strings(list)
95+
96+
var buf strings.Builder
97+
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
98+
fmt.Fprintln(&buf)
99+
fmt.Fprintf(&buf, "package platform\n")
100+
fmt.Fprintln(&buf)
101+
fmt.Fprintf(&buf, "var osArchSupportsCgo = map[string]bool{\n")
102+
for _, plat := range list {
103+
fmt.Fprintf(&buf, "\t\t%s: %v,\n", quote(plat), cgoEnabled[plat])
104+
}
105+
fmt.Fprintf(&buf, "}\n")
106+
107+
writefile(buf.String(), file, writeSkipSame)
108+
}

src/cmd/dist/buildtool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func bootstrapBuildTools() {
121121

122122
mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
123123
mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
124+
mkzosarch("", pathf("%s/src/internal/platform/zosarch.go", goroot))
124125

125126
// Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
126127
// We use a subdirectory of $GOROOT/pkg because that's the

src/cmd/go/internal/cfg/cfg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func defaultContext() build.Context {
141141
// (1) environment, (2) go/env file, (3) runtime constants,
142142
// while go/build.Default.GOOS/GOARCH are derived from the preference list
143143
// (1) environment, (2) runtime constants.
144+
//
144145
// We know ctxt.GOOS/GOARCH == runtime.GOOS/GOARCH;
145146
// no matter how that happened, go/build.Default will make the
146147
// same decision (either the environment variables are set explicitly

src/cmd/go/internal/work/action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"debug/elf"
1515
"encoding/json"
1616
"fmt"
17+
"internal/platform"
1718
"os"
1819
"path/filepath"
1920
"strings"
@@ -355,7 +356,7 @@ func closeBuilders() {
355356
}
356357

357358
func CheckGOOSARCHPair(goos, goarch string) error {
358-
if _, ok := cfg.OSArchSupportsCgo[goos+"/"+goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
359+
if !platform.BuildModeSupported(cfg.BuildContext.Compiler, "default", goos, goarch) {
359360
return fmt.Errorf("unsupported GOOS/GOARCH pair %s/%s", goos, goarch)
360361
}
361362
return nil

src/cmd/go/internal/work/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func buildModeInit() {
283283
base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
284284
}
285285

286-
if !platform.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
286+
if cfg.BuildBuildmode != "default" && !platform.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
287287
base.Fatalf("-buildmode=%s not supported on %s/%s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
288288
}
289289

src/cmd/go/note_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package main_test
66

77
import (
8-
"go/build"
8+
"internal/testenv"
99
"runtime"
1010
"testing"
1111

@@ -32,7 +32,7 @@ func TestNoteReading(t *testing.T) {
3232
}
3333

3434
switch {
35-
case !build.Default.CgoEnabled:
35+
case !testenv.HasCGO():
3636
t.Skipf("skipping - no cgo, so assuming external linking not available")
3737
case runtime.GOOS == "plan9":
3838
t.Skipf("skipping - external linking not supported")

src/cmd/go/testdata/script/list_all_gobuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# go list all should work with GOOS=linux because all packages build on Linux
22
env GOOS=linux
3+
env GOARCH=amd64
34
go list all
45

56
# go list all should work with GOOS=darwin, but it used to fail because

0 commit comments

Comments
 (0)