libgo: update to Go 1.11
[official-gcc.git] / libgo / go / cmd / go / internal / work / init.go
blobde0cd4282b369761bd7e7e97ec16aa516a65a516
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.
5 // Build initialization (after flag parsing).
7 package work
9 import (
10 "cmd/go/internal/base"
11 "cmd/go/internal/cfg"
12 "cmd/go/internal/load"
13 "flag"
14 "fmt"
15 "os"
16 "path/filepath"
17 "strings"
20 func BuildInit() {
21 load.ModInit()
22 instrumentInit()
23 buildModeInit()
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)
33 cfg.BuildPkgdir = p
37 func instrumentInit() {
38 if !cfg.BuildRace && !cfg.BuildMSan {
39 return
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)
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)
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
59 mode := "race"
60 if cfg.BuildMSan {
61 mode = "msan"
63 modeFlag := "-" + mode
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)
69 forcedGcflags = append(forcedGcflags, modeFlag)
70 forcedLdflags = append(forcedLdflags, modeFlag)
72 if cfg.BuildContext.InstallSuffix != "" {
73 cfg.BuildContext.InstallSuffix += "_"
75 cfg.BuildContext.InstallSuffix += mode
76 cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, mode)
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)
97 // Use -shared so that the result is
98 // suitable for inclusion in a PIE or
99 // shared library.
100 codegenArg = "-shared"
103 if gccgo {
104 codegenArg = "-fPIC"
106 cfg.ExeSuffix = ".a"
107 ldBuildmode = "c-archive"
108 case "c-shared":
109 pkgsFilter = oneMainPkg
110 if gccgo {
111 codegenArg = "-fPIC"
112 } else {
113 switch platform {
114 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x",
115 "android/amd64", "android/arm", "android/arm64", "android/386",
116 "freebsd/amd64":
117 codegenArg = "-shared"
118 case "darwin/amd64", "darwin/386":
119 case "windows/amd64", "windows/386":
120 // Do not add usual .exe suffix to the .dll file.
121 cfg.ExeSuffix = ""
122 default:
123 base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
126 ldBuildmode = "c-shared"
127 case "default":
128 switch platform {
129 case "android/arm", "android/arm64", "android/amd64", "android/386":
130 codegenArg = "-shared"
131 ldBuildmode = "pie"
132 case "darwin/arm", "darwin/arm64":
133 codegenArg = "-shared"
134 fallthrough
135 default:
136 ldBuildmode = "exe"
138 if gccgo {
139 codegenArg = ""
141 case "exe":
142 pkgsFilter = pkgsMain
143 ldBuildmode = "exe"
144 // Set the pkgsFilter to oneMainPkg if the user passed a specific binary output
145 // and is using buildmode=exe for a better error message.
146 // See issue #20017.
147 if cfg.BuildO != "" {
148 pkgsFilter = oneMainPkg
150 case "pie":
151 if cfg.BuildRace {
152 base.Fatalf("-buildmode=pie not supported when -race is enabled")
154 if gccgo {
155 codegenArg = "-fPIE"
156 } else {
157 switch platform {
158 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
159 "android/amd64", "android/arm", "android/arm64", "android/386",
160 "freebsd/amd64":
161 codegenArg = "-shared"
162 case "darwin/amd64":
163 codegenArg = "-shared"
164 default:
165 base.Fatalf("-buildmode=pie not supported on %s\n", platform)
168 ldBuildmode = "pie"
169 case "shared":
170 pkgsFilter = pkgsNotMain
171 if gccgo {
172 codegenArg = "-fPIC"
173 } else {
174 switch platform {
175 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
176 default:
177 base.Fatalf("-buildmode=shared not supported on %s\n", platform)
179 codegenArg = "-dynlink"
181 if cfg.BuildO != "" {
182 base.Fatalf("-buildmode=shared and -o not supported together")
184 ldBuildmode = "shared"
185 case "plugin":
186 pkgsFilter = oneMainPkg
187 if gccgo {
188 codegenArg = "-fPIC"
189 } else {
190 switch platform {
191 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
192 "android/amd64", "android/arm", "android/arm64", "android/386":
193 case "darwin/amd64":
194 // Skip DWARF generation due to #21647
195 forcedLdflags = append(forcedLdflags, "-w")
196 default:
197 base.Fatalf("-buildmode=plugin not supported on %s\n", platform)
199 codegenArg = "-dynlink"
201 cfg.ExeSuffix = ".so"
202 ldBuildmode = "plugin"
203 default:
204 base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
206 if cfg.BuildLinkshared {
207 if gccgo {
208 codegenArg = "-fPIC"
209 } else {
210 switch platform {
211 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
212 forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1")
213 default:
214 base.Fatalf("-linkshared not supported on %s\n", platform)
216 codegenArg = "-dynlink"
217 // TODO(mwhudson): remove -w when that gets fixed in linker.
218 forcedLdflags = append(forcedLdflags, "-linkshared", "-w")
221 if codegenArg != "" {
222 if gccgo {
223 forcedGccgoflags = append([]string{codegenArg}, forcedGccgoflags...)
224 } else {
225 forcedAsmflags = append([]string{codegenArg}, forcedAsmflags...)
226 forcedGcflags = append([]string{codegenArg}, forcedGcflags...)
228 // Don't alter InstallSuffix when modifying default codegen args.
229 if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared {
230 if cfg.BuildContext.InstallSuffix != "" {
231 cfg.BuildContext.InstallSuffix += "_"
233 cfg.BuildContext.InstallSuffix += codegenArg[1:]
237 switch cfg.BuildMod {
238 case "":
239 // ok
240 case "readonly", "vendor":
241 if load.ModLookup == nil && !inGOFLAGS("-mod") {
242 base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod)
244 default:
245 base.Fatalf("-mod=%s not supported (can be '', 'readonly', or 'vendor')", cfg.BuildMod)
249 func inGOFLAGS(flag string) bool {
250 for _, goflag := range base.GOFLAGS() {
251 name := goflag
252 if strings.HasPrefix(name, "--") {
253 name = name[1:]
255 if i := strings.Index(name, "="); i >= 0 {
256 name = name[:i]
258 if name == flag {
259 return true
262 return false