Skip to content

Commit

Permalink
go/build: recognize "unix" build tag
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Rob Pike <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
ianlancetaylor committed Mar 28, 2022
1 parent 32fdad1 commit c3fcd01
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
37 changes: 33 additions & 4 deletions src/cmd/dist/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cmd/go/internal/help/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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').
Expand Down
3 changes: 3 additions & 0 deletions src/go/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 24 additions & 3 deletions src/go/build/syslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit c3fcd01

Please sign in to comment.