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