2
0

autoinit.lua 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- Generate vtk autoinit header file. Substitution for cmake vtk_module_autoinit.
  2. --
  3. -- Usage:
  4. --
  5. -- add_rules("@vtk/autoinit") -- all modules are included by default
  6. rule("autoinit")
  7. on_config(function (target)
  8. import("core.cache.memcache")
  9. local properties = memcache.get("rule.vtk.autogen")
  10. if properties then
  11. -- add target definitions for autogen header
  12. local autogen_dir = target:autogendir()
  13. local autogen_header_dir = path.directory(path.directory(path.directory(path.directory(autogen_dir))))
  14. local autogen_header = path.join(autogen_header_dir, "vtkModuleAutoInit_all.h")
  15. for module, property in pairs(properties) do
  16. if property.needs_autoinit and property.implementations then
  17. target:add("defines", format("vtk%s_AUTOINIT_INCLUDE=\"%s\"", module, autogen_header:gsub("\\", "/")))
  18. end
  19. end
  20. else
  21. -- parse vtk cmake configuration files
  22. local vtk_root = target:pkg("vtk"):installdir()
  23. local vtk_version = target:pkg("vtk"):version()
  24. local vtk_ver = format("vtk-%s.%s", vtk_version:major(), vtk_version:minor())
  25. local vtk_dir = path.join(vtk_root, "lib", "cmake", vtk_ver)
  26. local property_file = path.join(vtk_dir, "VTK-vtk-module-properties.cmake")
  27. assert(os.isfile(property_file), "VTK cmake configuration files not found!")
  28. local property_file_content = io.readfile(property_file):split('\n', {plain = true})
  29. properties = {}
  30. for _, line in ipairs(property_file_content) do
  31. local module = line:match("set_property%(TARGET \"VTK::(.-)\".+%)")
  32. if module then
  33. if properties[module] == nil then
  34. properties[module] = {needs_autoinit = false}
  35. end
  36. local needs_autoinit = line:find("set_property%(TARGET \"VTK::.-\" PROPERTY \"INTERFACE_vtk_module_needs_autoinit\" \"1\"%)")
  37. if needs_autoinit then
  38. properties[module].needs_autoinit = true
  39. end
  40. local implementable = line:find("set_property%(TARGET \"VTK::.-\" PROPERTY \"INTERFACE_vtk_module_implementable\" \"TRUE\"%)")
  41. if implementable then
  42. properties[module].implementable = true
  43. end
  44. local implements = line:match("set_property%(TARGET \"VTK::.-\" PROPERTY \"INTERFACE_vtk_module_implements\" \"VTK::(.-)\"%)")
  45. if implements then
  46. properties[module].implements = implements
  47. end
  48. end
  49. end
  50. for module, property in pairs(properties) do
  51. local implement = property.implements
  52. if implement then
  53. if properties[implement] and properties[implement].needs_autoinit then
  54. assert(properties[implement].implementable, format("VTK module %s is not implementable!", implement))
  55. if properties[implement].implementations == nil then
  56. properties[implement].implementations = {"vtk" .. module}
  57. else
  58. table.insert(properties[implement].implementations, "vtk" .. module)
  59. end
  60. end
  61. end
  62. end
  63. -- generate autogen header
  64. local autogen_dir = target:autogendir()
  65. local autogen_header_dir = path.directory(path.directory(path.directory(path.directory(autogen_dir))))
  66. local autogen_header = path.join(autogen_header_dir, "vtkModuleAutoInit_all.h")
  67. local autogen_content = ""
  68. for module, property in pairs(properties) do
  69. if property.needs_autoinit and property.implementations then
  70. local implementation_content = table.concat(property.implementations, ",")
  71. autogen_content = autogen_content .. format("#define vtk%s_AUTOINIT %d(%s)\n", module, #property.implementations, implementation_content)
  72. target:add("defines", format("vtk%s_AUTOINIT_INCLUDE=\"%s\"", module, autogen_header:gsub("\\", "/")))
  73. end
  74. end
  75. if not memcache.get("rule.vtk.autogen") then
  76. memcache.set("rule.vtk.autogen", properties)
  77. io.writefile(autogen_header, autogen_content)
  78. end
  79. end
  80. end)