Skip to content

Commit

Permalink
cmd: consolidate "known" os/arch tables into separate package
Browse files Browse the repository at this point in the history
Common up the the "known OS/Arch" tables from { cmd/go/internal/imports,
cmd/go/internal/modindex, go/build } and relocate them to a new
package, internal/syslist. No change in functionality.

Updates #68606.

Change-Id: I6414a05c96b8fddbdbd9678d322cb49d9b1b0af3
Reviewed-on: https://go-review.googlesource.com/c/go/+/601357
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Michael Matloob <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
thanm committed Jul 29, 2024
1 parent 5feca45 commit 9f0491c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 170 deletions.
75 changes: 5 additions & 70 deletions src/cmd/go/internal/imports/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"go/build/constraint"
"internal/syslist"
"strings"
"unicode"
)
Expand Down Expand Up @@ -213,7 +214,7 @@ func matchTag(name string, tags map[string]bool, prefer bool) bool {
case "darwin":
return tags["ios"]
case "unix":
return unixOS[cfg.BuildContext.GOOS]
return syslist.UnixOS[cfg.BuildContext.GOOS]
default:
return false
}
Expand Down Expand Up @@ -295,80 +296,14 @@ func MatchFile(name string, tags map[string]bool) bool {
l = l[:n-1]
}
n := len(l)
if n >= 2 && KnownOS[l[n-2]] && KnownArch[l[n-1]] {
if n >= 2 && syslist.KnownOS[l[n-2]] && syslist.KnownArch[l[n-1]] {
return matchTag(l[n-2], tags, true) && matchTag(l[n-1], tags, true)
}
if n >= 1 && KnownOS[l[n-1]] {
if n >= 1 && syslist.KnownOS[l[n-1]] {
return matchTag(l[n-1], tags, true)
}
if n >= 1 && KnownArch[l[n-1]] {
if n >= 1 && syslist.KnownArch[l[n-1]] {
return matchTag(l[n-1], tags, true)
}
return true
}

var KnownOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"js": true,
"linux": true,
"nacl": true, // legacy; don't remove
"netbsd": true,
"openbsd": true,
"plan9": true,
"solaris": true,
"wasip1": true,
"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 is the same list as in go/build/syslist.go and 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,
}

var KnownArch = map[string]bool{
"386": true,
"amd64": true,
"amd64p32": true, // legacy; don't remove
"arm": true,
"armbe": true,
"arm64": true,
"arm64be": true,
"ppc64": true,
"ppc64le": true,
"mips": true,
"mipsle": true,
"mips64": true,
"mips64le": true,
"mips64p32": true,
"mips64p32le": true,
"loong64": true,
"ppc": true,
"riscv": true,
"riscv64": true,
"s390": true,
"s390x": true,
"sparc": true,
"sparc64": true,
"wasm": true,
}
7 changes: 4 additions & 3 deletions src/cmd/go/internal/modindex/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go/build"
"go/build/constraint"
"go/token"
"internal/syslist"
"io"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -878,7 +879,7 @@ 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] {
if name == "unix" && syslist.UnixOS[ctxt.GOOS] {
return true
}
if name == "boringcrypto" {
Expand Down Expand Up @@ -941,14 +942,14 @@ func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
l = l[:n-1]
}
n := len(l)
if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
if n >= 2 && syslist.KnownOS[l[n-2]] && syslist.KnownArch[l[n-1]] {
if allTags != nil {
// In case we short-circuit on l[n-1].
allTags[l[n-2]] = true
}
return ctxt.matchTag(l[n-1], allTags) && ctxt.matchTag(l[n-2], allTags)
}
if n >= 1 && (knownOS[l[n-1]] || knownArch[l[n-1]]) {
if n >= 1 && (syslist.KnownOS[l[n-1]] || syslist.KnownArch[l[n-1]]) {
return ctxt.matchTag(l[n-1], allTags)
}
return true
Expand Down
78 changes: 0 additions & 78 deletions src/cmd/go/internal/modindex/syslist.go

This file was deleted.

6 changes: 3 additions & 3 deletions src/cmd/go/internal/script/conds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package script

import (
"cmd/go/internal/imports"
"fmt"
"internal/syslist"
"os"
"runtime"
"sync"
Expand All @@ -25,7 +25,7 @@ func DefaultConds() map[string]Cond {
if suffix == runtime.GOOS {
return true, nil
}
if _, ok := imports.KnownOS[suffix]; !ok {
if _, ok := syslist.KnownOS[suffix]; !ok {
return false, fmt.Errorf("unrecognized GOOS %q", suffix)
}
return false, nil
Expand All @@ -37,7 +37,7 @@ func DefaultConds() map[string]Cond {
if suffix == runtime.GOARCH {
return true, nil
}
if _, ok := imports.KnownArch[suffix]; !ok {
if _, ok := syslist.KnownArch[suffix]; !ok {
return false, fmt.Errorf("unrecognized GOOS %q", suffix)
}
return false, nil
Expand Down
7 changes: 4 additions & 3 deletions src/go/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"internal/goroot"
"internal/goversion"
"internal/platform"
"internal/syslist"
"io"
"io/fs"
"os"
Expand Down Expand Up @@ -1976,7 +1977,7 @@ 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] {
if name == "unix" && syslist.UnixOS[ctxt.GOOS] {
return true
}
if name == "boringcrypto" {
Expand Down Expand Up @@ -2039,14 +2040,14 @@ func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
l = l[:n-1]
}
n := len(l)
if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
if n >= 2 && syslist.KnownOS[l[n-2]] && syslist.KnownArch[l[n-1]] {
if allTags != nil {
// In case we short-circuit on l[n-1].
allTags[l[n-2]] = true
}
return ctxt.matchTag(l[n-1], allTags) && ctxt.matchTag(l[n-2], allTags)
}
if n >= 1 && (knownOS[l[n-1]] || knownArch[l[n-1]]) {
if n >= 1 && (syslist.KnownOS[l[n-1]] || syslist.KnownArch[l[n-1]]) {
return ctxt.matchTag(l[n-1], allTags)
}
return true
Expand Down
3 changes: 2 additions & 1 deletion src/go/build/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var depsRules = `
internal/nettrace,
internal/platform,
internal/profilerecord,
internal/syslist,
internal/trace/traceviewer/format,
log/internal,
math/bits,
Expand Down Expand Up @@ -337,7 +338,7 @@ var depsRules = `
go/doc/comment, go/parser, internal/lazyregexp, text/template
< go/doc;
go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform, internal/syslist
< go/build;
# databases
Expand Down
4 changes: 2 additions & 2 deletions src/internal/goarch/gengoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
var goarches []string

func main() {
data, err := os.ReadFile("../../go/build/syslist.go")
data, err := os.ReadFile("../../internal/syslist/syslist.go")
if err != nil {
log.Fatal(err)
}
const goarchPrefix = `var knownArch = map[string]bool{`
const goarchPrefix = `var KnownArch = map[string]bool{`
inGOARCH := false
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, goarchPrefix) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/goos/gengoos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
var gooses []string

func main() {
data, err := os.ReadFile("../../go/build/syslist.go")
data, err := os.ReadFile("../../internal/syslist/syslist..go")
if err != nil {
log.Fatal(err)
}
const goosPrefix = `var knownOS = map[string]bool{`
const goosPrefix = `var KnownOS = map[string]bool{`
inGOOS := false
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, goosPrefix) {
Expand Down
19 changes: 11 additions & 8 deletions src/go/build/syslist.go → src/internal/syslist/syslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package build
// Package syslist stores tables of OS and ARCH names that are
// (or at one point were) acceptable build targets.

package syslist

// Note that this file is read by internal/goarch/gengoarch.go and by
// internal/goos/gengoos.go. If you change this file, look at those
// files as well.

// knownOS is the list of past, present, and future known GOOS values.
// 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{
// If you add an entry to this list, look at UnixOS, below.
var KnownOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
Expand All @@ -32,11 +35,11 @@ var knownOS = map[string]bool{
"zos": true,
}

// unixOS is the set of GOOS values matched by the "unix" build tag.
// 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 and
// cmd/go/internal/imports/build.go.
var unixOS = map[string]bool{
var UnixOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
Expand All @@ -51,9 +54,9 @@ var unixOS = map[string]bool{
"solaris": true,
}

// knownArch is the list of past, present, and future known GOARCH values.
// 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{
var KnownArch = map[string]bool{
"386": true,
"amd64": true,
"amd64p32": true,
Expand Down

0 comments on commit 9f0491c

Please sign in to comment.