-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcompile_test.go
129 lines (111 loc) · 2.85 KB
/
compile_test.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
package cxgo
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/gotranspile/cxgo/libs"
"github.com/gotranspile/cxgo/types"
)
func gccCompile(t testing.TB, out, cfile string) {
var buf bytes.Buffer
cmd := exec.Command("gcc", "-O0", "-o", out, cfile)
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
if err != nil {
t.Fatalf("gcc compilation failed: %v\n%s", err, buf.String())
}
}
func gccCompileAndExec(t testing.TB, dir, cfile string) progOut {
cbin := filepath.Join(dir, "out")
gccCompile(t, cbin, cfile)
return progExecute(dir, cbin)
}
func goCompile(t testing.TB, out, dir string) {
var buf bytes.Buffer
cmd := exec.Command("go", "build", "-o", out, ".")
cmd.Dir = dir
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
if err != nil {
t.Fatalf("go compilation failed: %v\n%s", err, buf.String())
}
}
func goCompileAndExec(t testing.TB, dir string) progOut {
gobin := filepath.Join(dir, "out")
goCompile(t, gobin, dir)
return progExecute(dir, gobin)
}
func goTranspileAndExec(t testing.TB, cxgo, dir string, cfile string) progOut {
goProject(t, dir, cxgo)
gofile := filepath.Join(dir, "main.go")
env := libs.NewEnv(types.Config32())
err := Translate(filepath.Dir(cfile), cfile, dir, env, Config{
Package: "main",
GoFile: gofile,
MaxDecls: -1,
ForwardDecl: false,
})
require.NoError(t, err)
goProjectMod(t, dir)
gosrc, err := os.ReadFile(gofile)
require.NoError(t, err)
t.Logf("// === Go source ===\n%s", string(gosrc))
return goCompileAndExec(t, dir)
}
type progOut struct {
Code int
Err error
Out string
}
func execInDir(wd string, bin string, args ...string) error {
cmd := exec.Command(bin, args...)
cmd.Dir = wd
return cmd.Run()
}
func progExecute(wd, bin string) progOut {
var buf bytes.Buffer
cmd := exec.Command(bin)
cmd.Dir = wd
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
out := progOut{
Err: err,
Out: buf.String(),
}
if e, ok := err.(*exec.ExitError); ok && e.Exited() {
out.Code = e.ExitCode()
out.Err = nil
}
return out
}
func goProject(t testing.TB, out, cxgo string) {
cxgo, err := filepath.Abs(cxgo)
require.NoError(t, err)
err = os.MkdirAll(out, 0755)
require.NoError(t, err)
gomod := fmt.Sprintf(`module main
go 1.20
require (
github.com/gotranspile/cxgo v0.0.0-local
)
replace github.com/gotranspile/cxgo v0.0.0-local => %s`, cxgo)
err = os.WriteFile(filepath.Join(out, "go.mod"), []byte(gomod), 0644)
require.NoError(t, err)
// allows running go mod tidy without having other source files and still keep require above
err = os.WriteFile(filepath.Join(out, "dummy.go"), []byte(`
package main
import _ "github.com/gotranspile/cxgo/runtime/libc"
`), 0644)
require.NoError(t, err)
}
func goProjectMod(t testing.TB, out string) {
err := execInDir(out, "go", "mod", "tidy")
require.NoError(t, err)
}