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 "sort" 12 ) 13 14 /* 15 * Helpers for building cmd/go and cmd/cgo. 16 */ 17 18 // mkzdefaultcc writes zdefaultcc.go: 19 // 20 // package main 21 // const defaultCC = <defaultcc> 22 // const defaultCXX = <defaultcxx> 23 // const defaultPkgConfig = <defaultpkgconfig> 24 // 25 // It is invoked to write cmd/go/zdefaultcc.go 26 // but we also write cmd/cgo/zdefaultcc.go 27 func mkzdefaultcc(dir, file string) { 28 out := fmt.Sprintf( 29 "// auto generated by go tool dist\n"+ 30 "\n"+ 31 "package main\n"+ 32 "\n"+ 33 "const defaultCC = `%s`\n"+ 34 "const defaultCXX = `%s`\n"+ 35 "const defaultPkgConfig = `%s`\n", 36 defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget) 37 38 writefile(out, file, writeSkipSame) 39 40 // Convert file name to replace: turn go into cgo. 41 i := len(file) - len("go/zdefaultcc.go") 42 file = file[:i] + "c" + file[i:] 43 writefile(out, file, writeSkipSame) 44 } 45 46 // mkzcgo writes zosarch.go for cmd/go. 47 func mkzosarch(dir, file string) { 48 // sort for deterministic zosarch.go file 49 var list []string 50 for plat := range cgoEnabled { 51 list = append(list, plat) 52 } 53 sort.Strings(list) 54 55 var buf bytes.Buffer 56 buf.WriteString("// auto generated by go tool dist\n\n") 57 buf.WriteString("package main\n\n") 58 fmt.Fprintf(&buf, "var osArchSupportsCgo = map[string]bool{\n") 59 for _, plat := range list { 60 fmt.Fprintf(&buf, "\t%q: %v,\n", plat, cgoEnabled[plat]) 61 } 62 fmt.Fprintf(&buf, "}\n") 63 writefile(buf.String(), file, writeSkipSame) 64 } 65 66 // mkzcgo writes zcgo.go for the go/build package: 67 // 68 // package build 69 // var cgoEnabled = map[string]bool{} 70 // 71 // It is invoked to write go/build/zcgo.go. 72 func mkzcgo(dir, file string) { 73 // sort for deterministic zcgo.go file 74 var list []string 75 for plat, hasCgo := range cgoEnabled { 76 if hasCgo { 77 list = append(list, plat) 78 } 79 } 80 sort.Strings(list) 81 82 var buf bytes.Buffer 83 84 fmt.Fprintf(&buf, 85 "// auto generated by go tool dist\n"+ 86 "\n"+ 87 "package build\n"+ 88 "\n"+ 89 "const defaultCGO_ENABLED = %q\n\n"+ 90 "var cgoEnabled = map[string]bool{\n", os.Getenv("CGO_ENABLED")) 91 for _, plat := range list { 92 fmt.Fprintf(&buf, "\t%q: true,\n", plat) 93 } 94 fmt.Fprintf(&buf, "}") 95 96 writefile(buf.String(), file, writeSkipSame) 97 } 98