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).
10 "cmd/go/internal/base"
12 "cmd/go/internal/load"
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
)
30 fmt
.Fprintf(os
.Stderr
, "go %s: evaluating -pkgdir: %v\n", flag
.Args()[0], err
)
37 func instrumentInit() {
38 if !cfg
.BuildRace
&& !cfg
.BuildMSan
{
41 if cfg
.BuildRace
&& cfg
.BuildMSan
{
42 fmt
.Fprintf(os
.Stderr
, "go %s: may not use -race and -msan simultaneously\n", flag
.Args()[0])
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
)
50 platform
:= cfg
.Goos
+ "/" + cfg
.Goarch
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])
55 case "linux/amd64", "linux/ppc64le", "freebsd/amd64", "netbsd/amd64", "darwin/amd64", "windows/amd64":
56 // race supported on these platforms
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
)
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"
82 platform
:= cfg
.Goos
+ "/" + cfg
.Goarch
83 switch cfg
.BuildBuildmode
{
85 pkgsFilter
= pkgsNotMain
87 pkgsFilter
= oneMainPkg
89 case "darwin/arm", "darwin/arm64":
90 codegenArg
= "-shared"
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
100 codegenArg
= "-shared"
107 ldBuildmode
= "c-archive"
109 pkgsFilter
= oneMainPkg
114 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x",
115 "android/amd64", "android/arm", "android/arm64", "android/386",
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.
123 base
.Fatalf("-buildmode=c-shared not supported on %s\n", platform
)
126 ldBuildmode
= "c-shared"
129 case "android/arm", "android/arm64", "android/amd64", "android/386":
130 codegenArg
= "-shared"
132 case "darwin/arm", "darwin/arm64":
133 codegenArg
= "-shared"
142 pkgsFilter
= pkgsMain
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.
147 if cfg
.BuildO
!= "" {
148 pkgsFilter
= oneMainPkg
152 base
.Fatalf("-buildmode=pie not supported when -race is enabled")
158 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
159 "android/amd64", "android/arm", "android/arm64", "android/386",
161 codegenArg
= "-shared"
163 codegenArg
= "-shared"
165 base
.Fatalf("-buildmode=pie not supported on %s\n", platform
)
170 pkgsFilter
= pkgsNotMain
175 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
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"
186 pkgsFilter
= oneMainPkg
191 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
192 "android/amd64", "android/arm", "android/arm64", "android/386":
194 // Skip DWARF generation due to #21647
195 forcedLdflags
= append(forcedLdflags
, "-w")
197 base
.Fatalf("-buildmode=plugin not supported on %s\n", platform
)
199 codegenArg
= "-dynlink"
201 cfg
.ExeSuffix
= ".so"
202 ldBuildmode
= "plugin"
204 base
.Fatalf("buildmode=%s not supported", cfg
.BuildBuildmode
)
206 if cfg
.BuildLinkshared
{
211 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
212 forcedAsmflags
= append(forcedAsmflags
, "-D=GOBUILDMODE_shared=1")
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
!= "" {
223 forcedGccgoflags
= append([]string{codegenArg
}, forcedGccgoflags
...)
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
{
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
)
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() {
252 if strings
.HasPrefix(name
, "--") {
255 if i
:= strings
.Index(name
, "="); i
>= 0 {