github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/go/internal/work/init.go (about) 1 // Copyright 2017 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 // Build initialization (after flag parsing). 6 7 package work 8 9 import ( 10 "cmd/go/internal/base" 11 "cmd/go/internal/cfg" 12 "cmd/go/internal/load" 13 "cmd/internal/sys" 14 "flag" 15 "fmt" 16 "os" 17 "path/filepath" 18 "strings" 19 ) 20 21 func BuildInit() { 22 load.ModInit() 23 instrumentInit() 24 buildModeInit() 25 26 // Make sure -pkgdir is absolute, because we run commands 27 // in different directories. 28 if cfg.BuildPkgdir != "" && !filepath.IsAbs(cfg.BuildPkgdir) { 29 p, err := filepath.Abs(cfg.BuildPkgdir) 30 if err != nil { 31 fmt.Fprintf(os.Stderr, "go %s: evaluating -pkgdir: %v\n", flag.Args()[0], err) 32 os.Exit(2) 33 } 34 cfg.BuildPkgdir = p 35 } 36 } 37 38 func instrumentInit() { 39 if !cfg.BuildRace && !cfg.BuildMSan { 40 return 41 } 42 if cfg.BuildRace && cfg.BuildMSan { 43 fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0]) 44 os.Exit(2) 45 } 46 if cfg.BuildMSan && !sys.MSanSupported(cfg.Goos, cfg.Goarch) { 47 fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch) 48 os.Exit(2) 49 } 50 if cfg.BuildRace { 51 if !sys.RaceDetectorSupported(cfg.Goos, cfg.Goarch) { 52 fmt.Fprintf(os.Stderr, "go %s: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0]) 53 os.Exit(2) 54 } 55 } 56 mode := "race" 57 if cfg.BuildMSan { 58 mode = "msan" 59 } 60 modeFlag := "-" + mode 61 62 if !cfg.BuildContext.CgoEnabled { 63 fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], modeFlag) 64 os.Exit(2) 65 } 66 forcedGcflags = append(forcedGcflags, modeFlag) 67 forcedLdflags = append(forcedLdflags, modeFlag) 68 69 if cfg.BuildContext.InstallSuffix != "" { 70 cfg.BuildContext.InstallSuffix += "_" 71 } 72 cfg.BuildContext.InstallSuffix += mode 73 cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, mode) 74 } 75 76 func buildModeInit() { 77 gccgo := cfg.BuildToolchainName == "gccgo" 78 var codegenArg string 79 platform := cfg.Goos + "/" + cfg.Goarch 80 switch cfg.BuildBuildmode { 81 case "archive": 82 pkgsFilter = pkgsNotMain 83 case "c-archive": 84 pkgsFilter = oneMainPkg 85 if gccgo { 86 codegenArg = "-fPIC" 87 } else { 88 switch platform { 89 case "darwin/arm", "darwin/arm64": 90 codegenArg = "-shared" 91 default: 92 switch cfg.Goos { 93 case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris": 94 if platform == "linux/ppc64" { 95 base.Fatalf("-buildmode=c-archive not supported on %s\n", platform) 96 } 97 // Use -shared so that the result is 98 // suitable for inclusion in a PIE or 99 // shared library. 100 codegenArg = "-shared" 101 } 102 } 103 } 104 cfg.ExeSuffix = ".a" 105 ldBuildmode = "c-archive" 106 case "c-shared": 107 pkgsFilter = oneMainPkg 108 if gccgo { 109 codegenArg = "-fPIC" 110 } else { 111 switch platform { 112 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x", 113 "android/amd64", "android/arm", "android/arm64", "android/386", 114 "freebsd/amd64": 115 codegenArg = "-shared" 116 case "darwin/amd64", "darwin/386": 117 case "windows/amd64", "windows/386": 118 // Do not add usual .exe suffix to the .dll file. 119 cfg.ExeSuffix = "" 120 default: 121 base.Fatalf("-buildmode=c-shared not supported on %s\n", platform) 122 } 123 } 124 ldBuildmode = "c-shared" 125 case "default": 126 switch platform { 127 case "android/arm", "android/arm64", "android/amd64", "android/386": 128 codegenArg = "-shared" 129 ldBuildmode = "pie" 130 case "darwin/arm", "darwin/arm64": 131 codegenArg = "-shared" 132 fallthrough 133 default: 134 ldBuildmode = "exe" 135 } 136 if gccgo { 137 codegenArg = "" 138 } 139 case "exe": 140 pkgsFilter = pkgsMain 141 ldBuildmode = "exe" 142 // Set the pkgsFilter to oneMainPkg if the user passed a specific binary output 143 // and is using buildmode=exe for a better error message. 144 // See issue #20017. 145 if cfg.BuildO != "" { 146 pkgsFilter = oneMainPkg 147 } 148 case "pie": 149 if cfg.BuildRace { 150 base.Fatalf("-buildmode=pie not supported when -race is enabled") 151 } 152 if gccgo { 153 codegenArg = "-fPIE" 154 } else { 155 switch platform { 156 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x", 157 "android/amd64", "android/arm", "android/arm64", "android/386", 158 "freebsd/amd64": 159 codegenArg = "-shared" 160 case "darwin/amd64": 161 codegenArg = "-shared" 162 default: 163 base.Fatalf("-buildmode=pie not supported on %s\n", platform) 164 } 165 } 166 ldBuildmode = "pie" 167 case "shared": 168 pkgsFilter = pkgsNotMain 169 if gccgo { 170 codegenArg = "-fPIC" 171 } else { 172 switch platform { 173 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x": 174 default: 175 base.Fatalf("-buildmode=shared not supported on %s\n", platform) 176 } 177 codegenArg = "-dynlink" 178 } 179 if cfg.BuildO != "" { 180 base.Fatalf("-buildmode=shared and -o not supported together") 181 } 182 ldBuildmode = "shared" 183 case "plugin": 184 pkgsFilter = oneMainPkg 185 if gccgo { 186 codegenArg = "-fPIC" 187 } else { 188 switch platform { 189 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le", 190 "android/amd64", "android/arm", "android/arm64", "android/386": 191 case "darwin/amd64": 192 // Skip DWARF generation due to #21647 193 forcedLdflags = append(forcedLdflags, "-w") 194 default: 195 base.Fatalf("-buildmode=plugin not supported on %s\n", platform) 196 } 197 codegenArg = "-dynlink" 198 } 199 cfg.ExeSuffix = ".so" 200 ldBuildmode = "plugin" 201 default: 202 base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode) 203 } 204 if cfg.BuildLinkshared { 205 if gccgo { 206 codegenArg = "-fPIC" 207 } else { 208 switch platform { 209 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x": 210 forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1") 211 default: 212 base.Fatalf("-linkshared not supported on %s\n", platform) 213 } 214 codegenArg = "-dynlink" 215 // TODO(mwhudson): remove -w when that gets fixed in linker. 216 forcedLdflags = append(forcedLdflags, "-linkshared", "-w") 217 } 218 } 219 if codegenArg != "" { 220 if gccgo { 221 forcedGccgoflags = append([]string{codegenArg}, forcedGccgoflags...) 222 } else { 223 forcedAsmflags = append([]string{codegenArg}, forcedAsmflags...) 224 forcedGcflags = append([]string{codegenArg}, forcedGcflags...) 225 } 226 // Don't alter InstallSuffix when modifying default codegen args. 227 if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared { 228 if cfg.BuildContext.InstallSuffix != "" { 229 cfg.BuildContext.InstallSuffix += "_" 230 } 231 cfg.BuildContext.InstallSuffix += codegenArg[1:] 232 } 233 } 234 235 switch cfg.BuildMod { 236 case "": 237 // ok 238 case "readonly", "vendor": 239 if load.ModLookup == nil && !inGOFLAGS("-mod") { 240 base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod) 241 } 242 default: 243 base.Fatalf("-mod=%s not supported (can be '', 'readonly', or 'vendor')", cfg.BuildMod) 244 } 245 } 246 247 func inGOFLAGS(flag string) bool { 248 for _, goflag := range base.GOFLAGS() { 249 name := goflag 250 if strings.HasPrefix(name, "--") { 251 name = name[1:] 252 } 253 if i := strings.Index(name, "="); i >= 0 { 254 name = name[:i] 255 } 256 if name == flag { 257 return true 258 } 259 } 260 return false 261 }