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
|
-- Copyright 2019 云风 https://github.com/cloudwu . All rights reserved.
-- License (the same with bgfx) : https://github.com/bkaradzic/bgfx/blob/master/LICENSE
local codegen = require "codegen"
local idl = codegen.idl "bgfx.idl"
local func_actions = {
c99 = "\n",
c99decl = "\n",
cppdecl = "\n",
interface_struct = "\n\t",
interface_import = ",\n\t\t\t",
c99_interface = "\n",
cpp_interface = "\n",
c99_functionid = "\n\t",
cpp_functionid = "\n\t\t",
}
local type_actions = {
cflags = "\n",
enums = "\n",
cenums = "\n",
structs = "\n",
cstructs = "\n",
handles = "\n",
chandles = "\n",
funcptrs = "\n",
cfuncptrs = "\n",
}
local function cfunc(f)
return function(func)
if not func.cpponly then
return f(func)
end
end
end
local funcgen = {}
local functemp = {}
functemp.interface_struct = "$CRET (*$CFUNCNAME)($CARGS);"
functemp.interface_import = "bgfx_$CFUNCNAME"
functemp.c99_interface = [[
BGFX_C_API $CRET bgfx_$CFUNCNAME($CARGS)
{
$CONVERSIONCTOC
$PRERETCTOCg_interface->$CFUNCNAME($CALLARGS);
$POSTRETCTOC
}
]]
functemp.c99_functionid = "BGFX_FUNCTION_ID_$CFUNCNAMEUPPER,"
functemp.cpp_functionid = "$CFUNCNAMECAML,"
for action,temp in pairs(functemp) do
funcgen[action] = cfunc(function(func)
return codegen.apply_functemp(func, temp)
end)
end
funcgen.cpp_interface= cfunc(function(func)
if not func.cfunc and not func.conly then
return codegen.apply_functemp(func, [[
$RET $CLASSNAME$FUNCNAME($CPPARGS)$CONST
{
$CONVERSIONCTOCPP
$PRERETCPPTOCg_interface->$CFUNCNAME($CALLARGSCPPTOC);
$POSTRETCPPTOC
}
]])
end
end)
funcgen.c99 = cfunc(function(func)
local temp
if func.cfunc then
temp = "/* BGFX_C_API $CRET bgfx_$CFUNCNAME($CARGS) */\n"
else
temp = [[
BGFX_C_API $CRET bgfx_$CFUNCNAME($CARGS)
{
$CONVERSION
$PRERET$CPPFUNC($CALLARGSCTOCPP);
$POSTRET
}
]]
end
return codegen.apply_functemp(func, temp)
end)
local function cppdecl(func)
local doc = func.comments
if not doc and func.comment then
doc = { func.comment }
end
if doc then
local cname
if not func.cpponly then
if func.multicfunc then
cname = {}
for _, name in ipairs(func.multicfunc) do
cname[#cname+1] = "bgfx_" .. name
end
else
cname = "bgfx_" .. func.cname
end
end
doc = codegen.doxygen_type(doc, func, cname)
end
local funcdecl = codegen.apply_functemp(func, "$RET $FUNCNAME($ARGS)$CONST;\n")
if doc then
return doc .. "\n" .. funcdecl
else
return funcdecl
end
end
function funcgen.cppdecl(func)
-- Don't generate member functions here
if not func.class and not func.conly then
return cppdecl(func)
end
end
funcgen.c99decl = cfunc(function(func)
local doc = func.comments
if not doc and func.comment then
doc = { func.comment }
end
if doc then
doc = codegen.doxygen_ctype(doc, func)
end
local funcdecl = codegen.apply_functemp(func, "BGFX_C_API $CRET bgfx_$CFUNCNAME($CARGS);")
if doc then
return "\n" .. doc .. "\n" .. funcdecl
else
return funcdecl
end
end)
local typegen = {}
local function add_doxygen(typedef, define, cstyle, cname)
local func = cstyle and codegen.doxygen_ctype or codegen.doxygen_type
local doc = func(typedef.comments, typedef, cname or typedef.cname)
if doc then
return doc .. "\n" .. define
else
return define
end
end
function typegen.enums(typedef)
if typedef.enum then
return add_doxygen(typedef, codegen.gen_enum_define(typedef), false, "bgfx_" .. typedef.cname)
end
end
function typegen.cenums(typedef)
if typedef.enum then
return add_doxygen(typedef, codegen.gen_enum_cdefine(typedef), true)
end
end
function typegen.cflags(typedef)
if typedef.flag then
return add_doxygen(typedef, codegen.gen_flag_cdefine(typedef), true)
end
end
function typegen.structs(typedef)
if typedef.struct and not typedef.namespace then
local methods = typedef.methods
if methods then
local m = {}
for _, func in ipairs(methods) do
m[#m+1] = cppdecl(func)
end
methods = m
end
return add_doxygen(typedef, codegen.gen_struct_define(typedef, methods))
end
end
function typegen.cstructs(typedef)
if typedef.struct then
return add_doxygen(typedef, codegen.gen_struct_cdefine(typedef), true)
end
end
function typegen.handles(typedef)
if typedef.handle then
return codegen.gen_handle(typedef)
end
end
function typegen.chandles(typedef)
if typedef.handle then
return codegen.gen_chandle(typedef)
end
end
function typegen.funcptrs(typedef)
if typedef.args then
return add_doxygen(typedef, codegen.gen_funcptr(typedef))
end
end
function typegen.cfuncptrs(typedef)
if typedef.args then
return add_doxygen(typedef, codegen.gen_cfuncptr(typedef), true)
end
end
local function codes()
local temp = {}
for k in pairs(func_actions) do
temp[k] = {}
end
for k in pairs(type_actions) do
temp[k] = {}
end
-- call actions with func
for _, f in ipairs(idl.funcs) do
for k in pairs(func_actions) do
local funcgen = funcgen[k]
if funcgen then
table.insert(temp[k], (funcgen(f)))
end
end
end
-- call actions with type
for _, typedef in ipairs(idl.types) do
for k in pairs(type_actions) do
local typegen = typegen[k]
if typegen then
table.insert(temp[k], (typegen(typedef)))
end
end
end
for k, indent in pairs(func_actions) do
temp[k] = table.concat(temp[k], indent)
end
for k, indent in pairs(type_actions) do
temp[k] = table.concat(temp[k], indent)
end
temp.version = string.format("#define BGFX_API_VERSION UINT32_C(%d)", idl._version or 0)
return temp
end
local codes_tbl = codes()
local function add_path(filename)
local path
if type(paths) == "string" then
path = paths
else
path = assert(paths[filename])
end
return path .. "/" .. filename
end
local function change_indent(str, indent)
if indent == "\t" then
-- strip trailing space only
return (str:gsub("(.-)\n", function (line)
return line:gsub("([ \t]*)$","\n") end))
else
return (str:gsub("(.-)\n", function (line)
return line:gsub("^(\t*)(.-)[ \t]*$",
function (tabs, content)
return indent:rep(#tabs) .. content .. "\n"
end)
end))
end
end
local gen = {}
function gen.apply(tempfile)
local f = assert(io.open(tempfile, "rb"))
local temp = f:read "a"
f:close()
codes_tbl.source = tempfile
return (temp:gsub("$([%l%d_]+)", codes_tbl))
end
function gen.format(codes, f)
return change_indent(codes, f.indent)
end
function gen.changed(codes, outputfile)
local out = io.open(outputfile, "rb")
if out then
local origin = out:read "a"
out:close()
return origin ~= codes
end
return true
end
function gen.write(codes, outputfile)
local out = assert(io.open(outputfile, "wb"))
out:write(codes)
out:close()
end
function gen.gen(tempfile, outputfile, indent)
print ("Generate", outputfile, "from", tempfile)
local codes = gen.apply(tempfile)
codes = change_indent(codes, indent)
if not gen.changed(codes, outputfile) then
print("No change")
end
gen.write(codes, outputfile)
end
return gen
|