test: recognize cgo build tag

This requires us to add a fake argument to issue36705.go so that the
test driver will build it with "go run" rather than "go tool compile".

Change-Id: Id08b97d898ee3e9d6c1fbb072a0a9317ed9faedd
Reviewed-on: https://go-review.googlesource.com/c/go/+/304569
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Ian Lance Taylor 2021-03-24 13:16:42 -07:00
parent dade83a588
commit 63e9f6d5f0
2 changed files with 18 additions and 7 deletions

View file

@ -1,5 +1,5 @@
// +build cgo
// run
// run fake-arg-to-force-use-of-go-run
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

View file

@ -56,6 +56,7 @@ func defaultAllCodeGen() bool {
var (
goos, goarch string
cgoEnabled bool
// dirs are the directories to look for *.go files in.
// TODO(bradfitz): just use all directories?
@ -82,6 +83,10 @@ func main() {
goos = getenv("GOOS", runtime.GOOS)
goarch = getenv("GOARCH", runtime.GOARCH)
cgoEnv, err := exec.Command(goTool(), "env", "CGO_ENABLED").Output()
if err == nil {
cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(string(cgoEnv)))
}
findExecCmd()
@ -367,9 +372,10 @@ func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
}
type context struct {
GOOS string
GOARCH string
noOptEnv bool
GOOS string
GOARCH string
cgoEnabled bool
noOptEnv bool
}
// shouldTest looks for build tags in a source file and returns
@ -391,9 +397,10 @@ func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
}
gcFlags := os.Getenv("GO_GCFLAGS")
ctxt := &context{
GOOS: goos,
GOARCH: goarch,
noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
GOOS: goos,
GOARCH: goarch,
cgoEnabled: cgoEnabled,
noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
}
words := strings.Fields(line)
@ -448,6 +455,10 @@ func (ctxt *context) match(name string) bool {
}
}
if name == "cgo" && ctxt.cgoEnabled {
return true
}
if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
return true
}