1
0
mirror of https://github.com/golang/go synced 2024-11-24 07:40:17 -07:00

go/build: recognize "unix" build tag

The new "unix" build tag matches any Unix or Unix-like system.
This is only recognized on go:build lines, not in file names.

For #20322
Fixes #51572

Change-Id: I3a991f9e69353b25e259bc6462709cdcd83640fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/389934
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Ian Lance Taylor 2022-03-03 18:21:37 -08:00
parent 32fdad19a2
commit c3fcd01177
5 changed files with 62 additions and 7 deletions

37
src/cmd/dist/build.go vendored
View File

@ -976,12 +976,41 @@ func packagefile(pkg string) string {
return pathf("%s/pkg/%s_%s/%s.a", goroot, goos, goarch, pkg)
}
// unixOS is the set of GOOS values matched by the "unix" build tag.
// This is the same list as in go/build/syslist.go.
var unixOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"linux": true,
"netbsd": true,
"openbsd": true,
"solaris": true,
}
// matchtag reports whether the tag matches this build.
func matchtag(tag string) bool {
return tag == "gc" || tag == goos || tag == goarch || tag == "cmd_go_bootstrap" || tag == "go1.1" ||
(goos == "android" && tag == "linux") ||
(goos == "illumos" && tag == "solaris") ||
(goos == "ios" && tag == "darwin")
switch tag {
case "gc", "cmd_go_bootstrap", "go1.1":
return true
case "linux":
return goos == "linux" || goos == "android"
case "solaris":
return goos == "solaris" || goos == "illumos"
case "darwin":
return goos == "darwin" || goos == "ios"
case goos, goarch:
return true
case "unix":
return unixOS[goos]
default:
return false
}
}
// shouldbuild reports whether we should build this file.

View File

@ -1844,6 +1844,7 @@
// GOOS environment variable.
// - the target architecture, as spelled by runtime.GOARCH, set with the
// GOARCH environment variable.
// - "unix", if GOOS is a Unix or Unix-like system.
// - the compiler being used, either "gc" or "gccgo"
// - "cgo", if the cgo command is supported (see CGO_ENABLED in
// 'go help environment').

View File

@ -843,6 +843,7 @@ During a particular build, the following words are satisfied:
GOOS environment variable.
- the target architecture, as spelled by runtime.GOARCH, set with the
GOARCH environment variable.
- "unix", if GOOS is a Unix or Unix-like system.
- the compiler being used, either "gc" or "gccgo"
- "cgo", if the cgo command is supported (see CGO_ENABLED in
'go help environment').

View File

@ -1913,6 +1913,9 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
if ctxt.GOOS == "ios" && name == "darwin" {
return true
}
if name == "unix" && unixOS[ctxt.GOOS] {
return true
}
// other tags
for _, tag := range ctxt.BuildTags {

View File

@ -4,9 +4,9 @@
package build
// Past, present, and future known GOOS and GOARCH values.
// Do not remove from this list, as these are used for go/build filename matching.
// knownOS is the list of past, present, and future known GOOS values.
// Do not remove from this list, as it is used for filename matching.
// If you add an entry to this list, look at unixOS, below.
var knownOS = map[string]bool{
"aix": true,
"android": true,
@ -26,6 +26,27 @@ var knownOS = map[string]bool{
"windows": true,
"zos": true,
}
// unixOS is the set of GOOS values matched by the "unix" build tag.
// This is not used for filename matching.
// This list also appears in cmd/dist/build.go.
var unixOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"linux": true,
"netbsd": true,
"openbsd": true,
"solaris": true,
}
// knownArch is the list of past, present, and future known GOARCH values.
// Do not remove from this list, as it is used for filename matching.
var knownArch = map[string]bool{
"386": true,
"amd64": true,