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