mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 17:21:18 -08:00
feature/featuretags: add MinTags, use it in three places
cmd/featuretags --min, cmd/tsconnect/wasmbuild, and a cmd/tailscaled test each computed "omit every feature except these" on their own. Move that into featuretags.MinTags so other Go code (such as upcoming natlab vmtests of minimal builds) can compute the tags without running cmd/featuretags. No behavior change. Updates #12614 Signed-off-by: Brad Fitzpatrick <[email protected]> Change-Id: Ie0a5faf1f1d3e4e035326572b16f20180671d1c4
This commit is contained in:
1 parent
f5f326030b
commit
e987d72be2
5 files changed
+66
-43
No files matched your search
@@ -36,27 +36,19 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
var keep = map[featuretags.FeatureTag]bool{}
|
||||
var keep []featuretags.FeatureTag
|
||||
for t := range strings.SplitSeq(*add, ",") {
|
||||
if t != "" {
|
||||
for ft := range featuretags.Requires(featuretags.FeatureTag(t)) {
|
||||
keep[ft] = true
|
||||
}
|
||||
keep = append(keep, featuretags.FeatureTag(t))
|
||||
}
|
||||
}
|
||||
var tags []string
|
||||
if keep[featuretags.CLI] {
|
||||
tags = append(tags, "ts_include_cli")
|
||||
}
|
||||
if *min {
|
||||
for _, f := range slices.Sorted(maps.Keys(features)) {
|
||||
if f == "" {
|
||||
continue
|
||||
}
|
||||
if !keep[f] && f.IsOmittable() {
|
||||
tags = append(tags, f.OmitTag())
|
||||
}
|
||||
}
|
||||
tags = featuretags.MinTags(keep...)
|
||||
} else if slices.ContainsFunc(keep, func(ft featuretags.FeatureTag) bool {
|
||||
return featuretags.Requires(ft).Contains(featuretags.CLI)
|
||||
}) {
|
||||
tags = append(tags, "ts_include_cli")
|
||||
}
|
||||
removeSet := set.Set[featuretags.FeatureTag]{}
|
||||
for v := range strings.SplitSeq(*remove, ",") {
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -304,13 +302,7 @@ func TestOmitUseProxy(t *testing.T) {
|
||||
}
|
||||
|
||||
func minTags() string {
|
||||
var tags []string
|
||||
for _, f := range slices.Sorted(maps.Keys(featuretags.Features)) {
|
||||
if f.IsOmittable() {
|
||||
tags = append(tags, f.OmitTag())
|
||||
}
|
||||
}
|
||||
return strings.Join(tags, ",")
|
||||
return strings.Join(featuretags.MinTags(), ",")
|
||||
}
|
||||
|
||||
func TestMinTailscaledNoCLI(t *testing.T) {
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"tailscale.com/feature/featuretags"
|
||||
@@ -113,23 +112,7 @@ type BuildInfo struct {
|
||||
// the same string (and therefore the same wasm bytes, given identical
|
||||
// inputs to `go build`).
|
||||
func Tags() string {
|
||||
keep := map[featuretags.FeatureTag]bool{}
|
||||
for _, ft := range Keep {
|
||||
for dep := range featuretags.Requires(ft) {
|
||||
keep[dep] = true
|
||||
}
|
||||
}
|
||||
var tags []string
|
||||
for ft := range featuretags.Features {
|
||||
if ft == "" || !ft.IsOmittable() {
|
||||
continue
|
||||
}
|
||||
if !keep[ft] {
|
||||
tags = append(tags, ft.OmitTag())
|
||||
}
|
||||
}
|
||||
slices.Sort(tags)
|
||||
return strings.Join(tags, ",")
|
||||
return strings.Join(featuretags.MinTags(Keep...), ",")
|
||||
}
|
||||
|
||||
// ProdCommand returns an *exec.Cmd that runs `go build` for
|
||||
|
||||
@@ -4,7 +4,11 @@
|
||||
// Package featuretags is a registry of all the ts_omit-able build tags.
|
||||
package featuretags
|
||||
|
||||
import "tailscale.com/util/set"
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"tailscale.com/util/set"
|
||||
)
|
||||
|
||||
// CLI is a special feature in the [Features] map that works opposite
|
||||
// from the others: it is opt-in, rather than opt-out, having a different
|
||||
@@ -62,6 +66,28 @@ func RequiredBy(ft FeatureTag) set.Set[FeatureTag] {
|
||||
return s
|
||||
}
|
||||
|
||||
// MinTags returns the sorted Go build tags for a minimal build that
|
||||
// includes only the features in keep and the features they require.
|
||||
// Every other omittable feature in [Features] gets its ts_omit_ tag, and
|
||||
// ts_include_cli is added if [CLI] is kept.
|
||||
func MinTags(keep ...FeatureTag) []string {
|
||||
kept := set.Set[FeatureTag]{}
|
||||
for _, ft := range keep {
|
||||
kept.AddSet(Requires(ft))
|
||||
}
|
||||
var tags []string
|
||||
if kept.Contains(CLI) {
|
||||
tags = append(tags, "ts_include_cli")
|
||||
}
|
||||
for ft := range Features {
|
||||
if ft != "" && ft.IsOmittable() && !kept.Contains(ft) {
|
||||
tags = append(tags, ft.OmitTag())
|
||||
}
|
||||
}
|
||||
slices.Sort(tags)
|
||||
return tags
|
||||
}
|
||||
|
||||
// featureDependsOn reports whether feature a (directly or indirectly) depends on b.
|
||||
// It returns true if a == b.
|
||||
func featureDependsOn(a, b FeatureTag) bool {
|
||||
|
||||
@@ -87,6 +87,36 @@ func TestRequiredBy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinTags(t *testing.T) {
|
||||
has := func(tags []string, tag string) bool { return slices.Contains(tags, tag) }
|
||||
|
||||
all := MinTags()
|
||||
if !slices.IsSorted(all) {
|
||||
t.Errorf("MinTags() not sorted: %v", all)
|
||||
}
|
||||
if has(all, "ts_include_cli") {
|
||||
t.Errorf("MinTags() includes ts_include_cli")
|
||||
}
|
||||
for ft := range Features {
|
||||
if ft.IsOmittable() && !has(all, ft.OmitTag()) {
|
||||
t.Errorf("MinTags() missing %q", ft.OmitTag())
|
||||
}
|
||||
}
|
||||
|
||||
// Keeping webclient must also keep what it requires.
|
||||
got := MinTags("webclient", CLI)
|
||||
for _, tag := range []string{"ts_omit_webclient", "ts_omit_serve", "ts_omit_netstack"} {
|
||||
if has(got, tag) {
|
||||
t.Errorf("MinTags(webclient, cli) includes %q", tag)
|
||||
}
|
||||
}
|
||||
for _, tag := range []string{"ts_include_cli", "ts_omit_drive"} {
|
||||
if !has(got, tag) {
|
||||
t.Errorf("MinTags(webclient, cli) missing %q", tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that all "ts_omit_foo" build tags are declared in featuretags.go
|
||||
func TestAllOmitBuildTagsDeclared(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
|
||||
Reference in new issue
Block a user