]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/work/init.go
internal/buildcfg: move build configuration out of cmd/internal/objabi
[gostls13.git] / src / cmd / go / internal / work / init.go
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/fsys"
13         "cmd/go/internal/modload"
14         "cmd/internal/sys"
15         "flag"
16         "fmt"
17         "internal/buildcfg"
18         "os"
19         "path/filepath"
20         "runtime"
21 )
22
23 func BuildInit() {
24         modload.Init()
25         instrumentInit()
26         buildModeInit()
27         if err := fsys.Init(base.Cwd); err != nil {
28                 base.Fatalf("go: %v", err)
29         }
30
31         // Make sure -pkgdir is absolute, because we run commands
32         // in different directories.
33         if cfg.BuildPkgdir != "" && !filepath.IsAbs(cfg.BuildPkgdir) {
34                 p, err := filepath.Abs(cfg.BuildPkgdir)
35                 if err != nil {
36                         fmt.Fprintf(os.Stderr, "go %s: evaluating -pkgdir: %v\n", flag.Args()[0], err)
37                         base.SetExitStatus(2)
38                         base.Exit()
39                 }
40                 cfg.BuildPkgdir = p
41         }
42
43         // Make sure CC and CXX are absolute paths
44         for _, key := range []string{"CC", "CXX"} {
45                 if path := cfg.Getenv(key); !filepath.IsAbs(path) && path != "" && path != filepath.Base(path) {
46                         base.Fatalf("go %s: %s environment variable is relative; must be absolute path: %s\n", flag.Args()[0], key, path)
47                 }
48         }
49
50         // For each experiment that has been enabled in the toolchain, define a
51         // build tag with the same name but prefixed by "goexperiment." which can be
52         // used for compiling alternative files for the experiment. This allows
53         // changes for the experiment, like extra struct fields in the runtime,
54         // without affecting the base non-experiment code at all.
55         for _, expt := range buildcfg.EnabledExperiments() {
56                 cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "goexperiment."+expt)
57         }
58 }
59
60 func instrumentInit() {
61         if !cfg.BuildRace && !cfg.BuildMSan {
62                 return
63         }
64         if cfg.BuildRace && cfg.BuildMSan {
65                 fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
66                 base.SetExitStatus(2)
67                 base.Exit()
68         }
69         if cfg.BuildMSan && !sys.MSanSupported(cfg.Goos, cfg.Goarch) {
70                 fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
71                 base.SetExitStatus(2)
72                 base.Exit()
73         }
74         if cfg.BuildRace {
75                 if !sys.RaceDetectorSupported(cfg.Goos, cfg.Goarch) {
76                         fmt.Fprintf(os.Stderr, "go %s: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64, darwin/arm64, and windows/amd64\n", flag.Args()[0])
77                         base.SetExitStatus(2)
78                         base.Exit()
79                 }
80         }
81         mode := "race"
82         if cfg.BuildMSan {
83                 mode = "msan"
84                 // MSAN does not support non-PIE binaries on ARM64.
85                 // See issue #33712 for details.
86                 if cfg.Goos == "linux" && cfg.Goarch == "arm64" && cfg.BuildBuildmode == "default" {
87                         cfg.BuildBuildmode = "pie"
88                 }
89         }
90         modeFlag := "-" + mode
91
92         if !cfg.BuildContext.CgoEnabled {
93                 if runtime.GOOS != cfg.Goos || runtime.GOARCH != cfg.Goarch {
94                         fmt.Fprintf(os.Stderr, "go %s: %s requires cgo\n", flag.Args()[0], modeFlag)
95                 } else {
96                         fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], modeFlag)
97                 }
98
99                 base.SetExitStatus(2)
100                 base.Exit()
101         }
102         forcedGcflags = append(forcedGcflags, modeFlag)
103         forcedLdflags = append(forcedLdflags, modeFlag)
104
105         if cfg.BuildContext.InstallSuffix != "" {
106                 cfg.BuildContext.InstallSuffix += "_"
107         }
108         cfg.BuildContext.InstallSuffix += mode
109         cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, mode)
110 }
111
112 func buildModeInit() {
113         gccgo := cfg.BuildToolchainName == "gccgo"
114         var codegenArg string
115
116         // Configure the build mode first, then verify that it is supported.
117         // That way, if the flag is completely bogus we will prefer to error out with
118         // "-buildmode=%s not supported" instead of naming the specific platform.
119
120         switch cfg.BuildBuildmode {
121         case "archive":
122                 pkgsFilter = pkgsNotMain
123         case "c-archive":
124                 pkgsFilter = oneMainPkg
125                 if gccgo {
126                         codegenArg = "-fPIC"
127                 } else {
128                         switch cfg.Goos {
129                         case "darwin", "ios":
130                                 switch cfg.Goarch {
131                                 case "arm64":
132                                         codegenArg = "-shared"
133                                 }
134
135                         case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
136                                 // Use -shared so that the result is
137                                 // suitable for inclusion in a PIE or
138                                 // shared library.
139                                 codegenArg = "-shared"
140                         }
141                 }
142                 cfg.ExeSuffix = ".a"
143                 ldBuildmode = "c-archive"
144         case "c-shared":
145                 pkgsFilter = oneMainPkg
146                 if gccgo {
147                         codegenArg = "-fPIC"
148                 } else {
149                         switch cfg.Goos {
150                         case "linux", "android", "freebsd":
151                                 codegenArg = "-shared"
152                         case "windows":
153                                 // Do not add usual .exe suffix to the .dll file.
154                                 cfg.ExeSuffix = ""
155                         }
156                 }
157                 ldBuildmode = "c-shared"
158         case "default":
159                 switch cfg.Goos {
160                 case "android":
161                         codegenArg = "-shared"
162                         ldBuildmode = "pie"
163                 case "windows":
164                         ldBuildmode = "pie"
165                 case "ios":
166                         codegenArg = "-shared"
167                         ldBuildmode = "pie"
168                 case "darwin":
169                         switch cfg.Goarch {
170                         case "arm64":
171                                 codegenArg = "-shared"
172                         }
173                         fallthrough
174                 default:
175                         ldBuildmode = "exe"
176                 }
177                 if gccgo {
178                         codegenArg = ""
179                 }
180         case "exe":
181                 pkgsFilter = pkgsMain
182                 ldBuildmode = "exe"
183                 // Set the pkgsFilter to oneMainPkg if the user passed a specific binary output
184                 // and is using buildmode=exe for a better error message.
185                 // See issue #20017.
186                 if cfg.BuildO != "" {
187                         pkgsFilter = oneMainPkg
188                 }
189         case "pie":
190                 if cfg.BuildRace {
191                         base.Fatalf("-buildmode=pie not supported when -race is enabled")
192                 }
193                 if gccgo {
194                         codegenArg = "-fPIE"
195                 } else {
196                         switch cfg.Goos {
197                         case "aix", "windows":
198                         default:
199                                 codegenArg = "-shared"
200                         }
201                 }
202                 ldBuildmode = "pie"
203         case "shared":
204                 pkgsFilter = pkgsNotMain
205                 if gccgo {
206                         codegenArg = "-fPIC"
207                 } else {
208                         codegenArg = "-dynlink"
209                 }
210                 if cfg.BuildO != "" {
211                         base.Fatalf("-buildmode=shared and -o not supported together")
212                 }
213                 ldBuildmode = "shared"
214         case "plugin":
215                 pkgsFilter = oneMainPkg
216                 if gccgo {
217                         codegenArg = "-fPIC"
218                 } else {
219                         codegenArg = "-dynlink"
220                 }
221                 cfg.ExeSuffix = ".so"
222                 ldBuildmode = "plugin"
223         default:
224                 base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
225         }
226
227         if !sys.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
228                 base.Fatalf("-buildmode=%s not supported on %s/%s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
229         }
230
231         if cfg.BuildLinkshared {
232                 if !sys.BuildModeSupported(cfg.BuildToolchainName, "shared", cfg.Goos, cfg.Goarch) {
233                         base.Fatalf("-linkshared not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
234                 }
235                 if gccgo {
236                         codegenArg = "-fPIC"
237                 } else {
238                         forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1",
239                                 "-linkshared")
240                         codegenArg = "-dynlink"
241                         forcedGcflags = append(forcedGcflags, "-linkshared")
242                         // TODO(mwhudson): remove -w when that gets fixed in linker.
243                         forcedLdflags = append(forcedLdflags, "-linkshared", "-w")
244                 }
245         }
246         if codegenArg != "" {
247                 if gccgo {
248                         forcedGccgoflags = append([]string{codegenArg}, forcedGccgoflags...)
249                 } else {
250                         forcedAsmflags = append([]string{codegenArg}, forcedAsmflags...)
251                         forcedGcflags = append([]string{codegenArg}, forcedGcflags...)
252                 }
253                 // Don't alter InstallSuffix when modifying default codegen args.
254                 if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared {
255                         if cfg.BuildContext.InstallSuffix != "" {
256                                 cfg.BuildContext.InstallSuffix += "_"
257                         }
258                         cfg.BuildContext.InstallSuffix += codegenArg[1:]
259                 }
260         }
261
262         switch cfg.BuildMod {
263         case "":
264                 // Behavior will be determined automatically, as if no flag were passed.
265         case "readonly", "vendor", "mod":
266                 if !cfg.ModulesEnabled && !base.InGOFLAGS("-mod") {
267                         base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod)
268                 }
269         default:
270                 base.Fatalf("-mod=%s not supported (can be '', 'mod', 'readonly', or 'vendor')", cfg.BuildMod)
271         }
272         if !cfg.ModulesEnabled {
273                 if cfg.ModCacheRW && !base.InGOFLAGS("-modcacherw") {
274                         base.Fatalf("build flag -modcacherw only valid when using modules")
275                 }
276                 if cfg.ModFile != "" && !base.InGOFLAGS("-mod") {
277                         base.Fatalf("build flag -modfile only valid when using modules")
278                 }
279         }
280 }