github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/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 "flag" 13 "fmt" 14 "os" 15 "path/filepath" 16 ) 17 18 func BuildInit() { 19 instrumentInit() 20 buildModeInit() 21 22 // Make sure -pkgdir is absolute, because we run commands 23 // in different directories. 24 if cfg.BuildPkgdir != "" && !filepath.IsAbs(cfg.BuildPkgdir) { 25 p, err := filepath.Abs(cfg.BuildPkgdir) 26 if err != nil { 27 fmt.Fprintf(os.Stderr, "go %s: evaluating -pkgdir: %v\n", flag.Args()[0], err) 28 os.Exit(2) 29 } 30 cfg.BuildPkgdir = p 31 } 32 } 33 34 func instrumentInit() { 35 if !cfg.BuildRace && !cfg.BuildMSan { 36 return 37 } 38 if cfg.BuildRace && cfg.BuildMSan { 39 fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0]) 40 os.Exit(2) 41 } 42 if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64") { 43 fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch) 44 os.Exit(2) 45 } 46 if cfg.Goarch != "amd64" || cfg.Goos != "linux" && cfg.Goos != "freebsd" && cfg.Goos != "darwin" && cfg.Goos != "windows" { 47 fmt.Fprintf(os.Stderr, "go %s: -race and -msan are only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0]) 48 os.Exit(2) 49 } 50 51 mode := "race" 52 if cfg.BuildMSan { 53 mode = "msan" 54 } 55 modeFlag := "-" + mode 56 57 if !cfg.BuildContext.CgoEnabled { 58 fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], modeFlag) 59 os.Exit(2) 60 } 61 forcedGcflags = append(forcedGcflags, modeFlag) 62 forcedLdflags = append(forcedLdflags, modeFlag) 63 64 if cfg.BuildContext.InstallSuffix != "" { 65 cfg.BuildContext.InstallSuffix += "_" 66 } 67 cfg.BuildContext.InstallSuffix += mode 68 cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, mode) 69 } 70 71 func buildModeInit() { 72 gccgo := cfg.BuildToolchainName == "gccgo" 73 var codegenArg string 74 platform := cfg.Goos + "/" + cfg.Goarch 75 switch cfg.BuildBuildmode { 76 case "archive": 77 pkgsFilter = pkgsNotMain 78 case "c-archive": 79 pkgsFilter = oneMainPkg 80 switch platform { 81 case "darwin/arm", "darwin/arm64": 82 codegenArg = "-shared" 83 default: 84 switch cfg.Goos { 85 case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris": 86 // Use -shared so that the result is 87 // suitable for inclusion in a PIE or 88 // shared library. 89 codegenArg = "-shared" 90 } 91 } 92 cfg.ExeSuffix = ".a" 93 ldBuildmode = "c-archive" 94 case "c-shared": 95 pkgsFilter = oneMainPkg 96 if gccgo { 97 codegenArg = "-fPIC" 98 } else { 99 switch platform { 100 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", 101 "android/amd64", "android/arm", "android/arm64", "android/386": 102 codegenArg = "-shared" 103 case "darwin/amd64", "darwin/386": 104 case "windows/amd64", "windows/386": 105 // Do not add usual .exe suffix to the .dll file. 106 cfg.ExeSuffix = "" 107 default: 108 base.Fatalf("-buildmode=c-shared not supported on %s\n", platform) 109 } 110 } 111 ldBuildmode = "c-shared" 112 case "default": 113 switch platform { 114 case "android/arm", "android/arm64", "android/amd64", "android/386": 115 codegenArg = "-shared" 116 ldBuildmode = "pie" 117 case "darwin/arm", "darwin/arm64": 118 codegenArg = "-shared" 119 fallthrough 120 default: 121 ldBuildmode = "exe" 122 } 123 case "exe": 124 pkgsFilter = pkgsMain 125 ldBuildmode = "exe" 126 case "pie": 127 if cfg.BuildRace { 128 base.Fatalf("-buildmode=pie not supported when -race is enabled") 129 } 130 if gccgo { 131 base.Fatalf("-buildmode=pie not supported by gccgo") 132 } else { 133 switch platform { 134 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x", 135 "android/amd64", "android/arm", "android/arm64", "android/386": 136 codegenArg = "-shared" 137 case "darwin/amd64": 138 codegenArg = "-shared" 139 default: 140 base.Fatalf("-buildmode=pie not supported on %s\n", platform) 141 } 142 } 143 ldBuildmode = "pie" 144 case "shared": 145 pkgsFilter = pkgsNotMain 146 if gccgo { 147 codegenArg = "-fPIC" 148 } else { 149 switch platform { 150 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x": 151 default: 152 base.Fatalf("-buildmode=shared not supported on %s\n", platform) 153 } 154 codegenArg = "-dynlink" 155 } 156 if cfg.BuildO != "" { 157 base.Fatalf("-buildmode=shared and -o not supported together") 158 } 159 ldBuildmode = "shared" 160 case "plugin": 161 pkgsFilter = oneMainPkg 162 if gccgo { 163 codegenArg = "-fPIC" 164 } else { 165 switch platform { 166 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le", 167 "android/amd64", "android/arm", "android/arm64", "android/386": 168 case "darwin/amd64": 169 // Skip DWARF generation due to #21647 170 forcedLdflags = append(forcedLdflags, "-w") 171 default: 172 base.Fatalf("-buildmode=plugin not supported on %s\n", platform) 173 } 174 codegenArg = "-dynlink" 175 } 176 cfg.ExeSuffix = ".so" 177 ldBuildmode = "plugin" 178 default: 179 base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode) 180 } 181 if cfg.BuildLinkshared { 182 if gccgo { 183 codegenArg = "-fPIC" 184 } else { 185 switch platform { 186 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x": 187 forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1") 188 default: 189 base.Fatalf("-linkshared not supported on %s\n", platform) 190 } 191 codegenArg = "-dynlink" 192 // TODO(mwhudson): remove -w when that gets fixed in linker. 193 forcedLdflags = append(forcedLdflags, "-linkshared", "-w") 194 } 195 } 196 if codegenArg != "" { 197 if gccgo { 198 forcedGccgoflags = append([]string{codegenArg}, forcedGccgoflags...) 199 } else { 200 forcedAsmflags = append([]string{codegenArg}, forcedAsmflags...) 201 forcedGcflags = append([]string{codegenArg}, forcedGcflags...) 202 } 203 // Don't alter InstallSuffix when modifying default codegen args. 204 if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared { 205 if cfg.BuildContext.InstallSuffix != "" { 206 cfg.BuildContext.InstallSuffix += "_" 207 } 208 cfg.BuildContext.InstallSuffix += codegenArg[1:] 209 } 210 } 211 }