gengoos.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build ignore
  5. package main
  6. import (
  7. "bytes"
  8. "fmt"
  9. "log"
  10. "os"
  11. "strconv"
  12. "strings"
  13. )
  14. var gooses []string
  15. func main() {
  16. data, err := os.ReadFile("../../go/build/syslist.go")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. const goosPrefix = `const goosList = `
  21. for _, line := range strings.Split(string(data), "\n") {
  22. if strings.HasPrefix(line, goosPrefix) {
  23. text, err := strconv.Unquote(strings.TrimPrefix(line, goosPrefix))
  24. if err != nil {
  25. log.Fatalf("parsing goosList: %v", err)
  26. }
  27. gooses = strings.Fields(text)
  28. }
  29. }
  30. for _, target := range gooses {
  31. if target == "nacl" {
  32. continue
  33. }
  34. var tags []string
  35. if target == "linux" {
  36. tags = append(tags, "!android") // must explicitly exclude android for linux
  37. }
  38. if target == "solaris" {
  39. tags = append(tags, "!illumos") // must explicitly exclude illumos for solaris
  40. }
  41. if target == "darwin" {
  42. tags = append(tags, "!ios") // must explicitly exclude ios for darwin
  43. }
  44. tags = append(tags, target) // must explicitly include target for bootstrapping purposes
  45. var buf bytes.Buffer
  46. fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n")
  47. fmt.Fprintf(&buf, "//go:build %s\n", strings.Join(tags, " && "))
  48. fmt.Fprintf(&buf, "package goos\n\n")
  49. fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target)
  50. for _, goos := range gooses {
  51. value := 0
  52. if goos == target {
  53. value = 1
  54. }
  55. fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goos), value)
  56. }
  57. err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0o666)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. }
  62. }