github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/src/cmd/go/internal/work/exec.go (about) 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 7 package work 8 9 import ( 10 "bytes" 11 "encoding/json" 12 "errors" 13 "fmt" 14 "io" 15 "io/ioutil" 16 "log" 17 "os" 18 "os/exec" 19 "path/filepath" 20 "regexp" 21 "runtime" 22 "strconv" 23 "strings" 24 "sync" 25 "time" 26 27 "cmd/go/internal/base" 28 "cmd/go/internal/cache" 29 "cmd/go/internal/cfg" 30 "cmd/go/internal/load" 31 "cmd/go/internal/str" 32 ) 33 34 // actionList returns the list of actions in the dag rooted at root 35 // as visited in a depth-first post-order traversal. 36 func actionList(root *Action) []*Action { 37 seen := map[*Action]bool{} 38 all := []*Action{} 39 var walk func(*Action) 40 walk = func(a *Action) { 41 if seen[a] { 42 return 43 } 44 seen[a] = true 45 for _, a1 := range a.Deps { 46 walk(a1) 47 } 48 all = append(all, a) 49 } 50 walk(root) 51 return all 52 } 53 54 // do runs the action graph rooted at root. 55 func (b *Builder) Do(root *Action) { 56 if c := cache.Default(); c != nil && !b.ComputeStaleOnly { 57 // If we're doing real work, take time at the end to trim the cache. 58 defer c.Trim() 59 } 60 61 // Build list of all actions, assigning depth-first post-order priority. 62 // The original implementation here was a true queue 63 // (using a channel) but it had the effect of getting 64 // distracted by low-level leaf actions to the detriment 65 // of completing higher-level actions. The order of 66 // work does not matter much to overall execution time, 67 // but when running "go test std" it is nice to see each test 68 // results as soon as possible. The priorities assigned 69 // ensure that, all else being equal, the execution prefers 70 // to do what it would have done first in a simple depth-first 71 // dependency order traversal. 72 all := actionList(root) 73 for i, a := range all { 74 a.priority = i 75 } 76 77 if cfg.DebugActiongraph != "" { 78 js := actionGraphJSON(root) 79 if err := ioutil.WriteFile(cfg.DebugActiongraph, []byte(js), 0666); err != nil { 80 fmt.Fprintf(os.Stderr, "go: writing action graph: %v\n", err) 81 base.SetExitStatus(1) 82 } 83 } 84 85 b.readySema = make(chan bool, len(all)) 86 87 // Initialize per-action execution state. 88 for _, a := range all { 89 for _, a1 := range a.Deps { 90 a1.triggers = append(a1.triggers, a) 91 } 92 a.pending = len(a.Deps) 93 if a.pending == 0 { 94 b.ready.push(a) 95 b.readySema <- true 96 } 97 } 98 99 // Handle runs a single action and takes care of triggering 100 // any actions that are runnable as a result. 101 handle := func(a *Action) { 102 var err error 103 104 if a.Func != nil && (!a.Failed || a.IgnoreFail) { 105 if err == nil { 106 err = a.Func(b, a) 107 } 108 } 109 110 // The actions run in parallel but all the updates to the 111 // shared work state are serialized through b.exec. 112 b.exec.Lock() 113 defer b.exec.Unlock() 114 115 if err != nil { 116 if err == errPrintedOutput { 117 base.SetExitStatus(2) 118 } else { 119 base.Errorf("%s", err) 120 } 121 a.Failed = true 122 } 123 124 for _, a0 := range a.triggers { 125 if a.Failed { 126 a0.Failed = true 127 } 128 if a0.pending--; a0.pending == 0 { 129 b.ready.push(a0) 130 b.readySema <- true 131 } 132 } 133 134 if a == root { 135 close(b.readySema) 136 } 137 } 138 139 var wg sync.WaitGroup 140 141 // Kick off goroutines according to parallelism. 142 // If we are using the -n flag (just printing commands) 143 // drop the parallelism to 1, both to make the output 144 // deterministic and because there is no real work anyway. 145 par := cfg.BuildP 146 if cfg.BuildN { 147 par = 1 148 } 149 for i := 0; i < par; i++ { 150 wg.Add(1) 151 go func() { 152 defer wg.Done() 153 for { 154 select { 155 case _, ok := <-b.readySema: 156 if !ok { 157 return 158 } 159 // Receiving a value from b.readySema entitles 160 // us to take from the ready queue. 161 b.exec.Lock() 162 a := b.ready.pop() 163 b.exec.Unlock() 164 handle(a) 165 case <-base.Interrupted: 166 base.SetExitStatus(1) 167 return 168 } 169 } 170 }() 171 } 172 173 wg.Wait() 174 } 175 176 // buildActionID computes the action ID for a build action. 177 func (b *Builder) buildActionID(a *Action) cache.ActionID { 178 p := a.Package 179 h := cache.NewHash("build " + p.ImportPath) 180 181 // Configuration independent of compiler toolchain. 182 // Note: buildmode has already been accounted for in buildGcflags 183 // and should not be inserted explicitly. Most buildmodes use the 184 // same compiler settings and can reuse each other's results. 185 // If not, the reason is already recorded in buildGcflags. 186 fmt.Fprintf(h, "compile\n") 187 // The compiler hides the exact value of $GOROOT 188 // when building things in GOROOT, 189 // but it does not hide the exact value of $GOPATH. 190 // Include the full dir in that case. 191 // Assume b.WorkDir is being trimmed properly. 192 if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) { 193 fmt.Fprintf(h, "dir %s\n", p.Dir) 194 } 195 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch) 196 fmt.Fprintf(h, "import %q\n", p.ImportPath) 197 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix) 198 if len(p.CgoFiles)+len(p.SwigFiles) > 0 { 199 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo")) 200 cppflags, cflags, cxxflags, fflags, _ := b.CFlags(p) 201 fmt.Fprintf(h, "CC=%q %q %q\n", b.ccExe(), cppflags, cflags) 202 if len(p.CXXFiles)+len(p.SwigFiles) > 0 { 203 fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags) 204 } 205 if len(p.FFiles) > 0 { 206 fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags) 207 } 208 // TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions? 209 } 210 if p.Internal.CoverMode != "" { 211 fmt.Fprintf(h, "cover %q %q\n", p.Internal.CoverMode, b.toolID("cover")) 212 } 213 214 // Configuration specific to compiler toolchain. 215 switch cfg.BuildToolchainName { 216 default: 217 base.Fatalf("buildActionID: unknown build toolchain %q", cfg.BuildToolchainName) 218 case "gc": 219 fmt.Fprintf(h, "compile %s %q %q\n", b.toolID("compile"), forcedGcflags, p.Internal.Gcflags) 220 if len(p.SFiles) > 0 { 221 fmt.Fprintf(h, "asm %q %q %q\n", b.toolID("asm"), forcedAsmflags, p.Internal.Asmflags) 222 } 223 fmt.Fprintf(h, "GO$GOARCH=%s\n", os.Getenv("GO"+strings.ToUpper(cfg.BuildContext.GOARCH))) // GO386, GOARM, etc 224 225 // TODO(rsc): Convince compiler team not to add more magic environment variables, 226 // or perhaps restrict the environment variables passed to subprocesses. 227 magic := []string{ 228 "GOCLOBBERDEADHASH", 229 "GOSSAFUNC", 230 "GO_SSA_PHI_LOC_CUTOFF", 231 "GOSSAHASH", 232 } 233 for _, env := range magic { 234 if x := os.Getenv(env); x != "" { 235 fmt.Fprintf(h, "magic %s=%s\n", env, x) 236 } 237 } 238 if os.Getenv("GOSSAHASH") != "" { 239 for i := 0; ; i++ { 240 env := fmt.Sprintf("GOSSAHASH%d", i) 241 x := os.Getenv(env) 242 if x == "" { 243 break 244 } 245 fmt.Fprintf(h, "magic %s=%s\n", env, x) 246 } 247 } 248 if os.Getenv("GSHS_LOGFILE") != "" { 249 // Clumsy hack. Compiler writes to this log file, 250 // so do not allow use of cache at all. 251 // We will still write to the cache but it will be 252 // essentially unfindable. 253 fmt.Fprintf(h, "nocache %d\n", time.Now().UnixNano()) 254 } 255 256 case "gccgo": 257 id, err := b.gccgoToolID(BuildToolchain.compiler(), "go") 258 if err != nil { 259 base.Fatalf("%v", err) 260 } 261 fmt.Fprintf(h, "compile %s %q %q\n", id, forcedGccgoflags, p.Internal.Gccgoflags) 262 fmt.Fprintf(h, "pkgpath %s\n", gccgoPkgpath(p)) 263 if len(p.SFiles) > 0 { 264 id, err = b.gccgoToolID(BuildToolchain.compiler(), "assembler-with-cpp") 265 // Ignore error; different assembler versions 266 // are unlikely to make any difference anyhow. 267 fmt.Fprintf(h, "asm %q\n", id) 268 } 269 } 270 271 // Input files. 272 inputFiles := str.StringList( 273 p.GoFiles, 274 p.CgoFiles, 275 p.CFiles, 276 p.CXXFiles, 277 p.FFiles, 278 p.MFiles, 279 p.HFiles, 280 p.SFiles, 281 p.SysoFiles, 282 p.SwigFiles, 283 p.SwigCXXFiles, 284 ) 285 for _, file := range inputFiles { 286 fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file))) 287 } 288 for _, a1 := range a.Deps { 289 p1 := a1.Package 290 if p1 != nil { 291 fmt.Fprintf(h, "import %s %s\n", p1.ImportPath, contentID(a1.buildID)) 292 } 293 } 294 295 return h.Sum() 296 } 297 298 // build is the action for building a single package. 299 // Note that any new influence on this logic must be reported in b.buildActionID above as well. 300 func (b *Builder) build(a *Action) (err error) { 301 p := a.Package 302 cached := false 303 if !p.BinaryOnly { 304 if b.useCache(a, p, b.buildActionID(a), p.Target) { 305 // If this build triggers a header install, run cgo to get the header. 306 // TODO(rsc): Once we can cache multiple file outputs from an action, 307 // the header should be cached, and then this awful test can be deleted. 308 // Need to look for install header actions depending on this action, 309 // or depending on a link that depends on this action. 310 needHeader := false 311 if (a.Package.UsesCgo() || a.Package.UsesSwig()) && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") { 312 for _, t1 := range a.triggers { 313 if t1.Mode == "install header" { 314 needHeader = true 315 goto CheckedHeader 316 } 317 } 318 for _, t1 := range a.triggers { 319 for _, t2 := range t1.triggers { 320 if t2.Mode == "install header" { 321 needHeader = true 322 goto CheckedHeader 323 } 324 } 325 } 326 } 327 CheckedHeader: 328 if b.ComputeStaleOnly || !a.needVet && !needHeader { 329 return nil 330 } 331 cached = true 332 } 333 defer b.flushOutput(a) 334 } 335 336 defer func() { 337 if err != nil && err != errPrintedOutput { 338 err = fmt.Errorf("go build %s: %v", a.Package.ImportPath, err) 339 } 340 }() 341 if cfg.BuildN { 342 // In -n mode, print a banner between packages. 343 // The banner is five lines so that when changes to 344 // different sections of the bootstrap script have to 345 // be merged, the banners give patch something 346 // to use to find its context. 347 b.Print("\n#\n# " + a.Package.ImportPath + "\n#\n\n") 348 } 349 350 if cfg.BuildV { 351 b.Print(a.Package.ImportPath + "\n") 352 } 353 354 if a.Package.BinaryOnly { 355 _, err := os.Stat(a.Package.Target) 356 if err == nil { 357 a.built = a.Package.Target 358 a.Target = a.Package.Target 359 a.buildID = b.fileHash(a.Package.Target) 360 a.Package.Stale = false 361 a.Package.StaleReason = "binary-only package" 362 return nil 363 } 364 if b.ComputeStaleOnly { 365 a.Package.Stale = true 366 a.Package.StaleReason = "missing or invalid binary-only package" 367 return nil 368 } 369 return fmt.Errorf("missing or invalid binary-only package") 370 } 371 372 if err := b.Mkdir(a.Objdir); err != nil { 373 return err 374 } 375 objdir := a.Objdir 376 377 // make target directory 378 dir, _ := filepath.Split(a.Target) 379 if dir != "" { 380 if err := b.Mkdir(dir); err != nil { 381 return err 382 } 383 } 384 385 var gofiles, cgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string 386 387 gofiles = append(gofiles, a.Package.GoFiles...) 388 cgofiles = append(cgofiles, a.Package.CgoFiles...) 389 cfiles = append(cfiles, a.Package.CFiles...) 390 sfiles = append(sfiles, a.Package.SFiles...) 391 cxxfiles = append(cxxfiles, a.Package.CXXFiles...) 392 393 if a.Package.UsesCgo() || a.Package.UsesSwig() { 394 if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(a.Package); err != nil { 395 return 396 } 397 } 398 399 // Run SWIG on each .swig and .swigcxx file. 400 // Each run will generate two files, a .go file and a .c or .cxx file. 401 // The .go file will use import "C" and is to be processed by cgo. 402 if a.Package.UsesSwig() { 403 outGo, outC, outCXX, err := b.swig(a, a.Package, objdir, pcCFLAGS) 404 if err != nil { 405 return err 406 } 407 cgofiles = append(cgofiles, outGo...) 408 cfiles = append(cfiles, outC...) 409 cxxfiles = append(cxxfiles, outCXX...) 410 } 411 412 // If we're doing coverage, preprocess the .go files and put them in the work directory 413 if a.Package.Internal.CoverMode != "" { 414 for i, file := range str.StringList(gofiles, cgofiles) { 415 var sourceFile string 416 var coverFile string 417 var key string 418 if strings.HasSuffix(file, ".cgo1.go") { 419 // cgo files have absolute paths 420 base := filepath.Base(file) 421 sourceFile = file 422 coverFile = objdir + base 423 key = strings.TrimSuffix(base, ".cgo1.go") + ".go" 424 } else { 425 sourceFile = filepath.Join(a.Package.Dir, file) 426 coverFile = objdir + file 427 key = file 428 } 429 coverFile = strings.TrimSuffix(coverFile, ".go") + ".cover.go" 430 cover := a.Package.Internal.CoverVars[key] 431 if cover == nil || base.IsTestFile(file) { 432 // Not covering this file. 433 continue 434 } 435 if err := b.cover(a, coverFile, sourceFile, 0666, cover.Var); err != nil { 436 return err 437 } 438 if i < len(gofiles) { 439 gofiles[i] = coverFile 440 } else { 441 cgofiles[i-len(gofiles)] = coverFile 442 } 443 } 444 } 445 446 // Run cgo. 447 if a.Package.UsesCgo() || a.Package.UsesSwig() { 448 // In a package using cgo, cgo compiles the C, C++ and assembly files with gcc. 449 // There is one exception: runtime/cgo's job is to bridge the 450 // cgo and non-cgo worlds, so it necessarily has files in both. 451 // In that case gcc only gets the gcc_* files. 452 var gccfiles []string 453 gccfiles = append(gccfiles, cfiles...) 454 cfiles = nil 455 if a.Package.Standard && a.Package.ImportPath == "runtime/cgo" { 456 filter := func(files, nongcc, gcc []string) ([]string, []string) { 457 for _, f := range files { 458 if strings.HasPrefix(f, "gcc_") { 459 gcc = append(gcc, f) 460 } else { 461 nongcc = append(nongcc, f) 462 } 463 } 464 return nongcc, gcc 465 } 466 sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles) 467 } else { 468 for _, sfile := range sfiles { 469 data, err := ioutil.ReadFile(filepath.Join(a.Package.Dir, sfile)) 470 if err == nil { 471 if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) || 472 bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) || 473 bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) { 474 return fmt.Errorf("package using cgo has Go assembly file %s", sfile) 475 } 476 } 477 } 478 gccfiles = append(gccfiles, sfiles...) 479 sfiles = nil 480 } 481 482 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) 483 if err != nil { 484 return err 485 } 486 if cfg.BuildToolchainName == "gccgo" { 487 cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags") 488 } 489 cgoObjects = append(cgoObjects, outObj...) 490 gofiles = append(gofiles, outGo...) 491 } 492 if cached && !a.needVet { 493 return nil 494 } 495 496 // Sanity check only, since Package.load already checked as well. 497 if len(gofiles) == 0 { 498 return &load.NoGoError{Package: a.Package} 499 } 500 501 // Prepare Go vet config if needed. 502 var vcfg *vetConfig 503 if a.needVet { 504 // Pass list of absolute paths to vet, 505 // so that vet's error messages will use absolute paths, 506 // so that we can reformat them relative to the directory 507 // in which the go command is invoked. 508 vcfg = &vetConfig{ 509 Compiler: cfg.BuildToolchainName, 510 Dir: a.Package.Dir, 511 GoFiles: mkAbsFiles(a.Package.Dir, gofiles), 512 ImportPath: a.Package.ImportPath, 513 ImportMap: make(map[string]string), 514 PackageFile: make(map[string]string), 515 } 516 a.vetCfg = vcfg 517 for i, raw := range a.Package.Internal.RawImports { 518 final := a.Package.Imports[i] 519 vcfg.ImportMap[raw] = final 520 } 521 } 522 523 // Prepare Go import config. 524 var icfg bytes.Buffer 525 for i, raw := range a.Package.Internal.RawImports { 526 final := a.Package.Imports[i] 527 if final != raw { 528 fmt.Fprintf(&icfg, "importmap %s=%s\n", raw, final) 529 } 530 } 531 532 // Compute the list of mapped imports in the vet config 533 // so that we can add any missing mappings below. 534 var vcfgMapped map[string]bool 535 if vcfg != nil { 536 vcfgMapped = make(map[string]bool) 537 for _, p := range vcfg.ImportMap { 538 vcfgMapped[p] = true 539 } 540 } 541 542 for _, a1 := range a.Deps { 543 p1 := a1.Package 544 if p1 == nil || p1.ImportPath == "" || a1.built == "" { 545 continue 546 } 547 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built) 548 if vcfg != nil { 549 // Add import mapping if needed 550 // (for imports like "runtime/cgo" that appear only in generated code). 551 if !vcfgMapped[p1.ImportPath] { 552 vcfg.ImportMap[p1.ImportPath] = p1.ImportPath 553 } 554 vcfg.PackageFile[p1.ImportPath] = a1.built 555 } 556 } 557 558 if cached { 559 // The cached package file is OK, so we don't need to run the compile. 560 // We've only going through the motions to prepare the vet configuration, 561 // which is now complete. 562 return nil 563 } 564 565 // Compile Go. 566 objpkg := objdir + "_pkg_.a" 567 ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), len(sfiles) > 0, gofiles) 568 if len(out) > 0 { 569 b.showOutput(a, a.Package.Dir, a.Package.ImportPath, b.processOutput(out)) 570 if err != nil { 571 return errPrintedOutput 572 } 573 } 574 if err != nil { 575 return err 576 } 577 if ofile != objpkg { 578 objects = append(objects, ofile) 579 } 580 581 // Copy .h files named for goos or goarch or goos_goarch 582 // to names using GOOS and GOARCH. 583 // For example, defs_linux_amd64.h becomes defs_GOOS_GOARCH.h. 584 _goos_goarch := "_" + cfg.Goos + "_" + cfg.Goarch 585 _goos := "_" + cfg.Goos 586 _goarch := "_" + cfg.Goarch 587 for _, file := range a.Package.HFiles { 588 name, ext := fileExtSplit(file) 589 switch { 590 case strings.HasSuffix(name, _goos_goarch): 591 targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext 592 if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 593 return err 594 } 595 case strings.HasSuffix(name, _goarch): 596 targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext 597 if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 598 return err 599 } 600 case strings.HasSuffix(name, _goos): 601 targ := file[:len(name)-len(_goos)] + "_GOOS." + ext 602 if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { 603 return err 604 } 605 } 606 } 607 608 for _, file := range cfiles { 609 out := file[:len(file)-len(".c")] + ".o" 610 if err := BuildToolchain.cc(b, a, objdir+out, file); err != nil { 611 return err 612 } 613 objects = append(objects, out) 614 } 615 616 // Assemble .s files. 617 if len(sfiles) > 0 { 618 ofiles, err := BuildToolchain.asm(b, a, sfiles) 619 if err != nil { 620 return err 621 } 622 objects = append(objects, ofiles...) 623 } 624 625 // For gccgo on ELF systems, we write the build ID as an assembler file. 626 // This lets us set the the SHF_EXCLUDE flag. 627 // This is read by readGccgoArchive in cmd/internal/buildid/buildid.go. 628 if a.buildID != "" && cfg.BuildToolchainName == "gccgo" { 629 switch cfg.Goos { 630 case "android", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris": 631 asmfile, err := b.gccgoBuildIDELFFile(a) 632 if err != nil { 633 return err 634 } 635 ofiles, err := BuildToolchain.asm(b, a, []string{asmfile}) 636 if err != nil { 637 return err 638 } 639 objects = append(objects, ofiles...) 640 } 641 } 642 643 // NOTE(rsc): On Windows, it is critically important that the 644 // gcc-compiled objects (cgoObjects) be listed after the ordinary 645 // objects in the archive. I do not know why this is. 646 // https://golang.org/issue/2601 647 objects = append(objects, cgoObjects...) 648 649 // Add system object files. 650 for _, syso := range a.Package.SysoFiles { 651 objects = append(objects, filepath.Join(a.Package.Dir, syso)) 652 } 653 654 // Pack into archive in objdir directory. 655 // If the Go compiler wrote an archive, we only need to add the 656 // object files for non-Go sources to the archive. 657 // If the Go compiler wrote an archive and the package is entirely 658 // Go sources, there is no pack to execute at all. 659 if len(objects) > 0 { 660 if err := BuildToolchain.pack(b, a, objpkg, objects); err != nil { 661 return err 662 } 663 } 664 665 if err := b.updateBuildID(a, objpkg, true); err != nil { 666 return err 667 } 668 669 a.built = objpkg 670 return nil 671 } 672 673 type vetConfig struct { 674 Compiler string 675 Dir string 676 GoFiles []string 677 ImportMap map[string]string 678 PackageFile map[string]string 679 ImportPath string 680 681 SucceedOnTypecheckFailure bool 682 } 683 684 // VetTool is the path to an alternate vet tool binary. 685 // The caller is expected to set it (if needed) before executing any vet actions. 686 var VetTool string 687 688 // VetFlags are the flags to pass to vet. 689 // The caller is expected to set them before executing any vet actions. 690 var VetFlags []string 691 692 func (b *Builder) vet(a *Action) error { 693 // a.Deps[0] is the build of the package being vetted. 694 // a.Deps[1] is the build of the "fmt" package. 695 696 vcfg := a.Deps[0].vetCfg 697 if vcfg == nil { 698 // Vet config should only be missing if the build failed. 699 if !a.Deps[0].Failed { 700 return fmt.Errorf("vet config not found") 701 } 702 return nil 703 } 704 705 if vcfg.ImportMap["fmt"] == "" { 706 a1 := a.Deps[1] 707 vcfg.ImportMap["fmt"] = "fmt" 708 vcfg.PackageFile["fmt"] = a1.built 709 } 710 711 // During go test, ignore type-checking failures during vet. 712 // We only run vet if the compilation has succeeded, 713 // so at least for now assume the bug is in vet. 714 // We know of at least #18395. 715 // TODO(rsc,gri): Try to remove this for Go 1.11. 716 vcfg.SucceedOnTypecheckFailure = cfg.CmdName == "test" 717 718 js, err := json.MarshalIndent(vcfg, "", "\t") 719 if err != nil { 720 return fmt.Errorf("internal error marshaling vet config: %v", err) 721 } 722 js = append(js, '\n') 723 if err := b.writeFile(a.Objdir+"vet.cfg", js); err != nil { 724 return err 725 } 726 727 var env []string 728 if cfg.BuildToolchainName == "gccgo" { 729 env = append(env, "GCCGO="+BuildToolchain.compiler()) 730 } 731 732 p := a.Package 733 tool := VetTool 734 if tool == "" { 735 tool = base.Tool("vet") 736 } 737 return b.run(a, p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, VetFlags, a.Objdir+"vet.cfg") 738 } 739 740 // linkActionID computes the action ID for a link action. 741 func (b *Builder) linkActionID(a *Action) cache.ActionID { 742 p := a.Package 743 h := cache.NewHash("link " + p.ImportPath) 744 745 // Toolchain-independent configuration. 746 fmt.Fprintf(h, "link\n") 747 fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) 748 fmt.Fprintf(h, "import %q\n", p.ImportPath) 749 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix) 750 751 // Toolchain-dependent configuration, shared with b.linkSharedActionID. 752 b.printLinkerConfig(h, p) 753 754 // Input files. 755 for _, a1 := range a.Deps { 756 p1 := a1.Package 757 if p1 != nil { 758 if a1.built != "" || a1.buildID != "" { 759 buildID := a1.buildID 760 if buildID == "" { 761 buildID = b.buildID(a1.built) 762 } 763 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID)) 764 } 765 // Because we put package main's full action ID into the binary's build ID, 766 // we must also put the full action ID into the binary's action ID hash. 767 if p1.Name == "main" { 768 fmt.Fprintf(h, "packagemain %s\n", a1.buildID) 769 } 770 if p1.Shlib != "" { 771 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib))) 772 } 773 } 774 } 775 776 return h.Sum() 777 } 778 779 // printLinkerConfig prints the linker config into the hash h, 780 // as part of the computation of a linker-related action ID. 781 func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) { 782 switch cfg.BuildToolchainName { 783 default: 784 base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName) 785 786 case "gc": 787 fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode) 788 if p != nil { 789 fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags) 790 } 791 fmt.Fprintf(h, "GO$GOARCH=%s\n", os.Getenv("GO"+strings.ToUpper(cfg.BuildContext.GOARCH))) // GO386, GOARM, etc 792 793 // The linker writes source file paths that say GOROOT_FINAL. 794 fmt.Fprintf(h, "GOROOT=%s\n", cfg.GOROOT_FINAL) 795 796 // TODO(rsc): Convince linker team not to add more magic environment variables, 797 // or perhaps restrict the environment variables passed to subprocesses. 798 magic := []string{ 799 "GO_EXTLINK_ENABLED", 800 } 801 for _, env := range magic { 802 if x := os.Getenv(env); x != "" { 803 fmt.Fprintf(h, "magic %s=%s\n", env, x) 804 } 805 } 806 807 // TODO(rsc): Do cgo settings and flags need to be included? 808 // Or external linker settings and flags? 809 810 case "gccgo": 811 id, err := b.gccgoToolID(BuildToolchain.linker(), "go") 812 if err != nil { 813 base.Fatalf("%v", err) 814 } 815 fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode) 816 // TODO(iant): Should probably include cgo flags here. 817 } 818 } 819 820 // link is the action for linking a single command. 821 // Note that any new influence on this logic must be reported in b.linkActionID above as well. 822 func (b *Builder) link(a *Action) (err error) { 823 if b.useCache(a, a.Package, b.linkActionID(a), a.Package.Target) { 824 return nil 825 } 826 defer b.flushOutput(a) 827 828 if err := b.Mkdir(a.Objdir); err != nil { 829 return err 830 } 831 832 importcfg := a.Objdir + "importcfg.link" 833 if err := b.writeLinkImportcfg(a, importcfg); err != nil { 834 return err 835 } 836 837 // make target directory 838 dir, _ := filepath.Split(a.Target) 839 if dir != "" { 840 if err := b.Mkdir(dir); err != nil { 841 return err 842 } 843 } 844 845 if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil { 846 return err 847 } 848 849 // Update the binary with the final build ID. 850 // But if OmitDebug is set, don't rewrite the binary, because we set OmitDebug 851 // on binaries that we are going to run and then delete. 852 // There's no point in doing work on such a binary. 853 // Worse, opening the binary for write here makes it 854 // essentially impossible to safely fork+exec due to a fundamental 855 // incompatibility between ETXTBSY and threads on modern Unix systems. 856 // See golang.org/issue/22220. 857 // We still call updateBuildID to update a.buildID, which is important 858 // for test result caching, but passing rewrite=false (final arg) 859 // means we don't actually rewrite the binary, nor store the 860 // result into the cache. 861 // Not calling updateBuildID means we also don't insert these 862 // binaries into the build object cache. That's probably a net win: 863 // less cache space wasted on large binaries we are not likely to 864 // need again. (On the other hand it does make repeated go test slower.) 865 if err := b.updateBuildID(a, a.Target, !a.Package.Internal.OmitDebug); err != nil { 866 return err 867 } 868 869 a.built = a.Target 870 return nil 871 } 872 873 func (b *Builder) writeLinkImportcfg(a *Action, file string) error { 874 // Prepare Go import cfg. 875 var icfg bytes.Buffer 876 for _, a1 := range a.Deps { 877 p1 := a1.Package 878 if p1 == nil { 879 continue 880 } 881 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built) 882 if p1.Shlib != "" { 883 fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib) 884 } 885 } 886 return b.writeFile(file, icfg.Bytes()) 887 } 888 889 // PkgconfigCmd returns a pkg-config binary name 890 // defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist. 891 func (b *Builder) PkgconfigCmd() string { 892 return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0] 893 } 894 895 // splitPkgConfigOutput parses the pkg-config output into a slice of 896 // flags. pkg-config always uses \ to escape special characters. 897 func splitPkgConfigOutput(out []byte) []string { 898 if len(out) == 0 { 899 return nil 900 } 901 var flags []string 902 flag := make([]byte, len(out)) 903 r, w := 0, 0 904 for r < len(out) { 905 switch out[r] { 906 case ' ', '\t', '\r', '\n': 907 if w > 0 { 908 flags = append(flags, string(flag[:w])) 909 } 910 w = 0 911 case '\\': 912 r++ 913 fallthrough 914 default: 915 if r < len(out) { 916 flag[w] = out[r] 917 w++ 918 } 919 } 920 r++ 921 } 922 if w > 0 { 923 flags = append(flags, string(flag[:w])) 924 } 925 return flags 926 } 927 928 // Calls pkg-config if needed and returns the cflags/ldflags needed to build the package. 929 func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) { 930 if pkgs := p.CgoPkgConfig; len(pkgs) > 0 { 931 var out []byte 932 out, err = b.runOut(p.Dir, p.ImportPath, nil, b.PkgconfigCmd(), "--cflags", pkgs) 933 if err != nil { 934 b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --cflags "+strings.Join(pkgs, " "), string(out)) 935 b.Print(err.Error() + "\n") 936 err = errPrintedOutput 937 return 938 } 939 if len(out) > 0 { 940 cflags = splitPkgConfigOutput(out) 941 } 942 out, err = b.runOut(p.Dir, p.ImportPath, nil, b.PkgconfigCmd(), "--libs", pkgs) 943 if err != nil { 944 b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --libs "+strings.Join(pkgs, " "), string(out)) 945 b.Print(err.Error() + "\n") 946 err = errPrintedOutput 947 return 948 } 949 if len(out) > 0 { 950 ldflags = strings.Fields(string(out)) 951 } 952 } 953 return 954 } 955 956 func (b *Builder) installShlibname(a *Action) error { 957 // TODO: BuildN 958 a1 := a.Deps[0] 959 err := ioutil.WriteFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"), 0666) 960 if err != nil { 961 return err 962 } 963 if cfg.BuildX { 964 b.Showcmd("", "echo '%s' > %s # internal", filepath.Base(a1.Target), a.Target) 965 } 966 return nil 967 } 968 969 func (b *Builder) linkSharedActionID(a *Action) cache.ActionID { 970 h := cache.NewHash("linkShared") 971 972 // Toolchain-independent configuration. 973 fmt.Fprintf(h, "linkShared\n") 974 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch) 975 976 // Toolchain-dependent configuration, shared with b.linkActionID. 977 b.printLinkerConfig(h, nil) 978 979 // Input files. 980 for _, a1 := range a.Deps { 981 p1 := a1.Package 982 if a1.built == "" { 983 continue 984 } 985 if p1 != nil { 986 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built))) 987 if p1.Shlib != "" { 988 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib))) 989 } 990 } 991 } 992 // Files named on command line are special. 993 for _, a1 := range a.Deps[0].Deps { 994 p1 := a1.Package 995 fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built))) 996 } 997 998 return h.Sum() 999 } 1000 1001 func (b *Builder) linkShared(a *Action) (err error) { 1002 if b.useCache(a, nil, b.linkSharedActionID(a), a.Target) { 1003 return nil 1004 } 1005 defer b.flushOutput(a) 1006 1007 if err := b.Mkdir(a.Objdir); err != nil { 1008 return err 1009 } 1010 1011 importcfg := a.Objdir + "importcfg.link" 1012 if err := b.writeLinkImportcfg(a, importcfg); err != nil { 1013 return err 1014 } 1015 1016 // TODO(rsc): There is a missing updateBuildID here, 1017 // but we have to decide where to store the build ID in these files. 1018 a.built = a.Target 1019 return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps) 1020 } 1021 1022 // BuildInstallFunc is the action for installing a single package or executable. 1023 func BuildInstallFunc(b *Builder, a *Action) (err error) { 1024 defer func() { 1025 if err != nil && err != errPrintedOutput { 1026 // a.Package == nil is possible for the go install -buildmode=shared 1027 // action that installs libmangledname.so, which corresponds to 1028 // a list of packages, not just one. 1029 sep, path := "", "" 1030 if a.Package != nil { 1031 sep, path = " ", a.Package.ImportPath 1032 } 1033 err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err) 1034 } 1035 }() 1036 1037 a1 := a.Deps[0] 1038 a.buildID = a1.buildID 1039 1040 // If we are using the eventual install target as an up-to-date 1041 // cached copy of the thing we built, then there's no need to 1042 // copy it into itself (and that would probably fail anyway). 1043 // In this case a1.built == a.Target because a1.built == p.Target, 1044 // so the built target is not in the a1.Objdir tree that b.cleanup(a1) removes. 1045 if a1.built == a.Target { 1046 a.built = a.Target 1047 b.cleanup(a1) 1048 // Whether we're smart enough to avoid a complete rebuild 1049 // depends on exactly what the staleness and rebuild algorithms 1050 // are, as well as potentially the state of the Go build cache. 1051 // We don't really want users to be able to infer (or worse start depending on) 1052 // those details from whether the modification time changes during 1053 // "go install", so do a best-effort update of the file times to make it 1054 // look like we rewrote a.Target even if we did not. Updating the mtime 1055 // may also help other mtime-based systems that depend on our 1056 // previous mtime updates that happened more often. 1057 // This is still not perfect - we ignore the error result, and if the file was 1058 // unwritable for some reason then pretending to have written it is also 1059 // confusing - but it's probably better than not doing the mtime update. 1060 // 1061 // But don't do that for the special case where building an executable 1062 // with -linkshared implicitly installs all its dependent libraries. 1063 // We want to hide that awful detail as much as possible, so don't 1064 // advertise it by touching the mtimes (usually the libraries are up 1065 // to date). 1066 if !a.buggyInstall { 1067 now := time.Now() 1068 os.Chtimes(a.Target, now, now) 1069 } 1070 return nil 1071 } 1072 if b.ComputeStaleOnly { 1073 return nil 1074 } 1075 1076 if err := b.Mkdir(a.Objdir); err != nil { 1077 return err 1078 } 1079 1080 perm := os.FileMode(0666) 1081 if a1.Mode == "link" { 1082 switch cfg.BuildBuildmode { 1083 case "c-archive", "c-shared", "plugin": 1084 default: 1085 perm = 0777 1086 } 1087 } 1088 1089 // make target directory 1090 dir, _ := filepath.Split(a.Target) 1091 if dir != "" { 1092 if err := b.Mkdir(dir); err != nil { 1093 return err 1094 } 1095 } 1096 1097 defer b.cleanup(a1) 1098 1099 return b.moveOrCopyFile(a, a.Target, a1.built, perm, false) 1100 } 1101 1102 // cleanup removes a's object dir to keep the amount of 1103 // on-disk garbage down in a large build. On an operating system 1104 // with aggressive buffering, cleaning incrementally like 1105 // this keeps the intermediate objects from hitting the disk. 1106 func (b *Builder) cleanup(a *Action) { 1107 if !cfg.BuildWork { 1108 if cfg.BuildX { 1109 b.Showcmd("", "rm -r %s", a.Objdir) 1110 } 1111 os.RemoveAll(a.Objdir) 1112 } 1113 } 1114 1115 // moveOrCopyFile is like 'mv src dst' or 'cp src dst'. 1116 func (b *Builder) moveOrCopyFile(a *Action, dst, src string, perm os.FileMode, force bool) error { 1117 if cfg.BuildN { 1118 b.Showcmd("", "mv %s %s", src, dst) 1119 return nil 1120 } 1121 1122 // If we can update the mode and rename to the dst, do it. 1123 // Otherwise fall back to standard copy. 1124 1125 // If the source is in the build cache, we need to copy it. 1126 if strings.HasPrefix(src, cache.DefaultDir()) { 1127 return b.copyFile(a, dst, src, perm, force) 1128 } 1129 1130 // On Windows, always copy the file, so that we respect the NTFS 1131 // permissions of the parent folder. https://golang.org/issue/22343. 1132 // What matters here is not cfg.Goos (the system we are building 1133 // for) but runtime.GOOS (the system we are building on). 1134 if runtime.GOOS == "windows" { 1135 return b.copyFile(a, dst, src, perm, force) 1136 } 1137 1138 // If the destination directory has the group sticky bit set, 1139 // we have to copy the file to retain the correct permissions. 1140 // https://golang.org/issue/18878 1141 if fi, err := os.Stat(filepath.Dir(dst)); err == nil { 1142 if fi.IsDir() && (fi.Mode()&os.ModeSetgid) != 0 { 1143 return b.copyFile(a, dst, src, perm, force) 1144 } 1145 } 1146 1147 // The perm argument is meant to be adjusted according to umask, 1148 // but we don't know what the umask is. 1149 // Create a dummy file to find out. 1150 // This avoids build tags and works even on systems like Plan 9 1151 // where the file mask computation incorporates other information. 1152 mode := perm 1153 f, err := os.OpenFile(filepath.Clean(dst)+"-go-tmp-umask", os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm) 1154 if err == nil { 1155 fi, err := f.Stat() 1156 if err == nil { 1157 mode = fi.Mode() & 0777 1158 } 1159 name := f.Name() 1160 f.Close() 1161 os.Remove(name) 1162 } 1163 1164 if err := os.Chmod(src, mode); err == nil { 1165 if err := os.Rename(src, dst); err == nil { 1166 if cfg.BuildX { 1167 b.Showcmd("", "mv %s %s", src, dst) 1168 } 1169 return nil 1170 } 1171 } 1172 1173 return b.copyFile(a, dst, src, perm, force) 1174 } 1175 1176 // copyFile is like 'cp src dst'. 1177 func (b *Builder) copyFile(a *Action, dst, src string, perm os.FileMode, force bool) error { 1178 if cfg.BuildN || cfg.BuildX { 1179 b.Showcmd("", "cp %s %s", src, dst) 1180 if cfg.BuildN { 1181 return nil 1182 } 1183 } 1184 1185 sf, err := os.Open(src) 1186 if err != nil { 1187 return err 1188 } 1189 defer sf.Close() 1190 1191 // Be careful about removing/overwriting dst. 1192 // Do not remove/overwrite if dst exists and is a directory 1193 // or a non-object file. 1194 if fi, err := os.Stat(dst); err == nil { 1195 if fi.IsDir() { 1196 return fmt.Errorf("build output %q already exists and is a directory", dst) 1197 } 1198 if !force && fi.Mode().IsRegular() && !isObject(dst) { 1199 return fmt.Errorf("build output %q already exists and is not an object file", dst) 1200 } 1201 } 1202 1203 // On Windows, remove lingering ~ file from last attempt. 1204 if base.ToolIsWindows { 1205 if _, err := os.Stat(dst + "~"); err == nil { 1206 os.Remove(dst + "~") 1207 } 1208 } 1209 1210 mayberemovefile(dst) 1211 df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 1212 if err != nil && base.ToolIsWindows { 1213 // Windows does not allow deletion of a binary file 1214 // while it is executing. Try to move it out of the way. 1215 // If the move fails, which is likely, we'll try again the 1216 // next time we do an install of this binary. 1217 if err := os.Rename(dst, dst+"~"); err == nil { 1218 os.Remove(dst + "~") 1219 } 1220 df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 1221 } 1222 if err != nil { 1223 return err 1224 } 1225 1226 _, err = io.Copy(df, sf) 1227 df.Close() 1228 if err != nil { 1229 mayberemovefile(dst) 1230 return fmt.Errorf("copying %s to %s: %v", src, dst, err) 1231 } 1232 return nil 1233 } 1234 1235 // writeFile writes the text to file. 1236 func (b *Builder) writeFile(file string, text []byte) error { 1237 if cfg.BuildN || cfg.BuildX { 1238 b.Showcmd("", "cat >%s << 'EOF' # internal\n%sEOF", file, text) 1239 } 1240 if cfg.BuildN { 1241 return nil 1242 } 1243 return ioutil.WriteFile(file, text, 0666) 1244 } 1245 1246 // Install the cgo export header file, if there is one. 1247 func (b *Builder) installHeader(a *Action) error { 1248 src := a.Objdir + "_cgo_install.h" 1249 if _, err := os.Stat(src); os.IsNotExist(err) { 1250 // If the file does not exist, there are no exported 1251 // functions, and we do not install anything. 1252 // TODO(rsc): Once we know that caching is rebuilding 1253 // at the right times (not missing rebuilds), here we should 1254 // probably delete the installed header, if any. 1255 if cfg.BuildX { 1256 b.Showcmd("", "# %s not created", src) 1257 } 1258 return nil 1259 } 1260 1261 dir, _ := filepath.Split(a.Target) 1262 if dir != "" { 1263 if err := b.Mkdir(dir); err != nil { 1264 return err 1265 } 1266 } 1267 1268 return b.moveOrCopyFile(a, a.Target, src, 0666, true) 1269 } 1270 1271 // cover runs, in effect, 1272 // go tool cover -mode=b.coverMode -var="varName" -o dst.go src.go 1273 func (b *Builder) cover(a *Action, dst, src string, perm os.FileMode, varName string) error { 1274 return b.run(a, a.Objdir, "cover "+a.Package.ImportPath, nil, 1275 cfg.BuildToolexec, 1276 base.Tool("cover"), 1277 "-mode", a.Package.Internal.CoverMode, 1278 "-var", varName, 1279 "-o", dst, 1280 src) 1281 } 1282 1283 var objectMagic = [][]byte{ 1284 {'!', '<', 'a', 'r', 'c', 'h', '>', '\n'}, // Package archive 1285 {'\x7F', 'E', 'L', 'F'}, // ELF 1286 {0xFE, 0xED, 0xFA, 0xCE}, // Mach-O big-endian 32-bit 1287 {0xFE, 0xED, 0xFA, 0xCF}, // Mach-O big-endian 64-bit 1288 {0xCE, 0xFA, 0xED, 0xFE}, // Mach-O little-endian 32-bit 1289 {0xCF, 0xFA, 0xED, 0xFE}, // Mach-O little-endian 64-bit 1290 {0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00}, // PE (Windows) as generated by 6l/8l and gcc 1291 {0x00, 0x00, 0x01, 0xEB}, // Plan 9 i386 1292 {0x00, 0x00, 0x8a, 0x97}, // Plan 9 amd64 1293 {0x00, 0x00, 0x06, 0x47}, // Plan 9 arm 1294 } 1295 1296 func isObject(s string) bool { 1297 f, err := os.Open(s) 1298 if err != nil { 1299 return false 1300 } 1301 defer f.Close() 1302 buf := make([]byte, 64) 1303 io.ReadFull(f, buf) 1304 for _, magic := range objectMagic { 1305 if bytes.HasPrefix(buf, magic) { 1306 return true 1307 } 1308 } 1309 return false 1310 } 1311 1312 // mayberemovefile removes a file only if it is a regular file 1313 // When running as a user with sufficient privileges, we may delete 1314 // even device files, for example, which is not intended. 1315 func mayberemovefile(s string) { 1316 if fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() { 1317 return 1318 } 1319 os.Remove(s) 1320 } 1321 1322 // fmtcmd formats a command in the manner of fmt.Sprintf but also: 1323 // 1324 // If dir is non-empty and the script is not in dir right now, 1325 // fmtcmd inserts "cd dir\n" before the command. 1326 // 1327 // fmtcmd replaces the value of b.WorkDir with $WORK. 1328 // fmtcmd replaces the value of goroot with $GOROOT. 1329 // fmtcmd replaces the value of b.gobin with $GOBIN. 1330 // 1331 // fmtcmd replaces the name of the current directory with dot (.) 1332 // but only when it is at the beginning of a space-separated token. 1333 // 1334 func (b *Builder) fmtcmd(dir string, format string, args ...interface{}) string { 1335 cmd := fmt.Sprintf(format, args...) 1336 if dir != "" && dir != "/" { 1337 cmd = strings.Replace(" "+cmd, " "+dir, " .", -1)[1:] 1338 if b.scriptDir != dir { 1339 b.scriptDir = dir 1340 cmd = "cd " + dir + "\n" + cmd 1341 } 1342 } 1343 if b.WorkDir != "" { 1344 cmd = strings.Replace(cmd, b.WorkDir, "$WORK", -1) 1345 } 1346 return cmd 1347 } 1348 1349 // showcmd prints the given command to standard output 1350 // for the implementation of -n or -x. 1351 func (b *Builder) Showcmd(dir string, format string, args ...interface{}) { 1352 b.output.Lock() 1353 defer b.output.Unlock() 1354 b.Print(b.fmtcmd(dir, format, args...) + "\n") 1355 } 1356 1357 // showOutput prints "# desc" followed by the given output. 1358 // The output is expected to contain references to 'dir', usually 1359 // the source directory for the package that has failed to build. 1360 // showOutput rewrites mentions of dir with a relative path to dir 1361 // when the relative path is shorter. This is usually more pleasant. 1362 // For example, if fmt doesn't compile and we are in src/html, 1363 // the output is 1364 // 1365 // $ go build 1366 // # fmt 1367 // ../fmt/print.go:1090: undefined: asdf 1368 // $ 1369 // 1370 // instead of 1371 // 1372 // $ go build 1373 // # fmt 1374 // /usr/gopher/go/src/fmt/print.go:1090: undefined: asdf 1375 // $ 1376 // 1377 // showOutput also replaces references to the work directory with $WORK. 1378 // 1379 // If a is not nil and a.output is not nil, showOutput appends to that slice instead of 1380 // printing to b.Print. 1381 // 1382 func (b *Builder) showOutput(a *Action, dir, desc, out string) { 1383 prefix := "# " + desc 1384 suffix := "\n" + out 1385 if reldir := base.ShortPath(dir); reldir != dir { 1386 suffix = strings.Replace(suffix, " "+dir, " "+reldir, -1) 1387 suffix = strings.Replace(suffix, "\n"+dir, "\n"+reldir, -1) 1388 } 1389 suffix = strings.Replace(suffix, " "+b.WorkDir, " $WORK", -1) 1390 1391 if a != nil && a.output != nil { 1392 a.output = append(a.output, prefix...) 1393 a.output = append(a.output, suffix...) 1394 return 1395 } 1396 1397 b.output.Lock() 1398 defer b.output.Unlock() 1399 b.Print(prefix, suffix) 1400 } 1401 1402 // errPrintedOutput is a special error indicating that a command failed 1403 // but that it generated output as well, and that output has already 1404 // been printed, so there's no point showing 'exit status 1' or whatever 1405 // the wait status was. The main executor, builder.do, knows not to 1406 // print this error. 1407 var errPrintedOutput = errors.New("already printed output - no need to show error") 1408 1409 var cgoLine = regexp.MustCompile(`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]`) 1410 var cgoTypeSigRe = regexp.MustCompile(`\b_C2?(type|func|var|macro)_\B`) 1411 1412 // run runs the command given by cmdline in the directory dir. 1413 // If the command fails, run prints information about the failure 1414 // and returns a non-nil error. 1415 func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs ...interface{}) error { 1416 out, err := b.runOut(dir, desc, env, cmdargs...) 1417 if len(out) > 0 { 1418 if desc == "" { 1419 desc = b.fmtcmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " ")) 1420 } 1421 b.showOutput(a, dir, desc, b.processOutput(out)) 1422 if err != nil { 1423 err = errPrintedOutput 1424 } 1425 } 1426 return err 1427 } 1428 1429 // processOutput prepares the output of runOut to be output to the console. 1430 func (b *Builder) processOutput(out []byte) string { 1431 if out[len(out)-1] != '\n' { 1432 out = append(out, '\n') 1433 } 1434 messages := string(out) 1435 // Fix up output referring to cgo-generated code to be more readable. 1436 // Replace x.go:19[/tmp/.../x.cgo1.go:18] with x.go:19. 1437 // Replace *[100]_Ctype_foo with *[100]C.foo. 1438 // If we're using -x, assume we're debugging and want the full dump, so disable the rewrite. 1439 if !cfg.BuildX && cgoLine.MatchString(messages) { 1440 messages = cgoLine.ReplaceAllString(messages, "") 1441 messages = cgoTypeSigRe.ReplaceAllString(messages, "C.") 1442 } 1443 return messages 1444 } 1445 1446 // runOut runs the command given by cmdline in the directory dir. 1447 // It returns the command output and any errors that occurred. 1448 func (b *Builder) runOut(dir string, desc string, env []string, cmdargs ...interface{}) ([]byte, error) { 1449 cmdline := str.StringList(cmdargs...) 1450 if cfg.BuildN || cfg.BuildX { 1451 var envcmdline string 1452 for _, e := range env { 1453 if j := strings.IndexByte(e, '='); j != -1 { 1454 if strings.ContainsRune(e[j+1:], '\'') { 1455 envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:]) 1456 } else { 1457 envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:]) 1458 } 1459 envcmdline += " " 1460 } 1461 } 1462 envcmdline += joinUnambiguously(cmdline) 1463 b.Showcmd(dir, "%s", envcmdline) 1464 if cfg.BuildN { 1465 return nil, nil 1466 } 1467 } 1468 1469 var buf bytes.Buffer 1470 cmd := exec.Command(cmdline[0], cmdline[1:]...) 1471 cmd.Stdout = &buf 1472 cmd.Stderr = &buf 1473 cmd.Dir = dir 1474 cmd.Env = base.MergeEnvLists(env, base.EnvForDir(cmd.Dir, os.Environ())) 1475 err := cmd.Run() 1476 1477 // err can be something like 'exit status 1'. 1478 // Add information about what program was running. 1479 // Note that if buf.Bytes() is non-empty, the caller usually 1480 // shows buf.Bytes() and does not print err at all, so the 1481 // prefix here does not make most output any more verbose. 1482 if err != nil { 1483 err = errors.New(cmdline[0] + ": " + err.Error()) 1484 } 1485 return buf.Bytes(), err 1486 } 1487 1488 // joinUnambiguously prints the slice, quoting where necessary to make the 1489 // output unambiguous. 1490 // TODO: See issue 5279. The printing of commands needs a complete redo. 1491 func joinUnambiguously(a []string) string { 1492 var buf bytes.Buffer 1493 for i, s := range a { 1494 if i > 0 { 1495 buf.WriteByte(' ') 1496 } 1497 q := strconv.Quote(s) 1498 if s == "" || strings.Contains(s, " ") || len(q) > len(s)+2 { 1499 buf.WriteString(q) 1500 } else { 1501 buf.WriteString(s) 1502 } 1503 } 1504 return buf.String() 1505 } 1506 1507 // mkdir makes the named directory. 1508 func (b *Builder) Mkdir(dir string) error { 1509 // Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "". 1510 if dir == "" { 1511 return nil 1512 } 1513 1514 b.exec.Lock() 1515 defer b.exec.Unlock() 1516 // We can be a little aggressive about being 1517 // sure directories exist. Skip repeated calls. 1518 if b.mkdirCache[dir] { 1519 return nil 1520 } 1521 b.mkdirCache[dir] = true 1522 1523 if cfg.BuildN || cfg.BuildX { 1524 b.Showcmd("", "mkdir -p %s", dir) 1525 if cfg.BuildN { 1526 return nil 1527 } 1528 } 1529 1530 if err := os.MkdirAll(dir, 0777); err != nil { 1531 return err 1532 } 1533 return nil 1534 } 1535 1536 // symlink creates a symlink newname -> oldname. 1537 func (b *Builder) Symlink(oldname, newname string) error { 1538 if cfg.BuildN || cfg.BuildX { 1539 b.Showcmd("", "ln -s %s %s", oldname, newname) 1540 if cfg.BuildN { 1541 return nil 1542 } 1543 } 1544 return os.Symlink(oldname, newname) 1545 } 1546 1547 // mkAbs returns an absolute path corresponding to 1548 // evaluating f in the directory dir. 1549 // We always pass absolute paths of source files so that 1550 // the error messages will include the full path to a file 1551 // in need of attention. 1552 func mkAbs(dir, f string) string { 1553 // Leave absolute paths alone. 1554 // Also, during -n mode we use the pseudo-directory $WORK 1555 // instead of creating an actual work directory that won't be used. 1556 // Leave paths beginning with $WORK alone too. 1557 if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") { 1558 return f 1559 } 1560 return filepath.Join(dir, f) 1561 } 1562 1563 type toolchain interface { 1564 // gc runs the compiler in a specific directory on a set of files 1565 // and returns the name of the generated output file. 1566 gc(b *Builder, a *Action, archive string, importcfg []byte, asmhdr bool, gofiles []string) (ofile string, out []byte, err error) 1567 // cc runs the toolchain's C compiler in a directory on a C file 1568 // to produce an output file. 1569 cc(b *Builder, a *Action, ofile, cfile string) error 1570 // asm runs the assembler in a specific directory on specific files 1571 // and returns a list of named output files. 1572 asm(b *Builder, a *Action, sfiles []string) ([]string, error) 1573 // pack runs the archive packer in a specific directory to create 1574 // an archive from a set of object files. 1575 // typically it is run in the object directory. 1576 pack(b *Builder, a *Action, afile string, ofiles []string) error 1577 // ld runs the linker to create an executable starting at mainpkg. 1578 ld(b *Builder, root *Action, out, importcfg, mainpkg string) error 1579 // ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions 1580 ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error 1581 1582 compiler() string 1583 linker() string 1584 } 1585 1586 type noToolchain struct{} 1587 1588 func noCompiler() error { 1589 log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler) 1590 return nil 1591 } 1592 1593 func (noToolchain) compiler() string { 1594 noCompiler() 1595 return "" 1596 } 1597 1598 func (noToolchain) linker() string { 1599 noCompiler() 1600 return "" 1601 } 1602 1603 func (noToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, asmhdr bool, gofiles []string) (ofile string, out []byte, err error) { 1604 return "", nil, noCompiler() 1605 } 1606 1607 func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) { 1608 return nil, noCompiler() 1609 } 1610 1611 func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error { 1612 return noCompiler() 1613 } 1614 1615 func (noToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string) error { 1616 return noCompiler() 1617 } 1618 1619 func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error { 1620 return noCompiler() 1621 } 1622 1623 func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error { 1624 return noCompiler() 1625 } 1626 1627 // gcc runs the gcc C compiler to create an object from a single C file. 1628 func (b *Builder) gcc(a *Action, p *load.Package, workdir, out string, flags []string, cfile string) error { 1629 return b.ccompile(a, p, out, flags, cfile, b.GccCmd(p.Dir, workdir)) 1630 } 1631 1632 // gxx runs the g++ C++ compiler to create an object from a single C++ file. 1633 func (b *Builder) gxx(a *Action, p *load.Package, workdir, out string, flags []string, cxxfile string) error { 1634 return b.ccompile(a, p, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir)) 1635 } 1636 1637 // gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file. 1638 func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flags []string, ffile string) error { 1639 return b.ccompile(a, p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir)) 1640 } 1641 1642 // ccompile runs the given C or C++ compiler and creates an object from a single source file. 1643 func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []string, file string, compiler []string) error { 1644 file = mkAbs(p.Dir, file) 1645 desc := p.ImportPath 1646 if !filepath.IsAbs(outfile) { 1647 outfile = filepath.Join(p.Dir, outfile) 1648 } 1649 output, err := b.runOut(filepath.Dir(file), desc, nil, compiler, flags, "-o", outfile, "-c", filepath.Base(file)) 1650 if len(output) > 0 { 1651 // On FreeBSD 11, when we pass -g to clang 3.8 it 1652 // invokes its internal assembler with -dwarf-version=2. 1653 // When it sees .section .note.GNU-stack, it warns 1654 // "DWARF2 only supports one section per compilation unit". 1655 // This warning makes no sense, since the section is empty, 1656 // but it confuses people. 1657 // We work around the problem by detecting the warning 1658 // and dropping -g and trying again. 1659 if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) { 1660 newFlags := make([]string, 0, len(flags)) 1661 for _, f := range flags { 1662 if !strings.HasPrefix(f, "-g") { 1663 newFlags = append(newFlags, f) 1664 } 1665 } 1666 if len(newFlags) < len(flags) { 1667 return b.ccompile(a, p, outfile, newFlags, file, compiler) 1668 } 1669 } 1670 1671 b.showOutput(a, p.Dir, desc, b.processOutput(output)) 1672 if err != nil { 1673 err = errPrintedOutput 1674 } else if os.Getenv("GO_BUILDER_NAME") != "" { 1675 return errors.New("C compiler warning promoted to error on Go builders") 1676 } 1677 } 1678 return err 1679 } 1680 1681 // gccld runs the gcc linker to create an executable from a set of object files. 1682 func (b *Builder) gccld(p *load.Package, objdir, out string, flags []string, objs []string) error { 1683 var cmd []string 1684 if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 { 1685 cmd = b.GxxCmd(p.Dir, objdir) 1686 } else { 1687 cmd = b.GccCmd(p.Dir, objdir) 1688 } 1689 return b.run(nil, p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags) 1690 } 1691 1692 // Grab these before main helpfully overwrites them. 1693 var ( 1694 origCC = os.Getenv("CC") 1695 origCXX = os.Getenv("CXX") 1696 ) 1697 1698 // gccCmd returns a gcc command line prefix 1699 // defaultCC is defined in zdefaultcc.go, written by cmd/dist. 1700 func (b *Builder) GccCmd(incdir, workdir string) []string { 1701 return b.compilerCmd(b.ccExe(), incdir, workdir) 1702 } 1703 1704 // gxxCmd returns a g++ command line prefix 1705 // defaultCXX is defined in zdefaultcc.go, written by cmd/dist. 1706 func (b *Builder) GxxCmd(incdir, workdir string) []string { 1707 return b.compilerCmd(b.cxxExe(), incdir, workdir) 1708 } 1709 1710 // gfortranCmd returns a gfortran command line prefix. 1711 func (b *Builder) gfortranCmd(incdir, workdir string) []string { 1712 return b.compilerCmd(b.fcExe(), incdir, workdir) 1713 } 1714 1715 // ccExe returns the CC compiler setting without all the extra flags we add implicitly. 1716 func (b *Builder) ccExe() []string { 1717 return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch)) 1718 } 1719 1720 // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly. 1721 func (b *Builder) cxxExe() []string { 1722 return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch)) 1723 } 1724 1725 // fcExe returns the FC compiler setting without all the extra flags we add implicitly. 1726 func (b *Builder) fcExe() []string { 1727 return b.compilerExe(os.Getenv("FC"), "gfortran") 1728 } 1729 1730 // compilerExe returns the compiler to use given an 1731 // environment variable setting (the value not the name) 1732 // and a default. The resulting slice is usually just the name 1733 // of the compiler but can have additional arguments if they 1734 // were present in the environment value. 1735 // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"]. 1736 func (b *Builder) compilerExe(envValue string, def string) []string { 1737 compiler := strings.Fields(envValue) 1738 if len(compiler) == 0 { 1739 compiler = []string{def} 1740 } 1741 return compiler 1742 } 1743 1744 // compilerCmd returns a command line prefix for the given environment 1745 // variable and using the default command when the variable is empty. 1746 func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string { 1747 // NOTE: env.go's mkEnv knows that the first three 1748 // strings returned are "gcc", "-I", incdir (and cuts them off). 1749 a := []string{compiler[0], "-I", incdir} 1750 a = append(a, compiler[1:]...) 1751 1752 // Definitely want -fPIC but on Windows gcc complains 1753 // "-fPIC ignored for target (all code is position independent)" 1754 if cfg.Goos != "windows" { 1755 a = append(a, "-fPIC") 1756 } 1757 a = append(a, b.gccArchArgs()...) 1758 // gcc-4.5 and beyond require explicit "-pthread" flag 1759 // for multithreading with pthread library. 1760 if cfg.BuildContext.CgoEnabled { 1761 switch cfg.Goos { 1762 case "windows": 1763 a = append(a, "-mthreads") 1764 default: 1765 a = append(a, "-pthread") 1766 } 1767 } 1768 1769 // disable ASCII art in clang errors, if possible 1770 if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") { 1771 a = append(a, "-fno-caret-diagnostics") 1772 } 1773 // clang is too smart about command-line arguments 1774 if b.gccSupportsFlag(compiler, "-Qunused-arguments") { 1775 a = append(a, "-Qunused-arguments") 1776 } 1777 1778 // disable word wrapping in error messages 1779 a = append(a, "-fmessage-length=0") 1780 1781 // Tell gcc not to include the work directory in object files. 1782 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { 1783 if workdir == "" { 1784 workdir = b.WorkDir 1785 } 1786 workdir = strings.TrimSuffix(workdir, string(filepath.Separator)) 1787 a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build") 1788 } 1789 1790 // Tell gcc not to include flags in object files, which defeats the 1791 // point of -fdebug-prefix-map above. 1792 if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") { 1793 a = append(a, "-gno-record-gcc-switches") 1794 } 1795 1796 // On OS X, some of the compilers behave as if -fno-common 1797 // is always set, and the Mach-O linker in 6l/8l assumes this. 1798 // See https://golang.org/issue/3253. 1799 if cfg.Goos == "darwin" { 1800 a = append(a, "-fno-common") 1801 } 1802 1803 return a 1804 } 1805 1806 // gccNoPie returns the flag to use to request non-PIE. On systems 1807 // with PIE (position independent executables) enabled by default, 1808 // -no-pie must be passed when doing a partial link with -Wl,-r. 1809 // But -no-pie is not supported by all compilers, and clang spells it -nopie. 1810 func (b *Builder) gccNoPie(linker []string) string { 1811 if b.gccSupportsFlag(linker, "-no-pie") { 1812 return "-no-pie" 1813 } 1814 if b.gccSupportsFlag(linker, "-nopie") { 1815 return "-nopie" 1816 } 1817 return "" 1818 } 1819 1820 // gccSupportsFlag checks to see if the compiler supports a flag. 1821 func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool { 1822 key := [2]string{compiler[0], flag} 1823 1824 b.exec.Lock() 1825 defer b.exec.Unlock() 1826 if b, ok := b.flagCache[key]; ok { 1827 return b 1828 } 1829 if b.flagCache == nil { 1830 b.flagCache = make(map[[2]string]bool) 1831 } 1832 // We used to write an empty C file, but that gets complicated with 1833 // go build -n. We tried using a file that does not exist, but that 1834 // fails on systems with GCC version 4.2.1; that is the last GPLv2 1835 // version of GCC, so some systems have frozen on it. 1836 // Now we pass an empty file on stdin, which should work at least for 1837 // GCC and clang. 1838 cmdArgs := str.StringList(compiler, flag, "-c", "-x", "c", "-") 1839 if cfg.BuildN || cfg.BuildX { 1840 b.Showcmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs)) 1841 if cfg.BuildN { 1842 return false 1843 } 1844 } 1845 cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) 1846 cmd.Dir = b.WorkDir 1847 cmd.Env = base.MergeEnvLists([]string{"LC_ALL=C"}, base.EnvForDir(cmd.Dir, os.Environ())) 1848 out, _ := cmd.CombinedOutput() 1849 // GCC says "unrecognized command line option". 1850 // clang says "unknown argument". 1851 // Older versions of GCC say "unrecognised debug output level". 1852 // For -fsplit-stack GCC says "'-fsplit-stack' is not supported". 1853 supported := !bytes.Contains(out, []byte("unrecognized")) && 1854 !bytes.Contains(out, []byte("unknown")) && 1855 !bytes.Contains(out, []byte("unrecognised")) && 1856 !bytes.Contains(out, []byte("is not supported")) 1857 b.flagCache[key] = supported 1858 return supported 1859 } 1860 1861 // gccArchArgs returns arguments to pass to gcc based on the architecture. 1862 func (b *Builder) gccArchArgs() []string { 1863 switch cfg.Goarch { 1864 case "386": 1865 return []string{"-m32"} 1866 case "amd64", "amd64p32": 1867 return []string{"-m64"} 1868 case "arm": 1869 return []string{"-marm"} // not thumb 1870 case "s390x": 1871 return []string{"-m64", "-march=z196"} 1872 case "mips64", "mips64le": 1873 return []string{"-mabi=64"} 1874 case "mips", "mipsle": 1875 return []string{"-mabi=32", "-march=mips32"} 1876 } 1877 return nil 1878 } 1879 1880 // envList returns the value of the given environment variable broken 1881 // into fields, using the default value when the variable is empty. 1882 func envList(key, def string) []string { 1883 v := os.Getenv(key) 1884 if v == "" { 1885 v = def 1886 } 1887 return strings.Fields(v) 1888 } 1889 1890 // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo. 1891 func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string) { 1892 defaults := "-g -O2" 1893 1894 cppflags = str.StringList(envList("CGO_CPPFLAGS", ""), p.CgoCPPFLAGS) 1895 cflags = str.StringList(envList("CGO_CFLAGS", defaults), p.CgoCFLAGS) 1896 cxxflags = str.StringList(envList("CGO_CXXFLAGS", defaults), p.CgoCXXFLAGS) 1897 fflags = str.StringList(envList("CGO_FFLAGS", defaults), p.CgoFFLAGS) 1898 ldflags = str.StringList(envList("CGO_LDFLAGS", defaults), p.CgoLDFLAGS) 1899 return 1900 } 1901 1902 var cgoRe = regexp.MustCompile(`[/\\:]`) 1903 1904 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) { 1905 p := a.Package 1906 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.CFlags(p) 1907 cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) 1908 cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...) 1909 // If we are compiling Objective-C code, then we need to link against libobjc 1910 if len(mfiles) > 0 { 1911 cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc") 1912 } 1913 1914 // Likewise for Fortran, except there are many Fortran compilers. 1915 // Support gfortran out of the box and let others pass the correct link options 1916 // via CGO_LDFLAGS 1917 if len(ffiles) > 0 { 1918 fc := os.Getenv("FC") 1919 if fc == "" { 1920 fc = "gfortran" 1921 } 1922 if strings.Contains(fc, "gfortran") { 1923 cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran") 1924 } 1925 } 1926 1927 if cfg.BuildMSan { 1928 cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...) 1929 cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...) 1930 } 1931 1932 // Allows including _cgo_export.h from .[ch] files in the package. 1933 cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir) 1934 1935 // cgo 1936 // TODO: CGO_FLAGS? 1937 gofiles := []string{objdir + "_cgo_gotypes.go"} 1938 cfiles := []string{"_cgo_export.c"} 1939 for _, fn := range cgofiles { 1940 f := strings.TrimSuffix(filepath.Base(fn), ".go") 1941 gofiles = append(gofiles, objdir+f+".cgo1.go") 1942 cfiles = append(cfiles, f+".cgo2.c") 1943 } 1944 1945 // TODO: make cgo not depend on $GOARCH? 1946 1947 cgoflags := []string{} 1948 if p.Standard && p.ImportPath == "runtime/cgo" { 1949 cgoflags = append(cgoflags, "-import_runtime_cgo=false") 1950 } 1951 if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo") { 1952 cgoflags = append(cgoflags, "-import_syscall=false") 1953 } 1954 1955 // Update $CGO_LDFLAGS with p.CgoLDFLAGS. 1956 var cgoenv []string 1957 if len(cgoLDFLAGS) > 0 { 1958 flags := make([]string, len(cgoLDFLAGS)) 1959 for i, f := range cgoLDFLAGS { 1960 flags[i] = strconv.Quote(f) 1961 } 1962 cgoenv = []string{"CGO_LDFLAGS=" + strings.Join(flags, " ")} 1963 } 1964 1965 if cfg.BuildToolchainName == "gccgo" { 1966 switch cfg.Goarch { 1967 case "386", "amd64": 1968 cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack") 1969 } 1970 cgoflags = append(cgoflags, "-gccgo") 1971 if pkgpath := gccgoPkgpath(p); pkgpath != "" { 1972 cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath) 1973 } 1974 } 1975 1976 switch cfg.BuildBuildmode { 1977 case "c-archive", "c-shared": 1978 // Tell cgo that if there are any exported functions 1979 // it should generate a header file that C code can 1980 // #include. 1981 cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h") 1982 } 1983 1984 if err := b.run(a, p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil { 1985 return nil, nil, err 1986 } 1987 outGo = append(outGo, gofiles...) 1988 1989 // Use sequential object file names to keep them distinct 1990 // and short enough to fit in the .a header file name slots. 1991 // We no longer collect them all into _all.o, and we'd like 1992 // tools to see both the .o suffix and unique names, so 1993 // we need to make them short enough not to be truncated 1994 // in the final archive. 1995 oseq := 0 1996 nextOfile := func() string { 1997 oseq++ 1998 return objdir + fmt.Sprintf("_x%03d.o", oseq) 1999 } 2000 2001 // gcc 2002 cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS) 2003 for _, cfile := range cfiles { 2004 ofile := nextOfile() 2005 if err := b.gcc(a, p, a.Objdir, ofile, cflags, objdir+cfile); err != nil { 2006 return nil, nil, err 2007 } 2008 outObj = append(outObj, ofile) 2009 } 2010 2011 for _, file := range gccfiles { 2012 ofile := nextOfile() 2013 if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil { 2014 return nil, nil, err 2015 } 2016 outObj = append(outObj, ofile) 2017 } 2018 2019 cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS) 2020 for _, file := range gxxfiles { 2021 ofile := nextOfile() 2022 if err := b.gxx(a, p, a.Objdir, ofile, cxxflags, file); err != nil { 2023 return nil, nil, err 2024 } 2025 outObj = append(outObj, ofile) 2026 } 2027 2028 for _, file := range mfiles { 2029 ofile := nextOfile() 2030 if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil { 2031 return nil, nil, err 2032 } 2033 outObj = append(outObj, ofile) 2034 } 2035 2036 fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS) 2037 for _, file := range ffiles { 2038 ofile := nextOfile() 2039 if err := b.gfortran(a, p, a.Objdir, ofile, fflags, file); err != nil { 2040 return nil, nil, err 2041 } 2042 outObj = append(outObj, ofile) 2043 } 2044 2045 switch cfg.BuildToolchainName { 2046 case "gc": 2047 importGo := objdir + "_cgo_import.go" 2048 if err := b.dynimport(a, p, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj); err != nil { 2049 return nil, nil, err 2050 } 2051 outGo = append(outGo, importGo) 2052 2053 case "gccgo": 2054 defunC := objdir + "_cgo_defun.c" 2055 defunObj := objdir + "_cgo_defun.o" 2056 if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil { 2057 return nil, nil, err 2058 } 2059 outObj = append(outObj, defunObj) 2060 2061 default: 2062 noCompiler() 2063 } 2064 2065 return outGo, outObj, nil 2066 } 2067 2068 // dynimport creates a Go source file named importGo containing 2069 // //go:cgo_import_dynamic directives for each symbol or library 2070 // dynamically imported by the object files outObj. 2071 func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error { 2072 cfile := objdir + "_cgo_main.c" 2073 ofile := objdir + "_cgo_main.o" 2074 if err := b.gcc(a, p, objdir, ofile, cflags, cfile); err != nil { 2075 return err 2076 } 2077 2078 linkobj := str.StringList(ofile, outObj, p.SysoFiles) 2079 dynobj := objdir + "_cgo_.o" 2080 2081 // we need to use -pie for Linux/ARM to get accurate imported sym 2082 ldflags := cgoLDFLAGS 2083 if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" { 2084 ldflags = append(ldflags, "-pie") 2085 } 2086 if err := b.gccld(p, objdir, dynobj, ldflags, linkobj); err != nil { 2087 return err 2088 } 2089 2090 // cgo -dynimport 2091 var cgoflags []string 2092 if p.Standard && p.ImportPath == "runtime/cgo" { 2093 cgoflags = []string{"-dynlinker"} // record path to dynamic linker 2094 } 2095 return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags) 2096 } 2097 2098 // Run SWIG on all SWIG input files. 2099 // TODO: Don't build a shared library, once SWIG emits the necessary 2100 // pragmas for external linking. 2101 func (b *Builder) swig(a *Action, p *load.Package, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) { 2102 if err := b.swigVersionCheck(); err != nil { 2103 return nil, nil, nil, err 2104 } 2105 2106 intgosize, err := b.swigIntSize(objdir) 2107 if err != nil { 2108 return nil, nil, nil, err 2109 } 2110 2111 for _, f := range p.SwigFiles { 2112 goFile, cFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, false, intgosize) 2113 if err != nil { 2114 return nil, nil, nil, err 2115 } 2116 if goFile != "" { 2117 outGo = append(outGo, goFile) 2118 } 2119 if cFile != "" { 2120 outC = append(outC, cFile) 2121 } 2122 } 2123 for _, f := range p.SwigCXXFiles { 2124 goFile, cxxFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, true, intgosize) 2125 if err != nil { 2126 return nil, nil, nil, err 2127 } 2128 if goFile != "" { 2129 outGo = append(outGo, goFile) 2130 } 2131 if cxxFile != "" { 2132 outCXX = append(outCXX, cxxFile) 2133 } 2134 } 2135 return outGo, outC, outCXX, nil 2136 } 2137 2138 // Make sure SWIG is new enough. 2139 var ( 2140 swigCheckOnce sync.Once 2141 swigCheck error 2142 ) 2143 2144 func (b *Builder) swigDoVersionCheck() error { 2145 out, err := b.runOut("", "", nil, "swig", "-version") 2146 if err != nil { 2147 return err 2148 } 2149 re := regexp.MustCompile(`[vV]ersion +([\d]+)([.][\d]+)?([.][\d]+)?`) 2150 matches := re.FindSubmatch(out) 2151 if matches == nil { 2152 // Can't find version number; hope for the best. 2153 return nil 2154 } 2155 2156 major, err := strconv.Atoi(string(matches[1])) 2157 if err != nil { 2158 // Can't find version number; hope for the best. 2159 return nil 2160 } 2161 const errmsg = "must have SWIG version >= 3.0.6" 2162 if major < 3 { 2163 return errors.New(errmsg) 2164 } 2165 if major > 3 { 2166 // 4.0 or later 2167 return nil 2168 } 2169 2170 // We have SWIG version 3.x. 2171 if len(matches[2]) > 0 { 2172 minor, err := strconv.Atoi(string(matches[2][1:])) 2173 if err != nil { 2174 return nil 2175 } 2176 if minor > 0 { 2177 // 3.1 or later 2178 return nil 2179 } 2180 } 2181 2182 // We have SWIG version 3.0.x. 2183 if len(matches[3]) > 0 { 2184 patch, err := strconv.Atoi(string(matches[3][1:])) 2185 if err != nil { 2186 return nil 2187 } 2188 if patch < 6 { 2189 // Before 3.0.6. 2190 return errors.New(errmsg) 2191 } 2192 } 2193 2194 return nil 2195 } 2196 2197 func (b *Builder) swigVersionCheck() error { 2198 swigCheckOnce.Do(func() { 2199 swigCheck = b.swigDoVersionCheck() 2200 }) 2201 return swigCheck 2202 } 2203 2204 // Find the value to pass for the -intgosize option to swig. 2205 var ( 2206 swigIntSizeOnce sync.Once 2207 swigIntSize string 2208 swigIntSizeError error 2209 ) 2210 2211 // This code fails to build if sizeof(int) <= 32 2212 const swigIntSizeCode = ` 2213 package main 2214 const i int = 1 << 32 2215 ` 2216 2217 // Determine the size of int on the target system for the -intgosize option 2218 // of swig >= 2.0.9. Run only once. 2219 func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) { 2220 if cfg.BuildN { 2221 return "$INTBITS", nil 2222 } 2223 src := filepath.Join(b.WorkDir, "swig_intsize.go") 2224 if err = ioutil.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil { 2225 return 2226 } 2227 srcs := []string{src} 2228 2229 p := load.GoFilesPackage(srcs) 2230 2231 if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, false, srcs); e != nil { 2232 return "32", nil 2233 } 2234 return "64", nil 2235 } 2236 2237 // Determine the size of int on the target system for the -intgosize option 2238 // of swig >= 2.0.9. 2239 func (b *Builder) swigIntSize(objdir string) (intsize string, err error) { 2240 swigIntSizeOnce.Do(func() { 2241 swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir) 2242 }) 2243 return swigIntSize, swigIntSizeError 2244 } 2245 2246 // Run SWIG on one SWIG input file. 2247 func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) { 2248 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.CFlags(p) 2249 var cflags []string 2250 if cxx { 2251 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS) 2252 } else { 2253 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS) 2254 } 2255 2256 n := 5 // length of ".swig" 2257 if cxx { 2258 n = 8 // length of ".swigcxx" 2259 } 2260 base := file[:len(file)-n] 2261 goFile := base + ".go" 2262 gccBase := base + "_wrap." 2263 gccExt := "c" 2264 if cxx { 2265 gccExt = "cxx" 2266 } 2267 2268 gccgo := cfg.BuildToolchainName == "gccgo" 2269 2270 // swig 2271 args := []string{ 2272 "-go", 2273 "-cgo", 2274 "-intgosize", intgosize, 2275 "-module", base, 2276 "-o", objdir + gccBase + gccExt, 2277 "-outdir", objdir, 2278 } 2279 2280 for _, f := range cflags { 2281 if len(f) > 3 && f[:2] == "-I" { 2282 args = append(args, f) 2283 } 2284 } 2285 2286 if gccgo { 2287 args = append(args, "-gccgo") 2288 if pkgpath := gccgoPkgpath(p); pkgpath != "" { 2289 args = append(args, "-go-pkgpath", pkgpath) 2290 } 2291 } 2292 if cxx { 2293 args = append(args, "-c++") 2294 } 2295 2296 out, err := b.runOut(p.Dir, p.ImportPath, nil, "swig", args, file) 2297 if err != nil { 2298 if len(out) > 0 { 2299 if bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo")) { 2300 return "", "", errors.New("must have SWIG version >= 3.0.6") 2301 } 2302 b.showOutput(a, p.Dir, p.ImportPath, b.processOutput(out)) // swig error 2303 return "", "", errPrintedOutput 2304 } 2305 return "", "", err 2306 } 2307 if len(out) > 0 { 2308 b.showOutput(a, p.Dir, p.ImportPath, b.processOutput(out)) // swig warning 2309 } 2310 2311 // If the input was x.swig, the output is x.go in the objdir. 2312 // But there might be an x.go in the original dir too, and if it 2313 // uses cgo as well, cgo will be processing both and will 2314 // translate both into x.cgo1.go in the objdir, overwriting one. 2315 // Rename x.go to _x_swig.go to avoid this problem. 2316 // We ignore files in the original dir that begin with underscore 2317 // so _x_swig.go cannot conflict with an original file we were 2318 // going to compile. 2319 goFile = objdir + goFile 2320 newGoFile := objdir + "_" + base + "_swig.go" 2321 if err := os.Rename(goFile, newGoFile); err != nil { 2322 return "", "", err 2323 } 2324 return newGoFile, objdir + gccBase + gccExt, nil 2325 } 2326 2327 // disableBuildID adjusts a linker command line to avoid creating a 2328 // build ID when creating an object file rather than an executable or 2329 // shared library. Some systems, such as Ubuntu, always add 2330 // --build-id to every link, but we don't want a build ID when we are 2331 // producing an object file. On some of those system a plain -r (not 2332 // -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a 2333 // plain -r. I don't know how to turn off --build-id when using clang 2334 // other than passing a trailing --build-id=none. So that is what we 2335 // do, but only on systems likely to support it, which is to say, 2336 // systems that normally use gold or the GNU linker. 2337 func (b *Builder) disableBuildID(ldflags []string) []string { 2338 switch cfg.Goos { 2339 case "android", "dragonfly", "linux", "netbsd": 2340 ldflags = append(ldflags, "-Wl,--build-id=none") 2341 } 2342 return ldflags 2343 } 2344 2345 // mkAbsFiles converts files into a list of absolute files, 2346 // assuming they were originally relative to dir, 2347 // and returns that new list. 2348 func mkAbsFiles(dir string, files []string) []string { 2349 abs := make([]string, len(files)) 2350 for i, f := range files { 2351 if !filepath.IsAbs(f) { 2352 f = filepath.Join(dir, f) 2353 } 2354 abs[i] = f 2355 } 2356 return abs 2357 }