github.com/euank/go@v0.0.0-20160829210321-495514729181/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/s390x", 40 "compile/internal/ssa", 41 "compile/internal/syntax", 42 "compile/internal/x86", 43 "internal/bio", 44 "internal/gcprog", 45 "internal/dwarf", 46 "internal/obj", 47 "internal/obj/arm", 48 "internal/obj/arm64", 49 "internal/obj/mips", 50 "internal/obj/ppc64", 51 "internal/obj/s390x", 52 "internal/obj/x86", 53 "internal/sys", 54 "link", 55 "link/internal/amd64", 56 "link/internal/arm", 57 "link/internal/arm64", 58 "link/internal/ld", 59 "link/internal/mips64", 60 "link/internal/ppc64", 61 "link/internal/s390x", 62 "link/internal/x86", 63 } 64 65 func bootstrapBuildTools() { 66 goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP") 67 if goroot_bootstrap == "" { 68 goroot_bootstrap = pathf("%s/go1.4", os.Getenv("HOME")) 69 } 70 xprintf("##### Building Go toolchain using %s.\n", goroot_bootstrap) 71 72 mkzbootstrap(pathf("%s/src/cmd/internal/obj/zbootstrap.go", goroot)) 73 74 // Use $GOROOT/pkg/bootstrap as the bootstrap workspace root. 75 // We use a subdirectory of $GOROOT/pkg because that's the 76 // space within $GOROOT where we store all generated objects. 77 // We could use a temporary directory outside $GOROOT instead, 78 // but it is easier to debug on failure if the files are in a known location. 79 workspace := pathf("%s/pkg/bootstrap", goroot) 80 xremoveall(workspace) 81 base := pathf("%s/src/bootstrap", workspace) 82 xmkdirall(base) 83 84 // Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths. 85 for _, dir := range bootstrapDirs { 86 src := pathf("%s/src/cmd/%s", goroot, dir) 87 dst := pathf("%s/%s", base, dir) 88 xmkdirall(dst) 89 for _, name := range xreaddirfiles(src) { 90 srcFile := pathf("%s/%s", src, name) 91 text := readfile(srcFile) 92 text = bootstrapFixImports(text, srcFile) 93 writefile(text, pathf("%s/%s", dst, name), 0) 94 } 95 } 96 97 // Set up environment for invoking Go 1.4 go command. 98 // GOROOT points at Go 1.4 GOROOT, 99 // GOPATH points at our bootstrap workspace, 100 // GOBIN is empty, so that binaries are installed to GOPATH/bin, 101 // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty, 102 // so that Go 1.4 builds whatever kind of binary it knows how to build. 103 // Restore GOROOT, GOPATH, and GOBIN when done. 104 // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH, 105 // because setup will take care of those when bootstrapBuildTools returns. 106 107 defer os.Setenv("GOROOT", os.Getenv("GOROOT")) 108 os.Setenv("GOROOT", goroot_bootstrap) 109 110 defer os.Setenv("GOPATH", os.Getenv("GOPATH")) 111 os.Setenv("GOPATH", workspace) 112 113 defer os.Setenv("GOBIN", os.Getenv("GOBIN")) 114 os.Setenv("GOBIN", "") 115 116 os.Setenv("GOOS", "") 117 os.Setenv("GOHOSTOS", "") 118 os.Setenv("GOARCH", "") 119 os.Setenv("GOHOSTARCH", "") 120 121 // Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to 122 // workaround bugs in Go 1.4's compiler. See discussion thread: 123 // https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ 124 run(workspace, ShowOutput|CheckExit, pathf("%s/bin/go", goroot_bootstrap), "install", "-gcflags=-l", "-v", "bootstrap/...") 125 126 // Copy binaries into tool binary directory. 127 for _, name := range bootstrapDirs { 128 if !strings.Contains(name, "/") { 129 copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec) 130 } 131 } 132 133 xprintf("\n") 134 } 135 136 func bootstrapFixImports(text, srcFile string) string { 137 lines := strings.SplitAfter(text, "\n") 138 inBlock := false 139 for i, line := range lines { 140 if strings.HasPrefix(line, "import (") { 141 inBlock = true 142 continue 143 } 144 if inBlock && strings.HasPrefix(line, ")") { 145 inBlock = false 146 continue 147 } 148 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) || 149 inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"")) { 150 lines[i] = strings.Replace(line, `"cmd/`, `"bootstrap/`, -1) 151 } 152 } 153 154 lines[0] = "// Do not edit. Bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0] 155 156 return strings.Join(lines, "") 157 }