gen.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build ignore
  4. // The gens.go program generates the feature_<feature>_enabled.go
  5. // and feature_<feature>_disabled.go files for each feature tag.
  6. package main
  7. import (
  8. "cmp"
  9. "fmt"
  10. "os"
  11. "strings"
  12. "tailscale.com/feature/featuretags"
  13. "tailscale.com/util/must"
  14. )
  15. const header = `// Copyright (c) Tailscale Inc & AUTHORS
  16. // SPDX-License-Identifier: BSD-3-Clause
  17. // Code g|e|n|e|r|a|t|e|d by gen.go; D|O N|OT E|D|I|T.
  18. `
  19. func main() {
  20. header := strings.ReplaceAll(header, "|", "") // to avoid this file being marked as generated
  21. for k, m := range featuretags.Features {
  22. if !k.IsOmittable() {
  23. continue
  24. }
  25. sym := "Has" + cmp.Or(m.Sym, strings.ToUpper(string(k)[:1])+string(k)[1:])
  26. for _, suf := range []string{"enabled", "disabled"} {
  27. bang := ""
  28. if suf == "enabled" {
  29. bang = "!" // !ts_omit_...
  30. }
  31. must.Do(os.WriteFile("feature_"+string(k)+"_"+suf+".go",
  32. fmt.Appendf(nil, "%s//go:build %s%s\n\npackage buildfeatures\n\n"+
  33. "// %s is whether the binary was built with support for modular feature %q.\n"+
  34. "// Specifically, it's whether the binary was NOT built with the %q build tag.\n"+
  35. "// It's a const so it can be used for dead code elimination.\n"+
  36. "const %s = %t\n",
  37. header, bang, k.OmitTag(), sym, m.Desc, k.OmitTag(), sym, suf == "enabled"), 0644))
  38. }
  39. }
  40. }