github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/runtime/internal/sys/gengoos.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 // +build ignore 7 8 package main 9 10 import ( 11 "bytes" 12 "fmt" 13 "log" 14 "os" 15 "strconv" 16 "strings" 17 ) 18 19 var gooses, goarches []string 20 21 func main() { 22 data, err := os.ReadFile("../../../go/build/syslist.go") 23 if err != nil { 24 log.Fatal(err) 25 } 26 const ( 27 goosPrefix = `const goosList = ` 28 goarchPrefix = `const goarchList = ` 29 ) 30 for _, line := range strings.Split(string(data), "\n") { 31 if strings.HasPrefix(line, goosPrefix) { 32 text, err := strconv.Unquote(strings.TrimPrefix(line, goosPrefix)) 33 if err != nil { 34 log.Fatalf("parsing goosList: %v", err) 35 } 36 gooses = strings.Fields(text) 37 } 38 if strings.HasPrefix(line, goarchPrefix) { 39 text, err := strconv.Unquote(strings.TrimPrefix(line, goarchPrefix)) 40 if err != nil { 41 log.Fatalf("parsing goarchList: %v", err) 42 } 43 goarches = strings.Fields(text) 44 } 45 } 46 47 for _, target := range gooses { 48 if target == "nacl" { 49 continue 50 } 51 var tags []string 52 if target == "linux" { 53 tags = append(tags, "!android") // must explicitly exclude android for linux 54 } 55 if target == "solaris" { 56 tags = append(tags, "!illumos") // must explicitly exclude illumos for solaris 57 } 58 if target == "darwin" { 59 tags = append(tags, "!ios") // must explicitly exclude ios for darwin 60 } 61 tags = append(tags, target) // must explicitly include target for bootstrapping purposes 62 var buf bytes.Buffer 63 fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n") 64 fmt.Fprintf(&buf, "//go:build %s\n", strings.Join(tags, " && ")) 65 fmt.Fprintf(&buf, "// +build %s\n\n", strings.Join(tags, ",")) 66 fmt.Fprintf(&buf, "package sys\n\n") 67 fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target) 68 for _, goos := range gooses { 69 value := 0 70 if goos == target { 71 value = 1 72 } 73 fmt.Fprintf(&buf, "const Goos%s = %d\n", strings.Title(goos), value) 74 } 75 err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666) 76 if err != nil { 77 log.Fatal(err) 78 } 79 } 80 81 for _, target := range goarches { 82 if target == "amd64p32" { 83 continue 84 } 85 var buf bytes.Buffer 86 fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n") 87 fmt.Fprintf(&buf, "//go:build %s\n", target) 88 fmt.Fprintf(&buf, "// +build %s\n\n", target) // must explicitly include target for bootstrapping purposes 89 fmt.Fprintf(&buf, "package sys\n\n") 90 fmt.Fprintf(&buf, "const GOARCH = `%s`\n\n", target) 91 for _, goarch := range goarches { 92 value := 0 93 if goarch == target { 94 value = 1 95 } 96 fmt.Fprintf(&buf, "const Goarch%s = %d\n", strings.Title(goarch), value) 97 } 98 err := os.WriteFile("zgoarch_"+target+".go", buf.Bytes(), 0666) 99 if err != nil { 100 log.Fatal(err) 101 } 102 } 103 }