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

     1  // Copyright 2017 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  package work
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"os/exec"
    12  	"strings"
    13  
    14  	"cmd/go/internal/base"
    15  	"cmd/go/internal/cache"
    16  	"cmd/go/internal/cfg"
    17  	"cmd/go/internal/fsys"
    18  	"cmd/go/internal/str"
    19  	"cmd/internal/buildid"
    20  )
    21  
    22  // Build IDs
    23  //
    24  // Go packages and binaries are stamped with build IDs that record both
    25  // the action ID, which is a hash of the inputs to the action that produced
    26  // the packages or binary, and the content ID, which is a hash of the action
    27  // output, namely the archive or binary itself. The hash is the same one
    28  // used by the build artifact cache (see cmd/go/internal/cache), but
    29  // truncated when stored in packages and binaries, as the full length is not
    30  // needed and is a bit unwieldy. The precise form is
    31  //
    32  //	actionID/[.../]contentID
    33  //
    34  // where the actionID and contentID are prepared by buildid.HashToString below.
    35  // and are found by looking for the first or last slash.
    36  // Usually the buildID is simply actionID/contentID, but see below for an
    37  // exception.
    38  //
    39  // The build ID serves two primary purposes.
    40  //
    41  // 1. The action ID half allows installed packages and binaries to serve as
    42  // one-element cache entries. If we intend to build math.a with a given
    43  // set of inputs summarized in the action ID, and the installed math.a already
    44  // has that action ID, we can reuse the installed math.a instead of rebuilding it.
    45  //
    46  // 2. The content ID half allows the easy preparation of action IDs for steps
    47  // that consume a particular package or binary. The content hash of every
    48  // input file for a given action must be included in the action ID hash.
    49  // Storing the content ID in the build ID lets us read it from the file with
    50  // minimal I/O, instead of reading and hashing the entire file.
    51  // This is especially effective since packages and binaries are typically
    52  // the largest inputs to an action.
    53  //
    54  // Separating action ID from content ID is important for reproducible builds.
    55  // The compiler is compiled with itself. If an output were represented by its
    56  // own action ID (instead of content ID) when computing the action ID of
    57  // the next step in the build process, then the compiler could never have its
    58  // own input action ID as its output action ID (short of a miraculous hash collision).
    59  // Instead we use the content IDs to compute the next action ID, and because
    60  // the content IDs converge, so too do the action IDs and therefore the
    61  // build IDs and the overall compiler binary. See cmd/dist's cmdbootstrap
    62  // for the actual convergence sequence.
    63  //
    64  // The “one-element cache” purpose is a bit more complex for installed
    65  // binaries. For a binary, like cmd/gofmt, there are two steps: compile
    66  // cmd/gofmt/*.go into main.a, and then link main.a into the gofmt binary.
    67  // We do not install gofmt's main.a, only the gofmt binary. Being able to
    68  // decide that the gofmt binary is up-to-date means computing the action ID
    69  // for the final link of the gofmt binary and comparing it against the
    70  // already-installed gofmt binary. But computing the action ID for the link
    71  // means knowing the content ID of main.a, which we did not keep.
    72  // To sidestep this problem, each binary actually stores an expanded build ID:
    73  //
    74  //	actionID(binary)/actionID(main.a)/contentID(main.a)/contentID(binary)
    75  //
    76  // (Note that this can be viewed equivalently as:
    77  //
    78  //	actionID(binary)/buildID(main.a)/contentID(binary)
    79  //
    80  // Storing the buildID(main.a) in the middle lets the computations that care
    81  // about the prefix or suffix halves ignore the middle and preserves the
    82  // original build ID as a contiguous string.)
    83  //
    84  // During the build, when it's time to build main.a, the gofmt binary has the
    85  // information needed to decide whether the eventual link would produce
    86  // the same binary: if the action ID for main.a's inputs matches and then
    87  // the action ID for the link step matches when assuming the given main.a
    88  // content ID, then the binary as a whole is up-to-date and need not be rebuilt.
    89  //
    90  // This is all a bit complex and may be simplified once we can rely on the
    91  // main cache, but at least at the start we will be using the content-based
    92  // staleness determination without a cache beyond the usual installed
    93  // package and binary locations.
    94  
    95  const buildIDSeparator = "/"
    96  
    97  // actionID returns the action ID half of a build ID.
    98  func actionID(buildID string) string {
    99  	i := strings.Index(buildID, buildIDSeparator)
   100  	if i < 0 {
   101  		return buildID
   102  	}
   103  	return buildID[:i]
   104  }
   105  
   106  // contentID returns the content ID half of a build ID.
   107  func contentID(buildID string) string {
   108  	return buildID[strings.LastIndex(buildID, buildIDSeparator)+1:]
   109  }
   110  
   111  // toolID returns the unique ID to use for the current copy of the
   112  // named tool (asm, compile, cover, link).
   113  //
   114  // It is important that if the tool changes (for example a compiler bug is fixed
   115  // and the compiler reinstalled), toolID returns a different string, so that old
   116  // package archives look stale and are rebuilt (with the fixed compiler).
   117  // This suggests using a content hash of the tool binary, as stored in the build ID.
   118  //
   119  // Unfortunately, we can't just open the tool binary, because the tool might be
   120  // invoked via a wrapper program specified by -toolexec and we don't know
   121  // what the wrapper program does. In particular, we want "-toolexec toolstash"
   122  // to continue working: it does no good if "-toolexec toolstash" is executing a
   123  // stashed copy of the compiler but the go command is acting as if it will run
   124  // the standard copy of the compiler. The solution is to ask the tool binary to tell
   125  // us its own build ID using the "-V=full" flag now supported by all tools.
   126  // Then we know we're getting the build ID of the compiler that will actually run
   127  // during the build. (How does the compiler binary know its own content hash?
   128  // We store it there using updateBuildID after the standard link step.)
   129  //
   130  // A final twist is that we'd prefer to have reproducible builds for release toolchains.
   131  // It should be possible to cross-compile for Windows from either Linux or Mac
   132  // or Windows itself and produce the same binaries, bit for bit. If the tool ID,
   133  // which influences the action ID half of the build ID, is based on the content ID,
   134  // then the Linux compiler binary and Mac compiler binary will have different tool IDs
   135  // and therefore produce executables with different action IDs.
   136  // To avoid this problem, for releases we use the release version string instead
   137  // of the compiler binary's content hash. This assumes that all compilers built
   138  // on all different systems are semantically equivalent, which is of course only true
   139  // modulo bugs. (Producing the exact same executables also requires that the different
   140  // build setups agree on details like $GOROOT and file name paths, but at least the
   141  // tool IDs do not make it impossible.)
   142  func (b *Builder) toolID(name string) string {
   143  	b.id.Lock()
   144  	id := b.toolIDCache[name]
   145  	b.id.Unlock()
   146  
   147  	if id != "" {
   148  		return id
   149  	}
   150  
   151  	path := base.Tool(name)
   152  	desc := "go tool " + name
   153  
   154  	// Special case: undocumented -vettool overrides usual vet,
   155  	// for testing vet or supplying an alternative analysis tool.
   156  	if name == "vet" && VetTool != "" {
   157  		path = VetTool
   158  		desc = VetTool
   159  	}
   160  
   161  	cmdline := str.StringList(cfg.BuildToolexec, path, "-V=full")
   162  	cmd := exec.Command(cmdline[0], cmdline[1:]...)
   163  	var stdout, stderr bytes.Buffer
   164  	cmd.Stdout = &stdout
   165  	cmd.Stderr = &stderr
   166  	if err := cmd.Run(); err != nil {
   167  		if stderr.Len() > 0 {
   168  			os.Stderr.Write(stderr.Bytes())
   169  		}
   170  		base.Fatalf("go: error obtaining buildID for %s: %v", desc, err)
   171  	}
   172  
   173  	line := stdout.String()
   174  	f := strings.Fields(line)
   175  	if len(f) < 3 || f[0] != name && path != VetTool || f[1] != "version" || f[2] == "devel" && !strings.HasPrefix(f[len(f)-1], "buildID=") {
   176  		base.Fatalf("go: parsing buildID from %s -V=full: unexpected output:\n\t%s", desc, line)
   177  	}
   178  	if f[2] == "devel" {
   179  		// On the development branch, use the content ID part of the build ID.
   180  		id = contentID(f[len(f)-1])
   181  	} else {
   182  		// For a release, the output is like: "compile version go1.9.1 X:framepointer".
   183  		// Use the whole line.
   184  		id = strings.TrimSpace(line)
   185  	}
   186  
   187  	b.id.Lock()
   188  	b.toolIDCache[name] = id
   189  	b.id.Unlock()
   190  
   191  	return id
   192  }
   193  
   194  // gccToolID returns the unique ID to use for a tool that is invoked
   195  // by the GCC driver. This is used particularly for gccgo, but this can also
   196  // be used for gcc, g++, gfortran, etc.; those tools all use the GCC
   197  // driver under different names. The approach used here should also
   198  // work for sufficiently new versions of clang. Unlike toolID, the
   199  // name argument is the program to run. The language argument is the
   200  // type of input file as passed to the GCC driver's -x option.
   201  //
   202  // For these tools we have no -V=full option to dump the build ID,
   203  // but we can run the tool with -v -### to reliably get the compiler proper
   204  // and hash that. That will work in the presence of -toolexec.
   205  //
   206  // In order to get reproducible builds for released compilers, we
   207  // detect a released compiler by the absence of "experimental" in the
   208  // --version output, and in that case we just use the version string.
   209  func (b *Builder) gccToolID(name, language string) (string, error) {
   210  	key := name + "." + language
   211  	b.id.Lock()
   212  	id := b.toolIDCache[key]
   213  	b.id.Unlock()
   214  
   215  	if id != "" {
   216  		return id, nil
   217  	}
   218  
   219  	// Invoke the driver with -### to see the subcommands and the
   220  	// version strings. Use -x to set the language. Pretend to
   221  	// compile an empty file on standard input.
   222  	cmdline := str.StringList(cfg.BuildToolexec, name, "-###", "-x", language, "-c", "-")
   223  	cmd := exec.Command(cmdline[0], cmdline[1:]...)
   224  	// Force untranslated output so that we see the string "version".
   225  	cmd.Env = append(os.Environ(), "LC_ALL=C")
   226  	out, err := cmd.CombinedOutput()
   227  	if err != nil {
   228  		return "", fmt.Errorf("%s: %v; output: %q", name, err, out)
   229  	}
   230  
   231  	version := ""
   232  	lines := strings.Split(string(out), "\n")
   233  	for _, line := range lines {
   234  		if fields := strings.Fields(line); len(fields) > 1 && fields[1] == "version" {
   235  			version = line
   236  			break
   237  		}
   238  	}
   239  	if version == "" {
   240  		return "", fmt.Errorf("%s: can not find version number in %q", name, out)
   241  	}
   242  
   243  	if !strings.Contains(version, "experimental") {
   244  		// This is a release. Use this line as the tool ID.
   245  		id = version
   246  	} else {
   247  		// This is a development version. The first line with
   248  		// a leading space is the compiler proper.
   249  		compiler := ""
   250  		for _, line := range lines {
   251  			if len(line) > 1 && line[0] == ' ' {
   252  				compiler = line
   253  				break
   254  			}
   255  		}
   256  		if compiler == "" {
   257  			return "", fmt.Errorf("%s: can not find compilation command in %q", name, out)
   258  		}
   259  
   260  		fields := strings.Fields(compiler)
   261  		if len(fields) == 0 {
   262  			return "", fmt.Errorf("%s: compilation command confusion %q", name, out)
   263  		}
   264  		exe := fields[0]
   265  		if !strings.ContainsAny(exe, `/\`) {
   266  			if lp, err := exec.LookPath(exe); err == nil {
   267  				exe = lp
   268  			}
   269  		}
   270  		id, err = buildid.ReadFile(exe)
   271  		if err != nil {
   272  			return "", err
   273  		}
   274  
   275  		// If we can't find a build ID, use a hash.
   276  		if id == "" {
   277  			id = b.fileHash(exe)
   278  		}
   279  	}
   280  
   281  	b.id.Lock()
   282  	b.toolIDCache[key] = id
   283  	b.id.Unlock()
   284  
   285  	return id, nil
   286  }
   287  
   288  // Check if assembler used by gccgo is GNU as.
   289  func assemblerIsGas() bool {
   290  	cmd := exec.Command(BuildToolchain.compiler(), "-print-prog-name=as")
   291  	assembler, err := cmd.Output()
   292  	if err == nil {
   293  		cmd := exec.Command(strings.TrimSpace(string(assembler)), "--version")
   294  		out, err := cmd.Output()
   295  		return err == nil && strings.Contains(string(out), "GNU")
   296  	} else {
   297  		return false
   298  	}
   299  }
   300  
   301  // gccgoBuildIDFile creates an assembler file that records the
   302  // action's build ID in an SHF_EXCLUDE section for ELF files or
   303  // in a CSECT in XCOFF files.
   304  func (b *Builder) gccgoBuildIDFile(a *Action) (string, error) {
   305  	sfile := a.Objdir + "_buildid.s"
   306  
   307  	var buf bytes.Buffer
   308  	if cfg.Goos == "aix" {
   309  		fmt.Fprintf(&buf, "\t.csect .go.buildid[XO]\n")
   310  	} else if (cfg.Goos != "solaris" && cfg.Goos != "illumos") || assemblerIsGas() {
   311  		fmt.Fprintf(&buf, "\t"+`.section .go.buildid,"e"`+"\n")
   312  	} else if cfg.Goarch == "sparc" || cfg.Goarch == "sparc64" {
   313  		fmt.Fprintf(&buf, "\t"+`.section ".go.buildid",#exclude`+"\n")
   314  	} else { // cfg.Goarch == "386" || cfg.Goarch == "amd64"
   315  		fmt.Fprintf(&buf, "\t"+`.section .go.buildid,#exclude`+"\n")
   316  	}
   317  	fmt.Fprintf(&buf, "\t.byte ")
   318  	for i := 0; i < len(a.buildID); i++ {
   319  		if i > 0 {
   320  			if i%8 == 0 {
   321  				fmt.Fprintf(&buf, "\n\t.byte ")
   322  			} else {
   323  				fmt.Fprintf(&buf, ",")
   324  			}
   325  		}
   326  		fmt.Fprintf(&buf, "%#02x", a.buildID[i])
   327  	}
   328  	fmt.Fprintf(&buf, "\n")
   329  	if cfg.Goos != "solaris" && cfg.Goos != "illumos" && cfg.Goos != "aix" {
   330  		secType := "@progbits"
   331  		if cfg.Goarch == "arm" {
   332  			secType = "%progbits"
   333  		}
   334  		fmt.Fprintf(&buf, "\t"+`.section .note.GNU-stack,"",%s`+"\n", secType)
   335  		fmt.Fprintf(&buf, "\t"+`.section .note.GNU-split-stack,"",%s`+"\n", secType)
   336  	}
   337  
   338  	if cfg.BuildN || cfg.BuildX {
   339  		for _, line := range bytes.Split(buf.Bytes(), []byte("\n")) {
   340  			b.Showcmd("", "echo '%s' >> %s", line, sfile)
   341  		}
   342  		if cfg.BuildN {
   343  			return sfile, nil
   344  		}
   345  	}
   346  
   347  	if err := os.WriteFile(sfile, buf.Bytes(), 0666); err != nil {
   348  		return "", err
   349  	}
   350  
   351  	return sfile, nil
   352  }
   353  
   354  // buildID returns the build ID found in the given file.
   355  // If no build ID is found, buildID returns the content hash of the file.
   356  func (b *Builder) buildID(file string) string {
   357  	b.id.Lock()
   358  	id := b.buildIDCache[file]
   359  	b.id.Unlock()
   360  
   361  	if id != "" {
   362  		return id
   363  	}
   364  
   365  	id, err := buildid.ReadFile(file)
   366  	if err != nil {
   367  		id = b.fileHash(file)
   368  	}
   369  
   370  	b.id.Lock()
   371  	b.buildIDCache[file] = id
   372  	b.id.Unlock()
   373  
   374  	return id
   375  }
   376  
   377  // fileHash returns the content hash of the named file.
   378  func (b *Builder) fileHash(file string) string {
   379  	file, _ = fsys.OverlayPath(file)
   380  	sum, err := cache.FileHash(file)
   381  	if err != nil {
   382  		return ""
   383  	}
   384  	return buildid.HashToString(sum)
   385  }
   386  
   387  // useCache tries to satisfy the action a, which has action ID actionHash,
   388  // by using a cached result from an earlier build. At the moment, the only
   389  // cached result is the installed package or binary at target.
   390  // If useCache decides that the cache can be used, it sets a.buildID
   391  // and a.built for use by parent actions and then returns true.
   392  // Otherwise it sets a.buildID to a temporary build ID for use in the build
   393  // and returns false. When useCache returns false the expectation is that
   394  // the caller will build the target and then call updateBuildID to finish the
   395  // build ID computation.
   396  // When useCache returns false, it may have initiated buffering of output
   397  // during a's work. The caller should defer b.flushOutput(a), to make sure
   398  // that flushOutput is eventually called regardless of whether the action
   399  // succeeds. The flushOutput call must happen after updateBuildID.
   400  func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string) bool {
   401  	// The second half of the build ID here is a placeholder for the content hash.
   402  	// It's important that the overall buildID be unlikely verging on impossible
   403  	// to appear in the output by chance, but that should be taken care of by
   404  	// the actionID half; if it also appeared in the input that would be like an
   405  	// engineered 120-bit partial SHA256 collision.
   406  	a.actionID = actionHash
   407  	actionID := buildid.HashToString(actionHash)
   408  	if a.json != nil {
   409  		a.json.ActionID = actionID
   410  	}
   411  	contentID := actionID // temporary placeholder, likely unique
   412  	a.buildID = actionID + buildIDSeparator + contentID
   413  
   414  	// Executable binaries also record the main build ID in the middle.
   415  	// See "Build IDs" comment above.
   416  	if a.Mode == "link" {
   417  		mainpkg := a.Deps[0]
   418  		a.buildID = actionID + buildIDSeparator + mainpkg.buildID + buildIDSeparator + contentID
   419  	}
   420  
   421  	// Check to see if target exists and matches the expected action ID.
   422  	// If so, it's up to date and we can reuse it instead of rebuilding it.
   423  	var buildID string
   424  	if target != "" && !cfg.BuildA {
   425  		buildID, _ = buildid.ReadFile(target)
   426  		if strings.HasPrefix(buildID, actionID+buildIDSeparator) {
   427  			a.buildID = buildID
   428  			if a.json != nil {
   429  				a.json.BuildID = a.buildID
   430  			}
   431  			a.built = target
   432  			// Poison a.Target to catch uses later in the build.
   433  			a.Target = "DO NOT USE - " + a.Mode
   434  			return true
   435  		}
   436  	}
   437  
   438  	// Special case for building a main package: if the only thing we
   439  	// want the package for is to link a binary, and the binary is
   440  	// already up-to-date, then to avoid a rebuild, report the package
   441  	// as up-to-date as well. See "Build IDs" comment above.
   442  	// TODO(rsc): Rewrite this code to use a TryCache func on the link action.
   443  	if target != "" && !cfg.BuildA && !b.NeedExport && a.Mode == "build" && len(a.triggers) == 1 && a.triggers[0].Mode == "link" {
   444  		buildID, err := buildid.ReadFile(target)
   445  		if err == nil {
   446  			id := strings.Split(buildID, buildIDSeparator)
   447  			if len(id) == 4 && id[1] == actionID {
   448  				// Temporarily assume a.buildID is the package build ID
   449  				// stored in the installed binary, and see if that makes
   450  				// the upcoming link action ID a match. If so, report that
   451  				// we built the package, safe in the knowledge that the
   452  				// link step will not ask us for the actual package file.
   453  				// Note that (*Builder).LinkAction arranged that all of
   454  				// a.triggers[0]'s dependencies other than a are also
   455  				// dependencies of a, so that we can be sure that,
   456  				// other than a.buildID, b.linkActionID is only accessing
   457  				// build IDs of completed actions.
   458  				oldBuildID := a.buildID
   459  				a.buildID = id[1] + buildIDSeparator + id[2]
   460  				linkID := buildid.HashToString(b.linkActionID(a.triggers[0]))
   461  				if id[0] == linkID {
   462  					// Best effort attempt to display output from the compile and link steps.
   463  					// If it doesn't work, it doesn't work: reusing the cached binary is more
   464  					// important than reprinting diagnostic information.
   465  					if c := cache.Default(); c != nil {
   466  						showStdout(b, c, a.actionID, "stdout")      // compile output
   467  						showStdout(b, c, a.actionID, "link-stdout") // link output
   468  					}
   469  
   470  					// Poison a.Target to catch uses later in the build.
   471  					a.Target = "DO NOT USE - main build pseudo-cache Target"
   472  					a.built = "DO NOT USE - main build pseudo-cache built"
   473  					if a.json != nil {
   474  						a.json.BuildID = a.buildID
   475  					}
   476  					return true
   477  				}
   478  				// Otherwise restore old build ID for main build.
   479  				a.buildID = oldBuildID
   480  			}
   481  		}
   482  	}
   483  
   484  	// Special case for linking a test binary: if the only thing we
   485  	// want the binary for is to run the test, and the test result is cached,
   486  	// then to avoid the link step, report the link as up-to-date.
   487  	// We avoid the nested build ID problem in the previous special case
   488  	// by recording the test results in the cache under the action ID half.
   489  	if !cfg.BuildA && len(a.triggers) == 1 && a.triggers[0].TryCache != nil && a.triggers[0].TryCache(b, a.triggers[0]) {
   490  		// Best effort attempt to display output from the compile and link steps.
   491  		// If it doesn't work, it doesn't work: reusing the test result is more
   492  		// important than reprinting diagnostic information.
   493  		if c := cache.Default(); c != nil {
   494  			showStdout(b, c, a.Deps[0].actionID, "stdout")      // compile output
   495  			showStdout(b, c, a.Deps[0].actionID, "link-stdout") // link output
   496  		}
   497  
   498  		// Poison a.Target to catch uses later in the build.
   499  		a.Target = "DO NOT USE -  pseudo-cache Target"
   500  		a.built = "DO NOT USE - pseudo-cache built"
   501  		return true
   502  	}
   503  
   504  	if b.IsCmdList {
   505  		// Invoked during go list to compute and record staleness.
   506  		if p := a.Package; p != nil && !p.Stale {
   507  			p.Stale = true
   508  			if cfg.BuildA {
   509  				p.StaleReason = "build -a flag in use"
   510  			} else {
   511  				p.StaleReason = "build ID mismatch"
   512  				for _, p1 := range p.Internal.Imports {
   513  					if p1.Stale && p1.StaleReason != "" {
   514  						if strings.HasPrefix(p1.StaleReason, "stale dependency: ") {
   515  							p.StaleReason = p1.StaleReason
   516  							break
   517  						}
   518  						if strings.HasPrefix(p.StaleReason, "build ID mismatch") {
   519  							p.StaleReason = "stale dependency: " + p1.ImportPath
   520  						}
   521  					}
   522  				}
   523  			}
   524  		}
   525  
   526  		// Fall through to update a.buildID from the build artifact cache,
   527  		// which will affect the computation of buildIDs for targets
   528  		// higher up in the dependency graph.
   529  	}
   530  
   531  	// Check the build artifact cache.
   532  	// We treat hits in this cache as being "stale" for the purposes of go list
   533  	// (in effect, "stale" means whether p.Target is up-to-date),
   534  	// but we're still happy to use results from the build artifact cache.
   535  	if c := cache.Default(); c != nil {
   536  		if !cfg.BuildA {
   537  			if file, _, err := c.GetFile(actionHash); err == nil {
   538  				if buildID, err := buildid.ReadFile(file); err == nil {
   539  					if err := showStdout(b, c, a.actionID, "stdout"); err == nil {
   540  						a.built = file
   541  						a.Target = "DO NOT USE - using cache"
   542  						a.buildID = buildID
   543  						if a.json != nil {
   544  							a.json.BuildID = a.buildID
   545  						}
   546  						if p := a.Package; p != nil {
   547  							// Clearer than explaining that something else is stale.
   548  							p.StaleReason = "not installed but available in build cache"
   549  						}
   550  						return true
   551  					}
   552  				}
   553  			}
   554  		}
   555  
   556  		// Begin saving output for later writing to cache.
   557  		a.output = []byte{}
   558  	}
   559  
   560  	return false
   561  }
   562  
   563  func showStdout(b *Builder, c *cache.Cache, actionID cache.ActionID, key string) error {
   564  	stdout, stdoutEntry, err := c.GetBytes(cache.Subkey(actionID, key))
   565  	if err != nil {
   566  		return err
   567  	}
   568  
   569  	if len(stdout) > 0 {
   570  		if cfg.BuildX || cfg.BuildN {
   571  			b.Showcmd("", "%s  # internal", joinUnambiguously(str.StringList("cat", c.OutputFile(stdoutEntry.OutputID))))
   572  		}
   573  		if !cfg.BuildN {
   574  			b.output.Lock()
   575  			defer b.output.Unlock()
   576  			b.Print(string(stdout))
   577  		}
   578  	}
   579  	return nil
   580  }
   581  
   582  // flushOutput flushes the output being queued in a.
   583  func (b *Builder) flushOutput(a *Action) {
   584  	b.output.Lock()
   585  	defer b.output.Unlock()
   586  	b.Print(string(a.output))
   587  	a.output = nil
   588  }
   589  
   590  // updateBuildID updates the build ID in the target written by action a.
   591  // It requires that useCache was called for action a and returned false,
   592  // and that the build was then carried out and given the temporary
   593  // a.buildID to record as the build ID in the resulting package or binary.
   594  // updateBuildID computes the final content ID and updates the build IDs
   595  // in the binary.
   596  //
   597  // Keep in sync with src/cmd/buildid/buildid.go
   598  func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
   599  	if cfg.BuildX || cfg.BuildN {
   600  		if rewrite {
   601  			b.Showcmd("", "%s # internal", joinUnambiguously(str.StringList(base.Tool("buildid"), "-w", target)))
   602  		}
   603  		if cfg.BuildN {
   604  			return nil
   605  		}
   606  	}
   607  
   608  	// Cache output from compile/link, even if we don't do the rest.
   609  	if c := cache.Default(); c != nil {
   610  		switch a.Mode {
   611  		case "build":
   612  			c.PutBytes(cache.Subkey(a.actionID, "stdout"), a.output)
   613  		case "link":
   614  			// Even though we don't cache the binary, cache the linker text output.
   615  			// We might notice that an installed binary is up-to-date but still
   616  			// want to pretend to have run the linker.
   617  			// Store it under the main package's action ID
   618  			// to make it easier to find when that's all we have.
   619  			for _, a1 := range a.Deps {
   620  				if p1 := a1.Package; p1 != nil && p1.Name == "main" {
   621  					c.PutBytes(cache.Subkey(a1.actionID, "link-stdout"), a.output)
   622  					break
   623  				}
   624  			}
   625  		}
   626  	}
   627  
   628  	// Find occurrences of old ID and compute new content-based ID.
   629  	r, err := os.Open(target)
   630  	if err != nil {
   631  		return err
   632  	}
   633  	matches, hash, err := buildid.FindAndHash(r, a.buildID, 0)
   634  	r.Close()
   635  	if err != nil {
   636  		return err
   637  	}
   638  	newID := a.buildID[:strings.LastIndex(a.buildID, buildIDSeparator)] + buildIDSeparator + buildid.HashToString(hash)
   639  	if len(newID) != len(a.buildID) {
   640  		return fmt.Errorf("internal error: build ID length mismatch %q vs %q", a.buildID, newID)
   641  	}
   642  
   643  	// Replace with new content-based ID.
   644  	a.buildID = newID
   645  	if a.json != nil {
   646  		a.json.BuildID = a.buildID
   647  	}
   648  	if len(matches) == 0 {
   649  		// Assume the user specified -buildid= to override what we were going to choose.
   650  		return nil
   651  	}
   652  
   653  	if rewrite {
   654  		w, err := os.OpenFile(target, os.O_RDWR, 0)
   655  		if err != nil {
   656  			return err
   657  		}
   658  		err = buildid.Rewrite(w, matches, newID)
   659  		if err != nil {
   660  			w.Close()
   661  			return err
   662  		}
   663  		if err := w.Close(); err != nil {
   664  			return err
   665  		}
   666  	}
   667  
   668  	// Cache package builds, but not binaries (link steps).
   669  	// The expectation is that binaries are not reused
   670  	// nearly as often as individual packages, and they're
   671  	// much larger, so the cache-footprint-to-utility ratio
   672  	// of binaries is much lower for binaries.
   673  	// Not caching the link step also makes sure that repeated "go run" at least
   674  	// always rerun the linker, so that they don't get too fast.
   675  	// (We don't want people thinking go is a scripting language.)
   676  	// Note also that if we start caching binaries, then we will
   677  	// copy the binaries out of the cache to run them, and then
   678  	// that will mean the go process is itself writing a binary
   679  	// and then executing it, so we will need to defend against
   680  	// ETXTBSY problems as discussed in exec.go and golang.org/issue/22220.
   681  	if c := cache.Default(); c != nil && a.Mode == "build" {
   682  		r, err := os.Open(target)
   683  		if err == nil {
   684  			if a.output == nil {
   685  				panic("internal error: a.output not set")
   686  			}
   687  			outputID, _, err := c.Put(a.actionID, r)
   688  			r.Close()
   689  			if err == nil && cfg.BuildX {
   690  				b.Showcmd("", "%s # internal", joinUnambiguously(str.StringList("cp", target, c.OutputFile(outputID))))
   691  			}
   692  			if b.NeedExport {
   693  				if err != nil {
   694  					return err
   695  				}
   696  				a.Package.Export = c.OutputFile(outputID)
   697  				a.Package.BuildID = a.buildID
   698  			}
   699  		}
   700  	}
   701  
   702  	return nil
   703  }
   704  

View as plain text