bindings-zig.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local zig_template = [[
  4. // Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  5. // License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  6. //
  7. // AUTO GENERATED! DO NOT EDIT!
  8. //
  9. const std = @import("std");
  10. pub const ViewId = u16;
  11. $types
  12. $funcs
  13. ]]
  14. local function isempty(s)
  15. return s == nil or s == ''
  16. end
  17. local function hasPrefix(str, prefix)
  18. return prefix == "" or str:sub(1, #prefix) == prefix
  19. end
  20. local function hasSuffix(str, suffix)
  21. return suffix == "" or str:sub(- #suffix) == suffix
  22. end
  23. local enum = {}
  24. local function convert_array(member)
  25. if string.find(member.array, "::") then
  26. return string.format("[%s]", enum[member.array])
  27. else
  28. return member.array
  29. end
  30. end
  31. local function gisub(s, pat, repl, n)
  32. pat = string.gsub(pat, '(%a)', function(v)
  33. return '[' .. string.upper(v) .. string.lower(v) .. ']'
  34. end)
  35. if n then
  36. return string.gsub(s, pat, repl, n)
  37. else
  38. return string.gsub(s, pat, repl)
  39. end
  40. end
  41. local function convert_type_0(arg)
  42. if hasPrefix(arg.ctype, "uint64_t") then
  43. return arg.ctype:gsub("uint64_t", "u64")
  44. elseif hasPrefix(arg.ctype, "int64_t") then
  45. return arg.ctype:gsub("int64_t", "i64")
  46. elseif hasPrefix(arg.ctype, "uint32_t") then
  47. return arg.ctype:gsub("uint32_t", "u32")
  48. elseif hasPrefix(arg.ctype, "int32_t") then
  49. return arg.ctype:gsub("int32_t", "i32")
  50. elseif hasPrefix(arg.ctype, "uint16_t") then
  51. return arg.ctype:gsub("uint16_t", "u16")
  52. elseif hasPrefix(arg.ctype, "uint8_t") then
  53. return arg.ctype:gsub("uint8_t", "u8")
  54. elseif hasPrefix(arg.ctype, "uintptr_t") then
  55. return arg.ctype:gsub("uintptr_t", "usize")
  56. elseif hasPrefix(arg.ctype, "float") then
  57. return arg.ctype:gsub("float", "f32")
  58. elseif arg.ctype == "const char*" then
  59. return "[*c]const u8"
  60. elseif hasPrefix(arg.ctype, "char") then
  61. return arg.ctype:gsub("char", "u8")
  62. elseif hasSuffix(arg.fulltype, "Handle") then
  63. return arg.fulltype
  64. elseif arg.ctype == "..." then
  65. return "..."
  66. elseif arg.ctype == "va_list" or arg.fulltype == "bx::AllocatorI*" or arg.fulltype == "CallbackI*" or arg.fulltype ==
  67. "ReleaseFn" then
  68. return "?*anyopaque"
  69. end
  70. return arg.fulltype
  71. end
  72. local function convert_type(arg)
  73. local ctype = convert_type_0(arg)
  74. ctype = ctype:gsub("::Enum", "")
  75. ctype = ctype:gsub(" &", "*")
  76. ctype = ctype:gsub("&", "*")
  77. ctype = ctype:gsub("char", "u8")
  78. ctype = ctype:gsub("float", "f32")
  79. ctype = ctype:gsub("const void%*", "?*const anyopaque")
  80. ctype = ctype:gsub("Encoder%*", "?*Encoder")
  81. if hasSuffix(ctype, "void*") then
  82. ctype = ctype:gsub("void%*", "?*anyopaque");
  83. elseif hasSuffix(ctype, "*") then
  84. ctype = "[*c]" .. ctype:gsub("*", "")
  85. end
  86. if arg.array ~= nil then
  87. ctype = ctype:gsub("const ", "")
  88. ctype = convert_array(arg) .. ctype
  89. end
  90. return ctype
  91. end
  92. local function convert_struct_type(arg)
  93. return convert_type(arg)
  94. end
  95. local function convert_ret_type(arg)
  96. return convert_type(arg)
  97. end
  98. local function wrap_simple_func(func, args, argNames)
  99. local zigFunc = {}
  100. local zigFuncTemplate = [[pub inline fn $func($params) $ret {
  101. return $cfunc($args);
  102. }]]
  103. -- transform name to camelCase from snake_case
  104. zigFunc.func = func.cname:gsub("_(.)", func.cname.upper)
  105. -- make 2d/3d upper case 2D/3D
  106. zigFunc.func = zigFunc.func:gsub("%dd", zigFunc.func.upper);
  107. zigFunc.params = table.concat(args, ", ")
  108. zigFunc.ret = convert_ret_type(func.ret)
  109. zigFunc.cfunc = "bgfx_" .. func.cname
  110. zigFunc.args = table.concat(argNames, ", ")
  111. return zigFuncTemplate:gsub("$(%l+)", zigFunc)
  112. end
  113. local function wrap_method(func, type, args, argNames, indent)
  114. local zigFunc = {}
  115. local zigFuncTemplate = [[%spub inline fn $func($params) $ret {
  116. %sreturn $cfunc($args);
  117. %s}]]
  118. zigFuncTemplate = string.format(zigFuncTemplate, indent, indent, indent);
  119. -- transform name to camelCase from snake_case
  120. zigFunc.func = func.cname:gsub("_(.)", func.cname.upper)
  121. -- remove type from name
  122. zigFunc.func = gisub(zigFunc.func, type, "");
  123. -- make first letter lowercase
  124. zigFunc.func = zigFunc.func:gsub("^%L", string.lower)
  125. -- make 2d/3d upper case 2D/3D
  126. zigFunc.func = zigFunc.func:gsub("%dd", zigFunc.func.upper);
  127. zigFunc.params = table.concat(args, ", ")
  128. zigFunc.ret = convert_ret_type(func.ret)
  129. -- remove C API pointer [*c] for fluent interfaces
  130. if zigFunc.ret == ("[*c]" .. type) then
  131. zigFunc.ret = zigFunc.ret:gsub("%[%*c%]", "*")
  132. end
  133. zigFunc.cfunc = "bgfx_" .. func.cname
  134. zigFunc.args = table.concat(argNames, ", ")
  135. return zigFuncTemplate:gsub("$(%l+)", zigFunc)
  136. end
  137. local converter = {}
  138. local yield = coroutine.yield
  139. local gen = {}
  140. local indent = ""
  141. function gen.gen()
  142. -- find the functions that have `this` first argument
  143. -- these belong to a type (struct) and we need to add them when converting structures
  144. local methods = {}
  145. for _, func in ipairs(idl["funcs"]) do
  146. if func.this ~= nil then
  147. if methods[func.this_type.type] == nil then
  148. methods[func.this_type.type] = {}
  149. end
  150. table.insert(methods[func.this_type.type], func)
  151. end
  152. end
  153. local r = zig_template:gsub("$(%l+)", function(what)
  154. local tmp = {}
  155. for _, object in ipairs(idl[what]) do
  156. local co = coroutine.create(converter[what])
  157. local any
  158. -- we're pretty confident there are no types that have the same name with a func
  159. local funcs = methods[object.name]
  160. while true do
  161. local ok, v = coroutine.resume(co, {
  162. obj = object,
  163. funcs = funcs
  164. })
  165. assert(ok, debug.traceback(co, v))
  166. if not v then
  167. break
  168. end
  169. table.insert(tmp, v)
  170. any = true
  171. end
  172. if any and tmp[#tmp] ~= "" then
  173. table.insert(tmp, "")
  174. end
  175. end
  176. return table.concat(tmp, "\n")
  177. end)
  178. return r
  179. end
  180. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  181. for _, v in ipairs(combined) do
  182. combined[v] = {}
  183. end
  184. local lastCombinedFlag
  185. local function FlagBlock(typ)
  186. local format = "0x%08x"
  187. local enumType = "u32"
  188. if typ.bits == 64 then
  189. format = "0x%016x"
  190. enumType = "u64"
  191. elseif typ.bits == 16 then
  192. format = "0x%04x"
  193. enumType = "u16"
  194. end
  195. local name = typ.name .. "Flags"
  196. yield("pub const " .. name .. " = " .. enumType .. ";")
  197. for idx, flag in ipairs(typ.flag) do
  198. if flag.comment ~= nil then
  199. if idx ~= 1 then
  200. yield("")
  201. end
  202. for _, comment in ipairs(flag.comment) do
  203. yield("/// " .. comment)
  204. end
  205. end
  206. local flagName = flag.name:gsub("_", "")
  207. -- make 2d/3d upper case 2D/3D
  208. flagName = flagName:gsub("%dd", flagName.upper);
  209. yield("pub const " .. name .. "_" .. flagName .. ": " .. name .. string.rep(" ", 22 - #(flagName)) .. " = " ..
  210. string.format(flag.format or format, flag.value) .. ";")
  211. end
  212. if typ.shift then
  213. yield(" " .. "Shift" .. string.rep(" ", 22 - #("Shift")) .. " = " .. flag.shift)
  214. end
  215. -- generate Mask
  216. if typ.mask then
  217. yield(" " .. "Mask" .. string.rep(" ", 22 - #("Mask")) .. " = " .. string.format(format, flag.mask))
  218. end
  219. end
  220. local function lastCombinedFlagBlock()
  221. if lastCombinedFlag then
  222. local typ = combined[lastCombinedFlag]
  223. if typ then
  224. FlagBlock(combined[lastCombinedFlag])
  225. yield("")
  226. end
  227. lastCombinedFlag = nil
  228. end
  229. end
  230. local function convert_struct_member(member)
  231. return member.name .. ": " .. convert_struct_type(member)
  232. end
  233. local namespace = ""
  234. function converter.types(params)
  235. local typ = params.obj
  236. local funcs = params.funcs
  237. if typ.handle then
  238. lastCombinedFlagBlock()
  239. yield("pub const " .. typ.name .. " = extern struct {")
  240. yield(" idx: c_ushort,")
  241. yield("};")
  242. elseif hasSuffix(typ.name, "::Enum") then
  243. lastCombinedFlagBlock()
  244. yield("pub const " .. typ.typename .. " = enum(c_int) {")
  245. for idx, enum in ipairs(typ.enum) do
  246. if enum.comment ~= nil then
  247. if idx ~= 1 then
  248. yield("")
  249. end
  250. for _, comment in ipairs(enum.comment) do
  251. yield(" /// " .. comment)
  252. end
  253. end
  254. yield(" " .. enum.name .. ",")
  255. end
  256. yield("");
  257. yield(" Count")
  258. yield("};")
  259. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  260. elseif typ.bits ~= nil then
  261. local prefix, name = typ.name:match "(%u%l+)(.*)"
  262. if prefix ~= lastCombinedFlag then
  263. lastCombinedFlagBlock()
  264. lastCombinedFlag = prefix
  265. end
  266. local combinedFlag = combined[prefix]
  267. if combinedFlag then
  268. combinedFlag.bits = typ.bits
  269. combinedFlag.name = prefix
  270. local flags = combinedFlag.flag or {}
  271. combinedFlag.flag = flags
  272. local lookup = combinedFlag.lookup or {}
  273. combinedFlag.lookup = lookup
  274. for _, flag in ipairs(typ.flag) do
  275. local flagName = name .. flag.name:gsub("_", "")
  276. local value = flag.value
  277. if value == nil then
  278. -- It's a combined flag
  279. value = 0
  280. for _, v in ipairs(flag) do
  281. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  282. end
  283. end
  284. lookup[flagName] = value
  285. table.insert(flags, {
  286. name = flagName,
  287. value = value,
  288. comment = flag.comment
  289. })
  290. end
  291. if typ.shift then
  292. table.insert(flags, {
  293. name = name .. "Shift",
  294. value = typ.shift,
  295. format = "%d",
  296. comment = typ.comment
  297. })
  298. end
  299. if typ.mask then
  300. -- generate Mask
  301. table.insert(flags, {
  302. name = name .. "Mask",
  303. value = typ.mask,
  304. comment = typ.comment
  305. })
  306. lookup[name .. "Mask"] = typ.mask
  307. end
  308. else
  309. FlagBlock(typ)
  310. end
  311. elseif typ.struct ~= nil then
  312. local skip = false
  313. if typ.namespace ~= nil then
  314. if namespace ~= typ.namespace then
  315. yield("pub const " .. typ.namespace .. " = extern struct {")
  316. namespace = typ.namespace
  317. indent = " "
  318. end
  319. elseif namespace ~= "" then
  320. indent = " "
  321. namespace = ""
  322. skip = true
  323. end
  324. if not skip then
  325. if typ.name ~= "Encoder" then
  326. yield(indent .. "pub const " .. typ.name .. " = extern struct {")
  327. else
  328. yield(indent .. "pub const " .. typ.name .. " = opaque {")
  329. end
  330. end
  331. for _, member in ipairs(typ.struct) do
  332. yield(indent .. indent .. convert_struct_member(member) .. ",")
  333. end
  334. if funcs ~= nil then
  335. for _, func in ipairs(funcs) do
  336. converter.funcs({
  337. obj = func,
  338. asMethod = true
  339. })
  340. end
  341. end
  342. yield(indent .. "};")
  343. end
  344. end
  345. function converter.funcs(params)
  346. local func = params.obj
  347. if func.cpponly then
  348. return
  349. elseif func.cppinline and not func.conly then
  350. return
  351. end
  352. -- skip for now, don't know how to handle variadic functions
  353. if func.cname == "dbg_text_printf" or func.cname == "dbg_text_vprintf" then
  354. return
  355. end
  356. local func_indent = (params.asMethod == true and indent .. indent or "")
  357. if func.comments ~= nil then
  358. for _, line in ipairs(func.comments) do
  359. yield(func_indent .. "/// " .. line)
  360. end
  361. local hasParams = false
  362. for _, arg in ipairs(func.args) do
  363. if arg.comment ~= nil then
  364. local comment = table.concat(arg.comment, " ")
  365. yield(func_indent .. "/// <param name=\"" .. arg.name .. "\">" .. comment .. "</param>")
  366. hasParams = true
  367. end
  368. end
  369. end
  370. local args = {}
  371. local argNames = {}
  372. if func.this ~= nil then
  373. local ptr = "[*c]"
  374. if params.asMethod == true then
  375. ptr = "*"
  376. end
  377. -- print("Function " .. func.name .. " has this: " .. func.this_type.type)
  378. if func.const ~= nil then
  379. ptr = ptr .. "const "
  380. end
  381. if func.this_type.type == "Encoder" then
  382. ptr = "?*"
  383. end
  384. args[1] = "self: " .. ptr .. func.this_type.type
  385. argNames[1] = "self"
  386. end
  387. for _, arg in ipairs(func.args) do
  388. local argName = arg.name
  389. -- local argName = arg.name:gsub("_", "")
  390. -- argName = argName:gsub("enum", "enumeration")
  391. -- argName = argName:gsub("type_", '@"type"')
  392. if not isempty(argName) then
  393. table.insert(argNames, argName)
  394. table.insert(args, argName .. ": " .. convert_type(arg))
  395. else
  396. table.insert(args, convert_type(arg))
  397. end
  398. end
  399. if (params.asMethod == true) then
  400. yield(wrap_method(func, func.this_type.type, args, argNames, func_indent))
  401. else
  402. if func.this == nil then
  403. yield(wrap_simple_func(func, args, argNames))
  404. end
  405. yield(
  406. "extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") " .. convert_ret_type(func.ret) ..
  407. ";")
  408. end
  409. end
  410. function gen.write(codes, outputfile)
  411. local out = assert(io.open(outputfile, "wb"))
  412. out:write(codes)
  413. out:close()
  414. print("Generating: " .. outputfile)
  415. end
  416. if (...) == nil then
  417. -- run `lua bindings-zig.lua` in command line
  418. print(gen.gen())
  419. end
  420. return gen