-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-commands.go
303 lines (257 loc) · 7.09 KB
/
go-commands.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"go101.org/gotv/internal/util"
)
func (gotv *gotv) tryRunningGoToolchainCommand(tv toolchainVersion, args []string) error {
if _, err := gotv.ensureToolchainVersion(&tv, false); err != nil {
return err
}
return gotv.runGoToolchainCommand(tv, args)
}
// After normalization, tv.kind may be only tag/brranch/revision
func (gotv *gotv) normalizeToolchainVersion(tv *toolchainVersion, dontChangeKind bool) error {
if tv.kind == kind_Tag || tv.kind == kind_Branch || tv.kind == kind_Revision {
return nil
}
if tv.kind == kind_Release {
var checkLatest = strings.HasSuffix(tv.version, ".")
var prefix string
if !checkLatest {
checkLatest = tv.version >= "1.21" && strings.Count(tv.version, ".") == 1
prefix = tv.version
} else {
prefix = tv.version[:len(tv.version)-1]
}
if checkLatest {
var latest = ""
for tag := range gotv.repoInfo.releaseTags {
if strings.HasPrefix(tag, prefix) {
if compareVersions(latest, tag) {
latest = tag
}
}
}
if latest == "" {
return fmt.Errorf("not latest version found for fake verison: %s", tv.version)
}
tv.version = latest
}
if dontChangeKind {
// For setDefaultVersion
return nil
}
if v := gotv.repoInfo.releaseTags[tv.version]; v == "" {
return fmt.Errorf("release version %s not found", tv.version)
} else {
tv.version = v
tv.kind = kind_Tag
return nil
}
}
if tv.kind == kind_Alias {
if tv.version == "tip" {
tv.version = "master"
} else if v := gotv.repoInfo.versionBranches[tv.version]; v != "" {
tv.version = v
} else {
return fmt.Errorf("release branch %s not found", tv.version)
}
tv.kind = kind_Branch
return nil
}
panic("unreachable. tv: " + tv.String())
}
func (gotv *gotv) ensureToolchainVersion(tv *toolchainVersion, forPinning bool) (_ string, err error) {
if _, err := gotv.ensureGoRepository(tv.forceSyncRepo); err != nil {
return "", err
}
if repoInfo, err := collectRepositoryInfo(gotv.repositoryDir); err != nil {
return "", err
} else {
gotv.repoInfo = repoInfo
}
if err := gotv.normalizeToolchainVersion(tv, false); err != nil {
return "", err
}
var bootstrapRoot = ""
if bootstrapRoot = os.Getenv("GOROOT_BOOTSTRAP"); bootstrapRoot == "" {
var bootstrapTV = determineBootstrapToolchainVersion(*tv)
if bootstrapTV == nil {
return "", errors.New("unable to build toolchain " + tv.String())
} else if bootstrapTV.kind != kind_Invalid {
bootstrapRoot, err = gotv.ensureToolchainVersion(bootstrapTV, false)
if err != nil {
return "", err
}
} else if runtime.GOOS == "windows" {
// It looks "make.bat" is unable to determine GOROOT_BOOTSTRAP,
// but "make.bash" is able to.
goExePath, err := exec.LookPath("go")
if err != nil {
return "", err
}
bootstrapRoot = filepath.Dir(filepath.Dir(goExePath))
}
}
var goCommandFilename string
if runtime.GOOS == "windows" {
goCommandFilename = "go.exe"
} else {
goCommandFilename = "go"
}
var goCommandPath, toolchainDir string
if forPinning {
toolchainDir = gotv.pinnedToolchainDir
goCommandPath = filepath.Join(toolchainDir, "bin", goCommandFilename)
// toolchainDir will be modify later.
var realToolchainDir = toolchainDir
defer func() {
// ToDo: realToolchainDir == toolchainDir is always false?
if err != nil || realToolchainDir == toolchainDir {
return
}
err = os.RemoveAll(realToolchainDir)
if err != nil {
return
}
err = os.Rename(toolchainDir, realToolchainDir)
}()
} else {
goCommandPath = filepath.Join(gotv.cacheDir, tv.folderName(), "bin", goCommandFilename)
toolchainDir = filepath.Dir(filepath.Dir(goCommandPath))
defer func() {
if err == nil {
gotv.versionGoCmdPaths[*tv] = goCommandPath
}
}()
}
const gotvInfoFile = "gotv.info"
type InfoFile struct {
Revision string `json:"revision"`
}
var infoFilePath = filepath.Join(toolchainDir, gotvInfoFile)
var revision = gotv.toolchainVersion2Revision(*tv)
if _, err := os.Stat(infoFilePath); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
} else {
var outdated = true
var info, err = os.ReadFile(infoFilePath)
if err == nil {
var file InfoFile
err = json.Unmarshal(info, &file)
if err == nil {
outdated = file.Revision != revision
}
}
if !outdated {
return toolchainDir, nil
}
}
if forPinning {
toolchainDir = gotv.pinnedToolchainDir + "_temp"
goCommandPath = filepath.Join(toolchainDir, "bin", goCommandFilename)
infoFilePath = filepath.Join(toolchainDir, gotvInfoFile)
}
if err := os.RemoveAll(toolchainDir); err != nil && !errors.Is(err, fs.ErrNotExist) {
return "", err
}
defer func() {
if err != nil {
_ = os.RemoveAll(toolchainDir)
}
}()
if err := gotv.copyBranchShallowly(*tv, toolchainDir); err != nil {
return "", err
}
defer func() {
if err == nil {
os.RemoveAll(filepath.Join(toolchainDir, ".git"))
os.Remove(filepath.Join(toolchainDir, ".gitignore"))
os.RemoveAll(filepath.Join(toolchainDir, ".github"))
}
}()
var makeScript string
if runtime.GOOS == "windows" {
makeScript = filepath.Join(toolchainDir, "src", "make.bat")
} else {
makeScript = filepath.Join(toolchainDir, "src", "make.bash")
}
var toolchainSrcDir = filepath.Dir(makeScript)
fmt.Println("[Run]:", gotv.replaceHomeDir(makeScript))
buildEnvs := func() []string {
var envs []string
//if runtime.GOOS == "windows" {
if bootstrapRoot != "" {
envs = []string{"CGO_ENABLED=0", "GOROOT_BOOTSTRAP=" + bootstrapRoot}
} else {
envs = []string{"CGO_ENABLED=0"}
}
return envs
}
if _, err := util.RunShell(time.Hour, toolchainSrcDir, buildEnvs, os.Stdout, nil, makeScript); err != nil {
return "", err
}
if _, err := os.Stat(goCommandPath); err != nil {
return "", err
}
var buf bytes.Buffer
fmt.Fprintf(&buf, `{"revision": "%s"}`, revision)
if err := os.WriteFile(infoFilePath, buf.Bytes(), 0644); err != nil {
return "", err
}
return toolchainDir, nil
}
func (gotv *gotv) runGoToolchainCommand(tv toolchainVersion, args []string) error {
goCommandPath, ok := gotv.versionGoCmdPaths[tv]
if !ok {
panic("toochain version " + tv.String() + " is not built?")
}
if _, err := os.Stat(goCommandPath); err != nil {
return err
}
fmt.Print("[Run]: ", gotv.replaceHomeDir(goCommandPath))
for _, a := range args {
fmt.Print(" ", a)
}
fmt.Println()
// change PATH env var
{
goBinPath := filepath.Dir(goCommandPath)
oldpath, newpath := os.Getenv("PATH"), ""
if oldpath == "" {
newpath = goBinPath
} else {
newpath = goBinPath + string(os.PathListSeparator) + oldpath
}
os.Setenv("PATH", newpath)
defer func() {
os.Setenv("PATH", oldpath)
}()
}
buildEnv := func() []string {
return []string{
// https://github.com/golang/go/issues/57001
"GOTOOLCHAIN=local",
}
}
_, err := util.RunShellCommand(time.Hour, "", buildEnv, os.Stdout, os.Stderr, goCommandPath, args...)
if err != nil {
if ee, ok := err.(*exec.ExitError); ok { // always okay
os.Exit(ee.ExitCode())
}
}
return err // must be nil
}