cmd/go: fix incorrect determining default value of CGO_ENABLED

The default value is the value obtained when
no environment variables are set and go env  -w is not used.

In the past,
we used the current value
(may be modified by an environment variable to a non-default value),
error was used as the default value.

For #69994

Change-Id: Iead3a6cacd04dc51a094ffb9f7bb7553320fcd78
Reviewed-on: https://go-review.googlesource.com/c/go/+/621995
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
qiulaidongfeng 2024-10-23 20:55:43 +08:00 committed by Michael Matloob
parent bd1f9a4963
commit 8cd6d68a08
6 changed files with 13 additions and 23 deletions

View file

@ -673,7 +673,6 @@ var gentab = []struct {
file string
gen func(dir, file string)
}{
{"go/build", "zcgo.go", mkzcgo},
{"cmd/go/internal/cfg", "zdefaultcc.go", mkzdefaultcc},
{"internal/runtime/sys", "zversion.go", mkzversion},
{"time/tzdata", "zzipdata.go", mktzdata},

View file

@ -7,7 +7,6 @@ package main
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
@ -108,22 +107,6 @@ func defaultCCFunc(name string, defaultcc map[string]string) string {
return buf.String()
}
// mkzcgo writes zcgo.go for the go/build package:
//
// package build
// const defaultCGO_ENABLED = <CGO_ENABLED>
//
// It is invoked to write go/build/zcgo.go.
func mkzcgo(dir, file string) {
var buf strings.Builder
writeHeader(&buf)
fmt.Fprintf(&buf, "package build\n")
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "const defaultCGO_ENABLED = %s\n", quote(os.Getenv("CGO_ENABLED")))
writefile(buf.String(), file, writeSkipSame)
}
// mktzdata src/time/tzdata/zzipdata.go:
//
// package tzdata

View file

@ -6,6 +6,7 @@ package main
import (
"fmt"
"os"
"strings"
)
@ -66,6 +67,7 @@ func mkbuildcfg(file string) {
fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
fmt.Fprintf(&buf, "const DefaultGOFIPS140 = `%s`\n", gofips140)
fmt.Fprintf(&buf, "const DefaultCGO_ENABLED = %s\n", quote(os.Getenv("CGO_ENABLED")))
writefile(buf.String(), file, writeSkipSame)
}

View file

@ -13,6 +13,7 @@ import (
"go/build"
"internal/buildcfg"
"internal/cfg"
"internal/platform"
"io"
"io/fs"
"os"
@ -140,10 +141,12 @@ func defaultContext() build.Context {
// Recreate that logic here with the new GOOS/GOARCH setting.
// We need to run steps 2 and 3 to determine what the default value
// of CgoEnabled would be for computing CGOChanged.
defaultCgoEnabled := ctxt.CgoEnabled
if ctxt.GOOS != runtime.GOOS || ctxt.GOARCH != runtime.GOARCH {
defaultCgoEnabled = false
} else {
defaultCgoEnabled := false
if buildcfg.DefaultCGO_ENABLED == "1" {
defaultCgoEnabled = true
} else if buildcfg.DefaultCGO_ENABLED == "0" {
} else if runtime.GOARCH == ctxt.GOARCH && runtime.GOOS == ctxt.GOOS {
defaultCgoEnabled = platform.CgoSupported(ctxt.GOOS, ctxt.GOARCH)
// Use built-in default cgo setting for GOOS/GOARCH.
// Note that ctxt.GOOS/GOARCH are derived from the preference list
// (1) environment, (2) go/env file, (3) runtime constants,

View file

@ -11,6 +11,7 @@ env GO111MODULE=auto
env CGO_CFLAGS=nodefault
env CGO_CPPFLAGS=nodefault
env GOFIPS140=latest
[cgo] env CGO_ENABLED=0
go env -changed
# linux output like GOTOOLCHAIN='local'
@ -22,6 +23,7 @@ stdout 'GO111MODULE=''?auto''?'
stdout 'CGO_CFLAGS=''?nodefault''?'
stdout 'CGO_CPPFLAGS=''?nodefault''?'
stdout 'GOFIPS140=''?latest''?'
[cgo] stdout 'CGO_ENABLED=''?0''?'
go env -changed -json
stdout '"GOTOOLCHAIN": "local"'
@ -31,6 +33,7 @@ stdout '"GO111MODULE": "auto"'
stdout '"CGO_CFLAGS": "nodefault"'
stdout '"CGO_CPPFLAGS": "nodefault"'
stdout '"GOFIPS140": "latest"'
[cgo] stdout '"CGO_ENABLED": "0"'
[GOOS:windows] env GOOS=linux
[!GOOS:windows] env GOOS=windows

View file

@ -359,7 +359,7 @@ func defaultContext() Context {
env := os.Getenv("CGO_ENABLED")
if env == "" {
env = defaultCGO_ENABLED
env = buildcfg.DefaultCGO_ENABLED
}
switch env {
case "1":