Skip to content
Snippets Groups Projects
build.go 47 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2012 The Go Authors. All rights reserved.
    
    Russ Cox's avatar
    Russ Cox committed
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    )
    
    // Initialization for any invocation.
    
    Russ Cox's avatar
    Russ Cox committed
    
    // The usual variables.
    
    	goarch           string
    	gobin            string
    	gohostarch       string
    	gohostos         string
    	goos             string
    	goarm            string
    
    	go386            string
    
    	goroot           string
    	goroot_final     string
    	goextlinkenabled string
    	gogcflags        string // For running built compiler
    	goldflags        string
    	workdir          string
    	tooldir          string
    	oldgoos          string
    	oldgoarch        string
    	exe              string
    	defaultcc        map[string]string
    	defaultcxx       map[string]string
    	defaultcflags    string
    	defaultldflags   string
    	defaultpkgconfig string
    
    
    	rebuildall   bool
    	defaultclang bool
    
    	vflag int // verbosity
    
    Russ Cox's avatar
    Russ Cox committed
    
    // The known architectures.
    
    Russ Cox's avatar
    Russ Cox committed
    	"amd64",
    
    	"mips64",
    	"mips64le",
    
    Russ Cox's avatar
    Russ Cox committed
    	"ppc64",
    	"ppc64le",
    
    Russ Cox's avatar
    Russ Cox committed
    
    // The known operating systems.
    
    Russ Cox's avatar
    Russ Cox committed
    	"darwin",
    
    	"dragonfly",
    
    Cherry Zhang's avatar
    Cherry Zhang committed
    	"ios",
    
    Russ Cox's avatar
    Russ Cox committed
    	"linux",
    
    David Crawshaw's avatar
    David Crawshaw committed
    	"android",
    
    Russ Cox's avatar
    Russ Cox committed
    	"freebsd",
    
    	"nacl", // keep;
    
    Russ Cox's avatar
    Russ Cox committed
    	"netbsd",
    	"openbsd",
    	"plan9",
    	"windows",
    
    Russ Cox's avatar
    Russ Cox committed
    
    // find reports the first index of p in l[0:n], or else -1.
    
    func find(p string, l []string) int {
    	for i, s := range l {
    		if p == s {
    			return i
    		}
    	}
    	return -1
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    
    // xinit handles initialization of the various global state, like goroot and goarch.
    func xinit() {
    
    	b := os.Getenv("GOROOT")
    	if b == "" {
    
    		fatalf("$GOROOT must be set")
    
    	goroot = filepath.Clean(b)
    
    	b = os.Getenv("GOROOT_FINAL")
    	if b == "" {
    		b = goroot
    
    	goroot_final = b
    
    	b = os.Getenv("GOBIN")
    
    		b = pathf("%s/bin", goroot)
    
    	}
    	gobin = b
    
    	b = os.Getenv("GOOS")
    	if b == "" {
    		b = gohostos
    	}
    	goos = b
    	if find(goos, okgoos) < 0 {
    
    		fatalf("unknown $GOOS %s", goos)
    
    	}
    
    	b = os.Getenv("GOARM")
    	if b == "" {
    		b = xgetgoarm()
    	}
    	goarm = b
    
    
    	b = os.Getenv("GO386")
    	if b == "" {
    		b = "sse2"
    	}
    	go386 = b
    
    
    	b = os.Getenv("GOMIPS")
    	if b == "" {
    		b = "hardfloat"
    	}
    	gomips = b
    
    
    	b = os.Getenv("GOMIPS64")
    	if b == "" {
    		b = "hardfloat"
    	}
    	gomips64 = b
    
    
    	b = os.Getenv("GOPPC64")
    	if b == "" {
    		b = "power8"
    	}
    	goppc64 = b
    
    
    	if p := pathf("%s/src/all.bash", goroot); !isfile(p) {
    
    		fatalf("$GOROOT is not set correctly or not exported\n"+
    
    			"\tGOROOT=%s\n"+
    			"\t%s does not exist", goroot, p)
    	}
    
    	b = os.Getenv("GOHOSTARCH")
    	if b != "" {
    		gohostarch = b
    	}
    
    	if find(gohostarch, okgoarch) < 0 {
    
    		fatalf("unknown $GOHOSTARCH %s", gohostarch)
    
    	}
    
    	b = os.Getenv("GOARCH")
    	if b == "" {
    		b = gohostarch
    	}
    	goarch = b
    
    	if find(goarch, okgoarch) < 0 {
    
    		fatalf("unknown $GOARCH %s", goarch)
    
    	}
    
    	b = os.Getenv("GO_EXTLINK_ENABLED")
    	if b != "" {
    		if b != "0" && b != "1" {
    
    			fatalf("unknown $GO_EXTLINK_ENABLED %s", b)
    
    	gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
    
    	goldflags = os.Getenv("BOOT_GO_LDFLAGS")
    
    		cc, cxx = "clang", "clang++"
    
    	defaultcc = compilerEnv("CC", cc)
    
    	defaultcxx = compilerEnv("CXX", cxx)
    
    	defaultcflags = os.Getenv("CFLAGS")
    	defaultldflags = os.Getenv("LDFLAGS")
    
    	b = os.Getenv("PKG_CONFIG")
    	if b == "" {
    		b = "pkg-config"
    	}
    
    	// For tools being invoked but also for os.ExpandEnv.
    
    	os.Setenv("GO386", go386)
    
    	os.Setenv("GOARCH", goarch)
    	os.Setenv("GOARM", goarm)
    	os.Setenv("GOHOSTARCH", gohostarch)
    	os.Setenv("GOHOSTOS", gohostos)
    	os.Setenv("GOOS", goos)
    
    	os.Setenv("GOMIPS64", gomips64)
    
    	os.Setenv("GOROOT", goroot)
    	os.Setenv("GOROOT_FINAL", goroot_final)
    
    	// Use a build cache separate from the default user one.
    	// Also one that will be wiped out during startup, so that
    	// make.bash really does start from a clean slate.
    
    	os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot))
    
    Russ Cox's avatar
    Russ Cox committed
    	// Make the environment more predictable.
    
    	os.Setenv("LANG", "C")
    	os.Setenv("LANGUAGE", "en_US.UTF8")
    
    	if err := ioutil.WriteFile(pathf("%s/go.mod", workdir), []byte("module bootstrap"), 0666); err != nil {
    		fatalf("cannot write stub go.mod: %s", err)
    	}
    
    	tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    
    // compilerEnv returns a map from "goos/goarch" to the
    // compiler setting to use for that platform.
    // The entry for key "" covers any goos/goarch not explicitly set in the map.
    // For example, compilerEnv("CC", "gcc") returns the C compiler settings
    // read from $CC, defaulting to gcc.
    //
    // The result is a map because additional environment variables
    // can be set to change the compiler based on goos/goarch settings.
    // The following applies to all envNames but CC is assumed to simplify
    // the presentation.
    //
    // If no environment variables are set, we use def for all goos/goarch.
    // $CC, if set, applies to all goos/goarch but is overridden by the following.
    // $CC_FOR_TARGET, if set, applies to all goos/goarch except gohostos/gohostarch,
    // but is overridden by the following.
    // If gohostos=goos and gohostarch=goarch, then $CC_FOR_TARGET applies even for gohostos/gohostarch.
    // $CC_FOR_goos_goarch, if set, applies only to goos/goarch.
    func compilerEnv(envName, def string) map[string]string {
    	m := map[string]string{"": def}
    
    	if env := os.Getenv(envName); env != "" {
    		m[""] = env
    	}
    	if env := os.Getenv(envName + "_FOR_TARGET"); env != "" {
    		if gohostos != goos || gohostarch != goarch {
    			m[gohostos+"/"+gohostarch] = m[""]
    		}
    		m[""] = env
    	}
    
    	for _, goos := range okgoos {
    		for _, goarch := range okgoarch {
    			if env := os.Getenv(envName + "_FOR_" + goos + "_" + goarch); env != "" {
    				m[goos+"/"+goarch] = env
    			}
    		}
    	}
    
    	return m
    }
    
    // compilerEnvLookup returns the compiler settings for goos/goarch in map m.
    func compilerEnvLookup(m map[string]string, goos, goarch string) string {
    	if cc := m[goos+"/"+goarch]; cc != "" {
    		return cc
    	}
    	return m[""]
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // rmworkdir deletes the work directory.
    
    func rmworkdir() {
    	if vflag > 1 {
    		errprintf("rm -rf %s\n", workdir)
    	}
    	xremoveall(workdir)
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    
    // Remove trailing spaces.
    
    func chomp(s string) string {
    	return strings.TrimRight(s, " \t\r\n")
    
    func branchtag(branch string) (tag string, precise bool) {
    
    	log := run(goroot, CheckExit, "git", "log", "--decorate=full", "--format=format:%d", "master.."+branch)
    
    	for row, line := range strings.Split(log, "\n") {
    
    		// Each line is either blank, or looks like
    		//	  (tag: refs/tags/go1.4rc2, refs/remotes/origin/release-branch.go1.4, refs/heads/release-branch.go1.4)
    		// We need to find an element starting with refs/tags/.
    
    		const s = " refs/tags/"
    		i := strings.Index(line, s)
    
    		// Trim off known prefix.
    		line = line[i+len(s):]
    		// The tag name ends at a comma or paren.
    		j := strings.IndexAny(line, ",)")
    
    		if j < 0 {
    			continue // malformed line; ignore it
    		}
    
    		tag = line[:j]
    
    			precise = true // tag denotes HEAD
    		}
    		break
    	}
    	return
    
    
    // findgoversion determines the Go version to use in the version string.
    
    func findgoversion() string {
    
    	// The $GOROOT/VERSION file takes priority, for distributions
    
    	// without the source repo.
    
    	path := pathf("%s/VERSION", goroot)
    	if isfile(path) {
    		b := chomp(readfile(path))
    
    		// Commands such as "dist version > VERSION" will cause
    		// the shell to create an empty VERSION file and set dist's
    		// stdout to its fd. dist in turn looks at VERSION and uses
    		// its content if available, which is empty at this point.
    
    		// Only use the VERSION file if it is non-empty.
    		if b != "" {
    
    			// Some builders cross-compile the toolchain on linux-amd64
    			// and then copy the toolchain to the target builder (say, linux-arm)
    			// for use there. But on non-release (devel) branches, the compiler
    			// used on linux-amd64 will be an amd64 binary, and the compiler
    			// shipped to linux-arm will be an arm binary, so they will have different
    			// content IDs (they are binaries for different architectures) and so the
    			// packages compiled by the running-on-amd64 compiler will appear
    			// stale relative to the running-on-arm compiler. Avoid this by setting
    			// the version string to something that doesn't begin with devel.
    			// Then the version string will be used in place of the content ID,
    			// and the packages will look up-to-date.
    			// TODO(rsc): Really the builders could be writing out a better VERSION file instead,
    			// but it is easier to change cmd/dist than to try to make changes to
    			// the builder while Brad is away.
    			if strings.HasPrefix(b, "devel") {
    				if hostType := os.Getenv("META_BUILDLET_HOST_TYPE"); strings.Contains(hostType, "-cross") {
    					fmt.Fprintf(os.Stderr, "warning: changing VERSION from %q to %q\n", b, "builder "+hostType)
    					b = "builder " + hostType
    				}
    			}
    
    	}
    
    	// The $GOROOT/VERSION.cache file is a cache to avoid invoking
    
    	// git every time we run this command. Unlike VERSION, it gets
    
    	// deleted by the clean command.
    
    	path = pathf("%s/VERSION.cache", goroot)
    	if isfile(path) {
    		return chomp(readfile(path))
    
    	// Show a nicer error message if this isn't a Git repo.
    	if !isGitRepo() {
    
    		fatalf("FAILED: not a Git repo; must put a VERSION file in $GOROOT")
    
    	// Otherwise, use Git.
    
    	// What is the current branch?
    
    	branch := chomp(run(goroot, CheckExit, "git", "rev-parse", "--abbrev-ref", "HEAD"))
    
    
    	// What are the tags along the current branch?
    
    	// If we're on a release branch, use the closest matching tag
    	// that is on the release branch (and not on the master branch).
    
    	if strings.HasPrefix(branch, "release-branch.") {
    		tag, precise = branchtag(branch)
    	}
    
    		// Tag does not point at HEAD; add hash and date to version.
    
    		tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD"))
    
    
    	// Cache version.
    
    // isGitRepo reports whether the working directory is inside a Git repository.
    func isGitRepo() bool {
    
    	// NB: simply checking the exit code of `git rev-parse --git-dir` would
    	// suffice here, but that requires deviating from the infrastructure
    	// provided by `run`.
    	gitDir := chomp(run(goroot, 0, "git", "rev-parse", "--git-dir"))
    	if !filepath.IsAbs(gitDir) {
    		gitDir = filepath.Join(goroot, gitDir)
    
    	return isdir(gitDir)
    
    Russ Cox's avatar
    Russ Cox committed
    /*
     * Initial tree setup.
     */
    
    // The old tools that no longer live in $GOBIN or $GOROOT/bin.
    
    Russ Cox's avatar
    Russ Cox committed
    	"5a", "5c", "5g", "5l",
    	"6a", "6c", "6g", "6l",
    	"8a", "8c", "8g", "8l",
    
    	"9a", "9c", "9g", "9l",
    
    Russ Cox's avatar
    Russ Cox committed
    	"6cov",
    	"6nm",
    
    	"6prof",
    
    Russ Cox's avatar
    Russ Cox committed
    	"cgo",
    	"ebnflint",
    	"goapi",
    	"gofix",
    	"goinstall",
    	"gomake",
    	"gopack",
    	"gopprof",
    	"gotest",
    	"gotype",
    	"govet",
    	"goyacc",
    	"quietgcc",
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    // Unreleased directories (relative to $GOROOT) that should
    // not be in release branches.
    
    Russ Cox's avatar
    Russ Cox committed
    	"src/cmd/newlink",
    
    	"src/cmd/objwriter",
    
    	"src/debug/goobj",
    	"src/old",
    
    Russ Cox's avatar
    Russ Cox committed
    // setup sets up the tree for the initial build.
    
    	// Create bin directory.
    
    	if p := pathf("%s/bin", goroot); !isdir(p) {
    		xmkdir(p)
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    
    	// Create package directory.
    
    	if p := pathf("%s/pkg", goroot); !isdir(p) {
    		xmkdir(p)
    	}
    
    	p := pathf("%s/pkg/%s_%s", goroot, gohostos, gohostarch)
    	if rebuildall {
    		xremoveall(p)
    	}
    	xmkdirall(p)
    
    	if goos != gohostos || goarch != gohostarch {
    		p := pathf("%s/pkg/%s_%s", goroot, goos, goarch)
    		if rebuildall {
    			xremoveall(p)
    		}
    		xmkdirall(p)
    
    	// Create object directory.
    
    	// We used to use it for C objects.
    	// Now we use it for the build cache, to separate dist's cache
    	// from any other cache the user might have.
    	p = pathf("%s/pkg/obj/go-build", goroot)
    
    	if rebuildall {
    		xremoveall(p)
    	}
    	xmkdirall(p)
    
    	xatexit(func() { xremoveall(p) })
    
    
    	// Create tool directory.
    	// We keep it in pkg/, just like the object directory above.
    
    	if rebuildall {
    		xremoveall(tooldir)
    	}
    	xmkdirall(tooldir)
    
    
    	// Remove tool binaries from before the tool/gohostos_gohostarch
    
    	xremoveall(pathf("%s/bin/tool", goroot))
    
    Russ Cox's avatar
    Russ Cox committed
    
    	// Remove old pre-tool binaries.
    
    	for _, old := range oldtool {
    		xremove(pathf("%s/bin/%s", goroot, old))
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    	// If $GOBIN is set and has a Go compiler, it must be cleaned.
    
    	for _, char := range "56789" {
    
    		if isfile(pathf("%s/%c%s", gobin, char, "g")) {
    
    			for _, old := range oldtool {
    				xremove(pathf("%s/%s", gobin, old))
    			}
    			break
    
    	// For release, make sure excluded things are excluded.
    
    	goversion := findgoversion()
    
    	if strings.HasPrefix(goversion, "release.") || (strings.HasPrefix(goversion, "go") && !strings.Contains(goversion, "beta")) {
    		for _, dir := range unreleased {
    			if p := pathf("%s/%s", goroot, dir); isdir(p) {
    
    				fatalf("%s should not exist in release build", p)
    
    Russ Cox's avatar
    Russ Cox committed
     */
    
    // deptab lists changes to the default dependencies for a given prefix.
    
    // deps ending in /* read the whole directory; deps beginning with -
    
    Russ Cox's avatar
    Russ Cox committed
    // exclude files with that prefix.
    
    // Note that this table applies only to the build of cmd/go,
    // after the main compiler bootstrap.
    
    var deptab = []struct {
    	prefix string   // prefix of target
    	dep    []string // dependency tweaks for targets with that prefix
    }{
    
    	{"cmd/go/internal/cfg", []string{
    
    	{"runtime/internal/sys", []string{
    
    		"zversion.go",
    
    Russ Cox's avatar
    Russ Cox committed
    	}},
    
    Russ Cox's avatar
    Russ Cox committed
    
    // depsuffix records the allowed suffixes for source files.
    
    Russ Cox's avatar
    Russ Cox committed
    	".s",
    	".go",
    
    Russ Cox's avatar
    Russ Cox committed
    
    // gentab records how to generate some trivial files.
    
    var gentab = []struct {
    	nameprefix string
    	gen        func(string, string)
    }{
    
    	{"zdefaultcc.go", mkzdefaultcc},
    
    	{"zversion.go", mkzversion},
    
    
    	// not generated anymore, but delete the file if we see it
    	{"enam.c", nil},
    
    Russ Cox's avatar
    Russ Cox committed
    	{"anames5.c", nil},
    	{"anames6.c", nil},
    	{"anames8.c", nil},
    	{"anames9.c", nil},
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    // installed maps from a dir name (as given to install) to a chan
    // closed when the dir's package is installed.
    var installed = make(map[string]chan struct{})
    
    var installedMu sync.Mutex
    
    	<-startInstall(dir)
    }
    
    func startInstall(dir string) chan struct{} {
    	installedMu.Lock()
    	ch := installed[dir]
    	if ch == nil {
    		ch = make(chan struct{})
    		installed[dir] = ch
    		go runInstall(dir, ch)
    	}
    	installedMu.Unlock()
    	return ch
    }
    
    // runInstall installs the library, package, or binary associated with dir,
    // which is relative to $GOROOT/src.
    
    func runInstall(pkg string, ch chan struct{}) {
    	if pkg == "net" || pkg == "os/user" || pkg == "crypto/x509" {
    		fatalf("go_bootstrap cannot depend on cgo package %s", pkg)
    
    	if pkg == "unsafe" {
    
    	if vflag > 0 {
    		if goos != gohostos || goarch != gohostarch {
    
    			errprintf("%s (%s/%s)\n", pkg, goos, goarch)
    
    			errprintf("%s\n", pkg)
    
    	workdir := pathf("%s/%s", workdir, pkg)
    
    	var clean []string
    	defer func() {
    		for _, name := range clean {
    			xremove(name)
    		}
    	}()
    
    	// dir = full path to pkg.
    	dir := pathf("%s/src/%s", goroot, pkg)
    
    	// ispkg predicts whether the package should be linked as a binary, based
    	// on the name. There should be no "main" packages in vendor, since
    	// 'go mod vendor' will only copy imported packages there.
    	ispkg := !strings.HasPrefix(pkg, "cmd/") || strings.Contains(pkg, "/internal/") || strings.Contains(pkg, "/vendor/")
    
    Russ Cox's avatar
    Russ Cox committed
    	// Start final link command line.
    
    	// Note: code below knows that link.p[targ] is the target.
    
    	var (
    		link      []string
    		targ      int
    		ispackcmd bool
    	)
    
    Russ Cox's avatar
    Russ Cox committed
    		// Go library (package).
    
    		link = []string{"pack", packagefile(pkg)}
    
    		targ = len(link) - 1
    		xmkdirall(filepath.Dir(link[targ]))
    
    Russ Cox's avatar
    Russ Cox committed
    		// Go command.
    
    		elem := name
    		if elem == "go" {
    			elem = "go_bootstrap"
    		}
    
    		link = []string{pathf("%s/link", tooldir)}
    		if goos == "android" {
    			link = append(link, "-buildmode=pie")
    		}
    
    		if goldflags != "" {
    			link = append(link, goldflags)
    		}
    
    		link = append(link, "-extld="+compilerEnvLookup(defaultcc, goos, goarch))
    
    		link = append(link, "-o", pathf("%s/%s%s", tooldir, elem, exe))
    
    Russ Cox's avatar
    Russ Cox committed
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    
    	// Gather files that are sources for this target.
    	// Everything in that directory, and any target-specific
    	// additions.
    
    	files := xreaddir(dir)
    
    
    	// Remove files beginning with . or _,
    	// which are likely to be editor temporary files.
    	// This is the same heuristic build.ScanDir uses.
    	// There do exist real C files beginning with _,
    	// so limit that check to just Go files.
    
    	files = filter(files, func(p string) bool {
    		return !strings.HasPrefix(p, ".") && (!strings.HasPrefix(p, "_") || !strings.HasSuffix(p, ".go"))
    	})
    
    	for _, dt := range deptab {
    
    		if pkg == dt.prefix || strings.HasSuffix(dt.prefix, "/") && strings.HasPrefix(pkg, dt.prefix) {
    
    			for _, p := range dt.dep {
    				p = os.ExpandEnv(p)
    
    				files = append(files, p)
    
    Russ Cox's avatar
    Russ Cox committed
    	// Convert to absolute paths.
    
    		if !filepath.IsAbs(p) {
    
    			files[i] = pathf("%s/%s", dir, p)
    
    Russ Cox's avatar
    Russ Cox committed
    		}
    	}
    
    	// Is the target up-to-date?
    
    	var gofiles, sfiles, missing []string
    
    	stale := rebuildall
    	files = filter(files, func(p string) bool {
    		for _, suf := range depsuffix {
    			if strings.HasSuffix(p, suf) {
    				goto ok
    			}
    		}
    		return false
    
    Russ Cox's avatar
    Russ Cox committed
    	ok:
    
    		if !t.IsZero() && !strings.HasSuffix(p, ".a") && !shouldbuild(p, pkg) {
    
    		if strings.HasSuffix(p, ".go") {
    			gofiles = append(gofiles, p)
    
    		} else if strings.HasSuffix(p, ".s") {
    			sfiles = append(sfiles, p)
    
    Russ Cox's avatar
    Russ Cox committed
    		}
    
    		if t.After(ttarg) {
    			stale = true
    		}
    		if t.IsZero() {
    			missing = append(missing, p)
    		}
    		return true
    	})
    
    	// If there are no files to compile, we're done.
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    	// For package runtime, copy some files into the work space.
    
    	if pkg == "runtime" {
    
    		xmkdirall(pathf("%s/pkg/include", goroot))
    
    		// For use by assembly and C files.
    
    		copyfile(pathf("%s/pkg/include/textflag.h", goroot),
    
    			pathf("%s/src/runtime/textflag.h", goroot), 0)
    
    		copyfile(pathf("%s/pkg/include/funcdata.h", goroot),
    
    			pathf("%s/src/runtime/funcdata.h", goroot), 0)
    
    		copyfile(pathf("%s/pkg/include/asm_ppc64x.h", goroot),
    			pathf("%s/src/runtime/asm_ppc64x.h", goroot), 0)
    
    	}
    
    	// Generate any missing files; regenerate existing ones.
    
    	for _, p := range files {
    		elem := filepath.Base(p)
    		for _, gt := range gentab {
    			if gt.gen == nil {
    				continue
    			}
    			if strings.HasPrefix(elem, gt.nameprefix) {
    				if vflag > 1 {
    					errprintf("generate %s\n", p)
    				}
    
    				gt.gen(dir, p)
    
    				// Do not add generated file to clean list.
    
    				// In runtime, we want to be able to
    
    				// build the package with the go tool,
    				// and it assumes these generated files already
    				// exist (it does not know how to build them).
    				// The 'clean' command can remove
    				// the generated files.
    
    Russ Cox's avatar
    Russ Cox committed
    			}
    		}
    
    		// Did not rebuild p.
    
    			fatalf("missing file %s", p)
    
    Russ Cox's avatar
    Russ Cox committed
    	}
    
    
    	// Resolve imported packages to actual package paths.
    	// Make sure they're installed.
    	importMap := make(map[string]string)
    
    	for _, p := range gofiles {
    
    		for _, imp := range readimports(p) {
    			importMap[imp] = resolveVendor(imp, dir)
    		}
    	}
    	sortedImports := make([]string, 0, len(importMap))
    	for imp := range importMap {
    		sortedImports = append(sortedImports, imp)
    
    	sort.Strings(sortedImports)
    
    	for _, dep := range importMap {
    		startInstall(dep)
    
    	for _, dep := range importMap {
    		install(dep)
    
    	if goos != gohostos || goarch != gohostarch {
    
    		// We've generated the right files; the go command can do the build.
    
    			errprintf("skip build for cross-compile %s\n", pkg)
    
    	asmArgs := []string{
    		pathf("%s/asm", tooldir),
    		"-I", workdir,
    		"-I", pathf("%s/pkg/include", goroot),
    		"-D", "GOOS_" + goos,
    		"-D", "GOARCH_" + goarch,
    		"-D", "GOOS_GOARCH_" + goos + "_" + goarch,
    
    	}
    	if goarch == "mips" || goarch == "mipsle" {
    		// Define GOMIPS_value from gomips.
    		asmArgs = append(asmArgs, "-D", "GOMIPS_"+gomips)
    	}
    
    	if goarch == "mips64" || goarch == "mips64le" {
    
    		// Define GOMIPS64_value from gomips64.
    		asmArgs = append(asmArgs, "-D", "GOMIPS64_"+gomips64)
    	}
    	goasmh := pathf("%s/go_asm.h", workdir)
    
    	if IsRuntimePackagePath(pkg) {
    
    		asmArgs = append(asmArgs, "-compiling-runtime")
    
    		if os.Getenv("GOEXPERIMENT") == "regabi" {
    			// In order to make it easier to port runtime assembly
    			// to the register ABI, we introduce a macro
    			// indicating the experiment is enabled.
    			//
    			// Note: a similar change also appears in
    			// cmd/go/internal/work/gc.go.
    			//
    			// TODO(austin): Remove this once we commit to the
    			// register ABI (#40724).
    			asmArgs = append(asmArgs, "-D=GOEXPERIMENT_REGABI=1")
    		}
    	}
    
    
    	// Collect symabis from assembly code.
    	var symabis string
    	if len(sfiles) > 0 {
    		symabis = pathf("%s/symabis", workdir)
    		var wg sync.WaitGroup
    
    		asmabis := append(asmArgs[:len(asmArgs):len(asmArgs)], "-gensymabis", "-o", symabis)
    
    		asmabis = append(asmabis, sfiles...)
    		if err := ioutil.WriteFile(goasmh, nil, 0666); err != nil {
    			fatalf("cannot write empty go_asm.h: %s", err)
    		}
    
    		bgrun(&wg, dir, asmabis...)
    
    	// Build an importcfg file for the compiler.
    	buf := &bytes.Buffer{}
    	for _, imp := range sortedImports {
    		if imp == "unsafe" {
    			continue
    		}
    		dep := importMap[imp]
    		if imp != dep {
    			fmt.Fprintf(buf, "importmap %s=%s\n", imp, dep)
    		}
    		fmt.Fprintf(buf, "packagefile %s=%s\n", dep, packagefile(dep))
    	}
    	importcfg := pathf("%s/importcfg", workdir)
    	if err := ioutil.WriteFile(importcfg, buf.Bytes(), 0666); err != nil {
    		fatalf("cannot write importcfg file: %v", err)
    	}
    
    
    	// The next loop will compile individual non-Go files.
    	// Hand the Go files to the compiler en masse.
    
    	// For packages containing assembly, this writes go_asm.h, which
    
    	// the assembly files will need.
    
    	pkgName := pkg
    	if strings.HasPrefix(pkg, "cmd/") && strings.Count(pkg, "/") == 1 {
    		pkgName = "main"
    
    	}
    	b := pathf("%s/_go_.a", workdir)
    	clean = append(clean, b)
    	if !ispackcmd {
    		link = append(link, b)
    	} else {
    		archive = b
    
    	compile := []string{pathf("%s/compile", tooldir), "-std", "-pack", "-o", b, "-p", pkgName, "-importcfg", importcfg}
    
    		compile = append(compile, strings.Fields(gogcflags)...)
    
    	if pkg == "runtime" {
    
    		compile = append(compile, "-+")
    
    	if len(sfiles) > 0 {
    		compile = append(compile, "-asmhdr", goasmh)
    
    	if symabis != "" {
    		compile = append(compile, "-symabis", symabis)
    	}
    
    	if goos == "android" {
    		compile = append(compile, "-shared")
    	}
    
    	compile = append(compile, gofiles...)
    
    	var wg sync.WaitGroup
    	// We use bgrun and immediately wait for it instead of calling run() synchronously.
    	// This executes all jobs through the bgwork channel and allows the process
    	// to exit cleanly in case an error occurs.
    
    	bgrun(&wg, dir, compile...)
    
    Russ Cox's avatar
    Russ Cox committed
    	// Compile the files.
    
    		// Assembly file for a Go package.
    
    		compile := asmArgs[:len(asmArgs):len(asmArgs)]
    
    		doclean := true
    		b := pathf("%s/%s", workdir, filepath.Base(p))
    
    		// Change the last character of the output file (which was c or s).
    
    		b = b[:len(b)-1] + "o"
    
    		compile = append(compile, "-o", b, p)
    
    		bgrun(&wg, dir, compile...)
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    		link = append(link, b)
    		if doclean {
    			clean = append(clean, b)
    		}
    
    Russ Cox's avatar
    Russ Cox committed
    	}
    
    		xremove(link[targ])
    		dopack(link[targ], archive, link[targ+1:])
    		return
    
    Russ Cox's avatar
    Russ Cox committed
    	}
    
    	// Remove target before writing it.
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    
    // packagefile returns the path to a compiled .a file for the given package
    // path. Paths may need to be resolved with resolveVendor first.
    func packagefile(pkg string) string {
    	return pathf("%s/pkg/%s_%s/%s.a", goroot, goos, goarch, pkg)
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // matchfield reports whether the field (x,y,z) matches this build.
    // all the elements in the field must be satisfied.
    
    func matchfield(f string) bool {
    	for _, tag := range strings.Split(f, ",") {
    
    Russ Cox's avatar
    Russ Cox committed
    		if !matchtag(tag) {
    			return false
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // matchtag reports whether the tag (x or !x) matches this build.
    func matchtag(tag string) bool {
    	if tag == "" {
    		return false
    	}
    	if tag[0] == '!' {
    		if len(tag) == 1 || tag[1] == '!' {
    			return false
    		}
    		return !matchtag(tag[1:])
    	}
    
    	if os.Getenv("GOEXPERIMENT") == "regabi" && tag == "goexperiment.regabi" {
    		// TODO: maybe we can handle GOEXPERIMENT more generally.
    		// Or remove once we commit to regabi (#40724).
    		return true
    	}
    
    Cherry Zhang's avatar
    Cherry Zhang committed
    	return tag == "gc" || tag == goos || tag == goarch || tag == "cmd_go_bootstrap" || tag == "go1.1" ||
    		(goos == "android" && tag == "linux") ||
    		(goos == "illumos" && tag == "solaris") ||
    		(goos == "ios" && tag == "darwin")
    
    Russ Cox's avatar
    Russ Cox committed
    // shouldbuild reports whether we should build this file.
    // It applies the same rules that are used with context tags
    
    // in package go/build, except it's less picky about the order
    // of GOOS and GOARCH.
    
    Russ Cox's avatar
    Russ Cox committed
    // We also allow the special tag cmd_go_bootstrap.