genie.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. --
  2. -- Copyright 2010-2019 Branimir Karadzic. All rights reserved.
  3. -- License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. --
  5. newoption {
  6. trigger = "with-amalgamated",
  7. description = "Enable amalgamated build.",
  8. }
  9. newoption {
  10. trigger = "with-sdl",
  11. description = "Enable SDL entry.",
  12. }
  13. newoption {
  14. trigger = "with-glfw",
  15. description = "Enable GLFW entry.",
  16. }
  17. newoption {
  18. trigger = "with-wayland",
  19. description = "Use Wayland backend.",
  20. }
  21. newoption {
  22. trigger = "with-profiler",
  23. description = "Enable build with intrusive profiler.",
  24. }
  25. newoption {
  26. trigger = "with-shared-lib",
  27. description = "Enable building shared library.",
  28. }
  29. newoption {
  30. trigger = "with-tools",
  31. description = "Enable building tools.",
  32. }
  33. newoption {
  34. trigger = "with-combined-examples",
  35. description = "Enable building examples (combined as single executable).",
  36. }
  37. newoption {
  38. trigger = "with-examples",
  39. description = "Enable building examples.",
  40. }
  41. newaction {
  42. trigger = "idl",
  43. description = "Generate bgfx interface source code",
  44. execute = function ()
  45. local gen = require "bgfx-codegen"
  46. local function generate(tempfile, outputfile, indent)
  47. local codes = gen.apply(tempfile)
  48. codes = gen.format(codes, {indent = indent})
  49. gen.write(codes, outputfile)
  50. print("Generating: " .. outputfile)
  51. end
  52. generate("temp.bgfx.h" , "../include/bgfx/c99/bgfx.h", " ")
  53. generate("temp.bgfx.idl.inl", "../src/bgfx.idl.inl", "\t")
  54. generate("temp.defines.h", "../include/bgfx/defines.h", "\t")
  55. os.exit()
  56. end
  57. }
  58. newaction {
  59. trigger = "c#-idl",
  60. description = "Generate bgfx C# bindings.",
  61. execute = function ()
  62. local idl = require "idl"
  63. do
  64. local doxygen = require "doxygen"
  65. local source = doxygen.load "bgfx.idl"
  66. local f = assert(load(source, "bgfx.idl" , "t", idl))
  67. f()
  68. local codegen = require "codegen"
  69. codegen.nameconversion(idl.types, idl.funcs)
  70. end
  71. print("using System;")
  72. print("using System.Runtime.InteropServices;")
  73. print("using System.Security;")
  74. print("")
  75. print("internal struct NativeFunctions")
  76. print("{")
  77. local function ends_with(str, ending)
  78. return ending == "" or str:sub(-#ending) == ending
  79. end
  80. local function convert_type_0(arg)
  81. if arg.ctype == "uint64_t" then
  82. return "ulong"
  83. elseif arg.ctype == "uint32_t" then
  84. return "uint"
  85. elseif arg.ctype == "uint16_t" then
  86. return "ushort"
  87. elseif arg.ctype == "uint8_t" then
  88. return "byte"
  89. elseif arg.ctype == "const char*" then
  90. return "[MarshalAs(UnmanagedType.LPStr)] string"
  91. elseif ends_with(arg.fulltype, "Handle") then
  92. return arg.fulltype
  93. elseif ends_with(arg.fulltype, "::Enum") then
  94. return arg.fulltype:gsub("::Enum", "")
  95. end
  96. return arg.ctype
  97. end
  98. local function convert_type(arg)
  99. local ctype = convert_type_0(arg)
  100. return ctype:gsub("const ", "")
  101. end
  102. for _, typ in ipairs(idl.types) do
  103. if typ.handle then
  104. print("\tpublic struct " .. typ.name .. "{ public ushort idx; }")
  105. elseif ends_with(typ.name, "::Enum") then
  106. print("\tpublic enum " .. typ.typename)
  107. print("\t{")
  108. for _, enum in ipairs(typ.enum) do
  109. print("\t\t" .. enum.name .. ",")
  110. end
  111. print("\t}")
  112. print("")
  113. elseif typ.bits ~= nil then
  114. print("\t[Flags]")
  115. local format = "0x%08x"
  116. if typ.bits == 64 then
  117. format = "0x%016x"
  118. print("\tpublic enum " .. typ.name .. " : long")
  119. elseif typ.bits == 16 then
  120. format = "0x%04x"
  121. print("\tpublic enum " .. typ.name .. " : short")
  122. else
  123. print("\tpublic enum " .. typ.name)
  124. end
  125. print("\t{")
  126. -- local shift = typ.flag.shift
  127. -- local base = typ.flag.base or 0
  128. -- local cap = 1 << (typ.flag.range or 0)
  129. if typ.const then
  130. for _, flag in ipairs(typ.flag) do
  131. print("\t\t"
  132. .. flag.name
  133. .. string.rep(" ", 22 - #(flag.name))
  134. .. " = "
  135. .. string.format(format, flag.value)
  136. .. ","
  137. )
  138. end
  139. else
  140. for idx, flag in ipairs(typ.flag) do
  141. local hex = 1<<(idx-1)
  142. if flag.value then
  143. hex = 1<<(flag.value-1)
  144. end
  145. print("\t\t"
  146. .. flag.name
  147. .. string.rep(" ", 22 - #(flag.name))
  148. .. " = "
  149. .. string.format(format, hex)
  150. .. ","
  151. )
  152. end
  153. end
  154. print("\t}")
  155. print("")
  156. end
  157. end
  158. print("");
  159. for idx, func in ipairs(idl.funcs) do
  160. print("\t[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]")
  161. if func.ret.cpptype == "bool" then
  162. print("\t[return: MarshalAs(UnmanagedType:I1)]")
  163. end
  164. local first = ""
  165. local args = "("
  166. for _, arg in ipairs(func.args) do
  167. local argtype = convert_type(arg)
  168. args = args .. first .. argtype .. " " .. arg.name
  169. first = ", "
  170. end
  171. print("\tinternal static extern unsafe " .. convert_type(func.ret) .. " bgfx_" .. func.cname .. args .. ");")
  172. print("")
  173. end
  174. print("#if DEBUG")
  175. print("\tconst string DllName = \"bgfx_debug.dll\";")
  176. print("#else")
  177. print("\tconst string DllName = \"bgfx.dll\";")
  178. print("#endif")
  179. print("}")
  180. -- printtable("idl types", idl.types)
  181. -- printtable("idl funcs", idl.funcs)
  182. os.exit()
  183. end
  184. }
  185. solution "bgfx"
  186. configurations {
  187. "Debug",
  188. "Release",
  189. }
  190. if _ACTION:match "xcode*" then
  191. platforms {
  192. "Universal",
  193. }
  194. else
  195. platforms {
  196. "x32",
  197. "x64",
  198. -- "Xbox360",
  199. "Native", -- for targets where bitness is not specified
  200. }
  201. end
  202. language "C++"
  203. startproject "example-00-helloworld"
  204. MODULE_DIR = path.getabsolute("../")
  205. BGFX_DIR = path.getabsolute("..")
  206. BX_DIR = os.getenv("BX_DIR")
  207. BIMG_DIR = os.getenv("BIMG_DIR")
  208. local BGFX_BUILD_DIR = path.join(BGFX_DIR, ".build")
  209. local BGFX_THIRD_PARTY_DIR = path.join(BGFX_DIR, "3rdparty")
  210. if not BX_DIR then
  211. BX_DIR = path.getabsolute(path.join(BGFX_DIR, "../bx"))
  212. end
  213. if not BIMG_DIR then
  214. BIMG_DIR = path.getabsolute(path.join(BGFX_DIR, "../bimg"))
  215. end
  216. if not os.isdir(BX_DIR) or not os.isdir(BIMG_DIR) then
  217. if not os.isdir(BX_DIR) then
  218. print("bx not found at " .. BX_DIR)
  219. end
  220. if not os.isdir(BIMG_DIR) then
  221. print("bimg not found at " .. BIMG_DIR)
  222. end
  223. print("For more info see: https://bkaradzic.github.io/bgfx/build.html")
  224. os.exit()
  225. end
  226. dofile (path.join(BX_DIR, "scripts/toolchain.lua"))
  227. if not toolchain(BGFX_BUILD_DIR, BGFX_THIRD_PARTY_DIR) then
  228. return -- no action specified
  229. end
  230. function copyLib()
  231. end
  232. if _OPTIONS["with-wayland"] then
  233. defines { "WL_EGL_PLATFORM=1" }
  234. end
  235. if _OPTIONS["with-sdl"] then
  236. if os.is("windows") then
  237. if not os.getenv("SDL2_DIR") then
  238. print("Set SDL2_DIR enviroment variable.")
  239. end
  240. end
  241. end
  242. if _OPTIONS["with-profiler"] then
  243. defines {
  244. "ENTRY_CONFIG_PROFILER=1",
  245. "BGFX_CONFIG_PROFILER=1",
  246. }
  247. end
  248. function exampleProjectDefaults()
  249. debugdir (path.join(BGFX_DIR, "examples/runtime"))
  250. includedirs {
  251. path.join(BX_DIR, "include"),
  252. path.join(BIMG_DIR, "include"),
  253. path.join(BGFX_DIR, "include"),
  254. path.join(BGFX_DIR, "3rdparty"),
  255. path.join(BGFX_DIR, "examples/common"),
  256. }
  257. flags {
  258. "FatalWarnings",
  259. }
  260. links {
  261. "example-common",
  262. "example-glue",
  263. "bgfx",
  264. "bimg_decode",
  265. "bimg",
  266. "bx",
  267. }
  268. if _OPTIONS["with-sdl"] then
  269. defines { "ENTRY_CONFIG_USE_SDL=1" }
  270. links { "SDL2" }
  271. configuration { "linux or freebsd" }
  272. if _OPTIONS["with-wayland"] then
  273. links {
  274. "wayland-egl",
  275. }
  276. end
  277. configuration { "osx" }
  278. libdirs { "$(SDL2_DIR)/lib" }
  279. configuration {}
  280. end
  281. if _OPTIONS["with-glfw"] then
  282. defines { "ENTRY_CONFIG_USE_GLFW=1" }
  283. links { "glfw3" }
  284. configuration { "linux or freebsd" }
  285. if _OPTIONS["with-wayland"] then
  286. links {
  287. "wayland-egl",
  288. }
  289. else
  290. links {
  291. "Xrandr",
  292. "Xinerama",
  293. "Xi",
  294. "Xxf86vm",
  295. "Xcursor",
  296. }
  297. end
  298. configuration { "osx" }
  299. linkoptions {
  300. "-framework CoreVideo",
  301. "-framework IOKit",
  302. }
  303. configuration {}
  304. end
  305. configuration { "vs*", "x32 or x64" }
  306. linkoptions {
  307. "/ignore:4199", -- LNK4199: /DELAYLOAD:*.dll ignored; no imports found from *.dll
  308. }
  309. links { -- this is needed only for testing with GLES2/3 on Windows with VS2008
  310. "DelayImp",
  311. }
  312. configuration { "vs201*", "x32 or x64" }
  313. linkoptions { -- this is needed only for testing with GLES2/3 on Windows with VS201x
  314. "/DELAYLOAD:\"libEGL.dll\"",
  315. "/DELAYLOAD:\"libGLESv2.dll\"",
  316. }
  317. configuration { "mingw*" }
  318. targetextension ".exe"
  319. links {
  320. "gdi32",
  321. "psapi",
  322. }
  323. configuration { "vs20*", "x32 or x64" }
  324. links {
  325. "gdi32",
  326. "psapi",
  327. }
  328. configuration { "durango" }
  329. links {
  330. "d3d11_x",
  331. "d3d12_x",
  332. "combase",
  333. "kernelx",
  334. }
  335. configuration { "winstore*" }
  336. removelinks {
  337. "DelayImp",
  338. "gdi32",
  339. "psapi"
  340. }
  341. links {
  342. "d3d11",
  343. "d3d12",
  344. "dxgi"
  345. }
  346. linkoptions {
  347. "/ignore:4264" -- LNK4264: archiving object file compiled with /ZW into a static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata
  348. }
  349. -- WinRT targets need their own output directories or build files stomp over each other
  350. configuration { "x32", "winstore*" }
  351. targetdir (path.join(BGFX_BUILD_DIR, "win32_" .. _ACTION, "bin", _name))
  352. objdir (path.join(BGFX_BUILD_DIR, "win32_" .. _ACTION, "obj", _name))
  353. configuration { "x64", "winstore*" }
  354. targetdir (path.join(BGFX_BUILD_DIR, "win64_" .. _ACTION, "bin", _name))
  355. objdir (path.join(BGFX_BUILD_DIR, "win64_" .. _ACTION, "obj", _name))
  356. configuration { "ARM", "winstore*" }
  357. targetdir (path.join(BGFX_BUILD_DIR, "arm_" .. _ACTION, "bin", _name))
  358. objdir (path.join(BGFX_BUILD_DIR, "arm_" .. _ACTION, "obj", _name))
  359. configuration { "mingw-clang" }
  360. kind "ConsoleApp"
  361. configuration { "android*" }
  362. kind "ConsoleApp"
  363. targetextension ".so"
  364. linkoptions {
  365. "-shared",
  366. }
  367. links {
  368. "EGL",
  369. "GLESv2",
  370. }
  371. configuration { "asmjs" }
  372. kind "ConsoleApp"
  373. targetextension ".bc"
  374. configuration { "linux-* or freebsd", "not linux-steamlink" }
  375. links {
  376. "X11",
  377. "GL",
  378. "pthread",
  379. }
  380. configuration { "linux-steamlink" }
  381. links {
  382. "EGL",
  383. "GLESv2",
  384. "SDL2",
  385. "pthread",
  386. }
  387. configuration { "rpi" }
  388. links {
  389. "X11",
  390. "brcmGLESv2",
  391. "brcmEGL",
  392. "bcm_host",
  393. "vcos",
  394. "vchiq_arm",
  395. "pthread",
  396. }
  397. configuration { "osx" }
  398. linkoptions {
  399. "-framework Cocoa",
  400. "-framework QuartzCore",
  401. "-framework OpenGL",
  402. "-weak_framework Metal",
  403. }
  404. configuration { "ios* or tvos*" }
  405. kind "ConsoleApp"
  406. linkoptions {
  407. "-framework CoreFoundation",
  408. "-framework Foundation",
  409. "-framework OpenGLES",
  410. "-framework UIKit",
  411. "-framework QuartzCore",
  412. "-weak_framework Metal",
  413. }
  414. configuration { "xcode*", "ios" }
  415. kind "WindowedApp"
  416. files {
  417. path.join(BGFX_DIR, "examples/runtime/iOS-Info.plist"),
  418. }
  419. configuration { "xcode*", "tvos" }
  420. kind "WindowedApp"
  421. files {
  422. path.join(BGFX_DIR, "examples/runtime/tvOS-Info.plist"),
  423. }
  424. configuration { "qnx*" }
  425. targetextension ""
  426. links {
  427. "EGL",
  428. "GLESv2",
  429. }
  430. configuration {}
  431. strip()
  432. end
  433. function exampleProject(_combined, ...)
  434. if _combined then
  435. project ("examples")
  436. uuid (os.uuid("examples"))
  437. kind "WindowedApp"
  438. for _, name in ipairs({...}) do
  439. files {
  440. path.join(BGFX_DIR, "examples", name, "**.c"),
  441. path.join(BGFX_DIR, "examples", name, "**.cpp"),
  442. path.join(BGFX_DIR, "examples", name, "**.h"),
  443. }
  444. removefiles {
  445. path.join(BGFX_DIR, "examples", name, "**.bin.h"),
  446. }
  447. end
  448. files {
  449. path.join(BGFX_DIR, "examples/25-c99/helloworld.c"), -- hack for _main_
  450. }
  451. exampleProjectDefaults()
  452. else
  453. for _, name in ipairs({...}) do
  454. project ("example-" .. name)
  455. uuid (os.uuid("example-" .. name))
  456. kind "WindowedApp"
  457. files {
  458. path.join(BGFX_DIR, "examples", name, "**.c"),
  459. path.join(BGFX_DIR, "examples", name, "**.cpp"),
  460. path.join(BGFX_DIR, "examples", name, "**.h"),
  461. }
  462. removefiles {
  463. path.join(BGFX_DIR, "examples", name, "**.bin.h"),
  464. }
  465. defines {
  466. "ENTRY_CONFIG_IMPLEMENT_MAIN=1",
  467. }
  468. exampleProjectDefaults()
  469. end
  470. end
  471. end
  472. dofile "bgfx.lua"
  473. group "libs"
  474. bgfxProject("", "StaticLib", {})
  475. dofile(path.join(BX_DIR, "scripts/bx.lua"))
  476. dofile(path.join(BIMG_DIR, "scripts/bimg.lua"))
  477. dofile(path.join(BIMG_DIR, "scripts/bimg_decode.lua"))
  478. if _OPTIONS["with-tools"] then
  479. dofile(path.join(BIMG_DIR, "scripts/bimg_encode.lua"))
  480. end
  481. if _OPTIONS["with-examples"]
  482. or _OPTIONS["with-combined-examples"]
  483. or _OPTIONS["with-tools"] then
  484. group "examples"
  485. dofile "example-common.lua"
  486. end
  487. if _OPTIONS["with-examples"]
  488. or _OPTIONS["with-combined-examples"] then
  489. group "examples"
  490. exampleProject(_OPTIONS["with-combined-examples"]
  491. , "00-helloworld"
  492. , "01-cubes"
  493. , "02-metaballs"
  494. , "03-raymarch"
  495. , "04-mesh"
  496. , "05-instancing"
  497. , "06-bump"
  498. , "07-callback"
  499. , "08-update"
  500. , "09-hdr"
  501. , "10-font"
  502. , "11-fontsdf"
  503. , "12-lod"
  504. , "13-stencil"
  505. , "14-shadowvolumes"
  506. , "15-shadowmaps-simple"
  507. , "16-shadowmaps"
  508. , "17-drawstress"
  509. , "18-ibl"
  510. , "19-oit"
  511. , "20-nanovg"
  512. , "21-deferred"
  513. , "22-windows"
  514. , "23-vectordisplay"
  515. , "24-nbody"
  516. , "26-occlusion"
  517. , "27-terrain"
  518. , "28-wireframe"
  519. , "29-debugdraw"
  520. , "30-picking"
  521. , "31-rsm"
  522. , "32-particles"
  523. , "33-pom"
  524. , "34-mvs"
  525. , "35-dynamic"
  526. , "36-sky"
  527. , "37-gpudrivenrendering"
  528. , "38-bloom"
  529. , "39-assao"
  530. , "40-svt"
  531. )
  532. -- C99 source doesn't compile under WinRT settings
  533. if not premake.vstudio.iswinrt() then
  534. exampleProject(false, "25-c99")
  535. end
  536. end
  537. if _OPTIONS["with-shared-lib"] then
  538. group "libs"
  539. bgfxProject("-shared-lib", "SharedLib", {})
  540. end
  541. if _OPTIONS["with-tools"] then
  542. group "tools"
  543. dofile "shaderc.lua"
  544. dofile "texturec.lua"
  545. dofile "texturev.lua"
  546. dofile "geometryc.lua"
  547. dofile "geometryv.lua"
  548. end