1
2
3
4
5 package base
6
7 import (
8 "cmd/internal/cov/covcmd"
9 "cmd/internal/telemetry/counter"
10 "encoding/json"
11 "flag"
12 "fmt"
13 "internal/buildcfg"
14 "internal/platform"
15 "log"
16 "os"
17 "reflect"
18 "runtime"
19 "strings"
20
21 "cmd/internal/obj"
22 "cmd/internal/objabi"
23 "cmd/internal/sys"
24 )
25
26 func usage() {
27 fmt.Fprintf(os.Stderr, "usage: compile [options] file.go...\n")
28 objabi.Flagprint(os.Stderr)
29 Exit(2)
30 }
31
32
33
34 var Flag CmdFlags
35
36
37
38
39 type CountFlag int
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 type CmdFlags struct {
56
57 B CountFlag "help:\"disable bounds checking\""
58 C CountFlag "help:\"disable printing of columns in error messages\""
59 D string "help:\"set relative `path` for local imports\""
60 E CountFlag "help:\"debug symbol export\""
61 I func(string) "help:\"add `directory` to import search path\""
62 K CountFlag "help:\"debug missing line numbers\""
63 L CountFlag "help:\"also show actual source file names in error messages for positions affected by //line directives\""
64 N CountFlag "help:\"disable optimizations\""
65 S CountFlag "help:\"print assembly listing\""
66
67 W CountFlag "help:\"debug parse tree after type checking\""
68
69 LowerC int "help:\"concurrency during compilation (1 means no concurrency)\""
70 LowerD flag.Value "help:\"enable debugging settings; try -d help\""
71 LowerE CountFlag "help:\"no limit on number of errors reported\""
72 LowerH CountFlag "help:\"halt on error\""
73 LowerJ CountFlag "help:\"debug runtime-initialized variables\""
74 LowerL CountFlag "help:\"disable inlining\""
75 LowerM CountFlag "help:\"print optimization decisions\""
76 LowerO string "help:\"write output to `file`\""
77 LowerP *string "help:\"set expected package import `path`\""
78 LowerR CountFlag "help:\"debug generated wrappers\""
79 LowerT bool "help:\"enable tracing for debugging the compiler\""
80 LowerW CountFlag "help:\"debug type checking\""
81 LowerU CountFlag "help:\"emit unsorted warnings/errors\""
82 LowerV *bool "help:\"increase debug verbosity\""
83
84
85 Percent CountFlag "flag:\"%\" help:\"debug non-static initializers\""
86 CompilingRuntime bool "flag:\"+\" help:\"compiling runtime\""
87
88
89 AsmHdr string "help:\"write assembly header to `file`\""
90 ASan bool "help:\"build code compatible with C/C++ address sanitizer\""
91 Bench string "help:\"append benchmark times to `file`\""
92 BlockProfile string "help:\"write block profile to `file`\""
93 BuildID string "help:\"record `id` as the build id in the export metadata\""
94 CPUProfile string "help:\"write cpu profile to `file`\""
95 Complete bool "help:\"compiling complete package (no C or assembly)\""
96 ClobberDead bool "help:\"clobber dead stack slots (for debugging)\""
97 ClobberDeadReg bool "help:\"clobber dead registers (for debugging)\""
98 Dwarf bool "help:\"generate DWARF symbols\""
99 DwarfBASEntries *bool "help:\"use base address selection entries in DWARF\""
100 DwarfLocationLists *bool "help:\"add location lists to DWARF in optimized mode\""
101 Dynlink *bool "help:\"support references to Go symbols defined in other shared libraries\""
102 EmbedCfg func(string) "help:\"read go:embed configuration from `file`\""
103 Env func(string) "help:\"add `definition` of the form key=value to environment\""
104 GenDwarfInl int "help:\"generate DWARF inline info records\""
105 GoVersion string "help:\"required version of the runtime\""
106 ImportCfg func(string) "help:\"read import configuration from `file`\""
107 InstallSuffix string "help:\"set pkg directory `suffix`\""
108 JSON string "help:\"version,file for JSON compiler/optimizer detail output\""
109 Lang string "help:\"Go language version source code expects\""
110 LinkObj string "help:\"write linker-specific object to `file`\""
111 LinkShared *bool "help:\"generate code that will be linked against Go shared libraries\""
112 Live CountFlag "help:\"debug liveness analysis\""
113 MSan bool "help:\"build code compatible with C/C++ memory sanitizer\""
114 MemProfile string "help:\"write memory profile to `file`\""
115 MemProfileRate int "help:\"set runtime.MemProfileRate to `rate`\""
116 MutexProfile string "help:\"write mutex profile to `file`\""
117 NoLocalImports bool "help:\"reject local (relative) imports\""
118 CoverageCfg func(string) "help:\"read coverage configuration from `file`\""
119 Pack bool "help:\"write to file.a instead of file.o\""
120 Race bool "help:\"enable race detector\""
121 Shared *bool "help:\"generate code that can be linked into a shared library\""
122 SmallFrames bool "help:\"reduce the size limit for stack allocated objects\""
123 Spectre string "help:\"enable spectre mitigations in `list` (all, index, ret)\""
124 Std bool "help:\"compiling standard library\""
125 SymABIs string "help:\"read symbol ABIs from `file`\""
126 TraceProfile string "help:\"write an execution trace to `file`\""
127 TrimPath string "help:\"remove `prefix` from recorded source file paths\""
128 WB bool "help:\"enable write barrier\""
129 PgoProfile string "help:\"read profile or pre-process profile from `file`\""
130 ErrorURL bool "help:\"print explanatory URL with error message if applicable\""
131
132
133 Cfg struct {
134 Embed struct {
135 Patterns map[string][]string
136 Files map[string]string
137 }
138 ImportDirs []string
139 ImportMap map[string]string
140 PackageFile map[string]string
141 CoverageInfo *covcmd.CoverFixupConfig
142 SpectreIndex bool
143
144
145 Instrumenting bool
146 }
147 }
148
149 func addEnv(s string) {
150 i := strings.Index(s, "=")
151 if i < 0 {
152 log.Fatal("-env argument must be of the form key=value")
153 }
154 os.Setenv(s[:i], s[i+1:])
155 }
156
157
158 func ParseFlags() {
159 Flag.I = addImportDir
160
161 Flag.LowerC = runtime.GOMAXPROCS(0)
162 Flag.LowerD = objabi.NewDebugFlag(&Debug, DebugSSA)
163 Flag.LowerP = &Ctxt.Pkgpath
164 Flag.LowerV = &Ctxt.Debugvlog
165
166 Flag.Dwarf = buildcfg.GOARCH != "wasm"
167 Flag.DwarfBASEntries = &Ctxt.UseBASEntries
168 Flag.DwarfLocationLists = &Ctxt.Flag_locationlists
169 *Flag.DwarfLocationLists = true
170 Flag.Dynlink = &Ctxt.Flag_dynlink
171 Flag.EmbedCfg = readEmbedCfg
172 Flag.Env = addEnv
173 Flag.GenDwarfInl = 2
174 Flag.ImportCfg = readImportCfg
175 Flag.CoverageCfg = readCoverageCfg
176 Flag.LinkShared = &Ctxt.Flag_linkshared
177 Flag.Shared = &Ctxt.Flag_shared
178 Flag.WB = true
179
180 Debug.ConcurrentOk = true
181 Debug.CompressInstructions = 1
182 Debug.MaxShapeLen = 500
183 Debug.AlignHot = 1
184 Debug.InlFuncsWithClosures = 1
185 Debug.InlStaticInit = 1
186 Debug.FreeAppend = 1
187 Debug.PGOInline = 1
188 Debug.PGODevirtualize = 2
189 Debug.SyncFrames = -1
190 Debug.VariableMakeThreshold = 32
191 Debug.ZeroCopy = 1
192 Debug.RangeFuncCheck = 1
193 Debug.MergeLocals = 1
194
195 Debug.Checkptr = -1
196
197 Flag.Cfg.ImportMap = make(map[string]string)
198
199 objabi.AddVersionFlag()
200 registerFlags()
201 objabi.Flagparse(usage)
202 counter.CountFlags("compile/flag:", *flag.CommandLine)
203
204 if gcd := os.Getenv("GOCOMPILEDEBUG"); gcd != "" {
205
206
207 Flag.LowerD.Set(gcd)
208 }
209
210 if Debug.Gossahash != "" {
211 hashDebug = NewHashDebug("gossahash", Debug.Gossahash, nil)
212 }
213 obj.SetFIPSDebugHash(Debug.FIPSHash)
214
215
216
217 if Flag.Std && objabi.LookupPkgSpecial(Ctxt.Pkgpath).Runtime {
218 Flag.CompilingRuntime = true
219 }
220
221 Ctxt.Std = Flag.Std
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 if Debug.LoopVarHash != "" {
247
248 mostInlineOnly := true
249 if strings.HasPrefix(Debug.LoopVarHash, "IL") {
250
251
252
253
254
255 Debug.LoopVarHash = Debug.LoopVarHash[2:]
256 mostInlineOnly = false
257 }
258
259 LoopVarHash = NewHashDebug("loopvarhash", Debug.LoopVarHash, nil)
260 if Debug.LoopVar < 11 {
261 Debug.LoopVar = 1
262 }
263 LoopVarHash.SetInlineSuffixOnly(mostInlineOnly)
264 } else if buildcfg.Experiment.LoopVar && Debug.LoopVar == 0 {
265 Debug.LoopVar = 1
266 }
267
268 if Debug.Converthash != "" {
269 ConvertHash = NewHashDebug("converthash", Debug.Converthash, nil)
270 } else {
271
272 ConvertHash = NewHashDebug("converthash", "qn", nil)
273 }
274 if Debug.Fmahash != "" {
275 FmaHash = NewHashDebug("fmahash", Debug.Fmahash, nil)
276 }
277 if Debug.PGOHash != "" {
278 PGOHash = NewHashDebug("pgohash", Debug.PGOHash, nil)
279 }
280 if Debug.LiteralAllocHash != "" {
281 LiteralAllocHash = NewHashDebug("literalalloc", Debug.LiteralAllocHash, nil)
282 }
283
284 if Debug.MergeLocalsHash != "" {
285 MergeLocalsHash = NewHashDebug("mergelocals", Debug.MergeLocalsHash, nil)
286 }
287 if Debug.VariableMakeHash != "" {
288 VariableMakeHash = NewHashDebug("variablemake", Debug.VariableMakeHash, nil)
289 }
290
291 if Flag.MSan && !platform.MSanSupported(buildcfg.GOOS, buildcfg.GOARCH) {
292 log.Fatalf("%s/%s does not support -msan", buildcfg.GOOS, buildcfg.GOARCH)
293 }
294 if Flag.ASan && !platform.ASanSupported(buildcfg.GOOS, buildcfg.GOARCH) {
295 log.Fatalf("%s/%s does not support -asan", buildcfg.GOOS, buildcfg.GOARCH)
296 }
297 if Flag.Race && !platform.RaceDetectorSupported(buildcfg.GOOS, buildcfg.GOARCH) {
298 log.Fatalf("%s/%s does not support -race", buildcfg.GOOS, buildcfg.GOARCH)
299 }
300 if (*Flag.Shared || *Flag.Dynlink || *Flag.LinkShared) && !Ctxt.Arch.InFamily(sys.AMD64, sys.ARM, sys.ARM64, sys.I386, sys.Loong64, sys.MIPS64, sys.PPC64, sys.RISCV64, sys.S390X) {
301 log.Fatalf("%s/%s does not support -shared", buildcfg.GOOS, buildcfg.GOARCH)
302 }
303 parseSpectre(Flag.Spectre)
304
305 Ctxt.CompressInstructions = Debug.CompressInstructions != 0
306 Ctxt.Flag_shared = Ctxt.Flag_dynlink || Ctxt.Flag_shared
307 Ctxt.Flag_optimize = Flag.N == 0
308 Ctxt.Debugasm = int(Flag.S)
309 Ctxt.Flag_maymorestack = Debug.MayMoreStack
310 Ctxt.Flag_noRefName = Debug.NoRefName != 0
311
312 if flag.NArg() < 1 {
313 usage()
314 }
315
316 if Flag.GoVersion != "" && Flag.GoVersion != runtime.Version() {
317 fmt.Printf("compile: version %q does not match go tool version %q\n", runtime.Version(), Flag.GoVersion)
318 Exit(2)
319 }
320
321 if *Flag.LowerP == "" {
322 *Flag.LowerP = obj.UnlinkablePkg
323 }
324
325 if Flag.LowerO == "" {
326 p := flag.Arg(0)
327 if i := strings.LastIndex(p, "/"); i >= 0 {
328 p = p[i+1:]
329 }
330 if runtime.GOOS == "windows" {
331 if i := strings.LastIndex(p, `\`); i >= 0 {
332 p = p[i+1:]
333 }
334 }
335 if i := strings.LastIndex(p, "."); i >= 0 {
336 p = p[:i]
337 }
338 suffix := ".o"
339 if Flag.Pack {
340 suffix = ".a"
341 }
342 Flag.LowerO = p + suffix
343 }
344 switch {
345 case Flag.Race && Flag.MSan:
346 log.Fatal("cannot use both -race and -msan")
347 case Flag.Race && Flag.ASan:
348 log.Fatal("cannot use both -race and -asan")
349 case Flag.MSan && Flag.ASan:
350 log.Fatal("cannot use both -msan and -asan")
351 }
352 if Flag.Race || Flag.MSan || Flag.ASan {
353
354 if Debug.Checkptr == -1 {
355 Debug.Checkptr = 1
356 }
357 }
358
359 if Flag.LowerC < 1 {
360 log.Fatalf("-c must be at least 1, got %d", Flag.LowerC)
361 }
362 if !concurrentBackendAllowed() {
363 Flag.LowerC = 1
364 }
365
366 if Flag.CompilingRuntime {
367
368
369 Flag.N = 0
370 Ctxt.Flag_optimize = true
371
372
373 Debug.Checkptr = 0
374
375
376 Debug.Libfuzzer = 0
377 }
378
379 if Debug.Checkptr == -1 {
380 Debug.Checkptr = 0
381 }
382
383
384 Ctxt.Debugpcln = Debug.PCTab
385
386
387 if buildcfg.GOOS == "plan9" && buildcfg.GOARCH == "386" {
388 Debug.AlignHot = 0
389 }
390 }
391
392
393
394 func registerFlags() {
395 var (
396 boolType = reflect.TypeFor[bool]()
397 intType = reflect.TypeFor[int]()
398 stringType = reflect.TypeFor[string]()
399 ptrBoolType = reflect.TypeFor[*bool]()
400 ptrIntType = reflect.TypeFor[*int]()
401 ptrStringType = reflect.TypeFor[*string]()
402 countType = reflect.TypeFor[CountFlag]()
403 funcType = reflect.TypeFor[func(string)]()
404 )
405
406 v := reflect.ValueOf(&Flag).Elem()
407 t := v.Type()
408 for i := 0; i < t.NumField(); i++ {
409 f := t.Field(i)
410 if f.Name == "Cfg" {
411 continue
412 }
413
414 var name string
415 if len(f.Name) == 1 {
416 name = f.Name
417 } else if len(f.Name) == 6 && f.Name[:5] == "Lower" && 'A' <= f.Name[5] && f.Name[5] <= 'Z' {
418 name = string(rune(f.Name[5] + 'a' - 'A'))
419 } else {
420 name = strings.ToLower(f.Name)
421 }
422 if tag := f.Tag.Get("flag"); tag != "" {
423 name = tag
424 }
425
426 help := f.Tag.Get("help")
427 if help == "" {
428 panic(fmt.Sprintf("base.Flag.%s is missing help text", f.Name))
429 }
430
431 if k := f.Type.Kind(); (k == reflect.Ptr || k == reflect.Func) && v.Field(i).IsNil() {
432 panic(fmt.Sprintf("base.Flag.%s is uninitialized %v", f.Name, f.Type))
433 }
434
435 switch f.Type {
436 case boolType:
437 p := v.Field(i).Addr().Interface().(*bool)
438 flag.BoolVar(p, name, *p, help)
439 case intType:
440 p := v.Field(i).Addr().Interface().(*int)
441 flag.IntVar(p, name, *p, help)
442 case stringType:
443 p := v.Field(i).Addr().Interface().(*string)
444 flag.StringVar(p, name, *p, help)
445 case ptrBoolType:
446 p := v.Field(i).Interface().(*bool)
447 flag.BoolVar(p, name, *p, help)
448 case ptrIntType:
449 p := v.Field(i).Interface().(*int)
450 flag.IntVar(p, name, *p, help)
451 case ptrStringType:
452 p := v.Field(i).Interface().(*string)
453 flag.StringVar(p, name, *p, help)
454 case countType:
455 p := (*int)(v.Field(i).Addr().Interface().(*CountFlag))
456 objabi.Flagcount(name, help, p)
457 case funcType:
458 f := v.Field(i).Interface().(func(string))
459 objabi.Flagfn1(name, help, f)
460 default:
461 if val, ok := v.Field(i).Interface().(flag.Value); ok {
462 flag.Var(val, name, help)
463 } else {
464 panic(fmt.Sprintf("base.Flag.%s has unexpected type %s", f.Name, f.Type))
465 }
466 }
467 }
468 }
469
470
471
472 func concurrentFlagOk() bool {
473
474 return Flag.Percent == 0 &&
475 Flag.E == 0 &&
476 Flag.K == 0 &&
477 Flag.L == 0 &&
478 Flag.LowerH == 0 &&
479 Flag.LowerJ == 0 &&
480 Flag.LowerM == 0 &&
481 Flag.LowerR == 0
482 }
483
484 func concurrentBackendAllowed() bool {
485 if !concurrentFlagOk() {
486 return false
487 }
488
489
490
491
492
493 if Ctxt.Debugvlog || !Debug.ConcurrentOk || Flag.Live > 0 {
494 return false
495 }
496
497 if buildcfg.Experiment.FieldTrack {
498 return false
499 }
500
501 if Ctxt.Flag_dynlink || Flag.Race {
502 return false
503 }
504 return true
505 }
506
507 func addImportDir(dir string) {
508 if dir != "" {
509 Flag.Cfg.ImportDirs = append(Flag.Cfg.ImportDirs, dir)
510 }
511 }
512
513 func readImportCfg(file string) {
514 if Flag.Cfg.ImportMap == nil {
515 Flag.Cfg.ImportMap = make(map[string]string)
516 }
517 Flag.Cfg.PackageFile = map[string]string{}
518 data, err := os.ReadFile(file)
519 if err != nil {
520 log.Fatalf("-importcfg: %v", err)
521 }
522
523 for lineNum, line := range strings.Split(string(data), "\n") {
524 lineNum++
525 line = strings.TrimSpace(line)
526 if line == "" || strings.HasPrefix(line, "#") {
527 continue
528 }
529
530 verb, args, found := strings.Cut(line, " ")
531 if found {
532 args = strings.TrimSpace(args)
533 }
534 before, after, hasEq := strings.Cut(args, "=")
535
536 switch verb {
537 default:
538 log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb)
539 case "importmap":
540 if !hasEq || before == "" || after == "" {
541 log.Fatalf(`%s:%d: invalid importmap: syntax is "importmap old=new"`, file, lineNum)
542 }
543 Flag.Cfg.ImportMap[before] = after
544 case "packagefile":
545 if !hasEq || before == "" || after == "" {
546 log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum)
547 }
548 Flag.Cfg.PackageFile[before] = after
549 }
550 }
551 }
552
553 func readCoverageCfg(file string) {
554 var cfg covcmd.CoverFixupConfig
555 data, err := os.ReadFile(file)
556 if err != nil {
557 log.Fatalf("-coveragecfg: %v", err)
558 }
559 if err := json.Unmarshal(data, &cfg); err != nil {
560 log.Fatalf("error reading -coveragecfg file %q: %v", file, err)
561 }
562 Flag.Cfg.CoverageInfo = &cfg
563 }
564
565 func readEmbedCfg(file string) {
566 data, err := os.ReadFile(file)
567 if err != nil {
568 log.Fatalf("-embedcfg: %v", err)
569 }
570 if err := json.Unmarshal(data, &Flag.Cfg.Embed); err != nil {
571 log.Fatalf("%s: %v", file, err)
572 }
573 if Flag.Cfg.Embed.Patterns == nil {
574 log.Fatalf("%s: invalid embedcfg: missing Patterns", file)
575 }
576 if Flag.Cfg.Embed.Files == nil {
577 log.Fatalf("%s: invalid embedcfg: missing Files", file)
578 }
579 }
580
581
582 func parseSpectre(s string) {
583 for f := range strings.SplitSeq(s, ",") {
584 f = strings.TrimSpace(f)
585 switch f {
586 default:
587 log.Fatalf("unknown setting -spectre=%s", f)
588 case "":
589
590 case "all":
591 Flag.Cfg.SpectreIndex = true
592 Ctxt.Retpoline = true
593 case "index":
594 Flag.Cfg.SpectreIndex = true
595 case "ret":
596 Ctxt.Retpoline = true
597 }
598 }
599
600 if Flag.Cfg.SpectreIndex {
601 switch buildcfg.GOARCH {
602 case "amd64":
603
604 default:
605 log.Fatalf("GOARCH=%s does not support -spectre=index", buildcfg.GOARCH)
606 }
607 }
608 }
609
View as plain text