-
Notifications
You must be signed in to change notification settings - Fork 0
/
winstrap.go
271 lines (231 loc) · 6.46 KB
/
winstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var files = map[string]string{
// The "tdm64" one (despite the name) doesn't run on 64-bit Windows.
// But the tdm-gcc one does, and installs both 32- and 64-bit versions.
// No clue what tdm64 means.
// "tdm64-gcc-4.8.1-3.exe": "http://downloads.sourceforge.net/project/tdm-gcc/TDM-GCC%20Installer/tdm64-gcc-4.8.1-3.exe?r=http%3A%2F%2Ftdm-gcc.tdragon.net%2Fdownload&ts=1407729829&use_mirror=ufpr",
"tdm-gcc-4.9.2.exe": "https://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/Previous/1.1309.0/tdm-gcc-4.9.2.exe?r=http%3A%2F%2Ftdm-gcc.tdragon.net%2Fdownload&ts=1420336642&use_mirror=hivelocity",
"Wix35.msi": "http://storage.googleapis.com/winstrap/Wix35.msi",
"Install Git.exe": "https://github.com/msysgit/msysgit/releases/download/Git-1.9.5-preview20141217/Git-1.9.5-preview20141217.exe",
"Start Buildlet.exe": "https://storage.googleapis.com/go-builder-data/buildlet-stage0.windows-amd64",
}
var altMain func()
var (
flagYes = flag.Bool("yes", false, "Run without prompt")
homeDir = flag.String("home", defaultHome(), "custom home directory")
)
func waitForGo() {
if !awaitString("go") {
log.Printf("Canceled.")
awaitEnter()
os.Exit(0)
}
}
func main() {
if runtime.GOOS != "windows" {
altMain()
return
}
flag.Parse()
if !*flagYes {
log.Printf("This program will first download TDM-GCC, Wix, and Git, then let you optinally install Go.\nType 'go<enter>' to proceed.")
waitForGo()
}
// TODO(bradfitz): also download Go 1.4 into place at C:\Go1.4
runBatFile := filepath.Join(home(), "Desktop", "run-builder.bat")
if _, err := os.Stat(runBatFile); os.IsNotExist(err) {
ioutil.WriteFile(runBatFile, []byte(strings.Replace(runBuilderBatContents, "\n", "\r\n", -1)), 0755)
}
log.Printf("Downloading files.")
var errs []chan error
for file, url := range files {
errc := make(chan error)
errs = append(errs, errc)
go func(file, url string) {
errc <- download(file, url)
}(file, url)
}
var anyErr bool
for _, errc := range errs {
if err := <-errc; err != nil {
log.Printf("Download error: %v", err)
anyErr = true
}
}
if anyErr {
log.Printf("Download errors. Proceed? Type 'go'")
waitForGo()
}
checkGit()
checkGcc()
log.Printf("This program will now check out go. Type 'go' to proceed.")
waitForGo()
checkoutGo()
log.Printf("This program will now compile Go for 386 and amd64. Type 'go' to proceed.")
waitForGo()
runGoMakeBat("386")
runGoMakeBat("amd64")
log.Printf(`Installed go to %v, please add %v\bin to your PATH`, goroot(), goroot())
fmt.Println("[ Press enter to exit ]")
awaitEnter()
}
const gccPath = `C:\TDM-GCC-64\bin`
func runGoMakeBat(arch string) {
if arch != "386" && arch != "amd64" {
panic("invalid arch " + arch)
}
testFile := filepath.Join(goroot(), "pkg", "tool", "windows_"+arch, "api.exe")
if fileExists(testFile) {
log.Printf("Skipping make.bat for windows_%s; already built.", arch)
return
}
log.Printf("Running make.bat for arch %s ...", arch)
cmd := exec.Command(filepath.Join(goroot(), "src", "make.bat"))
cmd.Dir = filepath.Join(goroot(), "src")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append([]string{
"GOARCH=" + arch,
"PATH=" + gccPath + ";" + os.Getenv("PATH"),
}, removeEnvs(os.Environ(), "PATH")...)
err := cmd.Run()
if err != nil {
log.Fatalf("make.bat for arch %s: %v", arch, err)
}
log.Printf("ran make.bat for arch %s", arch)
}
func removeEnvs(envs []string, removeKeys ...string) []string {
var ret []string
for _, env := range envs {
include := true
for _, remove := range removeKeys {
if strings.HasPrefix(env, remove+"=") {
include = false
break
}
}
if include {
ret = append(ret, env)
}
}
return ret
}
func checkGit() {
for {
if _, ok := gitBin(); ok {
break
}
log.Print("Can't find git binary. Install Git and then press enter... (use middle option: make git available to cmd.exe)")
awaitEnter()
}
}
const gitDefaultPath = `C:\Program Files (x86)\Git\cmd\git.exe`
func gitBin() (string, bool) {
b, err := exec.LookPath("git")
if err != nil {
b = gitDefaultPath
}
return b, fileExists(b)
}
func checkGcc() {
for !fileExists(gccPath) {
log.Printf("%s doesn't exist. Install gcc and then press enter...", gccPath)
awaitEnter()
}
}
func checkoutGo() {
if fileExists(goroot()) {
log.Printf("GOROOT %s already exists; skipping git checkout", goroot())
return
}
log.Printf("Checking out Go source using git")
git, _ := gitBin()
cmd := exec.Command(git, "clone", "https://go.googlesource.com/go", "goroot")
cmd.Dir = home()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("git clone failed. Is Git installed? Re-run later. Error: %v", err)
}
log.Printf("Checked out Go.")
}
func awaitEnter() {
var buf [1]byte
os.Stdin.Read(buf[:])
}
func awaitString(want string) bool {
br := bufio.NewReader(os.Stdin)
ln, _, _ := br.ReadLine()
return strings.TrimSpace(string(ln)) == want
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func defaultHome() string { return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") }
func home() string {
if *homeDir != "" {
return *homeDir
}
return defaultHome()
}
func goroot() string { return filepath.Join(home(), "goroot") }
func gopath() string { return filepath.Join(home(), "gopath") }
func download(file, url string) error {
dst := filepath.Join(home(), "Desktop", file)
if _, err := os.Stat(dst); err == nil {
log.Printf("%s already on desktop; skipping", file)
return nil
}
res, err := http.Get(url)
if err != nil {
return fmt.Errorf("Error fetching %v: %v", url, err)
}
tmp := dst + ".tmp"
os.Remove(tmp)
os.Remove(dst)
f, err := os.Create(tmp)
if err != nil {
return err
}
n, err := io.Copy(f, res.Body)
res.Body.Close()
if err != nil {
return fmt.Errorf("Error reading %v: %v", url, err)
}
f.Close()
err = os.Rename(tmp, dst)
if err != nil {
return err
}
log.Printf("Downladed %s (%d bytes) to desktop", file, n)
return nil
}
const runBuilderBatContents = `echo Running the Go builder:
mkdir \Users\wingopher\gopath
RMDIR /S /Q c:\gobuilder
SET GOROOT_BOOTSTRAP=c:\Go1.4
SET GOPATH=\Users\wingopher\gopath
SET PATH=\Users\wingopher\goroot\bin;%PATH%
go get -u -v golang.org/x/tools/dashboard/builder
%GOPATH%\bin\builder -v -parallel windows-amd64 windows-386
`