wayland_protocols.lua 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. -- Generate source files from wayland protocol files
  2. -- for example
  3. --[[
  4. if is_plat("linux") then
  5. add_rules("[email protected]")
  6. on_load(function(target)
  7. local pkg = target:pkg("wayland-protocols")
  8. local wayland_protocols_dir = path.join(target:pkg("wayland-protocols"):installdir() or "/usr", "share", "wayland-protocols")
  9. local protocols = {path.join("stable", "xdg-shell", "xdg-shell.xml"),
  10. path.join("unstable", "xdg-decoration", "xdg-decoration-unstable-v1.xml")}
  11. for _, protocol in ipairs(protocols) do
  12. target:add("files", path.join(wayland_protocols_dir, protocol))
  13. end
  14. end)
  15. end
  16. ]]
  17. rule("wayland.protocols")
  18. set_extensions(".xml")
  19. on_load(function(target)
  20. if target:rule("c++.build") then
  21. local rule = target:rule("c++.build"):clone()
  22. rule:add("deps", "wayland.protocols", {order = true})
  23. target:rule_add(rule)
  24. end
  25. end)
  26. on_config(function(target)
  27. import("core.base.option")
  28. local outputdir = target:extraconf("rules", "wayland.protocols", "outputdir") or path.join(target:autogendir(), "rules", "wayland.protocols")
  29. if not os.isdir(outputdir) then
  30. os.mkdir(outputdir)
  31. end
  32. target:add("includedirs", outputdir)
  33. local dryrun = option.get("dry-run")
  34. local sourcebatches = target:sourcebatches()
  35. if not dryrun and sourcebatches["wayland.protocols"] and sourcebatches["wayland.protocols"].sourcefiles then
  36. for _, protocol in ipairs(sourcebatches["wayland.protocols"].sourcefiles) do
  37. local clientfile = path.join(outputdir, path.basename(protocol) .. ".h")
  38. local privatefile = path.join(outputdir, path.basename(protocol) .. ".c")
  39. -- for c++ module dependency discovery
  40. if not os.exists(clientfile) then
  41. os.touch(clientfile)
  42. end
  43. target:add("files", privatefile, {always_added = true})
  44. end
  45. end
  46. end)
  47. before_buildcmd_file(function(target, batchcmds, sourcefile, opt)
  48. import("lib.detect.find_tool")
  49. local outputdir = target:extraconf("rules", "wayland.protocols", "outputdir") or path.join(target:autogendir(), "rules", "wayland.protocols")
  50. local wayland_scanner = find_tool("wayland-scanner")
  51. assert(wayland_scanner, "wayland-scanner not found! please install wayland package")
  52. local clientfile = path.join(outputdir, path.basename(sourcefile) .. ".h")
  53. local privatefile = path.join(outputdir, path.basename(sourcefile) .. ".c")
  54. local client_flag = "client-header"
  55. local private_flag = "private-code"
  56. local client_flags = {client_flag, sourcefile, clientfile}
  57. local private_flags = {private_flag, sourcefile, privatefile}
  58. batchcmds:show_progress(opt.progress, "${color.build.object}generating.wayland.protocol.client %s", path.basename(sourcefile))
  59. batchcmds:vexecv(wayland_scanner.program, client_flags)
  60. batchcmds:show_progress(opt.progress, "${color.build.object}generating.wayland.protocol.private %s", path.basename(sourcefile))
  61. batchcmds:vexecv(wayland_scanner.program, private_flags)
  62. batchcmds:add_depfiles(sourcefile)
  63. batchcmds:set_depmtime(os.mtime(privatefile))
  64. batchcmds:set_depcache(target:dependfile(privatefile))
  65. end)