1// Copyright 2011 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// Action graph execution. 6 7package work 8 9import ( 10 "bytes" 11 "cmd/go/internal/fsys" 12 "context" 13 "encoding/json" 14 "errors" 15 "fmt" 16 exec "internal/execabs" 17 "internal/lazyregexp" 18 "io" 19 "io/fs" 20 "log" 21 "math/rand" 22 "os" 23 "path/filepath" 24 "regexp" 25 "runtime" 26 "strconv" 27 "strings" 28 "sync" 29 "time" 30 31 "cmd/go/internal/base" 32 "cmd/go/internal/cache" 33 "cmd/go/internal/cfg" 34 "cmd/go/internal/load" 35 "cmd/go/internal/modload" 36 "cmd/go/internal/str" 37 "cmd/go/internal/trace" 38) 39 40// actionList returns the list of actions in the dag rooted at root 41// as visited in a depth-first post-order traversal. 42func actionList(root *Action) []*Action { 43 seen := map[*Action]bool{} 44 all := []*Action{} 45 var walk func(*Action) 46 walk = func(a *Action) { 47 if seen[a] { 48 return 49 } 50 seen[a] = true 51 for _, a1 := range a.Deps { 52 walk(a1) 53 } 54 all = append(all, a) 55 } 56 walk(root) 57 return all 58} 59 60// do runs the action graph rooted at root. 61func (b *Builder) Do(ctx context.Context, root *Action) { 62 ctx, span := trace.StartSpan(ctx, "exec.Builder.Do ("+root.Mode+" "+root.Target+")") 63 defer span.Done() 64 65 if !b.IsCmdList { 66 // If we're doing real work, take time at the end to trim the cache. 67 c := cache.Default() 68 defer c.Trim() 69 } 70 71 // Build list of all actions, assigning depth-first post-order priority. 72 // The original implementation here was a true queue 73 // (using a channel) but it had the effect of getting 74 // distracted by low-level leaf actions to the detriment 75 // of completing higher-level actions. The order of 76 // work does not matter much to overall execution time, 77 // but when running "go test std" it is nice to see each test 78 // results as soon as possible. The priorities assigned 79 // ensure that, all else being equal, the execution prefers 80 // to do what it would have done first in a simple depth-first 81 // dependency order traversal. 82 all := actionList(root) 83 for i, a := range all { 84 a.priority = i 85 } 86 87 // Write action graph, without timing information, in case we fail and exit early. 88 writeActionGraph := func() { 89 if file := cfg.DebugActiongraph; file != "" { 90 if strings.HasSuffix(file, ".go") { 91 // Do not overwrite Go source code in: 92 // go build -debug-actiongraph x.go 93 base.Fatalf("go: refusing to write action graph to %v\n", file) 94 } 95 js := actionGraphJSON(root) 96 if err := os.WriteFile(file, []byte(js), 0666); err != nil { 97 fmt.Fprintf(os.Stderr, "go: writing action graph: %v\n", err) 98 base.SetExitStatus(1) 99 } 100 } 101 } 102 writeActionGraph() 103 104 b.readySema = make(chan bool, len(all)) 105 106 // Initialize per-action execution state. 107 for _, a := range all { 108 for _, a1 := range a.Deps { 109 a1.triggers = append(a1.triggers, a) 110 } 111 a.pending = len(a.Deps) 112 if a.pending == 0 { 113 b.ready.push(a) 114 b.readySema <- true 115 } 116 } 117 118 // Handle runs a single action and takes care of triggering 119 // any actions that are runnable as a result. 120 handle := func(ctx context.Context, a *Action) { 121 if a.json != nil { 122 a.json.TimeStart = time.Now() 123 } 124 var err error 125 if a.Func != nil && (!a.Failed || a.IgnoreFail) { 126 // TODO(matloob): Better action descriptions 127 desc := "Executing action " 128 if a.Package != nil { 129 desc += "(" + a.Mode + " " + a.Package.Desc() + ")" 130 } 131 ctx, span := trace.StartSpan(ctx, desc) 132 a.traceSpan = span 133 for _, d := range a.Deps { 134 trace.Flow(ctx, d.traceSpan, a.traceSpan) 135 } 136 err = a.Func(b, ctx, a) 137 span.Done() 138 } 139 if a.json != nil { 140 a.json.TimeDone = time.Now() 141 } 142 143 // The actions run in parallel but all the updates to the 144 // shared work state are serialized through b.exec. 145 b.exec.Lock() 146 defer b.exec.Unlock() 147 148 if err != nil { 149 if err == errPrintedOutput { 150 base.SetExitStatus(2) 151 } else { 152 base.Errorf("%s", err) 153 } 154 a.Failed = true 155 } 156 157 for _, a0 := range a.triggers { 158 if a.Failed { 159 a0.Failed = true 160 } 161 if a0.pending--; a0.pending == 0 { 162 b.ready.push(a0) 163 b.readySema <- true 164 } 165 } 166 167 if a == root { 168 close(b.readySema) 169 } 170 } 171 172 var wg sync.WaitGroup 173 174 // Kick off goroutines according to parallelism. 175 // If we are using the -n flag (just printing commands) 176 // drop the parallelism to 1, both to make the output 177 // deterministic and because there is no real work anyway. 178 par := cfg.BuildP 179 if cfg.BuildN { 180 par = 1 181 } 182 for i := 0; i < par; i++ { 183 wg.Add(1) 184 go func() { 185 ctx := trace.StartGoroutine(ctx) 186 defer wg.Done() 187 for { 188 select { 189 case _, ok := <-b.readySema: 190 if !ok { 191 return 192 } 193 // Receiving a value from b.readySema entitles 194 // us to take from the ready queue. 195 b.exec.Lock() 196 a := b.ready.pop() 197 b.exec.Unlock() 198 handle(ctx, a) 199 case <-base.Interrupted: 200 base.SetExitStatus(1) 201 return 202 } 203 } 204 }() 205 } 206 207 wg.Wait() 208 209 // Write action graph again, this time with timing information. 210 writeActionGraph() 211} 212 213// buildActionID computes the action ID for a build action. 214func (b *Builder) buildActionID(a *Action) cache.ActionID { 215 p := a.Package 216 h := cache.NewHash("build " + p.ImportPath) 217 218 // Configuration independent of compiler toolchain. 219 // Note: buildmode has already been accounted for in buildGcflags 220 // and should not be inserted explicitly. Most buildmodes use the 221 // same compiler settings and can reuse each other's results. 222 // If not, the reason is already recorded in buildGcflags. 223 fmt.Fprintf(h, "compile\n") 224 // Only include the package directory if it may affect the output. 225 // We trim workspace paths for all packages when -trimpath is set. 226 // The compiler hides the exact value of $GOROOT 227 // when building things in GOROOT. 228 // Assume b.WorkDir is being trimmed properly. 229 // When -trimpath is used with a package built from the module cache, 230 // use the module path and version instead of the directory. 231 if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) { 232 fmt.Fprintf(h, "dir %s\n", p.Dir) 233 } else if cfg.BuildTrimpath && p.Module != nil { 234 fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version) 235 } 236 if p.Module != nil { 237 fmt.Fprintf(h, "go %s\n", p.Module.GoVersion) 238 } 239 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch) 240 fmt.Fprintf(h, "import %q\n", p.ImportPath) 241 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix) 242 if cfg.BuildTrimpath { 243 fmt.Fprintln(h, "trimpath") 244 } 245 if p.Internal.ForceLibrary { 246 fmt.Fprintf(h, "forcelibrary\n") 247 } 248 if len(p.CgoFiles)+len(p.SwigFiles) > 0 { 249 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo")) 250 cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p) 251 fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags) 252 if len(p.CXXFiles)+len(p.SwigFiles) > 0 { 253 fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags) 254 } 255 if len(p.FFiles) > 0 { 256 fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags) 257 } 258 // TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions? 259 } 260 if p.Internal.CoverMode != "" { 261 fmt.Fprintf(h, "cover %q %q\n", p.Internal.CoverMode, b.toolID("cover")) 262 } 263 fmt.Fprintf(h, "modinfo %q\n", p.Internal.BuildInfo) 264 265 // Configuration specific to compiler toolchain. 266 switch cfg.BuildToolchainName { 267 default: 268 base.Fatalf("buildActionID: unknown build toolchain %q", cfg.BuildToolchainName) 269 case "gc": 270 fmt.Fprintf(h, "compile %s %q %q\n", b.toolID("compile"), forcedGcflags, p.Internal.Gcflags) 271 if len(p.SFiles) > 0 { 272 fmt.Fprintf(h, "asm %q %q %q\n", b.toolID("asm"), forcedAsmflags, p.Internal.Asmflags) 273 } 274 275 // GOARM, GOMIPS, etc. 276 key, val := cfg.GetArchEnv() 277 fmt.Fprintf(h, "%s=%s\n", key, val) 278 279 // TODO(rsc): Convince compiler team not to add more magic environment variables, 280 // or perhaps restrict the environment variables passed to subprocesses. 281 // Because these are clumsy, undocumented special-case hacks 282 // for debugging the compiler, they are not settable using 'go env -w', 283 // and so here we use os.Getenv, not cfg.Getenv. 284 magic := []string{ 285 "GOCLOBBERDEADHASH", 286 "GOSSAFUNC", 287 "GO_SSA_PHI_LOC_CUTOFF", 288 "GOSSAHASH", 289 } 290 for _, env := range magic { 291 if x := os.Getenv(env); x != "" { 292 fmt.Fprintf(h, "magic %s=%s\n", env, x) 293 } 294 } 295 if os.Getenv("GOSSAHASH") != "" { 296 for i := 0; ; i++ { 297 env := fmt.Sprintf("GOSSAHASH%d", i) 298 x := os.Getenv(env) 299 if x == "" { 300 break 301 } 302 fmt.Fprintf(h, "magic %s=%s\n", env, x) 303 } 304 } 305 if os.Getenv("GSHS_LOGFILE") != "" { 306 // Clumsy hack. Compiler writes to this log file, 307 // so do not allow use of cache at all. 308 // We will still write to the cache but it will be 309 // essentially unfindable. 310 fmt.Fprintf(h, "nocache %d\n", time.Now().UnixNano()) 311 } 312 313 case "gccgo": 314 id, err := b.gccgoToolID(BuildToolchain.compiler(), "go") 315 if err != nil { 316 base.Fatalf("%v", err) 317 } 318 fmt.Fprintf(h, "compile %s %q %q\n", id, forcedGccgoflags, p.Internal.Gccgoflags) 319 fmt.Fprintf(h, "pkgpath %s\n", gccgoPkgpath(p)) 320 fmt.Fprintf(h, "ar %q\n", BuildToolchain.(gccgoToolchain).ar()) 321 if len(p.SFiles) > 0 { 322 id, _ = b.gccgoToolID(BuildToolchain.compiler(), "assembler-with-cpp") 323 // Ignore error; different assembler versions 324 // are unlikely to make any difference anyhow. 325 fmt.Fprintf(h, "asm %q\n", id) 326 } 327 } 328 329 // Input files. 330 inputFiles := str.StringList( 331 p.GoFiles, 332 p.CgoFiles, 333 p.CFiles, 334 p.CXXFiles, 335 p.FFiles, 336 p.MFiles, 337 p.HFiles, 338 p.SFiles, 339 p.SysoFiles, 340 p.SwigFiles, 341 p.SwigCXXFiles, 342 p.EmbedFiles, 343 ) 344 for _, file := range inputFiles { 345 fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file))) 346 } 347 for _, a1 := range a.Deps { 348 p1 := a1.Package 349 if p1 != nil { 350 fmt.Fprintf(h, "import %s %s\n", p1.ImportPath, contentID(a1.buildID)) 351 } 352 } 353 354 return h.Sum() 355} 356 357// needCgoHdr reports whether the actions triggered by this one 358// expect to be able to access the cgo-generated header file. 359func (b *Builder) needCgoHdr(a *Action) bool { 360 // If this build triggers a header install, run cgo to get the header. 361 if !b.IsCmdList && (a.Package.UsesCgo() || a.Package.UsesSwig()) && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") { 362 for _, t1 := range a.triggers { 363 if t1.Mode == "install header" { 364 return true 365 } 366 } 367 for _, t1 := range a.triggers { 368 for _, t2 := range t1.triggers { 369 if t2.Mode == "install header" { 370 return true 371 } 372 } 373 } 374 } 375 return false 376} 377 378// allowedVersion reports whether the version v is an allowed version of go 379// (one that we can compile). 380// v is known to be of the form "1.23". 381func allowedVersion(v string) bool { 382 // Special case: no requirement. 383 if v == "" { 384 return true 385 } 386 // Special case "1.0" means "go1", which is OK. 387 if v == "1.0" { 388 return true 389 } 390 // Otherwise look through release tags of form "go1.23" for one that matches. 391 for _, tag := range cfg.BuildContext.ReleaseTags { 392 if strings.HasPrefix(tag, "go") && tag[2:] == v { 393 return true 394 } 395 } 396 return false 397} 398 399const ( 400 needBuild uint32 = 1 << iota 401 needCgoHdr 402 needVet 403 needCompiledGoFiles 404 needStale 405) 406 407// build is the action for building a single package. 408// Note that any new influence on this logic must be reported in b.buildActionID above as well. 409func (b *Builder) build(ctx context.Context, a *Action) (err error) { 410 p := a.Package 411 412 bit := func(x uint32, b bool) uint32 { 413 if b { 414 return x 415 } 416 return 0 417 } 418 419 cachedBuild := false 420 need := bit(needBuild, !b.IsCmdList && a.needBuild || b.NeedExport) | 421 bit(needCgoHdr, b.needCgoHdr(a)) | 422 bit(needVet, a.needVet) | 423 bit(needCompiledGoFiles, b.NeedCompiledGoFiles) 424 425 if !p.BinaryOnly { 426 if b.useCache(a, b.buildActionID(a), p.Target) { 427 // We found the main output in the cache. 428 // If we don't need any other outputs, we can stop. 429 // Otherwise, we need to write files to a.Objdir (needVet, needCgoHdr). 430 // Remember that we might have them in cache 431 // and check again after we create a.Objdir. 432 cachedBuild = true 433 a.output = []byte{} // start saving output in case we miss any cache results 434 need &^= needBuild 435 if b.NeedExport { 436 p.Export = a.built 437 p.BuildID = a.buildID 438 } 439 if need&needCompiledGoFiles != 0 { 440 if err := b.loadCachedSrcFiles(a); err == nil { 441 need &^= needCompiledGoFiles 442 } 443 } 444 } 445 446 // Source files might be cached, even if the full action is not 447 // (e.g., go list -compiled -find). 448 if !cachedBuild && need&needCompiledGoFiles != 0 { 449 if err := b.loadCachedSrcFiles(a); err == nil { 450 need &^= needCompiledGoFiles 451 } 452 } 453 454 if need == 0 { 455 return nil 456 } 457 defer b.flushOutput(a) 458 } 459 460 defer func() { 461 if err != nil && err != errPrintedOutput { 462 err = fmt.Errorf("go build %s: %v", a.Package.ImportPath, err) 463 } 464 if err != nil && b.IsCmdList && b.NeedError && p.Error == nil { 465 p.Error = &load.PackageError{Err: err} 466 } 467 }() 468 if cfg.BuildN { 469 // In -n mode, print a banner between packages. 470 // The banner is five lines so that when changes to 471 // different sections of the bootstrap script have to 472 // be merged, the banners give patch something 473 // to use to find its context. 474 b.Print("\n#\n# " + a.Package.ImportPath + "\n#\n\n") 475 } 476 477 if cfg.BuildV { 478 b.Print(a.Package.ImportPath + "\n") 479 } 480 481 if a.Package.BinaryOnly { 482 p.Stale = true 483 p.StaleReason = "binary-only packages are no longer supported" 484 if b.IsCmdList { 485 return nil 486 } 487 return errors.New("binary-only packages are no longer supported") 488 } 489 490 if err := b.Mkdir(a.Objdir); err != nil { 491 return err 492 } 493 objdir := a.Objdir 494 495 // Load cached cgo header, but only if we're skipping the main build (cachedBuild==true). 496 if cachedBuild && need&needCgoHdr != 0 { 497 if err := b.loadCachedCgoHdr(a); err == nil { 498 need &^= needCgoHdr 499 } 500 } 501 502 // Load cached vet config, but only if that's all we have left 503 // (need == needVet, not testing just the one bit). 504 // If we are going to do a full build anyway, 505 // we're going to regenerate the files below anyway. 506 if need == needVet { 507 if err := b.loadCachedVet(a); err == nil { 508 need &^= needVet 509 } 510 } 511 if need == 0 { 512 return nil 513 } 514 515 if err := allowInstall(a); err != nil { 516 return err 517 } 518 519 // make target directory 520 dir, _ := filepath.Split(a.Target) 521 if dir != "" { 522 if err := b.Mkdir(dir); err != nil { 523 return err 524 } 525 } 526 527 gofiles := str.StringList(a.Package.GoFiles) 528 cgofiles := str.StringList(a.Package.CgoFiles) 529 cfiles := str.StringList(a.Package.CFiles) 530 sfiles := str.StringList(a.Package.SFiles) 531 cxxfiles := str.StringList(a.Package.CXXFiles) 532 var objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string 533 534 if a.Package.UsesCgo() || a.Package.UsesSwig() { 535 if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(a.Package); err != nil { 536 return 537 } 538 } 539 540 // Compute overlays for .c/.cc/.h/etc. and if there are any overlays 541 // put correct contents of all those files in the objdir, to ensure 542 // the correct headers are included. nonGoOverlay is the overlay that 543 // points from nongo files to the copied files in objdir. 544 nonGoFileLists := [][]string{a.Package.CFiles, a.Package.SFiles, a.Package.CXXFiles, a.Package.HFiles, a.Package.FFiles} 545OverlayLoop: 546 for _, fs := range nonGoFileLists { 547 for _, f := range fs { 548 if _, ok := fsys.OverlayPath(mkAbs(p.Dir, f)); ok { 549 a.nonGoOverlay = make(map[string]string) 550 break OverlayLoop 551 } 552 } 553 } 554 if a.nonGoOverlay != nil { 555 for _, fs := range nonGoFileLists { 556 for i := range fs { 557 from := mkAbs(p.Dir, fs[i]) 558 opath, _ := fsys.OverlayPath(from) 559 dst := objdir + filepath.Base(fs[i]) 560 if err := b.copyFile(dst, opath, 0666, false); err != nil { 561 return err 562 } 563 a.nonGoOverlay[from] = dst 564 } 565 } 566 } 567 568 // Run SWIG on each .swig and .swigcxx file. 569 // Each run will generate two files, a .go file and a .c or .cxx file. 570 // The .go file will use import "C" and is to be processed by cgo. 571 if a.Package.UsesSwig() { 572 outGo, outC, outCXX, err := b.swig(a, a.Package, objdir, pcCFLAGS) 573 if err != nil { 574 return err 575 } 576 cgofiles = append(cgofiles, outGo...) 577 cfiles = append(cfiles, outC...) 578 cxxfiles = append(cxxfiles, outCXX...) 579 } 580 581 // If we're doing coverage, preprocess the .go files and put them in the work directory 582 if a.Package.Internal.CoverMode != "" { 583 for i, file := range str.StringList(gofiles, cgofiles) { 584 var sourceFile string 585 var coverFile string 586 var key string 587 if strings.HasSuffix(file, ".cgo1.go") { 588 // cgo files have absolute paths 589 base := filepath.Base(file) 590 sourceFile = file 591 coverFile = objdir + base 592 key = strings.TrimSuffix(base, ".cgo1.go") + ".go" 593 } else { 594 sourceFile = filepath.Join(a.Package.Dir, file) 595 coverFile = objdir + file 596 key = file 597 } 598 coverFile = strings.TrimSuffix(coverFile, ".go") + ".cover.go" 599 cover := a.Package.Internal.CoverVars[key] 600 if cover == nil || base.IsTestFile(file) { 601 // Not covering this file. 602 continue 603 } 604 if err := b.cover(a, coverFile, sourceFile, cover.Var); err != nil { 605 return err 606 } 607 if i < len(gofiles) { 608 gofiles[i] = coverFile 609 } else { 610 cgofiles[i-len(gofiles)] = coverFile 611 } 612 } 613 } 614 615 // Run cgo. 616 if a.Package.UsesCgo() || a.Package.UsesSwig() { 617 // In a package using cgo, cgo compiles the C, C++ and assembly files with gcc. 618 // There is one exception: runtime/cgo's job is to bridge the 619 // cgo and non-cgo worlds, so it necessarily has files in both. 620 // In that case gcc only gets the gcc_* files. 621 var gccfiles []string 622 gccfiles = append(gccfiles, cfiles...) 623 cfiles = nil 624 if a.Package.Standard && a.Package.ImportPath == "runtime/cgo" { 625 filter := func(files, nongcc, gcc []string) ([]string, []string) { 626 for _, f := range files { 627 if strings.HasPrefix(f, "gcc_") { 628 gcc = append(gcc, f) 629 } else { 630 nongcc = append(nongcc, f) 631 } 632 } 633 return nongcc, gcc 634 } 635 sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles) 636 } else { 637 for _, sfile := range sfiles { 638 data, err := os.ReadFile(filepath.Join(a.Package.Dir, sfile)) 639 if err == nil { 640 if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) || 641 bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) || 642 bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) { 643 return fmt.Errorf("package using cgo has Go assembly file %s", sfile) 644 } 645 } 646 } 647 gccfiles = append(gccfiles, sfiles...) 648 sfiles = nil 649 } 650 651 outGo, outObj, err := b.cgo(a, base.Tool("cgo"), objdir, pcCFLAGS, pcLDFLAGS, mkAbsFiles(a.Package.Dir, cgofiles), gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles) 652 if err != nil { 653 return err 654 } 655 if cfg.BuildToolchainName == "gccgo" { 656 cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags") 657 } 658 cgoObjects = append(cgoObjects, outObj...) 659 gofiles = append(gofiles, outGo...) 660 661 switch cfg.BuildBuildmode { 662 case "c-archive", "c-shared": 663 b.cacheCgoHdr(a) 664 } 665 } 666 667 var srcfiles []string // .go and non-.go 668 srcfiles = append(srcfiles, gofiles...) 669 srcfiles = append(srcfiles, sfiles...) 670 srcfiles = append(srcfiles, cfiles...) 671 srcfiles = append(srcfiles, cxxfiles...) 672 b.cacheSrcFiles(a, srcfiles) 673 674 // Running cgo generated the cgo header. 675 need &^= needCgoHdr 676 677 // Sanity check only, since Package.load already checked as well. 678 if len(gofiles) == 0 { 679 return &load.NoGoError{Package: a.Package} 680 } 681 682 // Prepare Go vet config if needed. 683 if need&needVet != 0 { 684 buildVetConfig(a, srcfiles) 685 need &^= needVet 686 } 687 if need&needCompiledGoFiles != 0 { 688 if err := b.loadCachedSrcFiles(a); err != nil { 689 return fmt.Errorf("loading compiled Go files from cache: %w", err) 690 } 691 need &^= needCompiledGoFiles 692 } 693 if need == 0 { 694 // Nothing left to do. 695 return nil 696 } 697 698 // Collect symbol ABI requirements from assembly. 699 symabis, err := BuildToolchain.symabis(b, a, sfiles) 700 if err != nil { 701 return err 702 } 703 704 // Prepare Go import config. 705 // We start it off with a comment so it can't be empty, so icfg.Bytes() below is never nil. 706 // It should never be empty anyway, but there have been bugs in the past that resulted 707 // in empty configs, which then unfortunately turn into "no config passed to compiler", 708 // and the compiler falls back to looking in pkg itself, which mostly works, 709 // except when it doesn't. 710 var icfg bytes.Buffer 711 fmt.Fprintf(&icfg, "# import config\n") 712 for i, raw := range a.Package.Internal.RawImports { 713 final := a.Package.Imports[i] 714 if final != raw { 715 fmt.Fprintf(&icfg, "importmap %s=%s\n", raw, final) 716 } 717 } 718 for _, a1 := range a.Deps { 719 p1 := a1.Package 720 if p1 == nil || p1.ImportPath == "" { 721 continue 722 } 723 if a1.built != "" { 724 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built) 725 } 726 } 727 728 // Prepare Go embed config if needed. 729 // Unlike the import config, it's okay for the embed config to be empty. 730 var embedcfg []byte 731 if len(p.Internal.Embed) > 0 { 732 var embed struct { 733 Patterns map[string][]string 734 Files map[string]string 735 } 736 embed.Patterns = p.Internal.Embed 737 embed.Files = make(map[string]string) 738 for _, file := range p.EmbedFiles { 739 embed.Files[file] = filepath.Join(p.Dir, file) 740 } 741 js, err := json.MarshalIndent(&embed, "", "\t") 742 if err != nil { 743 return fmt.Errorf("marshal embedcfg: %v", err) 744 } 745 embedcfg = js 746 } 747 748 if p.Internal.BuildInfo != "" && cfg.ModulesEnabled { 749 if err := b.writeFile(objdir+"_gomod_.go", modload.ModInfoProg(p.Internal.BuildInfo, cfg.BuildToolchainName == "gccgo")); err != nil { 750 return err 751 } 752 gofiles = append(gofiles, objdir+"_gomod_.go") 753 } 754 755 // Compile Go. 756 objpkg := objdir + "_pkg_.a" 757 ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, gofiles) 758 if len(out) > 0 { 759 output := b.processOutput(out) 760 if p.Module != nil && !allowedVersion(p.Module.GoVersion) { 761 output += "note: module requires Go " + p.Module.GoVersion + "\n" 762 } 763 b.showOutput(a, a.Package.Dir, a.Package.Desc(), output) 764 if err != nil { 765 return errPrintedOutput 766 } 767 } 768 if err != nil { 769 if p.Module != nil && !allowedVersion(p.Module.GoVersion) { 770 b.showOutput(a, a.Package.Dir, a.Package.Desc(), "note: module requires Go "+p.Module.GoVersion+"\n") 771 } 772 return err 773 } 774 if ofile != objpkg { 775 objects = append(objects, ofile) 776 } 777 778 // Copy .h files named for goos or goarch or goos_goarch 779 // to names using GOOS and GOARCH. 780 // For example, defs_linux_amd64.h becomes defs_GOOS_GOARCH.h. 781 _goos_goarch := "_" + cfg.Goos + "_" + cfg.Goarch 782 _goos := "_" + cfg.Goos 783 _goarch := "_" + cfg.Goarch 784 for _, file := range a.Package.HFiles { 785 name, ext := fileExtSplit(file) 786 switch { 787 case strings.HasSuffix(name, _goos_goarch): 788 targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext 789 if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 790 return err 791 } 792 case strings.HasSuffix(name, _goarch): 793 targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext 794 if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 795 return err 796 } 797 case strings.HasSuffix(name, _goos): 798 targ := file[:len(name)-len(_goos)] + "_GOOS." + ext 799 if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 800 return err 801 } 802 } 803 } 804 805 for _, file := range cfiles { 806 out := file[:len(file)-len(".c")] + ".o" 807 if err := BuildToolchain.cc(b, a, objdir+out, file); err != nil { 808 return err 809 } 810 objects = append(objects, out) 811 } 812 813 // Assemble .s files. 814 if len(sfiles) > 0 { 815 ofiles, err := BuildToolchain.asm(b, a, sfiles) 816 if err != nil { 817 return err 818 } 819 objects = append(objects, ofiles...) 820 } 821 822 // For gccgo on ELF systems, we write the build ID as an assembler file. 823 // This lets us set the SHF_EXCLUDE flag. 824 // This is read by readGccgoArchive in cmd/internal/buildid/buildid.go. 825 if a.buildID != "" && cfg.BuildToolchainName == "gccgo" { 826 switch cfg.Goos { 827 case "aix", "android", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris": 828 asmfile, err := b.gccgoBuildIDFile(a) 829 if err != nil { 830 return err 831 } 832 ofiles, err := BuildToolchain.asm(b, a, []string{asmfile}) 833 if err != nil { 834 return err 835 } 836 objects = append(objects, ofiles...) 837 } 838 } 839 840 // NOTE(rsc): On Windows, it is critically important that the 841 // gcc-compiled objects (cgoObjects) be listed after the ordinary 842 // objects in the archive. I do not know why this is. 843 // https://golang.org/issue/2601 844 objects = append(objects, cgoObjects...) 845 846 // Add system object files. 847 for _, syso := range a.Package.SysoFiles { 848 objects = append(objects, filepath.Join(a.Package.Dir, syso)) 849 } 850 851 // Pack into archive in objdir directory. 852 // If the Go compiler wrote an archive, we only need to add the 853 // object files for non-Go sources to the archive. 854 // If the Go compiler wrote an archive and the package is entirely 855 // Go sources, there is no pack to execute at all. 856 if len(objects) > 0 { 857 if err := BuildToolchain.pack(b, a, objpkg, objects); err != nil { 858 return err 859 } 860 } 861 862 if err := b.updateBuildID(a, objpkg, true); err != nil { 863 return err 864 } 865 866 a.built = objpkg 867 return nil 868} 869 870func (b *Builder) cacheObjdirFile(a *Action, c *cache.Cache, name string) error { 871 f, err := os.Open(a.Objdir + name) 872 if err != nil { 873 return err 874 } 875 defer f.Close() 876 _, _, err = c.Put(cache.Subkey(a.actionID, name), f) 877 return err 878} 879 880func (b *Builder) findCachedObjdirFile(a *Action, c *cache.Cache, name string) (string, error) { 881 file, _, err := c.GetFile(cache.Subkey(a.actionID, name)) 882 if err != nil { 883 return "", fmt.Errorf("loading cached file %s: %w", name, err) 884 } 885 return file, nil 886} 887 888func (b *Builder) loadCachedObjdirFile(a *Action, c *cache.Cache, name string) error { 889 cached, err := b.findCachedObjdirFile(a, c, name) 890 if err != nil { 891 return err 892 } 893 return b.copyFile(a.Objdir+name, cached, 0666, true) 894} 895 896func (b *Builder) cacheCgoHdr(a *Action) { 897 c := cache.Default() 898 b.cacheObjdirFile(a, c, "_cgo_install.h") 899} 900 901func (b *Builder) loadCachedCgoHdr(a *Action) error { 902 c := cache.Default() 903 return b.loadCachedObjdirFile(a, c, "_cgo_install.h") 904} 905 906func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) { 907 c := cache.Default() 908 var buf bytes.Buffer 909 for _, file := range srcfiles { 910 if !strings.HasPrefix(file, a.Objdir) { 911 // not generated 912 buf.WriteString("./") 913 buf.WriteString(file) 914 buf.WriteString("\n") 915 continue 916 } 917 name := file[len(a.Objdir):] 918 buf.WriteString(name) 919 buf.WriteString("\n") 920 if err := b.cacheObjdirFile(a, c, name); err != nil { 921 return 922 } 923 } 924 c.PutBytes(cache.Subkey(a.actionID, "srcfiles"), buf.Bytes()) 925} 926 927func (b *Builder) loadCachedVet(a *Action) error { 928 c := cache.Default() 929 list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles")) 930 if err != nil { 931 return fmt.Errorf("reading srcfiles list: %w", err) 932 } 933 var srcfiles []string 934 for _, name := range strings.Split(string(list), "\n") { 935 if name == "" { // end of list 936 continue 937 } 938 if strings.HasPrefix(name, "./") { 939 srcfiles = append(srcfiles, name[2:]) 940 continue 941 } 942 if err := b.loadCachedObjdirFile(a, c, name); err != nil { 943 return err 944 } 945 srcfiles = append(srcfiles, a.Objdir+name) 946 } 947 buildVetConfig(a, srcfiles) 948 return nil 949} 950 951func (b *Builder) loadCachedSrcFiles(a *Action) error { 952 c := cache.Default() 953 list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles")) 954 if err != nil { 955 return fmt.Errorf("reading srcfiles list: %w", err) 956 } 957 var files []string 958 for _, name := range strings.Split(string(list), "\n") { 959 if name == "" { // end of list 960 continue 961 } 962 if strings.HasPrefix(name, "./") { 963 files = append(files, name[len("./"):]) 964 continue 965 } 966 file, err := b.findCachedObjdirFile(a, c, name) 967 if err != nil { 968 return fmt.Errorf("finding %s: %w", name, err) 969 } 970 files = append(files, file) 971 } 972 a.Package.CompiledGoFiles = files 973 return nil 974} 975 976// vetConfig is the configuration passed to vet describing a single package. 977type vetConfig struct { 978 ID string // package ID (example: "fmt [fmt.test]") 979 Compiler string // compiler name (gc, gccgo) 980 Dir string // directory containing package 981 ImportPath string // canonical import path ("package path") 982 GoFiles []string // absolute paths to package source files 983 NonGoFiles []string // absolute paths to package non-Go files 984 IgnoredFiles []string // absolute paths to ignored source files 985 986 ImportMap map[string]string // map import path in source code to package path 987 PackageFile map[string]string // map package path to .a file with export data 988 Standard map[string]bool // map package path to whether it's in the standard library 989 PackageVetx map[string]string // map package path to vetx data from earlier vet run 990 VetxOnly bool // only compute vetx data; don't report detected problems 991 VetxOutput string // write vetx data to this output file 992 993 SucceedOnTypecheckFailure bool // awful hack; see #18395 and below 994} 995 996func buildVetConfig(a *Action, srcfiles []string) { 997 // Classify files based on .go extension. 998 // srcfiles does not include raw cgo files. 999 var gofiles, nongofiles []string 1000 for _, name := range srcfiles { 1001 if strings.HasSuffix(name, ".go") { 1002 gofiles = append(gofiles, name) 1003 } else { 1004 nongofiles = append(nongofiles, name) 1005 } 1006 } 1007 1008 ignored := str.StringList(a.Package.IgnoredGoFiles, a.Package.IgnoredOtherFiles) 1009 1010 // Pass list of absolute paths to vet, 1011 // so that vet's error messages will use absolute paths, 1012 // so that we can reformat them relative to the directory 1013 // in which the go command is invoked. 1014 vcfg := &vetConfig{ 1015 ID: a.Package.ImportPath, 1016 Compiler: cfg.BuildToolchainName, 1017 Dir: a.Package.Dir, 1018 GoFiles: mkAbsFiles(a.Package.Dir, gofiles), 1019 NonGoFiles: mkAbsFiles(a.Package.Dir, nongofiles), 1020 IgnoredFiles: mkAbsFiles(a.Package.Dir, ignored), 1021 ImportPath: a.Package.ImportPath, 1022 ImportMap: make(map[string]string), 1023 PackageFile: make(map[string]string), 1024 Standard: make(map[string]bool), 1025 } 1026 a.vetCfg = vcfg 1027 for i, raw := range a.Package.Internal.RawImports { 1028 final := a.Package.Imports[i] 1029 vcfg.ImportMap[raw] = final 1030 } 1031 1032 // Compute the list of mapped imports in the vet config 1033 // so that we can add any missing mappings below. 1034 vcfgMapped := make(map[string]bool) 1035 for _, p := range vcfg.ImportMap { 1036 vcfgMapped[p] = true 1037 } 1038 1039 for _, a1 := range a.Deps { 1040 p1 := a1.Package 1041 if p1 == nil || p1.ImportPath == "" { 1042 continue 1043 } 1044 // Add import mapping if needed 1045 // (for imports like "runtime/cgo" that appear only in generated code). 1046 if !vcfgMapped[p1.ImportPath] { 1047 vcfg.ImportMap[p1.ImportPath] = p1.ImportPath 1048 } 1049 if a1.built != "" { 1050 vcfg.PackageFile[p1.ImportPath] = a1.built 1051 } 1052 if p1.Standard { 1053 vcfg.Standard[p1.ImportPath] = true 1054 } 1055 } 1056} 1057 1058// VetTool is the path to an alternate vet tool binary. 1059// The caller is expected to set it (if needed) before executing any vet actions. 1060var VetTool string 1061 1062// VetFlags are the default flags to pass to vet. 1063// The caller is expected to set them before executing any vet actions. 1064var VetFlags []string 1065 1066// VetExplicit records whether the vet flags were set explicitly on the command line. 1067var VetExplicit bool 1068 1069func (b *Builder) vet(ctx context.Context, a *Action) error { 1070 // a.Deps[0] is the build of the package being vetted. 1071 // a.Deps[1] is the build of the "fmt" package. 1072 1073 a.Failed = false // vet of dependency may have failed but we can still succeed 1074 1075 if a.Deps[0].Failed { 1076 // The build of the package has failed. Skip vet check. 1077 // Vet could return export data for non-typecheck errors, 1078 // but we ignore it because the package cannot be compiled. 1079 return nil 1080 } 1081 1082 vcfg := a.Deps[0].vetCfg 1083 if vcfg == nil { 1084 // Vet config should only be missing if the build failed. 1085 return fmt.Errorf("vet config not found") 1086 } 1087 1088 vcfg.VetxOnly = a.VetxOnly 1089 vcfg.VetxOutput = a.Objdir + "vet.out" 1090 vcfg.PackageVetx = make(map[string]string) 1091 1092 h := cache.NewHash("vet " + a.Package.ImportPath) 1093 fmt.Fprintf(h, "vet %q\n", b.toolID("vet")) 1094 1095 vetFlags := VetFlags 1096 1097 // In GOROOT, we enable all the vet tests during 'go test', 1098 // not just the high-confidence subset. This gets us extra 1099 // checking for the standard library (at some compliance cost) 1100 // and helps us gain experience about how well the checks 1101 // work, to help decide which should be turned on by default. 1102 // The command-line still wins. 1103 // 1104 // Note that this flag change applies even when running vet as 1105 // a dependency of vetting a package outside std. 1106 // (Otherwise we'd have to introduce a whole separate 1107 // space of "vet fmt as a dependency of a std top-level vet" 1108 // versus "vet fmt as a dependency of a non-std top-level vet".) 1109 // This is OK as long as the packages that are farther down the 1110 // dependency tree turn on *more* analysis, as here. 1111 // (The unsafeptr check does not write any facts for use by 1112 // later vet runs, nor does unreachable.) 1113 if a.Package.Goroot && !VetExplicit && VetTool == "" { 1114 // Turn off -unsafeptr checks. 1115 // There's too much unsafe.Pointer code 1116 // that vet doesn't like in low-level packages 1117 // like runtime, sync, and reflect. 1118 // Note that $GOROOT/src/buildall.bash 1119 // does the same for the misc-compile trybots 1120 // and should be updated if these flags are 1121 // changed here. 1122 vetFlags = []string{"-unsafeptr=false"} 1123 1124 // Also turn off -unreachable checks during go test. 1125 // During testing it is very common to make changes 1126 // like hard-coded forced returns or panics that make 1127 // code unreachable. It's unreasonable to insist on files 1128 // not having any unreachable code during "go test". 1129 // (buildall.bash still runs with -unreachable enabled 1130 // for the overall whole-tree scan.) 1131 if cfg.CmdName == "test" { 1132 vetFlags = append(vetFlags, "-unreachable=false") 1133 } 1134 } 1135 1136 // Note: We could decide that vet should compute export data for 1137 // all analyses, in which case we don't need to include the flags here. 1138 // But that would mean that if an analysis causes problems like 1139 // unexpected crashes there would be no way to turn it off. 1140 // It seems better to let the flags disable export analysis too. 1141 fmt.Fprintf(h, "vetflags %q\n", vetFlags) 1142 1143 fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID) 1144 for _, a1 := range a.Deps { 1145 if a1.Mode == "vet" && a1.built != "" { 1146 fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built)) 1147 vcfg.PackageVetx[a1.Package.ImportPath] = a1.built 1148 } 1149 } 1150 key := cache.ActionID(h.Sum()) 1151 1152 if vcfg.VetxOnly && !cfg.BuildA { 1153 c := cache.Default() 1154 if file, _, err := c.GetFile(key); err == nil { 1155 a.built = file 1156 return nil 1157 } 1158 } 1159 1160 js, err := json.MarshalIndent(vcfg, "", "\t") 1161 if err != nil { 1162 return fmt.Errorf("internal error marshaling vet config: %v", err) 1163 } 1164 js = append(js, '\n') 1165 if err := b.writeFile(a.Objdir+"vet.cfg", js); err != nil { 1166 return err 1167 } 1168 1169 // TODO(rsc): Why do we pass $GCCGO to go vet? 1170 env := b.cCompilerEnv() 1171 if cfg.BuildToolchainName == "gccgo" { 1172 env = append(env, "GCCGO="+BuildToolchain.compiler()) 1173 } 1174 1175 p := a.Package 1176 tool := VetTool 1177 if tool == "" { 1178 tool = base.Tool("vet") 1179 } 1180 runErr := b.run(a, p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, vetFlags, a.Objdir+"vet.cfg") 1181 1182 // If vet wrote export data, save it for input to future vets. 1183 if f, err := os.Open(vcfg.VetxOutput); err == nil { 1184 a.built = vcfg.VetxOutput 1185 cache.Default().Put(key, f) 1186 f.Close() 1187 } 1188 1189 return runErr 1190} 1191 1192// linkActionID computes the action ID for a link action. 1193func (b *Builder) linkActionID(a *Action) cache.ActionID { 1194 p := a.Package 1195 h := cache.NewHash("link " + p.ImportPath) 1196 1197 // Toolchain-independent configuration. 1198 fmt.Fprintf(h, "link\n") 1199 fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) 1200 fmt.Fprintf(h, "import %q\n", p.ImportPath) 1201 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix) 1202 if cfg.BuildTrimpath { 1203 fmt.Fprintln(h, "trimpath") 1204 } 1205 1206 // Toolchain-dependent configuration, shared with b.linkSharedActionID. 1207 b.printLinkerConfig(h, p) 1208 1209 // Input files. 1210 for _, a1 := range a.Deps { 1211 p1 := a1.Package 1212 if p1 != nil { 1213 if a1.built != "" || a1.buildID != "" { 1214 buildID := a1.buildID 1215 if buildID == "" { 1216 buildID = b.buildID(a1.built) 1217 } 1218 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID)) 1219 } 1220 // Because we put package main's full action ID into the binary's build ID, 1221 // we must also put the full action ID into the binary's action ID hash. 1222 if p1.Name == "main" { 1223 fmt.Fprintf(h, "packagemain %s\n", a1.buildID) 1224 } 1225 if p1.Shlib != "" { 1226 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib))) 1227 } 1228 } 1229 } 1230 1231 return h.Sum() 1232} 1233 1234// printLinkerConfig prints the linker config into the hash h, 1235// as part of the computation of a linker-related action ID. 1236func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) { 1237 switch cfg.BuildToolchainName { 1238 default: 1239 base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName) 1240 1241 case "gc": 1242 fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode) 1243 if p != nil { 1244 fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags) 1245 } 1246 1247 // GOARM, GOMIPS, etc. 1248 key, val := cfg.GetArchEnv() 1249 fmt.Fprintf(h, "%s=%s\n", key, val) 1250 1251 // The linker writes source file paths that say GOROOT_FINAL, but 1252 // only if -trimpath is not specified (see ld() in gc.go). 1253 gorootFinal := cfg.GOROOT_FINAL 1254 if cfg.BuildTrimpath { 1255 gorootFinal = trimPathGoRootFinal 1256 } 1257 fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal) 1258 1259 // GO_EXTLINK_ENABLED controls whether the external linker is used. 1260 fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED")) 1261 1262 // TODO(rsc): Do cgo settings and flags need to be included? 1263 // Or external linker settings and flags? 1264 1265 case "gccgo": 1266 id, err := b.gccgoToolID(BuildToolchain.linker(), "go") 1267 if err != nil { 1268 base.Fatalf("%v", err) 1269 } 1270 fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode) 1271 // TODO(iant): Should probably include cgo flags here. 1272 } 1273} 1274 1275// link is the action for linking a single command. 1276// Note that any new influence on this logic must be reported in b.linkActionID above as well. 1277func (b *Builder) link(ctx context.Context, a *Action) (err error) { 1278 if b.useCache(a, b.linkActionID(a), a.Package.Target) || b.IsCmdList { 1279 return nil 1280 } 1281 defer b.flushOutput(a) 1282 1283 if err := b.Mkdir(a.Objdir); err != nil { 1284 return err 1285 } 1286 1287 importcfg := a.Objdir + "importcfg.link" 1288 if err := b.writeLinkImportcfg(a, importcfg); err != nil { 1289 return err 1290 } 1291 1292 if err := allowInstall(a); err != nil { 1293 return err 1294 } 1295 1296 // make target directory 1297 dir, _ := filepath.Split(a.Target) 1298 if dir != "" { 1299 if err := b.Mkdir(dir); err != nil { 1300 return err 1301 } 1302 } 1303 1304 if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil { 1305 return err 1306 } 1307 1308 // Update the binary with the final build ID. 1309 // But if OmitDebug is set, don't rewrite the binary, because we set OmitDebug 1310 // on binaries that we are going to run and then delete. 1311 // There's no point in doing work on such a binary. 1312 // Worse, opening the binary for write here makes it 1313 // essentially impossible to safely fork+exec due to a fundamental 1314 // incompatibility between ETXTBSY and threads on modern Unix systems. 1315 // See golang.org/issue/22220. 1316 // We still call updateBuildID to update a.buildID, which is important 1317 // for test result caching, but passing rewrite=false (final arg) 1318 // means we don't actually rewrite the binary, nor store the 1319 // result into the cache. That's probably a net win: 1320 // less cache space wasted on large binaries we are not likely to 1321 // need again. (On the other hand it does make repeated go test slower.) 1322 // It also makes repeated go run slower, which is a win in itself: 1323 // we don't want people to treat go run like a scripting environment. 1324 if err := b.updateBuildID(a, a.Target, !a.Package.Internal.OmitDebug); err != nil { 1325 return err 1326 } 1327 1328 a.built = a.Target 1329 return nil 1330} 1331 1332func (b *Builder) writeLinkImportcfg(a *Action, file string) error { 1333 // Prepare Go import cfg. 1334 var icfg bytes.Buffer 1335 for _, a1 := range a.Deps { 1336 p1 := a1.Package 1337 if p1 == nil { 1338 continue 1339 } 1340 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built) 1341 if p1.Shlib != "" { 1342 fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib) 1343 } 1344 } 1345 return b.writeFile(file, icfg.Bytes()) 1346} 1347 1348// PkgconfigCmd returns a pkg-config binary name 1349// defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist. 1350func (b *Builder) PkgconfigCmd() string { 1351 return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0] 1352} 1353 1354// splitPkgConfigOutput parses the pkg-config output into a slice of 1355// flags. This implements the algorithm from pkgconf/libpkgconf/argvsplit.c. 1356func splitPkgConfigOutput(out []byte) ([]string, error) { 1357 if len(out) == 0 { 1358 return nil, nil 1359 } 1360 var flags []string 1361 flag := make([]byte, 0, len(out)) 1362 escaped := false 1363 quote := byte(0) 1364 1365 for _, c := range out { 1366 if escaped { 1367 if quote != 0 { 1368 switch c { 1369 case '$', '`', '"', '\\': 1370 default: 1371 flag = append(flag, '\\') 1372 } 1373 flag = append(flag, c) 1374 } else { 1375 flag = append(flag, c) 1376 } 1377 escaped = false 1378 } else if quote != 0 { 1379 if c == quote { 1380 quote = 0 1381 } else { 1382 switch c { 1383 case '\\': 1384 escaped = true 1385 default: 1386 flag = append(flag, c) 1387 } 1388 } 1389 } else if strings.IndexByte(" \t\n\v\f\r", c) < 0 { 1390 switch c { 1391 case '\\': 1392 escaped = true 1393 case '\'', '"': 1394 quote = c 1395 default: 1396 flag = append(flag, c) 1397 } 1398 } else if len(flag) != 0 { 1399 flags = append(flags, string(flag)) 1400 flag = flag[:0] 1401 } 1402 } 1403 if escaped { 1404 return nil, errors.New("broken character escaping in pkgconf output ") 1405 } 1406 if quote != 0 { 1407 return nil, errors.New("unterminated quoted string in pkgconf output ") 1408 } else if len(flag) != 0 { 1409 flags = append(flags, string(flag)) 1410 } 1411 1412 return flags, nil 1413} 1414 1415// Calls pkg-config if needed and returns the cflags/ldflags needed to build the package. 1416func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) { 1417 if pcargs := p.CgoPkgConfig; len(pcargs) > 0 { 1418 // pkg-config permits arguments to appear anywhere in 1419 // the command line. Move them all to the front, before --. 1420 var pcflags []string 1421 var pkgs []string 1422 for _, pcarg := range pcargs { 1423 if pcarg == "--" { 1424 // We're going to add our own "--" argument. 1425 } else if strings.HasPrefix(pcarg, "--") { 1426 pcflags = append(pcflags, pcarg) 1427 } else { 1428 pkgs = append(pkgs, pcarg) 1429 } 1430 } 1431 for _, pkg := range pkgs { 1432 if !load.SafeArg(pkg) { 1433 return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg) 1434 } 1435 } 1436 var out []byte 1437 out, err = b.runOut(nil, p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs) 1438 if err != nil { 1439 b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --cflags "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out)) 1440 b.Print(err.Error() + "\n") 1441 return nil, nil, errPrintedOutput 1442 } 1443 if len(out) > 0 { 1444 cflags, err = splitPkgConfigOutput(out) 1445 if err != nil { 1446 return nil, nil, err 1447 } 1448 if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil { 1449 return nil, nil, err 1450 } 1451 } 1452 out, err = b.runOut(nil, p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs) 1453 if err != nil { 1454 b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --libs "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out)) 1455 b.Print(err.Error() + "\n") 1456 return nil, nil, errPrintedOutput 1457 } 1458 if len(out) > 0 { 1459 ldflags = strings.Fields(string(out)) 1460 if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil { 1461 return nil, nil, err 1462 } 1463 } 1464 } 1465 1466 return 1467} 1468 1469func (b *Builder) installShlibname(ctx context.Context, a *Action) error { 1470 if err := allowInstall(a); err != nil { 1471 return err 1472 } 1473 1474 // TODO: BuildN 1475 a1 := a.Deps[0] 1476 err := os.WriteFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"), 0666) 1477 if err != nil { 1478 return err 1479 } 1480 if cfg.BuildX { 1481 b.Showcmd("", "echo '%s' > %s # internal", filepath.Base(a1.Target), a.Target) 1482 } 1483 return nil 1484} 1485 1486func (b *Builder) linkSharedActionID(a *Action) cache.ActionID { 1487 h := cache.NewHash("linkShared") 1488 1489 // Toolchain-independent configuration. 1490 fmt.Fprintf(h, "linkShared\n") 1491 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch) 1492 1493 // Toolchain-dependent configuration, shared with b.linkActionID. 1494 b.printLinkerConfig(h, nil) 1495 1496 // Input files. 1497 for _, a1 := range a.Deps { 1498 p1 := a1.Package 1499 if a1.built == "" { 1500 continue 1501 } 1502 if p1 != nil { 1503 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built))) 1504 if p1.Shlib != "" { 1505 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib))) 1506 } 1507 } 1508 } 1509 // Files named on command line are special. 1510 for _, a1 := range a.Deps[0].Deps { 1511 p1 := a1.Package 1512 fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built))) 1513 } 1514 1515 return h.Sum() 1516} 1517 1518func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) { 1519 if b.useCache(a, b.linkSharedActionID(a), a.Target) || b.IsCmdList { 1520 return nil 1521 } 1522 defer b.flushOutput(a) 1523 1524 if err := allowInstall(a); err != nil { 1525 return err 1526 } 1527 1528 if err := b.Mkdir(a.Objdir); err != nil { 1529 return err 1530 } 1531 1532 importcfg := a.Objdir + "importcfg.link" 1533 if err := b.writeLinkImportcfg(a, importcfg); err != nil { 1534 return err 1535 } 1536 1537 // TODO(rsc): There is a missing updateBuildID here, 1538 // but we have to decide where to store the build ID in these files. 1539 a.built = a.Target 1540 return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps) 1541} 1542 1543// BuildInstallFunc is the action for installing a single package or executable. 1544func BuildInstallFunc(b *Builder, ctx context.Context, a *Action) (err error) { 1545 defer func() { 1546 if err != nil && err != errPrintedOutput { 1547 // a.Package == nil is possible for the go install -buildmode=shared 1548 // action that installs libmangledname.so, which corresponds to 1549 // a list of packages, not just one. 1550 sep, path := "", "" 1551 if a.Package != nil { 1552 sep, path = " ", a.Package.ImportPath 1553 } 1554 err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err) 1555 } 1556 }() 1557 1558 a1 := a.Deps[0] 1559 a.buildID = a1.buildID 1560 if a.json != nil { 1561 a.json.BuildID = a.buildID 1562 } 1563 1564 // If we are using the eventual install target as an up-to-date 1565 // cached copy of the thing we built, then there's no need to 1566 // copy it into itself (and that would probably fail anyway). 1567 // In this case a1.built == a.Target because a1.built == p.Target, 1568 // so the built target is not in the a1.Objdir tree that b.cleanup(a1) removes. 1569 if a1.built == a.Target { 1570 a.built = a.Target 1571 if !a.buggyInstall { 1572 b.cleanup(a1) 1573 } 1574 // Whether we're smart enough to avoid a complete rebuild 1575 // depends on exactly what the staleness and rebuild algorithms 1576 // are, as well as potentially the state of the Go build cache. 1577 // We don't really want users to be able to infer (or worse start depending on) 1578 // those details from whether the modification time changes during 1579 // "go install", so do a best-effort update of the file times to make it 1580 // look like we rewrote a.Target even if we did not. Updating the mtime 1581 // may also help other mtime-based systems that depend on our 1582 // previous mtime updates that happened more often. 1583 // This is still not perfect - we ignore the error result, and if the file was 1584 // unwritable for some reason then pretending to have written it is also 1585 // confusing - but it's probably better than not doing the mtime update. 1586 // 1587 // But don't do that for the special case where building an executable 1588 // with -linkshared implicitly installs all its dependent libraries. 1589 // We want to hide that awful detail as much as possible, so don't 1590 // advertise it by touching the mtimes (usually the libraries are up 1591 // to date). 1592 if !a.buggyInstall && !b.IsCmdList { 1593 if cfg.BuildN { 1594 b.Showcmd("", "touch %s", a.Target) 1595 } else if err := allowInstall(a); err == nil { 1596 now := time.Now() 1597 os.Chtimes(a.Target, now, now) 1598 } 1599 } 1600 return nil 1601 } 1602 1603 // If we're building for go list -export, 1604 // never install anything; just keep the cache reference. 1605 if b.IsCmdList { 1606 a.built = a1.built 1607 return nil 1608 } 1609 if err := allowInstall(a); err != nil { 1610 return err 1611 } 1612 1613 if err := b.Mkdir(a.Objdir); err != nil { 1614 return err 1615 } 1616 1617 perm := fs.FileMode(0666) 1618 if a1.Mode == "link" { 1619 switch cfg.BuildBuildmode { 1620 case "c-archive", "c-shared", "plugin": 1621 default: 1622 perm = 0777 1623 } 1624 } 1625 1626 // make target directory 1627 dir, _ := filepath.Split(a.Target) 1628 if dir != "" { 1629 if err := b.Mkdir(dir); err != nil { 1630 return err 1631 } 1632 } 1633 1634 if !a.buggyInstall { 1635 defer b.cleanup(a1) 1636 } 1637 1638 return b.moveOrCopyFile(a.Target, a1.built, perm, false) 1639} 1640 1641// allowInstall returns a non-nil error if this invocation of the go command is 1642// allowed to install a.Target. 1643// 1644// (The build of cmd/go running under its own test is forbidden from installing 1645// to its original GOROOT.) 1646var allowInstall = func(*Action) error { return nil } 1647 1648// cleanup removes a's object dir to keep the amount of 1649// on-disk garbage down in a large build. On an operating system 1650// with aggressive buffering, cleaning incrementally like 1651// this keeps the intermediate objects from hitting the disk. 1652func (b *Builder) cleanup(a *Action) { 1653 if !cfg.BuildWork { 1654 if cfg.BuildX { 1655 // Don't say we are removing the directory if 1656 // we never created it. 1657 if _, err := os.Stat(a.Objdir); err == nil || cfg.BuildN { 1658 b.Showcmd("", "rm -r %s", a.Objdir) 1659 } 1660 } 1661 os.RemoveAll(a.Objdir) 1662 } 1663} 1664 1665// moveOrCopyFile is like 'mv src dst' or 'cp src dst'. 1666func (b *Builder) moveOrCopyFile(dst, src string, perm fs.FileMode, force bool) error { 1667 if cfg.BuildN { 1668 b.Showcmd("", "mv %s %s", src, dst) 1669 return nil 1670 } 1671 1672 // If we can update the mode and rename to the dst, do it. 1673 // Otherwise fall back to standard copy. 1674 1675 // If the source is in the build cache, we need to copy it. 1676 if strings.HasPrefix(src, cache.DefaultDir()) { 1677 return b.copyFile(dst, src, perm, force) 1678 } 1679 1680 // On Windows, always copy the file, so that we respect the NTFS 1681 // permissions of the parent folder. https://golang.org/issue/22343. 1682 // What matters here is not cfg.Goos (the system we are building 1683 // for) but runtime.GOOS (the system we are building on). 1684 if runtime.GOOS == "windows" { 1685 return b.copyFile(dst, src, perm, force) 1686 } 1687 1688 // If the destination directory has the group sticky bit set, 1689 // we have to copy the file to retain the correct permissions. 1690 // https://golang.org/issue/18878 1691 if fi, err := os.Stat(filepath.Dir(dst)); err == nil { 1692 if fi.IsDir() && (fi.Mode()&fs.ModeSetgid) != 0 { 1693 return b.copyFile(dst, src, perm, force) 1694 } 1695 } 1696 1697 // The perm argument is meant to be adjusted according to umask, 1698 // but we don't know what the umask is. 1699 // Create a dummy file to find out. 1700 // This avoids build tags and works even on systems like Plan 9 1701 // where the file mask computation incorporates other information. 1702 mode := perm 1703 f, err := os.OpenFile(filepath.Clean(dst)+"-go-tmp-umask", os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm) 1704 if err == nil { 1705 fi, err := f.Stat() 1706 if err == nil { 1707 mode = fi.Mode() & 0777 1708 } 1709 name := f.Name() 1710 f.Close() 1711 os.Remove(name) 1712 } 1713 1714 if err := os.Chmod(src, mode); err == nil { 1715 if err := os.Rename(src, dst); err == nil { 1716 if cfg.BuildX { 1717 b.Showcmd("", "mv %s %s", src, dst) 1718 } 1719 return nil 1720 } 1721 } 1722 1723 return b.copyFile(dst, src, perm, force) 1724} 1725 1726// copyFile is like 'cp src dst'. 1727func (b *Builder) copyFile(dst, src string, perm fs.FileMode, force bool) error { 1728 if cfg.BuildN || cfg.BuildX { 1729 b.Showcmd("", "cp %s %s", src, dst) 1730 if cfg.BuildN { 1731 return nil 1732 } 1733 } 1734 1735 sf, err := os.Open(src) 1736 if err != nil { 1737 return err 1738 } 1739 defer sf.Close() 1740 1741 // Be careful about removing/overwriting dst. 1742 // Do not remove/overwrite if dst exists and is a directory 1743 // or a non-empty non-object file. 1744 if fi, err := os.Stat(dst); err == nil { 1745 if fi.IsDir() { 1746 return fmt.Errorf("build output %q already exists and is a directory", dst) 1747 } 1748 if !force && fi.Mode().IsRegular() && fi.Size() != 0 && !isObject(dst) { 1749 return fmt.Errorf("build output %q already exists and is not an object file", dst) 1750 } 1751 } 1752 1753 // On Windows, remove lingering ~ file from last attempt. 1754 if base.ToolIsWindows { 1755 if _, err := os.Stat(dst + "~"); err == nil { 1756 os.Remove(dst + "~") 1757 } 1758 } 1759 1760 mayberemovefile(dst) 1761 df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 1762 if err != nil && base.ToolIsWindows { 1763 // Windows does not allow deletion of a binary file 1764 // while it is executing. Try to move it out of the way. 1765 // If the move fails, which is likely, we'll try again the 1766 // next time we do an install of this binary. 1767 if err := os.Rename(dst, dst+"~"); err == nil { 1768 os.Remove(dst + "~") 1769 } 1770 df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 1771 } 1772 if err != nil { 1773 return fmt.Errorf("copying %s: %w", src, err) // err should already refer to dst 1774 } 1775 1776 _, err = io.Copy(df, sf) 1777 df.Close() 1778 if err != nil { 1779 mayberemovefile(dst) 1780 return fmt.Errorf("copying %s to %s: %v", src, dst, err) 1781 } 1782 return nil 1783} 1784 1785// writeFile writes the text to file. 1786func (b *Builder) writeFile(file string, text []byte) error { 1787 if cfg.BuildN || cfg.BuildX { 1788 b.Showcmd("", "cat >%s << 'EOF' # internal\n%sEOF", file, text) 1789 } 1790 if cfg.BuildN { 1791 return nil 1792 } 1793 return os.WriteFile(file, text, 0666) 1794} 1795 1796// Install the cgo export header file, if there is one. 1797func (b *Builder) installHeader(ctx context.Context, a *Action) error { 1798 src := a.Objdir + "_cgo_install.h" 1799 if _, err := os.Stat(src); os.IsNotExist(err) { 1800 // If the file does not exist, there are no exported 1801 // functions, and we do not install anything. 1802 // TODO(rsc): Once we know that caching is rebuilding 1803 // at the right times (not missing rebuilds), here we should 1804 // probably delete the installed header, if any. 1805 if cfg.BuildX { 1806 b.Showcmd("", "# %s not created", src) 1807 } 1808 return nil 1809 } 1810 1811 if err := allowInstall(a); err != nil { 1812 return err 1813 } 1814 1815 dir, _ := filepath.Split(a.Target) 1816 if dir != "" { 1817 if err := b.Mkdir(dir); err != nil { 1818 return err 1819 } 1820 } 1821 1822 return b.moveOrCopyFile(a.Target, src, 0666, true) 1823} 1824 1825// cover runs, in effect, 1826// go tool cover -mode=b.coverMode -var="varName" -o dst.go src.go 1827func (b *Builder) cover(a *Action, dst, src string, varName string) error { 1828 return b.run(a, a.Objdir, "cover "+a.Package.ImportPath, nil, 1829 cfg.BuildToolexec, 1830 base.Tool("cover"), 1831 "-mode", a.Package.Internal.CoverMode, 1832 "-var", varName, 1833 "-o", dst, 1834 src) 1835} 1836 1837var objectMagic = [][]byte{ 1838 {'!', '<', 'a', 'r', 'c', 'h', '>', '\n'}, // Package archive 1839 {'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'}, // Package AIX big archive 1840 {'\x7F', 'E', 'L', 'F'}, // ELF 1841 {0xFE, 0xED, 0xFA, 0xCE}, // Mach-O big-endian 32-bit 1842 {0xFE, 0xED, 0xFA, 0xCF}, // Mach-O big-endian 64-bit 1843 {0xCE, 0xFA, 0xED, 0xFE}, // Mach-O little-endian 32-bit 1844 {0xCF, 0xFA, 0xED, 0xFE}, // Mach-O little-endian 64-bit 1845 {0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00}, // PE (Windows) as generated by 6l/8l and gcc 1846 {0x00, 0x00, 0x01, 0xEB}, // Plan 9 i386 1847 {0x00, 0x00, 0x8a, 0x97}, // Plan 9 amd64 1848 {0x00, 0x00, 0x06, 0x47}, // Plan 9 arm 1849 {0x00, 0x61, 0x73, 0x6D}, // WASM 1850 {0x01, 0xDF}, // XCOFF 32bit 1851 {0x01, 0xF7}, // XCOFF 64bit 1852} 1853 1854func isObject(s string) bool { 1855 f, err := os.Open(s) 1856 if err != nil { 1857 return false 1858 } 1859 defer f.Close() 1860 buf := make([]byte, 64) 1861 io.ReadFull(f, buf) 1862 for _, magic := range objectMagic { 1863 if bytes.HasPrefix(buf, magic) { 1864 return true 1865 } 1866 } 1867 return false 1868} 1869 1870// mayberemovefile removes a file only if it is a regular file 1871// When running as a user with sufficient privileges, we may delete 1872// even device files, for example, which is not intended. 1873func mayberemovefile(s string) { 1874 if fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() { 1875 return 1876 } 1877 os.Remove(s) 1878} 1879 1880// fmtcmd formats a command in the manner of fmt.Sprintf but also: 1881// 1882// If dir is non-empty and the script is not in dir right now, 1883// fmtcmd inserts "cd dir\n" before the command. 1884// 1885// fmtcmd replaces the value of b.WorkDir with $WORK. 1886// fmtcmd replaces the value of goroot with $GOROOT. 1887// fmtcmd replaces the value of b.gobin with $GOBIN. 1888// 1889// fmtcmd replaces the name of the current directory with dot (.) 1890// but only when it is at the beginning of a space-separated token. 1891// 1892func (b *Builder) fmtcmd(dir string, format string, args ...interface{}) string { 1893 cmd := fmt.Sprintf(format, args...) 1894 if dir != "" && dir != "/" { 1895 dot := " ." 1896 if dir[len(dir)-1] == filepath.Separator { 1897 dot += string(filepath.Separator) 1898 } 1899 cmd = strings.ReplaceAll(" "+cmd, " "+dir, dot)[1:] 1900 if b.scriptDir != dir { 1901 b.scriptDir = dir 1902 cmd = "cd " + dir + "\n" + cmd 1903 } 1904 } 1905 if b.WorkDir != "" { 1906 cmd = strings.ReplaceAll(cmd, b.WorkDir, "$WORK") 1907 escaped := strconv.Quote(b.WorkDir) 1908 escaped = escaped[1 : len(escaped)-1] // strip quote characters 1909 if escaped != b.WorkDir { 1910 cmd = strings.ReplaceAll(cmd, escaped, "$WORK") 1911 } 1912 } 1913 return cmd 1914} 1915 1916// showcmd prints the given command to standard output 1917// for the implementation of -n or -x. 1918func (b *Builder) Showcmd(dir string, format string, args ...interface{}) { 1919 b.output.Lock() 1920 defer b.output.Unlock() 1921 b.Print(b.fmtcmd(dir, format, args...) + "\n") 1922} 1923 1924// showOutput prints "# desc" followed by the given output. 1925// The output is expected to contain references to 'dir', usually 1926// the source directory for the package that has failed to build. 1927// showOutput rewrites mentions of dir with a relative path to dir 1928// when the relative path is shorter. This is usually more pleasant. 1929// For example, if fmt doesn't compile and we are in src/html, 1930// the output is 1931// 1932// $ go build 1933// # fmt 1934// ../fmt/print.go:1090: undefined: asdf 1935// $ 1936// 1937// instead of 1938// 1939// $ go build 1940// # fmt 1941// /usr/gopher/go/src/fmt/print.go:1090: undefined: asdf 1942// $ 1943// 1944// showOutput also replaces references to the work directory with $WORK. 1945// 1946// If a is not nil and a.output is not nil, showOutput appends to that slice instead of 1947// printing to b.Print. 1948// 1949func (b *Builder) showOutput(a *Action, dir, desc, out string) { 1950 prefix := "# " + desc 1951 suffix := "\n" + out 1952 if reldir := base.ShortPath(dir); reldir != dir { 1953 suffix = strings.ReplaceAll(suffix, " "+dir, " "+reldir) 1954 suffix = strings.ReplaceAll(suffix, "\n"+dir, "\n"+reldir) 1955 } 1956 suffix = strings.ReplaceAll(suffix, " "+b.WorkDir, " $WORK") 1957 1958 if a != nil && a.output != nil { 1959 a.output = append(a.output, prefix...) 1960 a.output = append(a.output, suffix...) 1961 return 1962 } 1963 1964 b.output.Lock() 1965 defer b.output.Unlock() 1966 b.Print(prefix, suffix) 1967} 1968 1969// errPrintedOutput is a special error indicating that a command failed 1970// but that it generated output as well, and that output has already 1971// been printed, so there's no point showing 'exit status 1' or whatever 1972// the wait status was. The main executor, builder.do, knows not to 1973// print this error. 1974var errPrintedOutput = errors.New("already printed output - no need to show error") 1975 1976var cgoLine = lazyregexp.New(`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]`) 1977var cgoTypeSigRe = lazyregexp.New(`\b_C2?(type|func|var|macro)_\B`) 1978 1979// run runs the command given by cmdline in the directory dir. 1980// If the command fails, run prints information about the failure 1981// and returns a non-nil error. 1982func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs ...interface{}) error { 1983 out, err := b.runOut(a, dir, env, cmdargs...) 1984 if len(out) > 0 { 1985 if desc == "" { 1986 desc = b.fmtcmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " ")) 1987 } 1988 b.showOutput(a, dir, desc, b.processOutput(out)) 1989 if err != nil { 1990 err = errPrintedOutput 1991 } 1992 } 1993 return err 1994} 1995 1996// processOutput prepares the output of runOut to be output to the console. 1997func (b *Builder) processOutput(out []byte) string { 1998 if out[len(out)-1] != '\n' { 1999 out = append(out, '\n') 2000 } 2001 messages := string(out) 2002 // Fix up output referring to cgo-generated code to be more readable. 2003 // Replace x.go:19[/tmp/.../x.cgo1.go:18] with x.go:19. 2004 // Replace *[100]_Ctype_foo with *[100]C.foo. 2005 // If we're using -x, assume we're debugging and want the full dump, so disable the rewrite. 2006 if !cfg.BuildX && cgoLine.MatchString(messages) { 2007 messages = cgoLine.ReplaceAllString(messages, "") 2008 messages = cgoTypeSigRe.ReplaceAllString(messages, "C.") 2009 } 2010 return messages 2011} 2012 2013// runOut runs the command given by cmdline in the directory dir. 2014// It returns the command output and any errors that occurred. 2015// It accumulates execution time in a. 2016func (b *Builder) runOut(a *Action, dir string, env []string, cmdargs ...interface{}) ([]byte, error) { 2017 cmdline := str.StringList(cmdargs...) 2018 2019 for _, arg := range cmdline { 2020 // GNU binutils commands, including gcc and gccgo, interpret an argument 2021 // @foo anywhere in the command line (even following --) as meaning 2022 // "read and insert arguments from the file named foo." 2023 // Don't say anything that might be misinterpreted that way. 2024 if strings.HasPrefix(arg, "@") { 2025 return nil, fmt.Errorf("invalid command-line argument %s in command: %s", arg, joinUnambiguously(cmdline)) 2026 } 2027 } 2028 2029 if cfg.BuildN || cfg.BuildX { 2030 var envcmdline string 2031 for _, e := range env { 2032 if j := strings.IndexByte(e, '='); j != -1 { 2033 if strings.ContainsRune(e[j+1:], '\'') { 2034 envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:]) 2035 } else { 2036 envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:]) 2037 } 2038 envcmdline += " " 2039 } 2040 } 2041 envcmdline += joinUnambiguously(cmdline) 2042 b.Showcmd(dir, "%s", envcmdline) 2043 if cfg.BuildN { 2044 return nil, nil 2045 } 2046 } 2047 2048 var buf bytes.Buffer 2049 cmd := exec.Command(cmdline[0], cmdline[1:]...) 2050 if cmd.Path != "" { 2051 cmd.Args[0] = cmd.Path 2052 } 2053 cmd.Stdout = &buf 2054 cmd.Stderr = &buf 2055 cleanup := passLongArgsInResponseFiles(cmd) 2056 defer cleanup() 2057 cmd.Dir = dir 2058 cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir) 2059 2060 // Add the TOOLEXEC_IMPORTPATH environment variable for -toolexec tools. 2061 // It doesn't really matter if -toolexec isn't being used. 2062 if a != nil && a.Package != nil { 2063 cmd.Env = append(cmd.Env, "TOOLEXEC_IMPORTPATH="+a.Package.ImportPath) 2064 } 2065 2066 cmd.Env = append(cmd.Env, env...) 2067 start := time.Now() 2068 err := cmd.Run() 2069 if a != nil && a.json != nil { 2070 aj := a.json 2071 aj.Cmd = append(aj.Cmd, joinUnambiguously(cmdline)) 2072 aj.CmdReal += time.Since(start) 2073 if ps := cmd.ProcessState; ps != nil { 2074 aj.CmdUser += ps.UserTime() 2075 aj.CmdSys += ps.SystemTime() 2076 } 2077 } 2078 2079 // err can be something like 'exit status 1'. 2080 // Add information about what program was running. 2081 // Note that if buf.Bytes() is non-empty, the caller usually 2082 // shows buf.Bytes() and does not print err at all, so the 2083 // prefix here does not make most output any more verbose. 2084 if err != nil { 2085 err = errors.New(cmdline[0] + ": " + err.Error()) 2086 } 2087 return buf.Bytes(), err 2088} 2089 2090// joinUnambiguously prints the slice, quoting where necessary to make the 2091// output unambiguous. 2092// TODO: See issue 5279. The printing of commands needs a complete redo. 2093func joinUnambiguously(a []string) string { 2094 var buf bytes.Buffer 2095 for i, s := range a { 2096 if i > 0 { 2097 buf.WriteByte(' ') 2098 } 2099 q := strconv.Quote(s) 2100 // A gccgo command line can contain -( and -). 2101 // Make sure we quote them since they are special to the shell. 2102 // The trimpath argument can also contain > (part of =>) and ;. Quote those too. 2103 if s == "" || strings.ContainsAny(s, " ()>;") || len(q) > len(s)+2 { 2104 buf.WriteString(q) 2105 } else { 2106 buf.WriteString(s) 2107 } 2108 } 2109 return buf.String() 2110} 2111 2112// cCompilerEnv returns environment variables to set when running the 2113// C compiler. This is needed to disable escape codes in clang error 2114// messages that confuse tools like cgo. 2115func (b *Builder) cCompilerEnv() []string { 2116 return []string{"TERM=dumb"} 2117} 2118 2119// mkdir makes the named directory. 2120func (b *Builder) Mkdir(dir string) error { 2121 // Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "". 2122 if dir == "" { 2123 return nil 2124 } 2125 2126 b.exec.Lock() 2127 defer b.exec.Unlock() 2128 // We can be a little aggressive about being 2129 // sure directories exist. Skip repeated calls. 2130 if b.mkdirCache[dir] { 2131 return nil 2132 } 2133 b.mkdirCache[dir] = true 2134 2135 if cfg.BuildN || cfg.BuildX { 2136 b.Showcmd("", "mkdir -p %s", dir) 2137 if cfg.BuildN { 2138 return nil 2139 } 2140 } 2141 2142 if err := os.MkdirAll(dir, 0777); err != nil { 2143 return err 2144 } 2145 return nil 2146} 2147 2148// symlink creates a symlink newname -> oldname. 2149func (b *Builder) Symlink(oldname, newname string) error { 2150 // It's not an error to try to recreate an existing symlink. 2151 if link, err := os.Readlink(newname); err == nil && link == oldname { 2152 return nil 2153 } 2154 2155 if cfg.BuildN || cfg.BuildX { 2156 b.Showcmd("", "ln -s %s %s", oldname, newname) 2157 if cfg.BuildN { 2158 return nil 2159 } 2160 } 2161 return os.Symlink(oldname, newname) 2162} 2163 2164// mkAbs returns an absolute path corresponding to 2165// evaluating f in the directory dir. 2166// We always pass absolute paths of source files so that 2167// the error messages will include the full path to a file 2168// in need of attention. 2169func mkAbs(dir, f string) string { 2170 // Leave absolute paths alone. 2171 // Also, during -n mode we use the pseudo-directory $WORK 2172 // instead of creating an actual work directory that won't be used. 2173 // Leave paths beginning with $WORK alone too. 2174 if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") { 2175 return f 2176 } 2177 return filepath.Join(dir, f) 2178} 2179 2180type toolchain interface { 2181 // gc runs the compiler in a specific directory on a set of files 2182 // and returns the name of the generated output file. 2183 gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, out []byte, err error) 2184 // cc runs the toolchain's C compiler in a directory on a C file 2185 // to produce an output file. 2186 cc(b *Builder, a *Action, ofile, cfile string) error 2187 // asm runs the assembler in a specific directory on specific files 2188 // and returns a list of named output files. 2189 asm(b *Builder, a *Action, sfiles []string) ([]string, error) 2190 // symabis scans the symbol ABIs from sfiles and returns the 2191 // path to the output symbol ABIs file, or "" if none. 2192 symabis(b *Builder, a *Action, sfiles []string) (string, error) 2193 // pack runs the archive packer in a specific directory to create 2194 // an archive from a set of object files. 2195 // typically it is run in the object directory. 2196 pack(b *Builder, a *Action, afile string, ofiles []string) error 2197 // ld runs the linker to create an executable starting at mainpkg. 2198 ld(b *Builder, root *Action, out, importcfg, mainpkg string) error 2199 // ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions 2200 ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error 2201 2202 compiler() string 2203 linker() string 2204} 2205 2206type noToolchain struct{} 2207 2208func noCompiler() error { 2209 log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler) 2210 return nil 2211} 2212 2213func (noToolchain) compiler() string { 2214 noCompiler() 2215 return "" 2216} 2217 2218func (noToolchain) linker() string { 2219 noCompiler() 2220 return "" 2221} 2222 2223func (noToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, out []byte, err error) { 2224 return "", nil, noCompiler() 2225} 2226 2227func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) { 2228 return nil, noCompiler() 2229} 2230 2231func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) { 2232 return "", noCompiler() 2233} 2234 2235func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error { 2236 return noCompiler() 2237} 2238 2239func (noToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string) error { 2240 return noCompiler() 2241} 2242 2243func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error { 2244 return noCompiler() 2245} 2246 2247func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error { 2248 return noCompiler() 2249} 2250 2251// gcc runs the gcc C compiler to create an object from a single C file. 2252func (b *Builder) gcc(a *Action, p *load.Package, workdir, out string, flags []string, cfile string) error { 2253 return b.ccompile(a, p, out, flags, cfile, b.GccCmd(p.Dir, workdir)) 2254} 2255 2256// gxx runs the g++ C++ compiler to create an object from a single C++ file. 2257func (b *Builder) gxx(a *Action, p *load.Package, workdir, out string, flags []string, cxxfile string) error { 2258 return b.ccompile(a, p, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir)) 2259} 2260 2261// gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file. 2262func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flags []string, ffile string) error { 2263 return b.ccompile(a, p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir)) 2264} 2265 2266// ccompile runs the given C or C++ compiler and creates an object from a single source file. 2267func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []string, file string, compiler []string) error { 2268 file = mkAbs(p.Dir, file) 2269 desc := p.ImportPath 2270 outfile = mkAbs(p.Dir, outfile) 2271 2272 // Elide source directory paths if -trimpath or GOROOT_FINAL is set. 2273 // This is needed for source files (e.g., a .c file in a package directory). 2274 // TODO(golang.org/issue/36072): cgo also generates files with #line 2275 // directives pointing to the source directory. It should not generate those 2276 // when -trimpath is enabled. 2277 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { 2278 if cfg.BuildTrimpath { 2279 // Keep in sync with Action.trimpath. 2280 // The trimmed paths are a little different, but we need to trim in the 2281 // same situations. 2282 var from, toPath string 2283 if m := p.Module; m != nil { 2284 from = m.Dir 2285 toPath = m.Path + "@" + m.Version 2286 } else { 2287 from = p.Dir 2288 toPath = p.ImportPath 2289 } 2290 // -fdebug-prefix-map requires an absolute "to" path (or it joins the path 2291 // with the working directory). Pick something that makes sense for the 2292 // target platform. 2293 var to string 2294 if cfg.BuildContext.GOOS == "windows" { 2295 to = filepath.Join(`\\_\_`, toPath) 2296 } else { 2297 to = filepath.Join("/_", toPath) 2298 } 2299 flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+from+"="+to) 2300 } else if p.Goroot && cfg.GOROOT_FINAL != cfg.GOROOT { 2301 flags = append(flags[:len(flags):len(flags)], "-fdebug-prefix-map="+cfg.GOROOT+"="+cfg.GOROOT_FINAL) 2302 } 2303 } 2304 2305 overlayPath := file 2306 if p, ok := a.nonGoOverlay[overlayPath]; ok { 2307 overlayPath = p 2308 } 2309 output, err := b.runOut(a, filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath)) 2310 if len(output) > 0 { 2311 // On FreeBSD 11, when we pass -g to clang 3.8 it 2312 // invokes its internal assembler with -dwarf-version=2. 2313 // When it sees .section .note.GNU-stack, it warns 2314 // "DWARF2 only supports one section per compilation unit". 2315 // This warning makes no sense, since the section is empty, 2316 // but it confuses people. 2317 // We work around the problem by detecting the warning 2318 // and dropping -g and trying again. 2319 if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) { 2320 newFlags := make([]string, 0, len(flags)) 2321 for _, f := range flags { 2322 if !strings.HasPrefix(f, "-g") { 2323 newFlags = append(newFlags, f) 2324 } 2325 } 2326 if len(newFlags) < len(flags) { 2327 return b.ccompile(a, p, outfile, newFlags, file, compiler) 2328 } 2329 } 2330 2331 b.showOutput(a, p.Dir, desc, b.processOutput(output)) 2332 if err != nil { 2333 err = errPrintedOutput 2334 } else if os.Getenv("GO_BUILDER_NAME") != "" { 2335 return errors.New("C compiler warning promoted to error on Go builders") 2336 } 2337 } 2338 return err 2339} 2340 2341// gccld runs the gcc linker to create an executable from a set of object files. 2342func (b *Builder) gccld(a *Action, p *load.Package, objdir, outfile string, flags []string, objs []string) error { 2343 var cmd []string 2344 if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 { 2345 cmd = b.GxxCmd(p.Dir, objdir) 2346 } else { 2347 cmd = b.GccCmd(p.Dir, objdir) 2348 } 2349 2350 cmdargs := []interface{}{cmd, "-o", outfile, objs, flags} 2351 dir := p.Dir 2352 out, err := b.runOut(a, base.Cwd, b.cCompilerEnv(), cmdargs...) 2353 2354 if len(out) > 0 { 2355 // Filter out useless linker warnings caused by bugs outside Go. 2356 // See also cmd/link/internal/ld's hostlink method. 2357 var save [][]byte 2358 var skipLines int 2359 for _, line := range bytes.SplitAfter(out, []byte("\n")) { 2360 // golang.org/issue/26073 - Apple Xcode bug 2361 if bytes.Contains(line, []byte("ld: warning: text-based stub file")) { 2362 continue 2363 } 2364 2365 if skipLines > 0 { 2366 skipLines-- 2367 continue 2368 } 2369 2370 // Remove duplicate main symbol with runtime/cgo on AIX. 2371 // With runtime/cgo, two main are available: 2372 // One is generated by cgo tool with {return 0;}. 2373 // The other one is the main calling runtime.rt0_go 2374 // in runtime/cgo. 2375 // The second can't be used by cgo programs because 2376 // runtime.rt0_go is unknown to them. 2377 // Therefore, we let ld remove this main version 2378 // and used the cgo generated one. 2379 if p.ImportPath == "runtime/cgo" && bytes.Contains(line, []byte("ld: 0711-224 WARNING: Duplicate symbol: .main")) { 2380 skipLines = 1 2381 continue 2382 } 2383 2384 save = append(save, line) 2385 } 2386 out = bytes.Join(save, nil) 2387 if len(out) > 0 { 2388 b.showOutput(nil, dir, p.ImportPath, b.processOutput(out)) 2389 if err != nil { 2390 err = errPrintedOutput 2391 } 2392 } 2393 } 2394 return err 2395} 2396 2397// Grab these before main helpfully overwrites them. 2398var ( 2399 origCC = cfg.Getenv("CC") 2400 origCXX = cfg.Getenv("CXX") 2401) 2402 2403// gccCmd returns a gcc command line prefix 2404// defaultCC is defined in zdefaultcc.go, written by cmd/dist. 2405func (b *Builder) GccCmd(incdir, workdir string) []string { 2406 return b.compilerCmd(b.ccExe(), incdir, workdir) 2407} 2408 2409// gxxCmd returns a g++ command line prefix 2410// defaultCXX is defined in zdefaultcc.go, written by cmd/dist. 2411func (b *Builder) GxxCmd(incdir, workdir string) []string { 2412 return b.compilerCmd(b.cxxExe(), incdir, workdir) 2413} 2414 2415// gfortranCmd returns a gfortran command line prefix. 2416func (b *Builder) gfortranCmd(incdir, workdir string) []string { 2417 return b.compilerCmd(b.fcExe(), incdir, workdir) 2418} 2419 2420// ccExe returns the CC compiler setting without all the extra flags we add implicitly. 2421func (b *Builder) ccExe() []string { 2422 return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch)) 2423} 2424 2425// cxxExe returns the CXX compiler setting without all the extra flags we add implicitly. 2426func (b *Builder) cxxExe() []string { 2427 return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch)) 2428} 2429 2430// fcExe returns the FC compiler setting without all the extra flags we add implicitly. 2431func (b *Builder) fcExe() []string { 2432 return b.compilerExe(cfg.Getenv("FC"), "gfortran") 2433} 2434 2435// compilerExe returns the compiler to use given an 2436// environment variable setting (the value not the name) 2437// and a default. The resulting slice is usually just the name 2438// of the compiler but can have additional arguments if they 2439// were present in the environment value. 2440// For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"]. 2441func (b *Builder) compilerExe(envValue string, def string) []string { 2442 compiler := strings.Fields(envValue) 2443 if len(compiler) == 0 { 2444 compiler = strings.Fields(def) 2445 } 2446 return compiler 2447} 2448 2449// compilerCmd returns a command line prefix for the given environment 2450// variable and using the default command when the variable is empty. 2451func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string { 2452 // NOTE: env.go's mkEnv knows that the first three 2453 // strings returned are "gcc", "-I", incdir (and cuts them off). 2454 a := []string{compiler[0], "-I", incdir} 2455 a = append(a, compiler[1:]...) 2456 2457 // Definitely want -fPIC but on Windows gcc complains 2458 // "-fPIC ignored for target (all code is position independent)" 2459 if cfg.Goos != "windows" { 2460 a = append(a, "-fPIC") 2461 } 2462 a = append(a, b.gccArchArgs()...) 2463 // gcc-4.5 and beyond require explicit "-pthread" flag 2464 // for multithreading with pthread library. 2465 if cfg.BuildContext.CgoEnabled { 2466 switch cfg.Goos { 2467 case "windows": 2468 a = append(a, "-mthreads") 2469 default: 2470 a = append(a, "-pthread") 2471 } 2472 } 2473 2474 if cfg.Goos == "aix" { 2475 // mcmodel=large must always be enabled to allow large TOC. 2476 a = append(a, "-mcmodel=large") 2477 } 2478 2479 // disable ASCII art in clang errors, if possible 2480 if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") { 2481 a = append(a, "-fno-caret-diagnostics") 2482 } 2483 // clang is too smart about command-line arguments 2484 if b.gccSupportsFlag(compiler, "-Qunused-arguments") { 2485 a = append(a, "-Qunused-arguments") 2486 } 2487 2488 // disable word wrapping in error messages 2489 a = append(a, "-fmessage-length=0") 2490 2491 // Tell gcc not to include the work directory in object files. 2492 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { 2493 if workdir == "" { 2494 workdir = b.WorkDir 2495 } 2496 workdir = strings.TrimSuffix(workdir, string(filepath.Separator)) 2497 a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build") 2498 } 2499 2500 // Tell gcc not to include flags in object files, which defeats the 2501 // point of -fdebug-prefix-map above. 2502 if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") { 2503 a = append(a, "-gno-record-gcc-switches") 2504 } 2505 2506 // On OS X, some of the compilers behave as if -fno-common 2507 // is always set, and the Mach-O linker in 6l/8l assumes this. 2508 // See https://golang.org/issue/3253. 2509 if cfg.Goos == "darwin" || cfg.Goos == "ios" { 2510 a = append(a, "-fno-common") 2511 } 2512 2513 // gccgo uses the language-independent exception mechanism to 2514 // handle panics, so it always needs unwind tables. 2515 if cfg.BuildToolchainName == "gccgo" { 2516 a = append(a, "-funwind-tables") 2517 } 2518 2519 return a 2520} 2521 2522// gccNoPie returns the flag to use to request non-PIE. On systems 2523// with PIE (position independent executables) enabled by default, 2524// -no-pie must be passed when doing a partial link with -Wl,-r. 2525// But -no-pie is not supported by all compilers, and clang spells it -nopie. 2526func (b *Builder) gccNoPie(linker []string) string { 2527 if b.gccSupportsFlag(linker, "-no-pie") { 2528 return "-no-pie" 2529 } 2530 if b.gccSupportsFlag(linker, "-nopie") { 2531 return "-nopie" 2532 } 2533 return "" 2534} 2535 2536// gccSupportsFlag checks to see if the compiler supports a flag. 2537func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool { 2538 key := [2]string{compiler[0], flag} 2539 2540 b.exec.Lock() 2541 defer b.exec.Unlock() 2542 if b, ok := b.flagCache[key]; ok { 2543 return b 2544 } 2545 if b.flagCache == nil { 2546 b.flagCache = make(map[[2]string]bool) 2547 } 2548 2549 tmp := os.DevNull 2550 if runtime.GOOS == "windows" { 2551 f, err := os.CreateTemp(b.WorkDir, "") 2552 if err != nil { 2553 return false 2554 } 2555 f.Close() 2556 tmp = f.Name() 2557 defer os.Remove(tmp) 2558 } 2559 2560 // We used to write an empty C file, but that gets complicated with 2561 // go build -n. We tried using a file that does not exist, but that 2562 // fails on systems with GCC version 4.2.1; that is the last GPLv2 2563 // version of GCC, so some systems have frozen on it. 2564 // Now we pass an empty file on stdin, which should work at least for 2565 // GCC and clang. 2566 cmdArgs := str.StringList(compiler, flag, "-c", "-x", "c", "-", "-o", tmp) 2567 if cfg.BuildN || cfg.BuildX { 2568 b.Showcmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs)) 2569 if cfg.BuildN { 2570 return false 2571 } 2572 } 2573 cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) 2574 cmd.Dir = b.WorkDir 2575 cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir) 2576 cmd.Env = append(cmd.Env, "LC_ALL=C") 2577 out, _ := cmd.CombinedOutput() 2578 // GCC says "unrecognized command line option". 2579 // clang says "unknown argument". 2580 // Older versions of GCC say "unrecognised debug output level". 2581 // For -fsplit-stack GCC says "'-fsplit-stack' is not supported". 2582 supported := !bytes.Contains(out, []byte("unrecognized")) && 2583 !bytes.Contains(out, []byte("unknown")) && 2584 !bytes.Contains(out, []byte("unrecognised")) && 2585 !bytes.Contains(out, []byte("is not supported")) 2586 b.flagCache[key] = supported 2587 return supported 2588} 2589 2590// gccArchArgs returns arguments to pass to gcc based on the architecture. 2591func (b *Builder) gccArchArgs() []string { 2592 switch cfg.Goarch { 2593 case "386": 2594 return []string{"-m32"} 2595 case "amd64": 2596 if cfg.Goos == "darwin" { 2597 return []string{"-arch", "x86_64", "-m64"} 2598 } 2599 return []string{"-m64"} 2600 case "arm64": 2601 if cfg.Goos == "darwin" { 2602 return []string{"-arch", "arm64"} 2603 } 2604 case "arm": 2605 return []string{"-marm"} // not thumb 2606 case "s390x": 2607 return []string{"-m64", "-march=z196"} 2608 case "mips64", "mips64le": 2609 return []string{"-mabi=64"} 2610 case "mips", "mipsle": 2611 return []string{"-mabi=32", "-march=mips32"} 2612 case "ppc64": 2613 if cfg.Goos == "aix" { 2614 return []string{"-maix64"} 2615 } 2616 case "ppc": 2617 if cfg.Goos == "aix" { 2618 return []string{"-maix32"} 2619 } 2620 } 2621 return nil 2622} 2623 2624// envList returns the value of the given environment variable broken 2625// into fields, using the default value when the variable is empty. 2626func envList(key, def string) []string { 2627 v := cfg.Getenv(key) 2628 if v == "" { 2629 v = def 2630 } 2631 return strings.Fields(v) 2632} 2633 2634// CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo. 2635func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) { 2636 defaults := "-g -O2" 2637 2638 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil { 2639 return 2640 } 2641 if cflags, err = buildFlags("CFLAGS", defaults, p.CgoCFLAGS, checkCompilerFlags); err != nil { 2642 return 2643 } 2644 if cxxflags, err = buildFlags("CXXFLAGS", defaults, p.CgoCXXFLAGS, checkCompilerFlags); err != nil { 2645 return 2646 } 2647 if fflags, err = buildFlags("FFLAGS", defaults, p.CgoFFLAGS, checkCompilerFlags); err != nil { 2648 return 2649 } 2650 if ldflags, err = buildFlags("LDFLAGS", defaults, p.CgoLDFLAGS, checkLinkerFlags); err != nil { 2651 return 2652 } 2653 2654 return 2655} 2656 2657func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) { 2658 if err := check(name, "#cgo "+name, fromPackage); err != nil { 2659 return nil, err 2660 } 2661 return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil 2662} 2663 2664var cgoRe = lazyregexp.New(`[/\\:]`) 2665 2666func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) { 2667 p := a.Package 2668 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p) 2669 if err != nil { 2670 return nil, nil, err 2671 } 2672 2673 cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) 2674 cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...) 2675 // If we are compiling Objective-C code, then we need to link against libobjc 2676 if len(mfiles) > 0 { 2677 cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc") 2678 } 2679 2680 // Likewise for Fortran, except there are many Fortran compilers. 2681 // Support gfortran out of the box and let others pass the correct link options 2682 // via CGO_LDFLAGS 2683 if len(ffiles) > 0 { 2684 fc := cfg.Getenv("FC") 2685 if fc == "" { 2686 fc = "gfortran" 2687 } 2688 if strings.Contains(fc, "gfortran") { 2689 cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran") 2690 } 2691 } 2692 2693 if cfg.BuildMSan { 2694 cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...) 2695 cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...) 2696 } 2697 2698 // Allows including _cgo_export.h, as well as the user's .h files, 2699 // from .[ch] files in the package. 2700 cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir) 2701 2702 // cgo 2703 // TODO: CGO_FLAGS? 2704 gofiles := []string{objdir + "_cgo_gotypes.go"} 2705 cfiles := []string{"_cgo_export.c"} 2706 for _, fn := range cgofiles { 2707 f := strings.TrimSuffix(filepath.Base(fn), ".go") 2708 gofiles = append(gofiles, objdir+f+".cgo1.go") 2709 cfiles = append(cfiles, f+".cgo2.c") 2710 } 2711 2712 // TODO: make cgo not depend on $GOARCH? 2713 2714 cgoflags := []string{} 2715 if p.Standard && p.ImportPath == "runtime/cgo" { 2716 cgoflags = append(cgoflags, "-import_runtime_cgo=false") 2717 } 2718 if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo") { 2719 cgoflags = append(cgoflags, "-import_syscall=false") 2720 } 2721 2722 // Update $CGO_LDFLAGS with p.CgoLDFLAGS. 2723 // These flags are recorded in the generated _cgo_gotypes.go file 2724 // using //go:cgo_ldflag directives, the compiler records them in the 2725 // object file for the package, and then the Go linker passes them 2726 // along to the host linker. At this point in the code, cgoLDFLAGS 2727 // consists of the original $CGO_LDFLAGS (unchecked) and all the 2728 // flags put together from source code (checked). 2729 cgoenv := b.cCompilerEnv() 2730 if len(cgoLDFLAGS) > 0 { 2731 flags := make([]string, len(cgoLDFLAGS)) 2732 for i, f := range cgoLDFLAGS { 2733 flags[i] = strconv.Quote(f) 2734 } 2735 cgoenv = append(cgoenv, "CGO_LDFLAGS="+strings.Join(flags, " ")) 2736 } 2737 2738 if cfg.BuildToolchainName == "gccgo" { 2739 if b.gccSupportsFlag([]string{BuildToolchain.compiler()}, "-fsplit-stack") { 2740 cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack") 2741 } 2742 cgoflags = append(cgoflags, "-gccgo") 2743 if pkgpath := gccgoPkgpath(p); pkgpath != "" { 2744 cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath) 2745 } 2746 } 2747 2748 switch cfg.BuildBuildmode { 2749 case "c-archive", "c-shared": 2750 // Tell cgo that if there are any exported functions 2751 // it should generate a header file that C code can 2752 // #include. 2753 cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h") 2754 } 2755 2756 execdir := p.Dir 2757 2758 // Rewrite overlaid paths in cgo files. 2759 // cgo adds //line and #line pragmas in generated files with these paths. 2760 var trimpath []string 2761 for i := range cgofiles { 2762 path := mkAbs(p.Dir, cgofiles[i]) 2763 if opath, ok := fsys.OverlayPath(path); ok { 2764 cgofiles[i] = opath 2765 trimpath = append(trimpath, opath+"=>"+path) 2766 } 2767 } 2768 if len(trimpath) > 0 { 2769 cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";")) 2770 } 2771 2772 if err := b.run(a, execdir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil { 2773 return nil, nil, err 2774 } 2775 outGo = append(outGo, gofiles...) 2776 2777 // Use sequential object file names to keep them distinct 2778 // and short enough to fit in the .a header file name slots. 2779 // We no longer collect them all into _all.o, and we'd like 2780 // tools to see both the .o suffix and unique names, so 2781 // we need to make them short enough not to be truncated 2782 // in the final archive. 2783 oseq := 0 2784 nextOfile := func() string { 2785 oseq++ 2786 return objdir + fmt.Sprintf("_x%03d.o", oseq) 2787 } 2788 2789 // gcc 2790 cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS) 2791 for _, cfile := range cfiles { 2792 ofile := nextOfile() 2793 if err := b.gcc(a, p, a.Objdir, ofile, cflags, objdir+cfile); err != nil { 2794 return nil, nil, err 2795 } 2796 outObj = append(outObj, ofile) 2797 } 2798 2799 for _, file := range gccfiles { 2800 ofile := nextOfile() 2801 if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil { 2802 return nil, nil, err 2803 } 2804 outObj = append(outObj, ofile) 2805 } 2806 2807 cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS) 2808 for _, file := range gxxfiles { 2809 ofile := nextOfile() 2810 if err := b.gxx(a, p, a.Objdir, ofile, cxxflags, file); err != nil { 2811 return nil, nil, err 2812 } 2813 outObj = append(outObj, ofile) 2814 } 2815 2816 for _, file := range mfiles { 2817 ofile := nextOfile() 2818 if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil { 2819 return nil, nil, err 2820 } 2821 outObj = append(outObj, ofile) 2822 } 2823 2824 fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS) 2825 for _, file := range ffiles { 2826 ofile := nextOfile() 2827 if err := b.gfortran(a, p, a.Objdir, ofile, fflags, file); err != nil { 2828 return nil, nil, err 2829 } 2830 outObj = append(outObj, ofile) 2831 } 2832 2833 switch cfg.BuildToolchainName { 2834 case "gc": 2835 importGo := objdir + "_cgo_import.go" 2836 if err := b.dynimport(a, p, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj); err != nil { 2837 return nil, nil, err 2838 } 2839 outGo = append(outGo, importGo) 2840 2841 case "gccgo": 2842 defunC := objdir + "_cgo_defun.c" 2843 defunObj := objdir + "_cgo_defun.o" 2844 if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil { 2845 return nil, nil, err 2846 } 2847 outObj = append(outObj, defunObj) 2848 2849 default: 2850 noCompiler() 2851 } 2852 2853 // Double check the //go:cgo_ldflag comments in the generated files. 2854 // The compiler only permits such comments in files whose base name 2855 // starts with "_cgo_". Make sure that the comments in those files 2856 // are safe. This is a backstop against people somehow smuggling 2857 // such a comment into a file generated by cgo. 2858 if cfg.BuildToolchainName == "gc" && !cfg.BuildN { 2859 var flags []string 2860 for _, f := range outGo { 2861 if !strings.HasPrefix(filepath.Base(f), "_cgo_") { 2862 continue 2863 } 2864 2865 src, err := os.ReadFile(f) 2866 if err != nil { 2867 return nil, nil, err 2868 } 2869 2870 const cgoLdflag = "//go:cgo_ldflag" 2871 idx := bytes.Index(src, []byte(cgoLdflag)) 2872 for idx >= 0 { 2873 // We are looking at //go:cgo_ldflag. 2874 // Find start of line. 2875 start := bytes.LastIndex(src[:idx], []byte("\n")) 2876 if start == -1 { 2877 start = 0 2878 } 2879 2880 // Find end of line. 2881 end := bytes.Index(src[idx:], []byte("\n")) 2882 if end == -1 { 2883 end = len(src) 2884 } else { 2885 end += idx 2886 } 2887 2888 // Check for first line comment in line. 2889 // We don't worry about /* */ comments, 2890 // which normally won't appear in files 2891 // generated by cgo. 2892 commentStart := bytes.Index(src[start:], []byte("//")) 2893 commentStart += start 2894 // If that line comment is //go:cgo_ldflag, 2895 // it's a match. 2896 if bytes.HasPrefix(src[commentStart:], []byte(cgoLdflag)) { 2897 // Pull out the flag, and unquote it. 2898 // This is what the compiler does. 2899 flag := string(src[idx+len(cgoLdflag) : end]) 2900 flag = strings.TrimSpace(flag) 2901 flag = strings.Trim(flag, `"`) 2902 flags = append(flags, flag) 2903 } 2904 src = src[end:] 2905 idx = bytes.Index(src, []byte(cgoLdflag)) 2906 } 2907 } 2908 2909 // We expect to find the contents of cgoLDFLAGS in flags. 2910 if len(cgoLDFLAGS) > 0 { 2911 outer: 2912 for i := range flags { 2913 for j, f := range cgoLDFLAGS { 2914 if f != flags[i+j] { 2915 continue outer 2916 } 2917 } 2918 flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...) 2919 break 2920 } 2921 } 2922 2923 if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil { 2924 return nil, nil, err 2925 } 2926 } 2927 2928 return outGo, outObj, nil 2929} 2930 2931// dynimport creates a Go source file named importGo containing 2932// //go:cgo_import_dynamic directives for each symbol or library 2933// dynamically imported by the object files outObj. 2934func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error { 2935 cfile := objdir + "_cgo_main.c" 2936 ofile := objdir + "_cgo_main.o" 2937 if err := b.gcc(a, p, objdir, ofile, cflags, cfile); err != nil { 2938 return err 2939 } 2940 2941 linkobj := str.StringList(ofile, outObj, mkAbsFiles(p.Dir, p.SysoFiles)) 2942 dynobj := objdir + "_cgo_.o" 2943 2944 // we need to use -pie for Linux/ARM to get accurate imported sym 2945 ldflags := cgoLDFLAGS 2946 if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" { 2947 // -static -pie doesn't make sense, and causes link errors. 2948 // Issue 26197. 2949 n := make([]string, 0, len(ldflags)) 2950 for _, flag := range ldflags { 2951 if flag != "-static" { 2952 n = append(n, flag) 2953 } 2954 } 2955 ldflags = append(n, "-pie") 2956 } 2957 if err := b.gccld(a, p, objdir, dynobj, ldflags, linkobj); err != nil { 2958 return err 2959 } 2960 2961 // cgo -dynimport 2962 var cgoflags []string 2963 if p.Standard && p.ImportPath == "runtime/cgo" { 2964 cgoflags = []string{"-dynlinker"} // record path to dynamic linker 2965 } 2966 return b.run(a, base.Cwd, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags) 2967} 2968 2969// Run SWIG on all SWIG input files. 2970// TODO: Don't build a shared library, once SWIG emits the necessary 2971// pragmas for external linking. 2972func (b *Builder) swig(a *Action, p *load.Package, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) { 2973 if err := b.swigVersionCheck(); err != nil { 2974 return nil, nil, nil, err 2975 } 2976 2977 intgosize, err := b.swigIntSize(objdir) 2978 if err != nil { 2979 return nil, nil, nil, err 2980 } 2981 2982 for _, f := range p.SwigFiles { 2983 goFile, cFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, false, intgosize) 2984 if err != nil { 2985 return nil, nil, nil, err 2986 } 2987 if goFile != "" { 2988 outGo = append(outGo, goFile) 2989 } 2990 if cFile != "" { 2991 outC = append(outC, cFile) 2992 } 2993 } 2994 for _, f := range p.SwigCXXFiles { 2995 goFile, cxxFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, true, intgosize) 2996 if err != nil { 2997 return nil, nil, nil, err 2998 } 2999 if goFile != "" { 3000 outGo = append(outGo, goFile) 3001 } 3002 if cxxFile != "" { 3003 outCXX = append(outCXX, cxxFile) 3004 } 3005 } 3006 return outGo, outC, outCXX, nil 3007} 3008 3009// Make sure SWIG is new enough. 3010var ( 3011 swigCheckOnce sync.Once 3012 swigCheck error 3013) 3014 3015func (b *Builder) swigDoVersionCheck() error { 3016 out, err := b.runOut(nil, "", nil, "swig", "-version") 3017 if err != nil { 3018 return err 3019 } 3020 re := regexp.MustCompile(`[vV]ersion +([\d]+)([.][\d]+)?([.][\d]+)?`) 3021 matches := re.FindSubmatch(out) 3022 if matches == nil { 3023 // Can't find version number; hope for the best. 3024 return nil 3025 } 3026 3027 major, err := strconv.Atoi(string(matches[1])) 3028 if err != nil { 3029 // Can't find version number; hope for the best. 3030 return nil 3031 } 3032 const errmsg = "must have SWIG version >= 3.0.6" 3033 if major < 3 { 3034 return errors.New(errmsg) 3035 } 3036 if major > 3 { 3037 // 4.0 or later 3038 return nil 3039 } 3040 3041 // We have SWIG version 3.x. 3042 if len(matches[2]) > 0 { 3043 minor, err := strconv.Atoi(string(matches[2][1:])) 3044 if err != nil { 3045 return nil 3046 } 3047 if minor > 0 { 3048 // 3.1 or later 3049 return nil 3050 } 3051 } 3052 3053 // We have SWIG version 3.0.x. 3054 if len(matches[3]) > 0 { 3055 patch, err := strconv.Atoi(string(matches[3][1:])) 3056 if err != nil { 3057 return nil 3058 } 3059 if patch < 6 { 3060 // Before 3.0.6. 3061 return errors.New(errmsg) 3062 } 3063 } 3064 3065 return nil 3066} 3067 3068func (b *Builder) swigVersionCheck() error { 3069 swigCheckOnce.Do(func() { 3070 swigCheck = b.swigDoVersionCheck() 3071 }) 3072 return swigCheck 3073} 3074 3075// Find the value to pass for the -intgosize option to swig. 3076var ( 3077 swigIntSizeOnce sync.Once 3078 swigIntSize string 3079 swigIntSizeError error 3080) 3081 3082// This code fails to build if sizeof(int) <= 32 3083const swigIntSizeCode = ` 3084package main 3085const i int = 1 << 32 3086` 3087 3088// Determine the size of int on the target system for the -intgosize option 3089// of swig >= 2.0.9. Run only once. 3090func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) { 3091 if cfg.BuildN { 3092 return "$INTBITS", nil 3093 } 3094 src := filepath.Join(b.WorkDir, "swig_intsize.go") 3095 if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil { 3096 return 3097 } 3098 srcs := []string{src} 3099 3100 p := load.GoFilesPackage(context.TODO(), srcs) 3101 3102 if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, srcs); e != nil { 3103 return "32", nil 3104 } 3105 return "64", nil 3106} 3107 3108// Determine the size of int on the target system for the -intgosize option 3109// of swig >= 2.0.9. 3110func (b *Builder) swigIntSize(objdir string) (intsize string, err error) { 3111 swigIntSizeOnce.Do(func() { 3112 swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir) 3113 }) 3114 return swigIntSize, swigIntSizeError 3115} 3116 3117// Run SWIG on one SWIG input file. 3118func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) { 3119 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p) 3120 if err != nil { 3121 return "", "", err 3122 } 3123 3124 var cflags []string 3125 if cxx { 3126 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS) 3127 } else { 3128 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS) 3129 } 3130 3131 n := 5 // length of ".swig" 3132 if cxx { 3133 n = 8 // length of ".swigcxx" 3134 } 3135 base := file[:len(file)-n] 3136 goFile := base + ".go" 3137 gccBase := base + "_wrap." 3138 gccExt := "c" 3139 if cxx { 3140 gccExt = "cxx" 3141 } 3142 3143 gccgo := cfg.BuildToolchainName == "gccgo" 3144 3145 // swig 3146 args := []string{ 3147 "-go", 3148 "-cgo", 3149 "-intgosize", intgosize, 3150 "-module", base, 3151 "-o", objdir + gccBase + gccExt, 3152 "-outdir", objdir, 3153 } 3154 3155 for _, f := range cflags { 3156 if len(f) > 3 && f[:2] == "-I" { 3157 args = append(args, f) 3158 } 3159 } 3160 3161 if gccgo { 3162 args = append(args, "-gccgo") 3163 if pkgpath := gccgoPkgpath(p); pkgpath != "" { 3164 args = append(args, "-go-pkgpath", pkgpath) 3165 } 3166 } 3167 if cxx { 3168 args = append(args, "-c++") 3169 } 3170 3171 out, err := b.runOut(a, p.Dir, nil, "swig", args, file) 3172 if err != nil { 3173 if len(out) > 0 { 3174 if bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo")) { 3175 return "", "", errors.New("must have SWIG version >= 3.0.6") 3176 } 3177 b.showOutput(a, p.Dir, p.Desc(), b.processOutput(out)) // swig error 3178 return "", "", errPrintedOutput 3179 } 3180 return "", "", err 3181 } 3182 if len(out) > 0 { 3183 b.showOutput(a, p.Dir, p.Desc(), b.processOutput(out)) // swig warning 3184 } 3185 3186 // If the input was x.swig, the output is x.go in the objdir. 3187 // But there might be an x.go in the original dir too, and if it 3188 // uses cgo as well, cgo will be processing both and will 3189 // translate both into x.cgo1.go in the objdir, overwriting one. 3190 // Rename x.go to _x_swig.go to avoid this problem. 3191 // We ignore files in the original dir that begin with underscore 3192 // so _x_swig.go cannot conflict with an original file we were 3193 // going to compile. 3194 goFile = objdir + goFile 3195 newGoFile := objdir + "_" + base + "_swig.go" 3196 if err := os.Rename(goFile, newGoFile); err != nil { 3197 return "", "", err 3198 } 3199 return newGoFile, objdir + gccBase + gccExt, nil 3200} 3201 3202// disableBuildID adjusts a linker command line to avoid creating a 3203// build ID when creating an object file rather than an executable or 3204// shared library. Some systems, such as Ubuntu, always add 3205// --build-id to every link, but we don't want a build ID when we are 3206// producing an object file. On some of those system a plain -r (not 3207// -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a 3208// plain -r. I don't know how to turn off --build-id when using clang 3209// other than passing a trailing --build-id=none. So that is what we 3210// do, but only on systems likely to support it, which is to say, 3211// systems that normally use gold or the GNU linker. 3212func (b *Builder) disableBuildID(ldflags []string) []string { 3213 switch cfg.Goos { 3214 case "android", "dragonfly", "linux", "netbsd": 3215 ldflags = append(ldflags, "-Wl,--build-id=none") 3216 } 3217 return ldflags 3218} 3219 3220// mkAbsFiles converts files into a list of absolute files, 3221// assuming they were originally relative to dir, 3222// and returns that new list. 3223func mkAbsFiles(dir string, files []string) []string { 3224 abs := make([]string, len(files)) 3225 for i, f := range files { 3226 if !filepath.IsAbs(f) { 3227 f = filepath.Join(dir, f) 3228 } 3229 abs[i] = f 3230 } 3231 return abs 3232} 3233 3234// passLongArgsInResponseFiles modifies cmd such that, for 3235// certain programs, long arguments are passed in "response files", a 3236// file on disk with the arguments, with one arg per line. An actual 3237// argument starting with '@' means that the rest of the argument is 3238// a filename of arguments to expand. 3239// 3240// See issues 18468 (Windows) and 37768 (Darwin). 3241func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) { 3242 cleanup = func() {} // no cleanup by default 3243 3244 var argLen int 3245 for _, arg := range cmd.Args { 3246 argLen += len(arg) 3247 } 3248 3249 // If we're not approaching 32KB of args, just pass args normally. 3250 // (use 30KB instead to be conservative; not sure how accounting is done) 3251 if !useResponseFile(cmd.Path, argLen) { 3252 return 3253 } 3254 3255 tf, err := os.CreateTemp("", "args") 3256 if err != nil { 3257 log.Fatalf("error writing long arguments to response file: %v", err) 3258 } 3259 cleanup = func() { os.Remove(tf.Name()) } 3260 var buf bytes.Buffer 3261 for _, arg := range cmd.Args[1:] { 3262 fmt.Fprintf(&buf, "%s\n", encodeArg(arg)) 3263 } 3264 if _, err := tf.Write(buf.Bytes()); err != nil { 3265 tf.Close() 3266 cleanup() 3267 log.Fatalf("error writing long arguments to response file: %v", err) 3268 } 3269 if err := tf.Close(); err != nil { 3270 cleanup() 3271 log.Fatalf("error writing long arguments to response file: %v", err) 3272 } 3273 cmd.Args = []string{cmd.Args[0], "@" + tf.Name()} 3274 return cleanup 3275} 3276 3277// Windows has a limit of 32 KB arguments. To be conservative and not worry 3278// about whether that includes spaces or not, just use 30 KB. Darwin's limit is 3279// less clear. The OS claims 256KB, but we've seen failures with arglen as 3280// small as 50KB. 3281const ArgLengthForResponseFile = (30 << 10) 3282 3283func useResponseFile(path string, argLen int) bool { 3284 // Unless the program uses objabi.Flagparse, which understands 3285 // response files, don't use response files. 3286 // TODO: do we need more commands? asm? cgo? For now, no. 3287 prog := strings.TrimSuffix(filepath.Base(path), ".exe") 3288 switch prog { 3289 case "compile", "link": 3290 default: 3291 return false 3292 } 3293 3294 if argLen > ArgLengthForResponseFile { 3295 return true 3296 } 3297 3298 // On the Go build system, use response files about 10% of the 3299 // time, just to exercise this codepath. 3300 isBuilder := os.Getenv("GO_BUILDER_NAME") != "" 3301 if isBuilder && rand.Intn(10) == 0 { 3302 return true 3303 } 3304 3305 return false 3306} 3307 3308// encodeArg encodes an argument for response file writing. 3309func encodeArg(arg string) string { 3310 // If there aren't any characters we need to reencode, fastpath out. 3311 if !strings.ContainsAny(arg, "\\\n") { 3312 return arg 3313 } 3314 var b strings.Builder 3315 for _, r := range arg { 3316 switch r { 3317 case '\\': 3318 b.WriteByte('\\') 3319 b.WriteByte('\\') 3320 case '\n': 3321 b.WriteByte('\\') 3322 b.WriteByte('n') 3323 default: 3324 b.WriteRune(r) 3325 } 3326 } 3327 return b.String() 3328} 3329