-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
334 lines (298 loc) · 8.07 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
"go/ast"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/xhd2015/xgo/support/git"
"github.com/xhd2015/xgo/support/goparse"
"github.com/xhd2015/xgo/support/transform"
"github.com/xhd2015/xgo/support/filecopy"
)
type GenernateType string
const (
GenernateType_CompilerPatch GenernateType = "compiler-patch"
GenernateType_CompilerHelperCode GenernateType = "compiler-helper-code"
GenernateType_RuntimeDef GenernateType = "runtime-def"
GenernateType_StackTraceDef GenernateType = "stack-trace-def"
GenernateType_InstallSrc GenernateType = "install-src"
GenernateType_XgoRuntime GenernateType = "xgo-runtime"
)
func main() {
args := os.Args[1:]
var rootDir string
var subGens []GenernateType
n := len(args)
for i := 0; i < n; i++ {
arg := args[i]
if arg == "--root-dir" {
rootDir = args[i+1]
i++
continue
}
if !strings.HasPrefix(arg, "-") {
subGens = append(subGens, GenernateType(arg))
continue
}
fmt.Fprintf(os.Stderr, "unrecognized flag: %s\n", arg)
os.Exit(1)
}
err := generate(rootDir, subGens)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
type SubGens []GenernateType
func (c SubGens) Has(genType GenernateType) bool {
if len(c) == 0 {
return true
}
for _, subGen := range c {
if subGen == genType {
return true
}
}
return false
}
func generate(rootDir string, subGens SubGens) error {
if rootDir == "" {
resolvedRoot, err := git.ShowTopLevel("")
if err != nil {
return err
}
rootDir = resolvedRoot
}
if subGens.Has(GenernateType_CompilerPatch) {
err := generateCompilerPatch(rootDir)
if err != nil {
return err
}
}
if subGens.Has(GenernateType_RuntimeDef) {
err := generateRunTimeDefs(
filepath.Join(rootDir, "patch", "trap_runtime", "xgo_trap.go"),
filepath.Join(rootDir, "cmd", "xgo", "patch", "runtime_def_gen.go"),
filepath.Join(rootDir, "patch", "syntax", "syntax_gen.go"),
filepath.Join(rootDir, "patch", "trap_gen.go"),
)
if err != nil {
return err
}
}
if subGens.Has(GenernateType_StackTraceDef) {
err := copyTraceExport(
filepath.Join(rootDir, "runtime", "trace", "stack_export.go"),
filepath.Join(rootDir, "cmd", "xgo", "trace", "stack_export.go"),
)
if err != nil {
return err
}
}
if subGens.Has(GenernateType_CompilerHelperCode) {
info, err := generateFuncHelperCode(filepath.Join(rootDir, "patch", "syntax", "helper_code.go"))
if err != nil {
return err
}
infoCode := info.formatCode("syntax")
err = os.WriteFile(filepath.Join(rootDir, "patch", "syntax", "helper_code_gen.go"), []byte(infoCode), 0755)
if err != nil {
return err
}
}
if subGens.Has(GenernateType_InstallSrc) {
upgradeDst := filepath.Join(rootDir, "script", "install", "upgrade")
err := os.RemoveAll(upgradeDst)
if err != nil {
return err
}
err = copyUpgrade(filepath.Join(rootDir, "cmd", "xgo", "upgrade"), upgradeDst)
if err != nil {
return err
}
}
if subGens.Has(GenernateType_XgoRuntime) {
err := genXgoRuntime(rootDir)
if err != nil {
return err
}
}
return nil
}
const prelude = "// Code generated by script/generate; DO NOT EDIT.\n" + "\n"
func generateRunTimeDefs(file string, defFile string, syntaxFile string, trapFile string) error {
content, err := os.ReadFile(file)
if err != nil {
return err
}
code := string(content)
astFile, fset, err := parseGoFile(file, true)
if err != nil {
return err
}
var decls []string
var sigRegisterFunc string
var sigTrap string
for _, decl := range astFile.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if fn.Recv != nil {
continue
}
fnName := fn.Name.Name
if !strings.HasPrefix(fnName, "__xgo") {
continue
}
funcType := fn.Type
if fn.Name.Name == "__xgo_register_func" {
sigRegisterFunc = getSignature(code, fset, funcType)
} else if fn.Name.Name == "__xgo_trap" {
sigTrap = getSignature(code, fset, funcType)
}
decls = append(decls, getSlice(code, fset, funcType.Pos(), funcType.End()))
}
if sigRegisterFunc == "" {
return fmt.Errorf("__xgo_register_func not found")
}
if sigTrap == "" {
return fmt.Errorf("__xgo_trap not found")
}
declCode := "// xgo\n" + strings.Join(decls, "\n")
defGenCode := prelude + "package patch\n" + "\n" + "//go:generate go run ../../../script/generate " + string(GenernateType_RuntimeDef) + "\n" + "const RuntimeExtraDef = `\n" + declCode + "`"
syntaxGenCode := prelude + "package syntax\n" + "\n" + "const sig_gen__xgo_register_func = `" + sigRegisterFunc + "`"
trapGenCode := prelude + "package patch\n" + "\n" + "const sig_gen__xgo_trap = `" + sigTrap + "`"
err = ioutil.WriteFile(defFile, []byte(defGenCode), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(syntaxFile, []byte(syntaxGenCode), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(trapFile, []byte(trapGenCode), 0755)
if err != nil {
return err
}
return nil
}
type genInfo struct {
funcStub string
helperCode string
}
func (c *genInfo) formatCode(pkgName string) string {
codes := []string{
prelude,
fmt.Sprintf("package %s", pkgName),
}
codes = append(codes, fmt.Sprintf("const __xgo_stub_def = `%s`", c.funcStub))
codes = append(codes, "")
codes = append(codes, fmt.Sprintf("const helperCodeGen = `%s`", c.helperCode))
codes = append(codes, "")
return strings.Join(codes, "\n")
}
func generateFuncHelperCode(srcFile string) (*genInfo, error) {
astFile, fset, err := parseGoFile(srcFile, true)
if err != nil {
return nil, err
}
funcStub := transform.GetTypeDecl(astFile.Decls, "__xgo_local_func_stub")
if funcStub == nil {
return nil, fmt.Errorf("type __xgo_local_func_stub not found")
}
st, ok := funcStub.Type.(*ast.StructType)
if !ok {
return nil, fmt.Errorf("expect __xgo_local_func_stub to be StructType, actual: %T", funcStub.Type)
}
codeBytes, err := os.ReadFile(srcFile)
if err != nil {
return nil, err
}
code := string(codeBytes)
funcStubCode := getSlice(code, fset, st.Pos(), st.End())
// TODO: use astFile.FileEnd
var fileEnd token.Pos
fileEnd = astFile.End()
helperCode := getSlice(code, fset, astFile.Name.End(), fileEnd)
return &genInfo{
funcStub: funcStubCode,
helperCode: helperCode,
}, nil
}
func copyTraceExport(srcFile string, targetFile string) error {
contentBytes, err := os.ReadFile(srcFile)
if err != nil {
return err
}
content := string(contentBytes)
// replace package
content = strings.ReplaceAll(content, "package trace", "package trace")
content = prelude + content
return os.WriteFile(targetFile, []byte(content), 0755)
}
func copyUpgrade(srcDir string, targetDir string) error {
err := filecopy.CopyReplaceDir(srcDir, targetDir, false)
if err != nil {
return err
}
files, err := os.ReadDir(targetDir)
if err != nil {
return err
}
for _, file := range files {
fullFile := filepath.Join(targetDir, file.Name())
content, err := os.ReadFile(fullFile)
if err != nil {
return err
}
content = append([]byte(prelude), content...)
err = os.WriteFile(fullFile, content, 0755)
if err != nil {
return err
}
}
return nil
}
func getSlice(code string, fset *token.FileSet, start token.Pos, end token.Pos) string {
i := fset.Position(start).Offset
j := fset.Position(end).Offset
return code[i:j]
}
func getSignature(code string, fset *token.FileSet, funcType *ast.FuncType) string {
var end token.Pos
if funcType.Results != nil {
end = funcType.Results.End()
} else {
end = funcType.Params.End()
}
return "func" + getSlice(code, fset, funcType.Params.Pos(), end)
}
func parseGoFile(file string, hasPkg bool) (*ast.File, *token.FileSet, error) {
fileName := file
var contentReader io.Reader
if file == "-" {
fileName = "<stdin>"
contentReader = os.Stdin
} else {
readFile, err := os.Open(file)
if err != nil {
return nil, nil, err
}
contentReader = readFile
}
content, err := ioutil.ReadAll(contentReader)
if err != nil {
return nil, nil, err
}
if !hasPkg {
contentStr := string(content)
contentStr = goparse.AddMissingPackage(contentStr, "main")
content = []byte(contentStr)
}
return goparse.ParseFileCode(fileName, content)
}