github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/utils/goos/goos.go (about) 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 5 //go:build ignore 6 7 package main 8 9 import ( 10 "bytes" 11 "fmt" 12 "log" 13 "os" 14 "strings" 15 ) 16 17 // Copyright 2015 The Go Authors. All rights reserved. 18 // Use of this source code is governed by a BSD-style 19 // license that can be found in the LICENSE file. 20 21 // package goos contains GOOS-specific constants. 22 23 // The next line makes 'go generate' write the zgoos*.go files with 24 // per-OS information, including constants named Is$GOOS for every 25 // known GOOS. The constant is 1 on the current system, 0 otherwise; 26 // multiplying by them is useful for defining GOOS-specific constants. 27 // 28 //go:generate go run gengoos.go 29 30 var gooses []string 31 32 func main() { 33 data, err := os.ReadFile("syslist.go") 34 if err != nil { 35 log.Fatal(err) 36 } 37 const goosPrefix = `var knownOS = map[string]bool{` 38 inGOOS := false 39 for _, line := range strings.Split(string(data), "\n") { 40 if strings.HasPrefix(line, goosPrefix) { 41 inGOOS = true 42 } else if inGOOS && strings.HasPrefix(line, "}") { 43 break 44 } else if inGOOS { 45 goos := strings.Fields(line)[0] 46 goos = strings.TrimPrefix(goos, `"`) 47 goos = strings.TrimSuffix(goos, `":`) 48 gooses = append(gooses, goos) 49 } 50 } 51 52 for _, target := range gooses { 53 if target == "nacl" { 54 continue 55 } 56 var tags []string 57 if target == "linux" { 58 tags = append(tags, "!android") // must explicitly exclude android for linux 59 } 60 if target == "solaris" { 61 tags = append(tags, "!illumos") // must explicitly exclude illumos for solaris 62 } 63 if target == "darwin" { 64 tags = append(tags, "!ios") // must explicitly exclude ios for darwin 65 } 66 tags = append(tags, target) // must explicitly include target for bootstrapping purposes 67 var buf bytes.Buffer 68 fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n") 69 fmt.Fprintf(&buf, "//go:build %s\n\n", strings.Join(tags, " && ")) 70 fmt.Fprintf(&buf, "package goos\n\n") 71 fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target) 72 for _, goos := range gooses { 73 value := 0 74 if goos == target { 75 value = 1 76 } 77 fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goos), value) 78 } 79 err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666) 80 if err != nil { 81 log.Fatal(err) 82 } 83 } 84 }