github.com/muesli/go@v0.0.0-20170208044820-e410d2a81ef2/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. 22 // 23 // The list has have two kinds of entries: names beginning with cmd/ with 24 // no other slashes, which are commands, and other paths, which are packages 25 // supporting the commands. Packages in the standard library can be listed 26 // if a newer copy needs to be substituted for the Go 1.4 copy when used 27 // by the command packages. 28 // These will be imported during bootstrap as bootstrap/name, like bootstrap/math/big. 29 var bootstrapDirs = []string{ 30 "cmd/asm", 31 "cmd/asm/internal/arch", 32 "cmd/asm/internal/asm", 33 "cmd/asm/internal/flags", 34 "cmd/asm/internal/lex", 35 "cmd/compile", 36 "cmd/compile/internal/amd64", 37 "cmd/compile/internal/arm", 38 "cmd/compile/internal/arm64", 39 "cmd/compile/internal/gc", 40 "cmd/compile/internal/mips", 41 "cmd/compile/internal/mips64", 42 "cmd/compile/internal/ppc64", 43 "cmd/compile/internal/s390x", 44 "cmd/compile/internal/ssa", 45 "cmd/compile/internal/syntax", 46 "cmd/compile/internal/x86", 47 "cmd/internal/bio", 48 "cmd/internal/gcprog", 49 "cmd/internal/dwarf", 50 "cmd/internal/obj", 51 "cmd/internal/obj/arm", 52 "cmd/internal/obj/arm64", 53 "cmd/internal/obj/mips", 54 "cmd/internal/obj/ppc64", 55 "cmd/internal/obj/s390x", 56 "cmd/internal/obj/x86", 57 "cmd/internal/src", 58 "cmd/internal/sys", 59 "cmd/link", 60 "cmd/link/internal/amd64", 61 "cmd/link/internal/arm", 62 "cmd/link/internal/arm64", 63 "cmd/link/internal/ld", 64 "cmd/link/internal/mips", 65 "cmd/link/internal/mips64", 66 "cmd/link/internal/ppc64", 67 "cmd/link/internal/s390x", 68 "cmd/link/internal/x86", 69 "debug/pe", 70 "math/big", 71 } 72 73 // File prefixes that are ignored by go/build anyway, and cause 74 // problems with editor generated temporary files (#18931). 75 var ignorePrefixes = []string{ 76 ".", 77 "_", 78 } 79 80 // File suffixes that use build tags introduced since Go 1.4. 81 // These must not be copied into the bootstrap build directory. 82 var ignoreSuffixes = []string{ 83 "_arm64.s", 84 "_arm64.go", 85 } 86 87 func bootstrapBuildTools() { 88 goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP") 89 if goroot_bootstrap == "" { 90 goroot_bootstrap = pathf("%s/go1.4", os.Getenv("HOME")) 91 } 92 xprintf("##### Building Go toolchain using %s.\n", goroot_bootstrap) 93 94 mkzbootstrap(pathf("%s/src/cmd/internal/obj/zbootstrap.go", goroot)) 95 96 // Use $GOROOT/pkg/bootstrap as the bootstrap workspace root. 97 // We use a subdirectory of $GOROOT/pkg because that's the 98 // space within $GOROOT where we store all generated objects. 99 // We could use a temporary directory outside $GOROOT instead, 100 // but it is easier to debug on failure if the files are in a known location. 101 workspace := pathf("%s/pkg/bootstrap", goroot) 102 xremoveall(workspace) 103 base := pathf("%s/src/bootstrap", workspace) 104 xmkdirall(base) 105 106 // Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths. 107 for _, dir := range bootstrapDirs { 108 src := pathf("%s/src/%s", goroot, dir) 109 dst := pathf("%s/%s", base, dir) 110 xmkdirall(dst) 111 Dir: 112 for _, name := range xreaddirfiles(src) { 113 for _, pre := range ignorePrefixes { 114 if strings.HasPrefix(name, pre) { 115 continue Dir 116 } 117 } 118 for _, suf := range ignoreSuffixes { 119 if strings.HasSuffix(name, suf) { 120 continue Dir 121 } 122 } 123 srcFile := pathf("%s/%s", src, name) 124 text := readfile(srcFile) 125 text = bootstrapFixImports(text, srcFile) 126 writefile(text, pathf("%s/%s", dst, name), 0) 127 } 128 } 129 130 // Set up environment for invoking Go 1.4 go command. 131 // GOROOT points at Go 1.4 GOROOT, 132 // GOPATH points at our bootstrap workspace, 133 // GOBIN is empty, so that binaries are installed to GOPATH/bin, 134 // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty, 135 // so that Go 1.4 builds whatever kind of binary it knows how to build. 136 // Restore GOROOT, GOPATH, and GOBIN when done. 137 // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH, 138 // because setup will take care of those when bootstrapBuildTools returns. 139 140 defer os.Setenv("GOROOT", os.Getenv("GOROOT")) 141 os.Setenv("GOROOT", goroot_bootstrap) 142 143 defer os.Setenv("GOPATH", os.Getenv("GOPATH")) 144 os.Setenv("GOPATH", workspace) 145 146 defer os.Setenv("GOBIN", os.Getenv("GOBIN")) 147 os.Setenv("GOBIN", "") 148 149 os.Setenv("GOOS", "") 150 os.Setenv("GOHOSTOS", "") 151 os.Setenv("GOARCH", "") 152 os.Setenv("GOHOSTARCH", "") 153 154 // Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to 155 // workaround bugs in Go 1.4's compiler. See discussion thread: 156 // https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ 157 // Use the math_big_pure_go build tag to disable the assembly in math/big 158 // which may contain unsupported instructions. 159 run(workspace, ShowOutput|CheckExit, pathf("%s/bin/go", goroot_bootstrap), "install", "-gcflags=-l", "-tags=math_big_pure_go", "-v", "bootstrap/cmd/...") 160 161 // Copy binaries into tool binary directory. 162 for _, name := range bootstrapDirs { 163 if !strings.HasPrefix(name, "cmd/") { 164 continue 165 } 166 name = name[len("cmd/"):] 167 if !strings.Contains(name, "/") { 168 copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec) 169 } 170 } 171 172 xprintf("\n") 173 } 174 175 func bootstrapFixImports(text, srcFile string) string { 176 lines := strings.SplitAfter(text, "\n") 177 inBlock := false 178 for i, line := range lines { 179 if strings.HasPrefix(line, "import (") { 180 inBlock = true 181 continue 182 } 183 if inBlock && strings.HasPrefix(line, ")") { 184 inBlock = false 185 continue 186 } 187 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) || 188 inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"")) { 189 line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1) 190 for _, dir := range bootstrapDirs { 191 if strings.HasPrefix(dir, "cmd/") { 192 continue 193 } 194 line = strings.Replace(line, `"`+dir+`"`, `"bootstrap/`+dir+`"`, -1) 195 } 196 lines[i] = line 197 } 198 } 199 200 lines[0] = "// Do not edit. Bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0] 201 202 return strings.Join(lines, "") 203 }