-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
260 lines (239 loc) · 5.99 KB
/
main.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
package exec_tool
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
cmd_exec "github.com/xhd2015/xgo/support/cmd"
)
// invoking examples:
//
// go1.21.7/pkg/tool/darwin_amd64/compile -o /var/.../_pkg_.a -trimpath /var/...=> -p fmt -std -complete -buildid b_xx -goversion go1.21.7 -c=4 -nolocalimports -importcfg /var/.../importcfg -pack src/A.go src/B.go
// go1.21.7/pkg/tool/darwin_amd64/link -V=full
func Main(args []string) {
opts, err := parseOptions(args, true)
if err != nil {
fmt.Fprintf(os.Stderr, "exec_tool: %v\n", err)
os.Exit(1)
}
logCompileEnable := opts.logCompile
if logCompileEnable {
err = initLog()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
// close the file at exit
defer compileLogFile.Close()
}
// log compile args
if logCompileEnable {
logCompile("exec_tool %s\n", strings.Join(args, " "))
}
toolArgs := opts.remainArgs
var cmd string
if len(toolArgs) > 0 {
cmd = toolArgs[0]
toolArgs = toolArgs[1:]
}
if cmd == "" {
fmt.Fprintf(os.Stderr, "exec_tool missing command\n")
os.Exit(1)
}
// on Windows, cmd ends with .exe
baseName := filepath.Base(cmd)
isCompile := baseName == "compile"
if !isCompile && runtime.GOOS == "windows" {
isCompile = baseName == "compile.exe"
}
if !isCompile {
// invoke the process as is
runCommandExit(cmd, toolArgs)
return
}
err = handleCompile(cmd, opts, toolArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func handleCompile(cmd string, opts *options, args []string) error {
if hasFlag(args, "-V") {
runCommandExit(cmd, args)
return nil
}
debugWithDlv := opts.debugWithDlv
// pkg path: the argument after the -p
pkgPath := findArgAfterFlag(args, "-p")
if pkgPath == "" {
return fmt.Errorf("compile missing -p package")
}
debugPkg := opts.debug
var isDebug bool
if debugPkg != "" {
if debugPkg == "all" || debugPkg == pkgPath {
isDebug = true
}
}
xgoCompilerEnableEnv, ok := os.LookupEnv(XGO_COMPILER_ENABLE)
if !ok {
if opts.enable {
xgoCompilerEnableEnv = "true"
}
}
compilerBin := os.Getenv(XGO_COMPILER_BIN)
if compilerBin == "" {
compilerBin = cmd
}
var withDebugHint string
if isDebug {
withDebugHint = " with debug"
}
logCompileEnable := opts.logCompile
if logCompileEnable {
logCompile("compile %s%s\n", pkgPath, withDebugHint)
}
if isDebug {
// TODO: add env
if logCompileEnable || debugWithDlv {
dlvArgs := []string{"--api-version=2",
"--listen=localhost:2345",
"--check-go-version=false",
"--log=true",
// "--accept-multiclient", // exits when client exits
"--headless", "exec",
compilerBin,
"--",
}
envs := getDebugEnv(xgoCompilerEnableEnv)
// dlvArgs = append(dlvArgs, compilerBin)
dlvArgs = append(dlvArgs, args...)
var strPrint []string
envList := make([]string, 0, len(envs))
for k, v := range envs {
envKV := fmt.Sprintf("%s=%s", k, v)
if logCompileEnable {
strPrint = append(strPrint, fmt.Sprintf(" export %s", envKV))
}
envList = append(envList, envKV)
}
if logCompileEnable {
strPrint = append(strPrint, fmt.Sprintf(" dlv %s", strings.Join(dlvArgs, " ")))
logCompile("to debug with dlv:\n%s\n", strings.Join(strPrint, "\n"))
}
if debugWithDlv {
logCompile("connect to dlv using CLI: dlv connect localhost:2345\n")
logCompile("connect to dlv remote using vscode: %s\n", vscodeRemoteDebug)
var redir io.Writer
if compileLogFile != nil {
redir = compileLogFile
} else {
redir = io.Discard
}
err := cmd_exec.New().Env(envList).Stderr(redir).Stdout(redir).Run("dlv", dlvArgs...)
if err != nil {
return fmt.Errorf("dlv: %w", err)
}
for {
time.Sleep(60 * time.Hour)
}
}
}
debugCmd := getVscodeDebugCmd(compilerBin, xgoCompilerEnableEnv, args)
debugCmdJSON, err := json.MarshalIndent(debugCmd, "", " ")
if err != nil {
return err
}
if logCompileEnable {
logCompile("%s\n", string(debugCmdJSON))
}
vscodeFile := os.Getenv(XGO_DEBUG_VSCODE)
var nowait bool
var nowrite bool
qIdx := strings.LastIndex(vscodeFile, "?")
if qIdx >= 0 {
flags := vscodeFile[qIdx+1:]
vscodeFile = vscodeFile[:qIdx]
if flags == "nowait" {
nowait = true
} else if flags == "nowrite" {
nowrite = true
}
}
if !nowrite && vscodeFile != "" {
err = addVscodeDebug(vscodeFile, debugCmd)
if err != nil {
return err
}
}
if !nowait {
// wait the vscode debugger to finish the command
// sleep forever
for {
time.Sleep(60 * time.Hour)
}
}
}
// COMPILER_ALLOW_SYNTAX_REWRITE=${COMPILER_ALLOW_SYNTAX_REWRITE:-true} COMPILER_ALLOW_IR_REWRITE=${COMPILER_ALLOW_IR_REWRITE:-true} "$shdir/compile" ${@:2}
runCommandExitFilter(compilerBin, args, func(cmd *exec.Cmd) {
cmd.Env = append(cmd.Env,
"XGO_COMPILER_ENABLE="+xgoCompilerEnableEnv,
// "XGO_COMPILER_ENABLE=false",
"COMPILER_ALLOW_SYNTAX_REWRITE=true",
"COMPILER_ALLOW_IR_REWRITE=true",
)
})
return nil
}
func hasFlag(args []string, flag string) bool {
flagEq := flag + "="
for _, arg := range args {
if arg == flag || strings.HasPrefix(arg, flagEq) {
return true
}
}
return false
}
func findArgAfterFlag(args []string, flag string) string {
for i, arg := range args {
if arg == flag {
if i+1 < len(args) {
return args[i+1]
}
}
}
return ""
}
func runCommandExit(name string, args []string) {
runCommandExitFilter(name, args, nil)
}
func runCommandExitFilter(name string, args []string, f func(cmd *exec.Cmd)) {
err := runCommand(name, args, true, f)
if err != nil {
panic(fmt.Errorf("unexpected err: %w", err))
}
}
func runCommand(name string, args []string, exit bool, f func(cmd *exec.Cmd)) error {
// invoke the process as is
cmd := exec.Command(name, args...)
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if f != nil {
f(cmd)
}
err := cmd.Run()
if err != nil {
if exit {
os.Exit(cmd.ProcessState.ExitCode())
}
return err
}
return nil
}