rsc.io/go@v0.0.0-20150416155037-e040fd465409/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  	"5g",
    27  	"5l",
    28  	"6g",
    29  	"6l",
    30  	"7g",
    31  	"7l",
    32  	"8g",
    33  	"8l",
    34  	"9g",
    35  	"9l",
    36  	"asm",
    37  	"asm/internal/arch",
    38  	"asm/internal/asm",
    39  	"asm/internal/flags",
    40  	"asm/internal/lex",
    41  	"internal/asm",
    42  	"internal/gc/big",
    43  	"internal/gc",
    44  	"internal/ld",
    45  	"internal/obj",
    46  	"internal/obj/arm",
    47  	"internal/obj/arm64",
    48  	"internal/obj/ppc64",
    49  	"internal/obj/x86",
    50  	"old5a",
    51  	"old6a",
    52  	"old8a",
    53  	"old9a",
    54  }
    55  
    56  func bootstrapBuildTools() {
    57  	goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
    58  	if goroot_bootstrap == "" {
    59  		goroot_bootstrap = pathf("%s/go1.4", os.Getenv("HOME"))
    60  	}
    61  	xprintf("##### Building Go toolchain using %s.\n", goroot_bootstrap)
    62  
    63  	mkzbootstrap(pathf("%s/src/cmd/internal/obj/zbootstrap.go", goroot))
    64  
    65  	// Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
    66  	// We use a subdirectory of $GOROOT/pkg because that's the
    67  	// space within $GOROOT where we store all generated objects.
    68  	// We could use a temporary directory outside $GOROOT instead,
    69  	// but it is easier to debug on failure if the files are in a known location.
    70  	workspace := pathf("%s/pkg/bootstrap", goroot)
    71  	xremoveall(workspace)
    72  	base := pathf("%s/src/bootstrap", workspace)
    73  	xmkdirall(base)
    74  
    75  	// Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
    76  	for _, dir := range bootstrapDirs {
    77  		src := pathf("%s/src/cmd/%s", goroot, dir)
    78  		dst := pathf("%s/%s", base, dir)
    79  		xmkdirall(dst)
    80  		for _, name := range xreaddirfiles(src) {
    81  			srcFile := pathf("%s/%s", src, name)
    82  			text := readfile(srcFile)
    83  			text = bootstrapFixImports(text, srcFile)
    84  			writefile(text, pathf("%s/%s", dst, name), 0)
    85  		}
    86  	}
    87  
    88  	// Set up environment for invoking Go 1.4 go command.
    89  	// GOROOT points at Go 1.4 GOROOT,
    90  	// GOPATH points at our bootstrap workspace,
    91  	// GOBIN is empty, so that binaries are installed to GOPATH/bin,
    92  	// and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
    93  	// so that Go 1.4 builds whatever kind of binary it knows how to build.
    94  	// Restore GOROOT, GOPATH, and GOBIN when done.
    95  	// Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
    96  	// because setup will take care of those when bootstrapBuildTools returns.
    97  
    98  	defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
    99  	os.Setenv("GOROOT", goroot_bootstrap)
   100  
   101  	defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
   102  	os.Setenv("GOPATH", workspace)
   103  
   104  	defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
   105  	os.Setenv("GOBIN", "")
   106  
   107  	os.Setenv("GOOS", "")
   108  	os.Setenv("GOHOSTOS", "")
   109  	os.Setenv("GOARCH", "")
   110  	os.Setenv("GOHOSTARCH", "")
   111  
   112  	// Run Go 1.4 to build binaries.
   113  	run(workspace, ShowOutput|CheckExit, pathf("%s/bin/go", goroot_bootstrap), "install", "-v", "bootstrap/...")
   114  
   115  	// Copy binaries into tool binary directory.
   116  	for _, name := range bootstrapDirs {
   117  		if !strings.Contains(name, "/") {
   118  			copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), 1)
   119  		}
   120  	}
   121  
   122  	xprintf("\n")
   123  }
   124  
   125  func bootstrapFixImports(text, srcFile string) string {
   126  	lines := strings.SplitAfter(text, "\n")
   127  	inBlock := false
   128  	for i, line := range lines {
   129  		if strings.HasPrefix(line, "import (") {
   130  			inBlock = true
   131  			continue
   132  		}
   133  		if inBlock && strings.HasPrefix(line, ")") {
   134  			inBlock = false
   135  			continue
   136  		}
   137  		if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
   138  			inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"")) {
   139  			lines[i] = strings.Replace(line, `"cmd/`, `"bootstrap/`, -1)
   140  		}
   141  	}
   142  
   143  	lines[0] = "// Do not edit. Bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
   144  
   145  	return strings.Join(lines, "")
   146  }