github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/cmd/dist/buildtool.go (about)

     1  // Copyright 2015 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  // Build toolchain using Go 1.4.
     6  //
     7  // The general strategy is to copy the source files we need into
     8  // a new GOPATH workspace, adjust import paths appropriately,
     9  // invoke the Go 1.4 go command to build those sources,
    10  // and then copy the binaries back.
    11  
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"strings"
    17  )
    18  
    19  // bootstrapDirs is a list of directories holding code that must be
    20  // compiled with a Go 1.4 toolchain to produce the bootstrapTargets.
    21  // All directories in this list are relative to and must be below $GOROOT/src/cmd.
    22  // The list is assumed to have two kinds of entries: names without slashes,
    23  // which are commands, and entries beginning with internal/, which are
    24  // packages supporting the commands.
    25  var bootstrapDirs = []string{
    26  	"asm",
    27  	"asm/internal/arch",
    28  	"asm/internal/asm",
    29  	"asm/internal/flags",
    30  	"asm/internal/lex",
    31  	"compile",
    32  	"compile/internal/amd64",
    33  	"compile/internal/arm",
    34  	"compile/internal/arm64",
    35  	"compile/internal/big",
    36  	"compile/internal/gc",
    37  	"compile/internal/mips64",
    38  	"compile/internal/ppc64",
    39  	"compile/internal/ssa",
    40  	"compile/internal/x86",
    41  	"compile/internal/s390x",
    42  	"internal/bio",
    43  	"internal/gcprog",
    44  	"internal/obj",
    45  	"internal/obj/arm",
    46  	"internal/obj/arm64",
    47  	"internal/obj/mips",
    48  	"internal/obj/ppc64",
    49  	"internal/obj/s390x",
    50  	"internal/obj/x86",
    51  	"internal/sys",
    52  	"link",
    53  	"link/internal/amd64",
    54  	"link/internal/arm",
    55  	"link/internal/arm64",
    56  	"link/internal/ld",
    57  	"link/internal/mips64",
    58  	"link/internal/ppc64",
    59  	"link/internal/s390x",
    60  	"link/internal/x86",
    61  }
    62  
    63  func bootstrapBuildTools() {
    64  	goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
    65  	if goroot_bootstrap == "" {
    66  		goroot_bootstrap = pathf("%s/go1.4", os.Getenv("HOME"))
    67  	}
    68  	xprintf("##### Building Go toolchain using %s.\n", goroot_bootstrap)
    69  
    70  	mkzbootstrap(pathf("%s/src/cmd/internal/obj/zbootstrap.go", goroot))
    71  
    72  	// Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
    73  	// We use a subdirectory of $GOROOT/pkg because that's the
    74  	// space within $GOROOT where we store all generated objects.
    75  	// We could use a temporary directory outside $GOROOT instead,
    76  	// but it is easier to debug on failure if the files are in a known location.
    77  	workspace := pathf("%s/pkg/bootstrap", goroot)
    78  	xremoveall(workspace)
    79  	base := pathf("%s/src/bootstrap", workspace)
    80  	xmkdirall(base)
    81  
    82  	// Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
    83  	for _, dir := range bootstrapDirs {
    84  		src := pathf("%s/src/cmd/%s", goroot, dir)
    85  		dst := pathf("%s/%s", base, dir)
    86  		xmkdirall(dst)
    87  		for _, name := range xreaddirfiles(src) {
    88  			srcFile := pathf("%s/%s", src, name)
    89  			text := readfile(srcFile)
    90  			text = bootstrapFixImports(text, srcFile)
    91  			writefile(text, pathf("%s/%s", dst, name), 0)
    92  		}
    93  	}
    94  
    95  	// Set up environment for invoking Go 1.4 go command.
    96  	// GOROOT points at Go 1.4 GOROOT,
    97  	// GOPATH points at our bootstrap workspace,
    98  	// GOBIN is empty, so that binaries are installed to GOPATH/bin,
    99  	// and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
   100  	// so that Go 1.4 builds whatever kind of binary it knows how to build.
   101  	// Restore GOROOT, GOPATH, and GOBIN when done.
   102  	// Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
   103  	// because setup will take care of those when bootstrapBuildTools returns.
   104  
   105  	defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
   106  	os.Setenv("GOROOT", goroot_bootstrap)
   107  
   108  	defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
   109  	os.Setenv("GOPATH", workspace)
   110  
   111  	defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
   112  	os.Setenv("GOBIN", "")
   113  
   114  	os.Setenv("GOOS", "")
   115  	os.Setenv("GOHOSTOS", "")
   116  	os.Setenv("GOARCH", "")
   117  	os.Setenv("GOHOSTARCH", "")
   118  
   119  	// Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
   120  	// workaround bugs in Go 1.4's compiler. See discussion thread:
   121  	// https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
   122  	run(workspace, ShowOutput|CheckExit, pathf("%s/bin/go", goroot_bootstrap), "install", "-gcflags=-l", "-v", "bootstrap/...")
   123  
   124  	// Copy binaries into tool binary directory.
   125  	for _, name := range bootstrapDirs {
   126  		if !strings.Contains(name, "/") {
   127  			copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
   128  		}
   129  	}
   130  
   131  	xprintf("\n")
   132  }
   133  
   134  func bootstrapFixImports(text, srcFile string) string {
   135  	lines := strings.SplitAfter(text, "\n")
   136  	inBlock := false
   137  	for i, line := range lines {
   138  		if strings.HasPrefix(line, "import (") {
   139  			inBlock = true
   140  			continue
   141  		}
   142  		if inBlock && strings.HasPrefix(line, ")") {
   143  			inBlock = false
   144  			continue
   145  		}
   146  		if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
   147  			inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"")) {
   148  			lines[i] = strings.Replace(line, `"cmd/`, `"bootstrap/`, -1)
   149  		}
   150  	}
   151  
   152  	lines[0] = "// Do not edit. Bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
   153  
   154  	return strings.Join(lines, "")
   155  }