bindings-zig.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local zig_template = [[
  4. // Copyright 2011-2022 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. $types
  11. $funcs
  12. ]]
  13. local function isempty(s)
  14. return s == nil or s == ''
  15. end
  16. local function hasPrefix(str, prefix)
  17. return prefix == "" or str:sub(1, #prefix) == prefix
  18. end
  19. local function hasSuffix(str, suffix)
  20. return suffix == "" or str:sub(- #suffix) == suffix
  21. end
  22. local function convert_type_0(arg)
  23. if hasPrefix(arg.ctype, "uint64_t") then
  24. return arg.ctype:gsub("uint64_t", "u64")
  25. elseif hasPrefix(arg.ctype, "int64_t") then
  26. return arg.ctype:gsub("int64_t", "i64")
  27. elseif hasPrefix(arg.ctype, "uint32_t") then
  28. return arg.ctype:gsub("uint32_t", "u32")
  29. elseif hasPrefix(arg.ctype, "int32_t") then
  30. return arg.ctype:gsub("int32_t", "i32")
  31. elseif hasPrefix(arg.ctype, "uint16_t") then
  32. return arg.ctype:gsub("uint16_t", "u16")
  33. elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
  34. return arg.ctype:gsub("bgfx_view_id_t", "u16")
  35. elseif hasPrefix(arg.ctype, "uint8_t") then
  36. return arg.ctype:gsub("uint8_t", "u8")
  37. elseif hasPrefix(arg.ctype, "uintptr_t") then
  38. return arg.ctype:gsub("uintptr_t", "usize")
  39. elseif hasPrefix(arg.ctype, "float") then
  40. return arg.ctype:gsub("float", "f32")
  41. -- elseif arg.ctype == "bgfx_caps_gpu_t" then
  42. -- return arg.ctype:gsub("bgfx_caps_gpu_t", "u32")
  43. elseif arg.ctype == "const char*" then
  44. return "[*c]const u8"
  45. elseif hasPrefix(arg.ctype, "char") then
  46. return arg.ctype:gsub("char", "u8")
  47. elseif hasSuffix(arg.fulltype, "Handle") then
  48. return arg.fulltype
  49. elseif arg.ctype == "..." then
  50. return "..."
  51. elseif arg.ctype == "va_list"
  52. or arg.fulltype == "bx::AllocatorI*"
  53. or arg.fulltype == "CallbackI*"
  54. or arg.fulltype == "ReleaseFn" then
  55. return "?*anyopaque"
  56. elseif arg.fulltype == "const ViewId*" then
  57. return "[*c]c_ushort"
  58. end
  59. return arg.fulltype
  60. end
  61. local function convert_type(arg)
  62. local ctype = convert_type_0(arg)
  63. ctype = ctype:gsub("::Enum", "")
  64. ctype = ctype:gsub(" &", "*")
  65. ctype = ctype:gsub("&", "*")
  66. ctype = ctype:gsub("char", "u8")
  67. ctype = ctype:gsub("float", "f32")
  68. if hasPrefix(ctype, "const ") then
  69. ctype = ctype:gsub("const ", "")
  70. end
  71. if hasSuffix(ctype, "void*") then
  72. ctype = ctype:gsub("void%*", "?*anyopaque");
  73. elseif hasSuffix(ctype, "*") then
  74. ctype = "[*c]" .. ctype:gsub("*", "")
  75. end
  76. return ctype
  77. end
  78. local function convert_struct_type(arg)
  79. local ctype = convert_type(arg)
  80. if hasPrefix(arg.ctype, "bool") then
  81. ctype = ctype:gsub("bool", "bool")
  82. end
  83. return ctype
  84. end
  85. local function convert_ret_type(arg)
  86. return convert_type(arg)
  87. end
  88. local converter = {}
  89. local yield = coroutine.yield
  90. local indent = ""
  91. local gen = {}
  92. function gen.gen()
  93. local r = zig_template:gsub("$(%l+)", function(what)
  94. local tmp = {}
  95. for _, object in ipairs(idl[what]) do
  96. local co = coroutine.create(converter[what])
  97. local any
  98. while true do
  99. local ok, v = coroutine.resume(co, object)
  100. assert(ok, debug.traceback(co, v))
  101. if not v then
  102. break
  103. end
  104. table.insert(tmp, v)
  105. any = true
  106. end
  107. if any and tmp[#tmp] ~= "" then
  108. table.insert(tmp, "")
  109. end
  110. end
  111. return table.concat(tmp, "\n")
  112. end)
  113. return r
  114. end
  115. -- function gen.gen_dllname()
  116. -- return csharp_dllname_template
  117. -- end
  118. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  119. for _, v in ipairs(combined) do
  120. combined[v] = {}
  121. end
  122. local lastCombinedFlag
  123. local function FlagBlock(typ)
  124. local format = "0x%08x"
  125. local enumType = "c_uint"
  126. if typ.bits == 64 then
  127. format = "0x%016x"
  128. enumType = "c_ulong"
  129. elseif typ.bits == 16 then
  130. format = "0x%04x"
  131. enumType = "c_ushort"
  132. end
  133. yield("pub const " .. typ.name .. "Flags = enum(" .. enumType .. ") {")
  134. for idx, flag in ipairs(typ.flag) do
  135. if flag.comment ~= nil then
  136. if idx ~= 1 then
  137. yield("")
  138. end
  139. for _, comment in ipairs(flag.comment) do
  140. yield(" /// " .. comment)
  141. end
  142. end
  143. local flagName = flag.name:gsub("_", "")
  144. yield(" "
  145. .. flagName
  146. .. string.rep(" ", 22 - #(flagName))
  147. .. " = "
  148. .. string.format(flag.format or format, flag.value)
  149. .. ","
  150. )
  151. end
  152. if typ.shift then
  153. yield(" "
  154. .. "Shift"
  155. .. string.rep(" ", 22 - #("Shift"))
  156. .. " = "
  157. .. flag.shift
  158. )
  159. end
  160. -- generate Mask
  161. if typ.mask then
  162. yield(" "
  163. .. "Mask"
  164. .. string.rep(" ", 22 - #("Mask"))
  165. .. " = "
  166. .. string.format(format, flag.mask)
  167. )
  168. end
  169. yield("};")
  170. end
  171. local function lastCombinedFlagBlock()
  172. if lastCombinedFlag then
  173. local typ = combined[lastCombinedFlag]
  174. if typ then
  175. FlagBlock(combined[lastCombinedFlag])
  176. yield("")
  177. end
  178. lastCombinedFlag = nil
  179. end
  180. end
  181. local enum = {}
  182. local function convert_array(member)
  183. if string.find(member.array, "::") then
  184. return string.format("[%d]", enum[member.array])
  185. else
  186. return member.array
  187. end
  188. end
  189. local function convert_struct_member(member)
  190. if member.array then
  191. return member.name .. ": " .. convert_array(member) .. convert_struct_type(member)
  192. else
  193. return member.name .. ": " .. convert_struct_type(member)
  194. end
  195. end
  196. local namespace = ""
  197. function converter.types(typ)
  198. if typ.handle then
  199. lastCombinedFlagBlock()
  200. yield("pub const " .. typ.name .. " = extern struct {")
  201. yield(" idx: c_ushort,")
  202. yield("};")
  203. elseif hasSuffix(typ.name, "::Enum") then
  204. lastCombinedFlagBlock()
  205. yield("pub const " .. typ.typename .. " = enum(c_int) {")
  206. for idx, enum in ipairs(typ.enum) do
  207. if enum.comment ~= nil then
  208. if idx ~= 1 then
  209. yield("")
  210. end
  211. for _, comment in ipairs(enum.comment) do
  212. yield(" /// " .. comment)
  213. end
  214. end
  215. yield(" " .. enum.name .. ",")
  216. end
  217. yield("");
  218. yield(" Count")
  219. yield("};")
  220. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  221. elseif typ.bits ~= nil then
  222. local prefix, name = typ.name:match "(%u%l+)(.*)"
  223. if prefix ~= lastCombinedFlag then
  224. lastCombinedFlagBlock()
  225. lastCombinedFlag = prefix
  226. end
  227. local combinedFlag = combined[prefix]
  228. if combinedFlag then
  229. combinedFlag.bits = typ.bits
  230. combinedFlag.name = prefix
  231. local flags = combinedFlag.flag or {}
  232. combinedFlag.flag = flags
  233. local lookup = combinedFlag.lookup or {}
  234. combinedFlag.lookup = lookup
  235. for _, flag in ipairs(typ.flag) do
  236. local flagName = name .. flag.name:gsub("_", "")
  237. local value = flag.value
  238. if value == nil then
  239. -- It's a combined flag
  240. value = 0
  241. for _, v in ipairs(flag) do
  242. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  243. end
  244. end
  245. lookup[flagName] = value
  246. table.insert(flags, {
  247. name = flagName,
  248. value = value,
  249. comment = flag.comment,
  250. })
  251. end
  252. if typ.shift then
  253. table.insert(flags, {
  254. name = name .. "Shift",
  255. value = typ.shift,
  256. format = "%d",
  257. comment = typ.comment,
  258. })
  259. end
  260. if typ.mask then
  261. -- generate Mask
  262. table.insert(flags, {
  263. name = name .. "Mask",
  264. value = typ.mask,
  265. comment = typ.comment,
  266. })
  267. lookup[name .. "Mask"] = typ.mask
  268. end
  269. else
  270. FlagBlock(typ)
  271. end
  272. elseif typ.struct ~= nil then
  273. local skip = false
  274. if typ.namespace ~= nil then
  275. if namespace ~= typ.namespace then
  276. yield("pub const " .. typ.namespace .. " = extern struct {")
  277. -- yield("{")
  278. namespace = typ.namespace
  279. indent = " "
  280. end
  281. elseif namespace ~= "" then
  282. indent = " "
  283. namespace = ""
  284. skip = true
  285. end
  286. if not skip then
  287. yield(indent .. "pub const " .. typ.name .. " = extern struct {")
  288. -- yield(indent .. "{")
  289. end
  290. for _, member in ipairs(typ.struct) do
  291. yield(
  292. indent .. indent .. convert_struct_member(member) .. ","
  293. )
  294. end
  295. yield(indent .. "};")
  296. end
  297. end
  298. function converter.funcs(func)
  299. if func.cpponly then
  300. return
  301. end
  302. if func.comments ~= nil then
  303. for _, line in ipairs(func.comments) do
  304. yield("/// " .. line)
  305. end
  306. local hasParams = false
  307. for _, arg in ipairs(func.args) do
  308. if arg.comment ~= nil then
  309. local comment = table.concat(arg.comment, " ")
  310. yield("/// <param name=\""
  311. .. arg.name
  312. .. "\">"
  313. .. comment
  314. .. "</param>"
  315. )
  316. hasParams = true
  317. end
  318. end
  319. end
  320. local args = {}
  321. if func.this ~= nil then
  322. args[1] = "self: [*c]" .. func.this_type.type
  323. end
  324. for _, arg in ipairs(func.args) do
  325. local argName = arg.name:gsub("_", "")
  326. argName = argName:gsub("enum", "enumeration")
  327. if not isempty(argName) then
  328. table.insert(args, argName .. ": " .. convert_type(arg))
  329. else
  330. table.insert(args, convert_type(arg))
  331. end
  332. end
  333. yield("pub extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") callconv(.C) " .. convert_ret_type(func.ret) .. ";")
  334. end
  335. -- printtable("idl types", idl.types)
  336. -- printtable("idl funcs", idl.funcs)
  337. function gen.write(codes, outputfile)
  338. local out = assert(io.open(outputfile, "wb"))
  339. out:write(codes)
  340. out:close()
  341. print("Generating: " .. outputfile)
  342. end
  343. if (...) == nil then
  344. -- run `lua bindings-cs.lua` in command line
  345. print(gen.gen())
  346. end
  347. return gen