Source file src/cmd/go/internal/work/action.go

     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 creation (planning).
     6  
     7  package work
     8  
     9  import (
    10  	"bufio"
    11  	"bytes"
    12  	"cmd/internal/cov/covcmd"
    13  	"cmd/internal/par"
    14  	"container/heap"
    15  	"context"
    16  	"debug/elf"
    17  	"encoding/json"
    18  	"fmt"
    19  	"internal/platform"
    20  	"os"
    21  	"path/filepath"
    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  	"cmd/go/internal/trace"
    32  	"cmd/internal/buildid"
    33  	"cmd/internal/robustio"
    34  )
    35  
    36  // A Builder holds global state about a build.
    37  // It does not hold per-package state, because we
    38  // build packages in parallel, and the builder is shared.
    39  type Builder struct {
    40  	WorkDir            string                    // the temporary work directory (ends in filepath.Separator)
    41  	actionCache        map[cacheKey]*Action      // a cache of already-constructed actions
    42  	flagCache          map[[2]string]bool        // a cache of supported compiler flags
    43  	gccCompilerIDCache map[string]cache.ActionID // cache for gccCompilerID
    44  
    45  	IsCmdList           bool // running as part of go list; set p.Stale and additional fields below
    46  	NeedError           bool // list needs p.Error
    47  	NeedExport          bool // list needs p.Export
    48  	NeedCompiledGoFiles bool // list needs p.CompiledGoFiles
    49  	AllowErrors         bool // errors don't immediately exit the program
    50  
    51  	objdirSeq int // counter for NewObjdir
    52  	pkgSeq    int
    53  
    54  	backgroundSh *Shell // Shell that per-Action Shells are derived from
    55  
    56  	exec      sync.Mutex
    57  	readySema chan bool
    58  	ready     actionQueue
    59  
    60  	id             sync.Mutex
    61  	toolIDCache    par.Cache[string, string] // tool name -> tool ID
    62  	gccToolIDCache map[string]string         // tool name -> tool ID
    63  	buildIDCache   map[string]string         // file name -> build ID
    64  }
    65  
    66  // NOTE: Much of Action would not need to be exported if not for test.
    67  // Maybe test functionality should move into this package too?
    68  
    69  // An Actor runs an action.
    70  type Actor interface {
    71  	Act(*Builder, context.Context, *Action) error
    72  }
    73  
    74  // An ActorFunc is an Actor that calls the function.
    75  type ActorFunc func(*Builder, context.Context, *Action) error
    76  
    77  func (f ActorFunc) Act(b *Builder, ctx context.Context, a *Action) error {
    78  	return f(b, ctx, a)
    79  }
    80  
    81  // An Action represents a single action in the action graph.
    82  type Action struct {
    83  	Mode       string        // description of action operation
    84  	Package    *load.Package // the package this action works on
    85  	Deps       []*Action     // actions that must happen before this one
    86  	Actor      Actor         // the action itself (nil = no-op)
    87  	IgnoreFail bool          // whether to run f even if dependencies fail
    88  	TestOutput *bytes.Buffer // test output buffer
    89  	Args       []string      // additional args for runProgram
    90  
    91  	triggers []*Action // inverse of deps
    92  
    93  	buggyInstall bool // is this a buggy install (see -linkshared)?
    94  
    95  	TryCache func(*Builder, *Action) bool // callback for cache bypass
    96  
    97  	CacheExecutable bool // Whether to cache executables produced by link steps
    98  
    99  	// Generated files, directories.
   100  	Objdir   string         // directory for intermediate objects
   101  	Target   string         // goal of the action: the created package or executable
   102  	built    string         // the actual created package or executable
   103  	actionID cache.ActionID // cache ID of action input
   104  	buildID  string         // build ID of action output
   105  
   106  	VetxOnly  bool       // Mode=="vet": only being called to supply info about dependencies
   107  	needVet   bool       // Mode=="build": need to fill in vet config
   108  	needBuild bool       // Mode=="build": need to do actual build (can be false if needVet is true)
   109  	vetCfg    *vetConfig // vet config
   110  	output    []byte     // output redirect buffer (nil means use b.Print)
   111  
   112  	sh *Shell // lazily created per-Action shell; see Builder.Shell
   113  
   114  	// Execution state.
   115  	pending      int               // number of deps yet to complete
   116  	priority     int               // relative execution priority
   117  	Failed       *Action           // set to root cause if the action failed
   118  	json         *actionJSON       // action graph information
   119  	nonGoOverlay map[string]string // map from non-.go source files to copied files in objdir. Nil if no overlay is used.
   120  	traceSpan    *trace.Span
   121  }
   122  
   123  // BuildActionID returns the action ID section of a's build ID.
   124  func (a *Action) BuildActionID() string { return actionID(a.buildID) }
   125  
   126  // BuildContentID returns the content ID section of a's build ID.
   127  func (a *Action) BuildContentID() string { return contentID(a.buildID) }
   128  
   129  // BuildID returns a's build ID.
   130  func (a *Action) BuildID() string { return a.buildID }
   131  
   132  // BuiltTarget returns the actual file that was built. This differs
   133  // from Target when the result was cached.
   134  func (a *Action) BuiltTarget() string { return a.built }
   135  
   136  // An actionQueue is a priority queue of actions.
   137  type actionQueue []*Action
   138  
   139  // Implement heap.Interface
   140  func (q *actionQueue) Len() int           { return len(*q) }
   141  func (q *actionQueue) Swap(i, j int)      { (*q)[i], (*q)[j] = (*q)[j], (*q)[i] }
   142  func (q *actionQueue) Less(i, j int) bool { return (*q)[i].priority < (*q)[j].priority }
   143  func (q *actionQueue) Push(x any)         { *q = append(*q, x.(*Action)) }
   144  func (q *actionQueue) Pop() any {
   145  	n := len(*q) - 1
   146  	x := (*q)[n]
   147  	*q = (*q)[:n]
   148  	return x
   149  }
   150  
   151  func (q *actionQueue) push(a *Action) {
   152  	if a.json != nil {
   153  		a.json.TimeReady = time.Now()
   154  	}
   155  	heap.Push(q, a)
   156  }
   157  
   158  func (q *actionQueue) pop() *Action {
   159  	return heap.Pop(q).(*Action)
   160  }
   161  
   162  type actionJSON struct {
   163  	ID         int
   164  	Mode       string
   165  	Package    string
   166  	Deps       []int     `json:",omitempty"`
   167  	IgnoreFail bool      `json:",omitempty"`
   168  	Args       []string  `json:",omitempty"`
   169  	Link       bool      `json:",omitempty"`
   170  	Objdir     string    `json:",omitempty"`
   171  	Target     string    `json:",omitempty"`
   172  	Priority   int       `json:",omitempty"`
   173  	Failed     bool      `json:",omitempty"`
   174  	Built      string    `json:",omitempty"`
   175  	VetxOnly   bool      `json:",omitempty"`
   176  	NeedVet    bool      `json:",omitempty"`
   177  	NeedBuild  bool      `json:",omitempty"`
   178  	ActionID   string    `json:",omitempty"`
   179  	BuildID    string    `json:",omitempty"`
   180  	TimeReady  time.Time `json:",omitempty"`
   181  	TimeStart  time.Time `json:",omitempty"`
   182  	TimeDone   time.Time `json:",omitempty"`
   183  
   184  	Cmd     []string      // `json:",omitempty"`
   185  	CmdReal time.Duration `json:",omitempty"`
   186  	CmdUser time.Duration `json:",omitempty"`
   187  	CmdSys  time.Duration `json:",omitempty"`
   188  }
   189  
   190  // cacheKey is the key for the action cache.
   191  type cacheKey struct {
   192  	mode string
   193  	p    *load.Package
   194  }
   195  
   196  func actionGraphJSON(a *Action) string {
   197  	var workq []*Action
   198  	var inWorkq = make(map[*Action]int)
   199  
   200  	add := func(a *Action) {
   201  		if _, ok := inWorkq[a]; ok {
   202  			return
   203  		}
   204  		inWorkq[a] = len(workq)
   205  		workq = append(workq, a)
   206  	}
   207  	add(a)
   208  
   209  	for i := 0; i < len(workq); i++ {
   210  		for _, dep := range workq[i].Deps {
   211  			add(dep)
   212  		}
   213  	}
   214  
   215  	list := make([]*actionJSON, 0, len(workq))
   216  	for id, a := range workq {
   217  		if a.json == nil {
   218  			a.json = &actionJSON{
   219  				Mode:       a.Mode,
   220  				ID:         id,
   221  				IgnoreFail: a.IgnoreFail,
   222  				Args:       a.Args,
   223  				Objdir:     a.Objdir,
   224  				Target:     a.Target,
   225  				Failed:     a.Failed != nil,
   226  				Priority:   a.priority,
   227  				Built:      a.built,
   228  				VetxOnly:   a.VetxOnly,
   229  				NeedBuild:  a.needBuild,
   230  				NeedVet:    a.needVet,
   231  			}
   232  			if a.Package != nil {
   233  				// TODO(rsc): Make this a unique key for a.Package somehow.
   234  				a.json.Package = a.Package.ImportPath
   235  			}
   236  			for _, a1 := range a.Deps {
   237  				a.json.Deps = append(a.json.Deps, inWorkq[a1])
   238  			}
   239  		}
   240  		list = append(list, a.json)
   241  	}
   242  
   243  	js, err := json.MarshalIndent(list, "", "\t")
   244  	if err != nil {
   245  		fmt.Fprintf(os.Stderr, "go: writing debug action graph: %v\n", err)
   246  		return ""
   247  	}
   248  	return string(js)
   249  }
   250  
   251  // BuildMode specifies the build mode:
   252  // are we just building things or also installing the results?
   253  type BuildMode int
   254  
   255  const (
   256  	ModeBuild BuildMode = iota
   257  	ModeInstall
   258  	ModeBuggyInstall
   259  
   260  	ModeVetOnly = 1 << 8
   261  )
   262  
   263  // NewBuilder returns a new Builder ready for use.
   264  //
   265  // If workDir is the empty string, NewBuilder creates a WorkDir if needed
   266  // and arranges for it to be removed in case of an unclean exit.
   267  // The caller must Close the builder explicitly to clean up the WorkDir
   268  // before a clean exit.
   269  func NewBuilder(workDir string) *Builder {
   270  	b := new(Builder)
   271  
   272  	b.actionCache = make(map[cacheKey]*Action)
   273  	b.gccToolIDCache = make(map[string]string)
   274  	b.buildIDCache = make(map[string]string)
   275  
   276  	printWorkDir := false
   277  	if workDir != "" {
   278  		b.WorkDir = workDir
   279  	} else if cfg.BuildN {
   280  		b.WorkDir = "$WORK"
   281  	} else {
   282  		if !buildInitStarted {
   283  			panic("internal error: NewBuilder called before BuildInit")
   284  		}
   285  		tmp, err := os.MkdirTemp(cfg.Getenv("GOTMPDIR"), "go-build")
   286  		if err != nil {
   287  			base.Fatalf("go: creating work dir: %v", err)
   288  		}
   289  		if !filepath.IsAbs(tmp) {
   290  			abs, err := filepath.Abs(tmp)
   291  			if err != nil {
   292  				os.RemoveAll(tmp)
   293  				base.Fatalf("go: creating work dir: %v", err)
   294  			}
   295  			tmp = abs
   296  		}
   297  		b.WorkDir = tmp
   298  		builderWorkDirs.Store(b, b.WorkDir)
   299  		printWorkDir = cfg.BuildX || cfg.BuildWork
   300  	}
   301  
   302  	b.backgroundSh = NewShell(b.WorkDir, nil)
   303  
   304  	if printWorkDir {
   305  		b.BackgroundShell().Printf("WORK=%s\n", b.WorkDir)
   306  	}
   307  
   308  	if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil {
   309  		fmt.Fprintf(os.Stderr, "go: %v\n", err)
   310  		base.SetExitStatus(2)
   311  		base.Exit()
   312  	}
   313  
   314  	for _, tag := range cfg.BuildContext.BuildTags {
   315  		if strings.Contains(tag, ",") {
   316  			fmt.Fprintf(os.Stderr, "go: -tags space-separated list contains comma\n")
   317  			base.SetExitStatus(2)
   318  			base.Exit()
   319  		}
   320  	}
   321  
   322  	return b
   323  }
   324  
   325  var builderWorkDirs sync.Map // *Builder → WorkDir
   326  
   327  func (b *Builder) Close() error {
   328  	wd, ok := builderWorkDirs.Load(b)
   329  	if !ok {
   330  		return nil
   331  	}
   332  	defer builderWorkDirs.Delete(b)
   333  
   334  	if b.WorkDir != wd.(string) {
   335  		base.Errorf("go: internal error: Builder WorkDir unexpectedly changed from %s to %s", wd, b.WorkDir)
   336  	}
   337  
   338  	if !cfg.BuildWork {
   339  		if err := robustio.RemoveAll(b.WorkDir); err != nil {
   340  			return err
   341  		}
   342  	}
   343  	b.WorkDir = ""
   344  	return nil
   345  }
   346  
   347  func closeBuilders() {
   348  	leakedBuilders := 0
   349  	builderWorkDirs.Range(func(bi, _ any) bool {
   350  		leakedBuilders++
   351  		if err := bi.(*Builder).Close(); err != nil {
   352  			base.Error(err)
   353  		}
   354  		return true
   355  	})
   356  
   357  	if leakedBuilders > 0 && base.GetExitStatus() == 0 {
   358  		fmt.Fprintf(os.Stderr, "go: internal error: Builder leaked on successful exit\n")
   359  		base.SetExitStatus(1)
   360  	}
   361  }
   362  
   363  func CheckGOOSARCHPair(goos, goarch string) error {
   364  	if !platform.BuildModeSupported(cfg.BuildContext.Compiler, "default", goos, goarch) {
   365  		return fmt.Errorf("unsupported GOOS/GOARCH pair %s/%s", goos, goarch)
   366  	}
   367  	return nil
   368  }
   369  
   370  // NewObjdir returns the name of a fresh object directory under b.WorkDir.
   371  // It is up to the caller to call b.Mkdir on the result at an appropriate time.
   372  // The result ends in a slash, so that file names in that directory
   373  // can be constructed with direct string addition.
   374  //
   375  // NewObjdir must be called only from a single goroutine at a time,
   376  // so it is safe to call during action graph construction, but it must not
   377  // be called during action graph execution.
   378  func (b *Builder) NewObjdir() string {
   379  	b.objdirSeq++
   380  	return str.WithFilePathSeparator(filepath.Join(b.WorkDir, fmt.Sprintf("b%03d", b.objdirSeq)))
   381  }
   382  
   383  // readpkglist returns the list of packages that were built into the shared library
   384  // at shlibpath. For the native toolchain this list is stored, newline separated, in
   385  // an ELF note with name "Go\x00\x00" and type 1. For GCCGO it is extracted from the
   386  // .go_export section.
   387  func readpkglist(shlibpath string) (pkgs []*load.Package) {
   388  	var stk load.ImportStack
   389  	if cfg.BuildToolchainName == "gccgo" {
   390  		f, err := elf.Open(shlibpath)
   391  		if err != nil {
   392  			base.Fatal(fmt.Errorf("failed to open shared library: %v", err))
   393  		}
   394  		defer f.Close()
   395  		sect := f.Section(".go_export")
   396  		if sect == nil {
   397  			base.Fatal(fmt.Errorf("%s: missing .go_export section", shlibpath))
   398  		}
   399  		data, err := sect.Data()
   400  		if err != nil {
   401  			base.Fatal(fmt.Errorf("%s: failed to read .go_export section: %v", shlibpath, err))
   402  		}
   403  		pkgpath := []byte("pkgpath ")
   404  		for _, line := range bytes.Split(data, []byte{'\n'}) {
   405  			if path, found := bytes.CutPrefix(line, pkgpath); found {
   406  				path = bytes.TrimSuffix(path, []byte{';'})
   407  				pkgs = append(pkgs, load.LoadPackageWithFlags(string(path), base.Cwd(), &stk, nil, 0))
   408  			}
   409  		}
   410  	} else {
   411  		pkglistbytes, err := buildid.ReadELFNote(shlibpath, "Go\x00\x00", 1)
   412  		if err != nil {
   413  			base.Fatalf("readELFNote failed: %v", err)
   414  		}
   415  		scanner := bufio.NewScanner(bytes.NewBuffer(pkglistbytes))
   416  		for scanner.Scan() {
   417  			t := scanner.Text()
   418  			pkgs = append(pkgs, load.LoadPackageWithFlags(t, base.Cwd(), &stk, nil, 0))
   419  		}
   420  	}
   421  	return
   422  }
   423  
   424  // cacheAction looks up {mode, p} in the cache and returns the resulting action.
   425  // If the cache has no such action, f() is recorded and returned.
   426  // TODO(rsc): Change the second key from *load.Package to interface{},
   427  // to make the caching in linkShared less awkward?
   428  func (b *Builder) cacheAction(mode string, p *load.Package, f func() *Action) *Action {
   429  	a := b.actionCache[cacheKey{mode, p}]
   430  	if a == nil {
   431  		a = f()
   432  		b.actionCache[cacheKey{mode, p}] = a
   433  	}
   434  	return a
   435  }
   436  
   437  // AutoAction returns the "right" action for go build or go install of p.
   438  func (b *Builder) AutoAction(mode, depMode BuildMode, p *load.Package) *Action {
   439  	if p.Name == "main" {
   440  		return b.LinkAction(mode, depMode, p)
   441  	}
   442  	return b.CompileAction(mode, depMode, p)
   443  }
   444  
   445  // buildActor implements the Actor interface for package build
   446  // actions. For most package builds this simply means invoking th
   447  // *Builder.build method; in the case of "go test -cover" for
   448  // a package with no test files, we stores some additional state
   449  // information in the build actor to help with reporting.
   450  type buildActor struct {
   451  	// name of static meta-data file fragment emitted by the cover
   452  	// tool as part of the package build action, for selected
   453  	// "go test -cover" runs.
   454  	covMetaFileName string
   455  }
   456  
   457  // newBuildActor returns a new buildActor object, setting up the
   458  // covMetaFileName field if 'genCoverMeta' flag is set.
   459  func newBuildActor(p *load.Package, genCoverMeta bool) *buildActor {
   460  	ba := &buildActor{}
   461  	if genCoverMeta {
   462  		ba.covMetaFileName = covcmd.MetaFileForPackage(p.ImportPath)
   463  	}
   464  	return ba
   465  }
   466  
   467  func (ba *buildActor) Act(b *Builder, ctx context.Context, a *Action) error {
   468  	return b.build(ctx, a)
   469  }
   470  
   471  // pgoActionID computes the action ID for a preprocess PGO action.
   472  func (b *Builder) pgoActionID(input string) cache.ActionID {
   473  	h := cache.NewHash("preprocess PGO profile " + input)
   474  
   475  	fmt.Fprintf(h, "preprocess PGO profile\n")
   476  	fmt.Fprintf(h, "preprofile %s\n", b.toolID("preprofile"))
   477  	fmt.Fprintf(h, "input %q\n", b.fileHash(input))
   478  
   479  	return h.Sum()
   480  }
   481  
   482  // pgoActor implements the Actor interface for preprocessing PGO profiles.
   483  type pgoActor struct {
   484  	// input is the path to the original pprof profile.
   485  	input string
   486  }
   487  
   488  func (p *pgoActor) Act(b *Builder, ctx context.Context, a *Action) error {
   489  	if b.useCache(a, b.pgoActionID(p.input), a.Target, !b.IsCmdList) || b.IsCmdList {
   490  		return nil
   491  	}
   492  	defer b.flushOutput(a)
   493  
   494  	sh := b.Shell(a)
   495  
   496  	if err := sh.Mkdir(a.Objdir); err != nil {
   497  		return err
   498  	}
   499  
   500  	if err := sh.run(".", p.input, nil, cfg.BuildToolexec, base.Tool("preprofile"), "-o", a.Target, "-i", p.input); err != nil {
   501  		return err
   502  	}
   503  
   504  	// N.B. Builder.build looks for the out in a.built, regardless of
   505  	// whether this came from cache.
   506  	a.built = a.Target
   507  
   508  	if !cfg.BuildN {
   509  		// Cache the output.
   510  		//
   511  		// N.B. We don't use updateBuildID here, as preprocessed PGO profiles
   512  		// do not contain a build ID. updateBuildID is typically responsible
   513  		// for adding to the cache, thus we must do so ourselves instead.
   514  
   515  		r, err := os.Open(a.Target)
   516  		if err != nil {
   517  			return fmt.Errorf("error opening target for caching: %w", err)
   518  		}
   519  
   520  		c := cache.Default()
   521  		outputID, _, err := c.Put(a.actionID, r)
   522  		r.Close()
   523  		if err != nil {
   524  			return fmt.Errorf("error adding target to cache: %w", err)
   525  		}
   526  		if cfg.BuildX {
   527  			sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cp", a.Target, c.OutputFile(outputID))))
   528  		}
   529  	}
   530  
   531  	return nil
   532  }
   533  
   534  // CompileAction returns the action for compiling and possibly installing
   535  // (according to mode) the given package. The resulting action is only
   536  // for building packages (archives), never for linking executables.
   537  // depMode is the action (build or install) to use when building dependencies.
   538  // To turn package main into an executable, call b.Link instead.
   539  func (b *Builder) CompileAction(mode, depMode BuildMode, p *load.Package) *Action {
   540  	vetOnly := mode&ModeVetOnly != 0
   541  	mode &^= ModeVetOnly
   542  
   543  	if mode != ModeBuild && p.Target == "" {
   544  		// No permanent target.
   545  		mode = ModeBuild
   546  	}
   547  	if mode != ModeBuild && p.Name == "main" {
   548  		// We never install the .a file for a main package.
   549  		mode = ModeBuild
   550  	}
   551  
   552  	// Construct package build action.
   553  	a := b.cacheAction("build", p, func() *Action {
   554  		a := &Action{
   555  			Mode:    "build",
   556  			Package: p,
   557  			Actor:   newBuildActor(p, p.Internal.Cover.GenMeta),
   558  			Objdir:  b.NewObjdir(),
   559  		}
   560  
   561  		if p.Error == nil || !p.Error.IsImportCycle {
   562  			for _, p1 := range p.Internal.Imports {
   563  				a.Deps = append(a.Deps, b.CompileAction(depMode, depMode, p1))
   564  			}
   565  		}
   566  
   567  		if p.Internal.PGOProfile != "" {
   568  			pgoAction := b.cacheAction("preprocess PGO profile "+p.Internal.PGOProfile, nil, func() *Action {
   569  				a := &Action{
   570  					Mode:   "preprocess PGO profile",
   571  					Actor:  &pgoActor{input: p.Internal.PGOProfile},
   572  					Objdir: b.NewObjdir(),
   573  				}
   574  				a.Target = filepath.Join(a.Objdir, "pgo.preprofile")
   575  
   576  				return a
   577  			})
   578  			a.Deps = append(a.Deps, pgoAction)
   579  		}
   580  
   581  		if p.Standard {
   582  			switch p.ImportPath {
   583  			case "builtin", "unsafe":
   584  				// Fake packages - nothing to build.
   585  				a.Mode = "built-in package"
   586  				a.Actor = nil
   587  				return a
   588  			}
   589  
   590  			// gccgo standard library is "fake" too.
   591  			if cfg.BuildToolchainName == "gccgo" {
   592  				// the target name is needed for cgo.
   593  				a.Mode = "gccgo stdlib"
   594  				a.Target = p.Target
   595  				a.Actor = nil
   596  				return a
   597  			}
   598  		}
   599  
   600  		return a
   601  	})
   602  
   603  	// Find the build action; the cache entry may have been replaced
   604  	// by the install action during (*Builder).installAction.
   605  	buildAction := a
   606  	switch buildAction.Mode {
   607  	case "build", "built-in package", "gccgo stdlib":
   608  		// ok
   609  	case "build-install":
   610  		buildAction = a.Deps[0]
   611  	default:
   612  		panic("lost build action: " + buildAction.Mode)
   613  	}
   614  	buildAction.needBuild = buildAction.needBuild || !vetOnly
   615  
   616  	// Construct install action.
   617  	if mode == ModeInstall || mode == ModeBuggyInstall {
   618  		a = b.installAction(a, mode)
   619  	}
   620  
   621  	return a
   622  }
   623  
   624  // VetAction returns the action for running go vet on package p.
   625  // It depends on the action for compiling p.
   626  // If the caller may be causing p to be installed, it is up to the caller
   627  // to make sure that the install depends on (runs after) vet.
   628  func (b *Builder) VetAction(mode, depMode BuildMode, p *load.Package) *Action {
   629  	a := b.vetAction(mode, depMode, p)
   630  	a.VetxOnly = false
   631  	return a
   632  }
   633  
   634  func (b *Builder) vetAction(mode, depMode BuildMode, p *load.Package) *Action {
   635  	// Construct vet action.
   636  	a := b.cacheAction("vet", p, func() *Action {
   637  		a1 := b.CompileAction(mode|ModeVetOnly, depMode, p)
   638  
   639  		// vet expects to be able to import "fmt".
   640  		var stk load.ImportStack
   641  		stk.Push(load.NewImportInfo("vet", nil))
   642  		p1, err := load.LoadImportWithFlags("fmt", p.Dir, p, &stk, nil, 0)
   643  		if err != nil {
   644  			base.Fatalf("unexpected error loading fmt package from package %s: %v", p.ImportPath, err)
   645  		}
   646  		stk.Pop()
   647  		aFmt := b.CompileAction(ModeBuild, depMode, p1)
   648  
   649  		var deps []*Action
   650  		if a1.buggyInstall {
   651  			// (*Builder).vet expects deps[0] to be the package
   652  			// and deps[1] to be "fmt". If we see buggyInstall
   653  			// here then a1 is an install of a shared library,
   654  			// and the real package is a1.Deps[0].
   655  			deps = []*Action{a1.Deps[0], aFmt, a1}
   656  		} else {
   657  			deps = []*Action{a1, aFmt}
   658  		}
   659  		for _, p1 := range p.Internal.Imports {
   660  			deps = append(deps, b.vetAction(mode, depMode, p1))
   661  		}
   662  
   663  		a := &Action{
   664  			Mode:       "vet",
   665  			Package:    p,
   666  			Deps:       deps,
   667  			Objdir:     a1.Objdir,
   668  			VetxOnly:   true,
   669  			IgnoreFail: true, // it's OK if vet of dependencies "fails" (reports problems)
   670  		}
   671  		if a1.Actor == nil {
   672  			// Built-in packages like unsafe.
   673  			return a
   674  		}
   675  		deps[0].needVet = true
   676  		a.Actor = ActorFunc((*Builder).vet)
   677  		return a
   678  	})
   679  	return a
   680  }
   681  
   682  // LinkAction returns the action for linking p into an executable
   683  // and possibly installing the result (according to mode).
   684  // depMode is the action (build or install) to use when compiling dependencies.
   685  func (b *Builder) LinkAction(mode, depMode BuildMode, p *load.Package) *Action {
   686  	// Construct link action.
   687  	a := b.cacheAction("link", p, func() *Action {
   688  		a := &Action{
   689  			Mode:    "link",
   690  			Package: p,
   691  		}
   692  
   693  		a1 := b.CompileAction(ModeBuild, depMode, p)
   694  		a.Actor = ActorFunc((*Builder).link)
   695  		a.Deps = []*Action{a1}
   696  		a.Objdir = a1.Objdir
   697  
   698  		// An executable file. (This is the name of a temporary file.)
   699  		// Because we run the temporary file in 'go run' and 'go test',
   700  		// the name will show up in ps listings. If the caller has specified
   701  		// a name, use that instead of a.out. The binary is generated
   702  		// in an otherwise empty subdirectory named exe to avoid
   703  		// naming conflicts. The only possible conflict is if we were
   704  		// to create a top-level package named exe.
   705  		name := "a.out"
   706  		if p.Internal.ExeName != "" {
   707  			name = p.Internal.ExeName
   708  		} else if (cfg.Goos == "darwin" || cfg.Goos == "windows") && cfg.BuildBuildmode == "c-shared" && p.Target != "" {
   709  			// On OS X, the linker output name gets recorded in the
   710  			// shared library's LC_ID_DYLIB load command.
   711  			// The code invoking the linker knows to pass only the final
   712  			// path element. Arrange that the path element matches what
   713  			// we'll install it as; otherwise the library is only loadable as "a.out".
   714  			// On Windows, DLL file name is recorded in PE file
   715  			// export section, so do like on OS X.
   716  			_, name = filepath.Split(p.Target)
   717  		}
   718  		a.Target = a.Objdir + filepath.Join("exe", name) + cfg.ExeSuffix
   719  		a.built = a.Target
   720  		b.addTransitiveLinkDeps(a, a1, "")
   721  
   722  		// Sequence the build of the main package (a1) strictly after the build
   723  		// of all other dependencies that go into the link. It is likely to be after
   724  		// them anyway, but just make sure. This is required by the build ID-based
   725  		// shortcut in (*Builder).useCache(a1), which will call b.linkActionID(a).
   726  		// In order for that linkActionID call to compute the right action ID, all the
   727  		// dependencies of a (except a1) must have completed building and have
   728  		// recorded their build IDs.
   729  		a1.Deps = append(a1.Deps, &Action{Mode: "nop", Deps: a.Deps[1:]})
   730  		return a
   731  	})
   732  
   733  	if mode == ModeInstall || mode == ModeBuggyInstall {
   734  		a = b.installAction(a, mode)
   735  	}
   736  
   737  	return a
   738  }
   739  
   740  // installAction returns the action for installing the result of a1.
   741  func (b *Builder) installAction(a1 *Action, mode BuildMode) *Action {
   742  	// Because we overwrite the build action with the install action below,
   743  	// a1 may already be an install action fetched from the "build" cache key,
   744  	// and the caller just doesn't realize.
   745  	if strings.HasSuffix(a1.Mode, "-install") {
   746  		if a1.buggyInstall && mode == ModeInstall {
   747  			//  Congratulations! The buggy install is now a proper install.
   748  			a1.buggyInstall = false
   749  		}
   750  		return a1
   751  	}
   752  
   753  	// If there's no actual action to build a1,
   754  	// there's nothing to install either.
   755  	// This happens if a1 corresponds to reusing an already-built object.
   756  	if a1.Actor == nil {
   757  		return a1
   758  	}
   759  
   760  	p := a1.Package
   761  	return b.cacheAction(a1.Mode+"-install", p, func() *Action {
   762  		// The install deletes the temporary build result,
   763  		// so we need all other actions, both past and future,
   764  		// that attempt to depend on the build to depend instead
   765  		// on the install.
   766  
   767  		// Make a private copy of a1 (the build action),
   768  		// no longer accessible to any other rules.
   769  		buildAction := new(Action)
   770  		*buildAction = *a1
   771  
   772  		// Overwrite a1 with the install action.
   773  		// This takes care of updating past actions that
   774  		// point at a1 for the build action; now they will
   775  		// point at a1 and get the install action.
   776  		// We also leave a1 in the action cache as the result
   777  		// for "build", so that actions not yet created that
   778  		// try to depend on the build will instead depend
   779  		// on the install.
   780  		*a1 = Action{
   781  			Mode:    buildAction.Mode + "-install",
   782  			Actor:   ActorFunc(BuildInstallFunc),
   783  			Package: p,
   784  			Objdir:  buildAction.Objdir,
   785  			Deps:    []*Action{buildAction},
   786  			Target:  p.Target,
   787  			built:   p.Target,
   788  
   789  			buggyInstall: mode == ModeBuggyInstall,
   790  		}
   791  
   792  		b.addInstallHeaderAction(a1)
   793  		return a1
   794  	})
   795  }
   796  
   797  // addTransitiveLinkDeps adds to the link action a all packages
   798  // that are transitive dependencies of a1.Deps.
   799  // That is, if a is a link of package main, a1 is the compile of package main
   800  // and a1.Deps is the actions for building packages directly imported by
   801  // package main (what the compiler needs). The linker needs all packages
   802  // transitively imported by the whole program; addTransitiveLinkDeps
   803  // makes sure those are present in a.Deps.
   804  // If shlib is non-empty, then a corresponds to the build and installation of shlib,
   805  // so any rebuild of shlib should not be added as a dependency.
   806  func (b *Builder) addTransitiveLinkDeps(a, a1 *Action, shlib string) {
   807  	// Expand Deps to include all built packages, for the linker.
   808  	// Use breadth-first search to find rebuilt-for-test packages
   809  	// before the standard ones.
   810  	// TODO(rsc): Eliminate the standard ones from the action graph,
   811  	// which will require doing a little bit more rebuilding.
   812  	workq := []*Action{a1}
   813  	haveDep := map[string]bool{}
   814  	if a1.Package != nil {
   815  		haveDep[a1.Package.ImportPath] = true
   816  	}
   817  	for i := 0; i < len(workq); i++ {
   818  		a1 := workq[i]
   819  		for _, a2 := range a1.Deps {
   820  			// TODO(rsc): Find a better discriminator than the Mode strings, once the dust settles.
   821  			if a2.Package == nil || (a2.Mode != "build-install" && a2.Mode != "build") || haveDep[a2.Package.ImportPath] {
   822  				continue
   823  			}
   824  			haveDep[a2.Package.ImportPath] = true
   825  			a.Deps = append(a.Deps, a2)
   826  			if a2.Mode == "build-install" {
   827  				a2 = a2.Deps[0] // walk children of "build" action
   828  			}
   829  			workq = append(workq, a2)
   830  		}
   831  	}
   832  
   833  	// If this is go build -linkshared, then the link depends on the shared libraries
   834  	// in addition to the packages themselves. (The compile steps do not.)
   835  	if cfg.BuildLinkshared {
   836  		haveShlib := map[string]bool{shlib: true}
   837  		for _, a1 := range a.Deps {
   838  			p1 := a1.Package
   839  			if p1 == nil || p1.Shlib == "" || haveShlib[filepath.Base(p1.Shlib)] {
   840  				continue
   841  			}
   842  			haveShlib[filepath.Base(p1.Shlib)] = true
   843  			// TODO(rsc): The use of ModeInstall here is suspect, but if we only do ModeBuild,
   844  			// we'll end up building an overall library or executable that depends at runtime
   845  			// on other libraries that are out-of-date, which is clearly not good either.
   846  			// We call it ModeBuggyInstall to make clear that this is not right.
   847  			a.Deps = append(a.Deps, b.linkSharedAction(ModeBuggyInstall, ModeBuggyInstall, p1.Shlib, nil))
   848  		}
   849  	}
   850  }
   851  
   852  // addInstallHeaderAction adds an install header action to a, if needed.
   853  // The action a should be an install action as generated by either
   854  // b.CompileAction or b.LinkAction with mode=ModeInstall,
   855  // and so a.Deps[0] is the corresponding build action.
   856  func (b *Builder) addInstallHeaderAction(a *Action) {
   857  	// Install header for cgo in c-archive and c-shared modes.
   858  	p := a.Package
   859  	if p.UsesCgo() && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") {
   860  		hdrTarget := a.Target[:len(a.Target)-len(filepath.Ext(a.Target))] + ".h"
   861  		if cfg.BuildContext.Compiler == "gccgo" && cfg.BuildO == "" {
   862  			// For the header file, remove the "lib"
   863  			// added by go/build, so we generate pkg.h
   864  			// rather than libpkg.h.
   865  			dir, file := filepath.Split(hdrTarget)
   866  			file = strings.TrimPrefix(file, "lib")
   867  			hdrTarget = filepath.Join(dir, file)
   868  		}
   869  		ah := &Action{
   870  			Mode:    "install header",
   871  			Package: a.Package,
   872  			Deps:    []*Action{a.Deps[0]},
   873  			Actor:   ActorFunc((*Builder).installHeader),
   874  			Objdir:  a.Deps[0].Objdir,
   875  			Target:  hdrTarget,
   876  		}
   877  		a.Deps = append(a.Deps, ah)
   878  	}
   879  }
   880  
   881  // buildmodeShared takes the "go build" action a1 into the building of a shared library of a1.Deps.
   882  // That is, the input a1 represents "go build pkgs" and the result represents "go build -buildmode=shared pkgs".
   883  func (b *Builder) buildmodeShared(mode, depMode BuildMode, args []string, pkgs []*load.Package, a1 *Action) *Action {
   884  	name, err := libname(args, pkgs)
   885  	if err != nil {
   886  		base.Fatalf("%v", err)
   887  	}
   888  	return b.linkSharedAction(mode, depMode, name, a1)
   889  }
   890  
   891  // linkSharedAction takes a grouping action a1 corresponding to a list of built packages
   892  // and returns an action that links them together into a shared library with the name shlib.
   893  // If a1 is nil, shlib should be an absolute path to an existing shared library,
   894  // and then linkSharedAction reads that library to find out the package list.
   895  func (b *Builder) linkSharedAction(mode, depMode BuildMode, shlib string, a1 *Action) *Action {
   896  	fullShlib := shlib
   897  	shlib = filepath.Base(shlib)
   898  	a := b.cacheAction("build-shlib "+shlib, nil, func() *Action {
   899  		if a1 == nil {
   900  			// TODO(rsc): Need to find some other place to store config,
   901  			// not in pkg directory. See golang.org/issue/22196.
   902  			pkgs := readpkglist(fullShlib)
   903  			a1 = &Action{
   904  				Mode: "shlib packages",
   905  			}
   906  			for _, p := range pkgs {
   907  				a1.Deps = append(a1.Deps, b.CompileAction(mode, depMode, p))
   908  			}
   909  		}
   910  
   911  		// Fake package to hold ldflags.
   912  		// As usual shared libraries are a kludgy, abstraction-violating special case:
   913  		// we let them use the flags specified for the command-line arguments.
   914  		p := &load.Package{}
   915  		p.Internal.CmdlinePkg = true
   916  		p.Internal.Ldflags = load.BuildLdflags.For(p)
   917  		p.Internal.Gccgoflags = load.BuildGccgoflags.For(p)
   918  
   919  		// Add implicit dependencies to pkgs list.
   920  		// Currently buildmode=shared forces external linking mode, and
   921  		// external linking mode forces an import of runtime/cgo (and
   922  		// math on arm). So if it was not passed on the command line and
   923  		// it is not present in another shared library, add it here.
   924  		// TODO(rsc): Maybe this should only happen if "runtime" is in the original package set.
   925  		// TODO(rsc): This should probably be changed to use load.LinkerDeps(p).
   926  		// TODO(rsc): We don't add standard library imports for gccgo
   927  		// because they are all always linked in anyhow.
   928  		// Maybe load.LinkerDeps should be used and updated.
   929  		a := &Action{
   930  			Mode:    "go build -buildmode=shared",
   931  			Package: p,
   932  			Objdir:  b.NewObjdir(),
   933  			Actor:   ActorFunc((*Builder).linkShared),
   934  			Deps:    []*Action{a1},
   935  		}
   936  		a.Target = filepath.Join(a.Objdir, shlib)
   937  		if cfg.BuildToolchainName != "gccgo" {
   938  			add := func(a1 *Action, pkg string, force bool) {
   939  				for _, a2 := range a1.Deps {
   940  					if a2.Package != nil && a2.Package.ImportPath == pkg {
   941  						return
   942  					}
   943  				}
   944  				var stk load.ImportStack
   945  				p := load.LoadPackageWithFlags(pkg, base.Cwd(), &stk, nil, 0)
   946  				if p.Error != nil {
   947  					base.Fatalf("load %s: %v", pkg, p.Error)
   948  				}
   949  				// Assume that if pkg (runtime/cgo or math)
   950  				// is already accounted for in a different shared library,
   951  				// then that shared library also contains runtime,
   952  				// so that anything we do will depend on that library,
   953  				// so we don't need to include pkg in our shared library.
   954  				if force || p.Shlib == "" || filepath.Base(p.Shlib) == pkg {
   955  					a1.Deps = append(a1.Deps, b.CompileAction(depMode, depMode, p))
   956  				}
   957  			}
   958  			add(a1, "runtime/cgo", false)
   959  			if cfg.Goarch == "arm" {
   960  				add(a1, "math", false)
   961  			}
   962  
   963  			// The linker step still needs all the usual linker deps.
   964  			// (For example, the linker always opens runtime.a.)
   965  			ldDeps, err := load.LinkerDeps(nil)
   966  			if err != nil {
   967  				base.Error(err)
   968  			}
   969  			for _, dep := range ldDeps {
   970  				add(a, dep, true)
   971  			}
   972  		}
   973  		b.addTransitiveLinkDeps(a, a1, shlib)
   974  		return a
   975  	})
   976  
   977  	// Install result.
   978  	if (mode == ModeInstall || mode == ModeBuggyInstall) && a.Actor != nil {
   979  		buildAction := a
   980  
   981  		a = b.cacheAction("install-shlib "+shlib, nil, func() *Action {
   982  			// Determine the eventual install target.
   983  			// The install target is root/pkg/shlib, where root is the source root
   984  			// in which all the packages lie.
   985  			// TODO(rsc): Perhaps this cross-root check should apply to the full
   986  			// transitive package dependency list, not just the ones named
   987  			// on the command line?
   988  			pkgDir := a1.Deps[0].Package.Internal.Build.PkgTargetRoot
   989  			for _, a2 := range a1.Deps {
   990  				if dir := a2.Package.Internal.Build.PkgTargetRoot; dir != pkgDir {
   991  					base.Fatalf("installing shared library: cannot use packages %s and %s from different roots %s and %s",
   992  						a1.Deps[0].Package.ImportPath,
   993  						a2.Package.ImportPath,
   994  						pkgDir,
   995  						dir)
   996  				}
   997  			}
   998  			// TODO(rsc): Find out and explain here why gccgo is different.
   999  			if cfg.BuildToolchainName == "gccgo" {
  1000  				pkgDir = filepath.Join(pkgDir, "shlibs")
  1001  			}
  1002  			target := filepath.Join(pkgDir, shlib)
  1003  
  1004  			a := &Action{
  1005  				Mode:   "go install -buildmode=shared",
  1006  				Objdir: buildAction.Objdir,
  1007  				Actor:  ActorFunc(BuildInstallFunc),
  1008  				Deps:   []*Action{buildAction},
  1009  				Target: target,
  1010  			}
  1011  			for _, a2 := range buildAction.Deps[0].Deps {
  1012  				p := a2.Package
  1013  				pkgTargetRoot := p.Internal.Build.PkgTargetRoot
  1014  				if pkgTargetRoot == "" {
  1015  					continue
  1016  				}
  1017  				a.Deps = append(a.Deps, &Action{
  1018  					Mode:    "shlibname",
  1019  					Package: p,
  1020  					Actor:   ActorFunc((*Builder).installShlibname),
  1021  					Target:  filepath.Join(pkgTargetRoot, p.ImportPath+".shlibname"),
  1022  					Deps:    []*Action{a.Deps[0]},
  1023  				})
  1024  			}
  1025  			return a
  1026  		})
  1027  	}
  1028  
  1029  	return a
  1030  }
  1031  

View as plain text