/
binding-generator.go
170 lines (149 loc) · 3.63 KB
/
binding-generator.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
package main
import (
"encoding/json"
"fmt"
"gogi"
"io/ioutil"
"path/filepath"
"os"
//"os/exec"
"strings"
)
type Deps struct {
Pkgs []string
Headers []string
Typedefs map[string]string
Imports []string
}
var knownPackages map[string] Deps
var common string
func CreatePackageRoot(pkg string) string {
root := filepath.Join("src/gi", pkg)
os.Remove(root) ; os.MkdirAll(root, os.ModePerm)
return root
}
func OpenSourceFile(root, pkg string) *os.File {
f, _ := os.Create(filepath.Join(root, pkg + ".go"))
return f
}
func Process(namespace string) {
infos := gogi.GetInfos(namespace)
fmt.Printf("Generating bindings for %s...\n", namespace)
var c_code string
var go_code string
for _, info := range infos {
if info.IsDeprecated() {
continue
}
var g, c string
switch info.Type {
case gogi.Object:
g, c = gogi.WriteObject(info)
case gogi.Struct:
g, c = gogi.WriteStruct(info)
case gogi.Enum, gogi.Flags:
g, c = gogi.WriteEnum(info)
case gogi.Function:
g, c = gogi.WriteFunction(info, nil)
default:
//fmt.Printf("unknown info '%s' of type %s\n", info.GetName(), gogi.InfoTypeToString(info.Type))
}
/*
if info.Type == gogi.Object {
switch info.GetName() {
case "Window", "Bin", "Container", "Widget", "InitiallyUnowned", "Object":
g, c := gogi.WriteObject(info)
go_code += g + "\n"
if c != "" {
c_code += c + "\n"
}
}
} else if info.Type == gogi.Enum {
g, c := gogi.WriteEnum(info)
go_code += g + "\n"
if c != "" {
c_code += c + "\n"
}
}
*/
if g != "" { go_code += g + "\n" }
if c != "" { c_code += c + "\n" }
}
pkg := strings.ToLower(namespace)
deps, deps_exist := knownPackages[namespace]
pkg_root := CreatePackageRoot(pkg)
f := OpenSourceFile(pkg_root, pkg)
f.WriteString("package " + pkg + "\n\n")
f.WriteString("/*\n")
if deps_exist {
f.WriteString(fmt.Sprintf("#cgo pkg-config: %s\n", strings.Join(deps.Pkgs, " ")))
for _, header := range deps.Headers {
f.WriteString(fmt.Sprintf("#include <%s>\n", header))
}
for key, value := range deps.Typedefs {
f.WriteString(fmt.Sprintf("#define %s %s\n", key, value))
}
f.WriteString("\n")
f.WriteString("GList *EMPTY_GLIST = NULL;\n")
}
f.WriteString(c_code + "\n")
f.WriteString("*/\nimport \"C\"\n")
for _, imp := range deps.Imports {
f.WriteString(fmt.Sprintf("import \"%s\"\n", imp))
}
// TODO: find a way to keep track of additional imports
//f.WriteString("import \"unsafe\"\n\n")
f.WriteString(go_code)
f.WriteString("\n" + common)
f.Close()
// now build it
/*
println("Compiling...")
cmd := exec.Command("go", "install", pkg)
err = cmd.Run()
if err != nil {
println(err.Error())
}
*/
}
func main() {
if len(os.Args) < 2 {
fmt.Println("usage: go run binding-generator.go <namespace>")
return
}
var err error
knownPackages = make(map[string]Deps)
deps_config, _ := os.Open("deps.json")
deps_decoder := json.NewDecoder(deps_config)
err = deps_decoder.Decode(&knownPackages)
if err != nil {
println(err.Error())
}
deps_config.Close()
namespace := os.Args[1]
gogi.Init()
loaded := gogi.LoadNamespace(namespace)
if !loaded {
fmt.Printf("Failed to load namespace '%s'\n", namespace)
return
}
var commonBytes []byte
commonBytes, err = ioutil.ReadFile("misc/common.go")
if err != nil {
fmt.Println("Failed to read misc/common.go")
return
} else {
common = string(commonBytes)
}
/*
dependencies := gogi.GetDependencies(namespace)
for _, dep := range dependencies {
nameAndVersion := strings.Split(dep, "-")
name := nameAndVersion[0]
//version := nameAndVersion[1]
Process(name)
}
*/
Process(namespace)
fmt.Println("done.")
}