-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
219 lines (187 loc) · 5.27 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"gobject/gi"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var config config_type
type config_type struct {
out_go *bufio.Writer
out_c *bufio.Writer
out_h *bufio.Writer
namespace string
version string
pkg string
gns string
renames map[string]string
blacklist map[string]map[string]bool
whitelist map[string]map[string]bool
method_blacklist map[string]map[string]bool
method_whitelist map[string]map[string]bool
disguised_types map[string]bool
word_subst map[string]string
}
func (this *config_type) load(path string) {
var tmp struct {
Namespace string `json:"namespace"`
Version string `json:"version"`
Blacklist map[string][]string `json:"blacklist"`
Whitelist map[string][]string `json:"whitelist"`
MethodBlacklist map[string][]string `json:"method-blacklist"`
MethodWhitelist map[string][]string `json:"method-whitelist"`
Renames map[string]string `json:"renames"`
}
err := parse_json_with_comments(path, &tmp)
if err != nil {
panic(err)
}
this.namespace = tmp.Namespace
this.version = tmp.Version
this.blacklist = map_list_to_map_map(tmp.Blacklist)
this.whitelist = map_list_to_map_map(tmp.Whitelist)
this.method_blacklist = map_list_to_map_map(tmp.MethodBlacklist)
this.method_whitelist = map_list_to_map_map(tmp.MethodWhitelist)
this.renames = tmp.Renames
this.pkg = strings.ToLower(tmp.Namespace)
if this.namespace != "GObject" {
this.gns = "gobject."
}
}
func (this *config_type) load_sys(path string) {
var tmp struct {
DisguisedTypes []string `json:"disguised-types"`
WordSubst map[string]string `json:"word-subst"`
}
err := parse_json_with_comments(path, &tmp)
if err != nil {
panic(err)
}
this.disguised_types = list_to_map(tmp.DisguisedTypes)
this.word_subst = tmp.WordSubst
}
func (this *config_type) rename(path, oldname string) string {
if newname, ok := this.renames[path]; ok {
return newname
}
return oldname
}
func (this *config_type) is_disguised(name string) bool {
_, ok := this.disguised_types[name]
return ok
}
func (this *config_type) is_object_blacklisted(bi *gi.BaseInfo) bool {
switch bi.Type() {
case gi.INFO_TYPE_UNION:
return config.is_blacklisted("unions", bi.Name())
case gi.INFO_TYPE_STRUCT:
return config.is_blacklisted("structs", bi.Name())
case gi.INFO_TYPE_ENUM, gi.INFO_TYPE_FLAGS:
return config.is_blacklisted("enums", bi.Name())
case gi.INFO_TYPE_CONSTANT:
return config.is_blacklisted("constants", bi.Name())
case gi.INFO_TYPE_CALLBACK:
return config.is_blacklisted("callbacks", bi.Name())
case gi.INFO_TYPE_FUNCTION:
c := bi.Container()
if c != nil {
return config.is_method_blacklisted(c.Name(), bi.Name())
}
return config.is_blacklisted("functions", bi.Name())
case gi.INFO_TYPE_INTERFACE:
return config.is_blacklisted("interfaces", bi.Name())
case gi.INFO_TYPE_OBJECT:
return config.is_blacklisted("objects", bi.Name())
default:
println("TODO: %s (%s)\n", bi.Name(), bi.Type())
return true
}
panic("unreachable")
}
func (this *config_type) is_blacklisted(section, entry string) bool {
// check if the entry is in the blacklist
if section_map, ok := this.blacklist[section]; ok {
if _, ok := section_map[entry]; ok {
return true
}
}
// check if the entry is missing from the whitelist
if section_map, ok := this.whitelist[section]; ok {
if _, ok := section_map[entry]; !ok {
return true
}
}
return false
}
func (this *config_type) is_method_blacklisted(class, method string) bool {
// don't want to see these
if method == "ref" || method == "unref" {
return true
}
if class_map, ok := this.method_blacklist[class]; ok {
if _, ok := class_map[method]; ok {
return true
}
}
if class_map, ok := this.method_whitelist[class]; ok {
if _, ok := class_map[method]; !ok {
return true
}
}
return false
}
func parse_json_with_comments(filename string, data interface{}) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
d := json.NewDecoder(new_comment_skipper(f))
err = d.Decode(data)
if err != nil {
return err
}
return nil
}
func main() {
sysconfig_path := flag.String("config", "", "specify global config file")
output_dir := flag.String("o", "", "override output directory")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <dir>\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
return
}
// figure in/out paths
in_dir, in_base := filepath.Split(flag.Arg(0))
in_path := flag.Arg(0)
out_dir := in_dir
if *output_dir != "" {
out_dir = *output_dir
}
out_base := filepath.Join(out_dir, in_base[:len(in_base)-6])
// parse system config file
if *sysconfig_path != "" {
config.load_sys(*sysconfig_path)
}
// parse local config
config.load(filepath.Join(in_dir, "config.json"))
// load namespace
_, err := gi.DefaultRepository().Require(config.namespace, config.version, 0)
panic_if_error(err)
// load go template
go_template, err := ioutil.ReadFile(in_path)
panic_if_error(err)
// generate bindings
bg := new_binding_generator(out_base)
defer bg.release()
bg.generate(string(go_template))
}