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