github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/dist/buildgo.go (about) 1 // Copyright 2012 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 package main 6 7 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "path/filepath" 12 "sort" 13 "strings" 14 ) 15 16 /* 17 * Helpers for building cmd/go and cmd/cgo. 18 */ 19 20 // mkzdefaultcc writes zdefaultcc.go: 21 // 22 // package main 23 // const defaultCC = <defaultcc> 24 // const defaultCXX = <defaultcxx> 25 // const defaultPkgConfig = <defaultpkgconfig> 26 // 27 // It is invoked to write cmd/go/internal/cfg/zdefaultcc.go 28 // but we also write cmd/cgo/zdefaultcc.go 29 func mkzdefaultcc(dir, file string) { 30 if strings.Contains(file, filepath.FromSlash("go/internal/cfg")) { 31 var buf bytes.Buffer 32 fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") 33 fmt.Fprintln(&buf) 34 fmt.Fprintf(&buf, "package cfg\n") 35 fmt.Fprintln(&buf) 36 fmt.Fprintf(&buf, "const DefaultPkgConfig = `%s`\n", defaultpkgconfig) 37 buf.WriteString(defaultCCFunc("DefaultCC", defaultcc)) 38 buf.WriteString(defaultCCFunc("DefaultCXX", defaultcxx)) 39 writefile(buf.String(), file, writeSkipSame) 40 return 41 } 42 43 var buf bytes.Buffer 44 fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") 45 fmt.Fprintln(&buf) 46 fmt.Fprintf(&buf, "package main\n") 47 fmt.Fprintln(&buf) 48 fmt.Fprintf(&buf, "const defaultPkgConfig = `%s`\n", defaultpkgconfig) 49 buf.WriteString(defaultCCFunc("defaultCC", defaultcc)) 50 buf.WriteString(defaultCCFunc("defaultCXX", defaultcxx)) 51 writefile(buf.String(), file, writeSkipSame) 52 } 53 54 func defaultCCFunc(name string, defaultcc map[string]string) string { 55 var buf bytes.Buffer 56 57 fmt.Fprintf(&buf, "func %s(goos, goarch string) string {\n", name) 58 fmt.Fprintf(&buf, "\tswitch goos+`/`+goarch {\n") 59 var keys []string 60 for k := range defaultcc { 61 if k != "" { 62 keys = append(keys, k) 63 } 64 } 65 sort.Strings(keys) 66 for _, k := range keys { 67 fmt.Fprintf(&buf, "\tcase %q:\n\t\treturn %q\n", k, defaultcc[k]) 68 } 69 fmt.Fprintf(&buf, "\t}\n") 70 fmt.Fprintf(&buf, "\treturn %q\n", defaultcc[""]) 71 fmt.Fprintf(&buf, "}\n") 72 73 return buf.String() 74 } 75 76 // mkzcgo writes zosarch.go for cmd/go. 77 func mkzosarch(dir, file string) { 78 // sort for deterministic zosarch.go file 79 var list []string 80 for plat := range cgoEnabled { 81 list = append(list, plat) 82 } 83 sort.Strings(list) 84 85 var buf bytes.Buffer 86 fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n\n") 87 fmt.Fprintf(&buf, "package cfg\n\n") 88 fmt.Fprintf(&buf, "var OSArchSupportsCgo = map[string]bool{\n") 89 for _, plat := range list { 90 fmt.Fprintf(&buf, "\t%q: %v,\n", plat, cgoEnabled[plat]) 91 } 92 fmt.Fprintf(&buf, "}\n") 93 94 writefile(buf.String(), file, writeSkipSame) 95 } 96 97 // mkzcgo writes zcgo.go for the go/build package: 98 // 99 // package build 100 // var cgoEnabled = map[string]bool{} 101 // 102 // It is invoked to write go/build/zcgo.go. 103 func mkzcgo(dir, file string) { 104 // sort for deterministic zcgo.go file 105 var list []string 106 for plat, hasCgo := range cgoEnabled { 107 if hasCgo { 108 list = append(list, plat) 109 } 110 } 111 sort.Strings(list) 112 113 var buf bytes.Buffer 114 fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") 115 fmt.Fprintln(&buf) 116 fmt.Fprintf(&buf, "package build\n") 117 fmt.Fprintln(&buf) 118 fmt.Fprintf(&buf, "const defaultCGO_ENABLED = %q\n", os.Getenv("CGO_ENABLED")) 119 fmt.Fprintln(&buf) 120 fmt.Fprintf(&buf, "var cgoEnabled = map[string]bool{\n") 121 for _, plat := range list { 122 fmt.Fprintf(&buf, "\t%q: true,\n", plat) 123 } 124 fmt.Fprintf(&buf, "}\n") 125 126 writefile(buf.String(), file, writeSkipSame) 127 }