github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/work/action.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 creation (planning).
     6  
     7  package work
     8  
     9  import (
    10  	"github.com/shogo82148/std/bytes"
    11  	"github.com/shogo82148/std/context"
    12  	"github.com/shogo82148/std/sync"
    13  
    14  	"github.com/shogo82148/std/cmd/go/internal/cache"
    15  	"github.com/shogo82148/std/cmd/go/internal/load"
    16  	"github.com/shogo82148/std/cmd/go/internal/trace"
    17  )
    18  
    19  // A Builder holds global state about a build.
    20  // It does not hold per-package state, because we
    21  // build packages in parallel, and the builder is shared.
    22  type Builder struct {
    23  	WorkDir            string
    24  	actionCache        map[cacheKey]*Action
    25  	flagCache          map[[2]string]bool
    26  	gccCompilerIDCache map[string]cache.ActionID
    27  
    28  	IsCmdList           bool
    29  	NeedError           bool
    30  	NeedExport          bool
    31  	NeedCompiledGoFiles bool
    32  	AllowErrors         bool
    33  
    34  	objdirSeq int
    35  	pkgSeq    int
    36  
    37  	backgroundSh *Shell
    38  
    39  	exec      sync.Mutex
    40  	readySema chan bool
    41  	ready     actionQueue
    42  
    43  	id           sync.Mutex
    44  	toolIDCache  map[string]string
    45  	buildIDCache map[string]string
    46  }
    47  
    48  // An Actor runs an action.
    49  type Actor interface {
    50  	Act(*Builder, context.Context, *Action) error
    51  }
    52  
    53  // An ActorFunc is an Actor that calls the function.
    54  type ActorFunc func(*Builder, context.Context, *Action) error
    55  
    56  func (f ActorFunc) Act(b *Builder, ctx context.Context, a *Action) error
    57  
    58  // An Action represents a single action in the action graph.
    59  type Action struct {
    60  	Mode       string
    61  	Package    *load.Package
    62  	Deps       []*Action
    63  	Actor      Actor
    64  	IgnoreFail bool
    65  	TestOutput *bytes.Buffer
    66  	Args       []string
    67  
    68  	triggers []*Action
    69  
    70  	buggyInstall bool
    71  
    72  	TryCache func(*Builder, *Action) bool
    73  
    74  	// Generated files, directories.
    75  	Objdir   string
    76  	Target   string
    77  	built    string
    78  	actionID cache.ActionID
    79  	buildID  string
    80  
    81  	VetxOnly  bool
    82  	needVet   bool
    83  	needBuild bool
    84  	vetCfg    *vetConfig
    85  	output    []byte
    86  
    87  	sh *Shell
    88  
    89  	// Execution state.
    90  	pending      int
    91  	priority     int
    92  	Failed       bool
    93  	json         *actionJSON
    94  	nonGoOverlay map[string]string
    95  	traceSpan    *trace.Span
    96  }
    97  
    98  // BuildActionID returns the action ID section of a's build ID.
    99  func (a *Action) BuildActionID() string
   100  
   101  // BuildContentID returns the content ID section of a's build ID.
   102  func (a *Action) BuildContentID() string
   103  
   104  // BuildID returns a's build ID.
   105  func (a *Action) BuildID() string
   106  
   107  // BuiltTarget returns the actual file that was built. This differs
   108  // from Target when the result was cached.
   109  func (a *Action) BuiltTarget() string
   110  
   111  // BuildMode specifies the build mode:
   112  // are we just building things or also installing the results?
   113  type BuildMode int
   114  
   115  const (
   116  	ModeBuild BuildMode = iota
   117  	ModeInstall
   118  	ModeBuggyInstall
   119  
   120  	ModeVetOnly = 1 << 8
   121  )
   122  
   123  // NewBuilder returns a new Builder ready for use.
   124  //
   125  // If workDir is the empty string, NewBuilder creates a WorkDir if needed
   126  // and arranges for it to be removed in case of an unclean exit.
   127  // The caller must Close the builder explicitly to clean up the WorkDir
   128  // before a clean exit.
   129  func NewBuilder(workDir string) *Builder
   130  
   131  func (b *Builder) Close() error
   132  
   133  func CheckGOOSARCHPair(goos, goarch string) error
   134  
   135  // NewObjdir returns the name of a fresh object directory under b.WorkDir.
   136  // It is up to the caller to call b.Mkdir on the result at an appropriate time.
   137  // The result ends in a slash, so that file names in that directory
   138  // can be constructed with direct string addition.
   139  //
   140  // NewObjdir must be called only from a single goroutine at a time,
   141  // so it is safe to call during action graph construction, but it must not
   142  // be called during action graph execution.
   143  func (b *Builder) NewObjdir() string
   144  
   145  // AutoAction returns the "right" action for go build or go install of p.
   146  func (b *Builder) AutoAction(mode, depMode BuildMode, p *load.Package) *Action
   147  
   148  // CompileAction returns the action for compiling and possibly installing
   149  // (according to mode) the given package. The resulting action is only
   150  // for building packages (archives), never for linking executables.
   151  // depMode is the action (build or install) to use when building dependencies.
   152  // To turn package main into an executable, call b.Link instead.
   153  func (b *Builder) CompileAction(mode, depMode BuildMode, p *load.Package) *Action
   154  
   155  // VetAction returns the action for running go vet on package p.
   156  // It depends on the action for compiling p.
   157  // If the caller may be causing p to be installed, it is up to the caller
   158  // to make sure that the install depends on (runs after) vet.
   159  func (b *Builder) VetAction(mode, depMode BuildMode, p *load.Package) *Action
   160  
   161  // LinkAction returns the action for linking p into an executable
   162  // and possibly installing the result (according to mode).
   163  // depMode is the action (build or install) to use when compiling dependencies.
   164  func (b *Builder) LinkAction(mode, depMode BuildMode, p *load.Package) *Action