vtkModuleJson.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #[==[
  2. @ingroup module-impl
  3. @brief Output a boolean to JSON
  4. Appends a condition as a JSON boolean with the given dictionary key name to the
  5. given string variable.
  6. ~~~
  7. _vtk_json_bool(<output> <name> <cond>)
  8. ~~~
  9. #]==]
  10. macro (_vtk_json_bool output name cond)
  11. if (${cond})
  12. set(val "true")
  13. else ()
  14. set(val "false")
  15. endif ()
  16. string(APPEND "${output}" "\"${name}\": ${val}, ")
  17. unset(val)
  18. endmacro ()
  19. #[==[
  20. @ingroup module-impl
  21. @brief Output a string list to JSON
  22. Appends a variable as a JSON list of strings with the given dictionary key name
  23. to the given string variable.
  24. ~~~
  25. _vtk_json_string_list(<output> <name> <cond>)
  26. ~~~
  27. #]==]
  28. macro (_vtk_json_string_list output name var)
  29. set(list "[")
  30. foreach (value IN LISTS "${var}")
  31. string(APPEND list "\"${value}\", ")
  32. endforeach ()
  33. string(APPEND list "]")
  34. string(REPLACE ", ]" "]" list "${list}")
  35. string(APPEND "${output}" "\"${name}\": ${list}, ")
  36. unset(value)
  37. unset(list)
  38. endmacro ()
  39. #[==[
  40. @ingroup module-support
  41. @brief JSON metadata representation of modules
  42. Information about the modules built and/or available may be dumped to a JSON
  43. file.
  44. ~~~
  45. vtk_module_json(
  46. MODULES <module>...
  47. OUTPUT <path>)
  48. ~~~
  49. * `MODULES`: (Required) The modules to output information for.
  50. * `OUTPUT`: (Required) A JSON file describing the modules built will
  51. be output to this path. Relative paths are rooted to `CMAKE_BINARY_DIR`.
  52. Example output:
  53. ~~~{.json}
  54. {
  55. "modules": [
  56. {
  57. "name": "...",
  58. "library_name": "...",
  59. "enabled": <bool>,
  60. "implementable": <bool>,
  61. "third_party": <bool>,
  62. "wrap_exclude": <bool>,
  63. "kit": "...",
  64. "depends": [
  65. "..."
  66. ],
  67. "optional_depends": [
  68. "..."
  69. ],
  70. "private_depends": [
  71. "..."
  72. ],
  73. "implements": [
  74. "..."
  75. ],
  76. "headers": [
  77. "..."
  78. ]
  79. }
  80. ],
  81. "kits": [
  82. {
  83. "name": "...",
  84. "enabled": <bool>,
  85. "modules": [
  86. ]
  87. }
  88. ]
  89. }
  90. ~~~
  91. #]==]
  92. function (vtk_module_json)
  93. cmake_parse_arguments(_vtk_json
  94. ""
  95. "OUTPUT"
  96. "MODULES"
  97. ${ARGN})
  98. if (_vtk_json_UNPARSED_ARGUMENTS)
  99. message(FATAL_ERROR
  100. "Unparsed arguments for vtk_module_json: "
  101. "${_vtk_json_UNPARSED_ARGUMENTS}")
  102. endif ()
  103. if (NOT DEFINED _vtk_json_OUTPUT)
  104. message(FATAL_ERROR
  105. "The `OUTPUT` argument is required.")
  106. endif ()
  107. if (NOT _vtk_json_MODULES)
  108. message(FATAL_ERROR "No modules given to output.")
  109. endif ()
  110. if (NOT IS_ABSOLUTE "${_vtk_json_OUTPUT}")
  111. set(_vtk_json_OUTPUT "${CMAKE_BINARY_DIR}/${_vtk_json_OUTPUT}")
  112. endif ()
  113. set(_vtk_json_kits)
  114. set(_vtk_json_contents "{")
  115. string(APPEND _vtk_json_contents "\"modules\": {")
  116. foreach (_vtk_json_module IN LISTS _vtk_json_MODULES)
  117. get_property(_vtk_json_description GLOBAL
  118. PROPERTY "_vtk_module_${_vtk_json_module}_description")
  119. get_property(_vtk_json_implementable GLOBAL
  120. PROPERTY "_vtk_module_${_vtk_json_module}_implementable")
  121. get_property(_vtk_json_third_party GLOBAL
  122. PROPERTY "_vtk_module_${_vtk_json_module}_third_party")
  123. get_property(_vtk_json_wrap_exclude GLOBAL
  124. PROPERTY "_vtk_module_${_vtk_json_module}_wrap_exclude")
  125. get_property(_vtk_json_kit GLOBAL
  126. PROPERTY "_vtk_module_${_vtk_json_module}_kit")
  127. get_property(_vtk_json_depends GLOBAL
  128. PROPERTY "_vtk_module_${_vtk_json_module}_depends")
  129. get_property(_vtk_json_private_depends GLOBAL
  130. PROPERTY "_vtk_module_${_vtk_json_module}_private_depends")
  131. get_property(_vtk_json_optional_depends GLOBAL
  132. PROPERTY "_vtk_module_${_vtk_json_module}_optional_depends")
  133. get_property(_vtk_json_implements GLOBAL
  134. PROPERTY "_vtk_module_${_vtk_json_module}_implements")
  135. get_property(_vtk_json_library_name GLOBAL
  136. PROPERTY "_vtk_module_${_vtk_json_module}_library_name")
  137. get_property(_vtk_json_module_file GLOBAL
  138. PROPERTY "_vtk_module_${_vtk_json_module}_file")
  139. set(_vtk_json_kit_name "null")
  140. if (_vtk_json_kit)
  141. list(APPEND _vtk_json_kits
  142. "${_vtk_json_kit}")
  143. set(_vtk_json_kit_name "\"${_vtk_json_kit}\"")
  144. endif ()
  145. set(_vtk_json_headers "")
  146. if (TARGET "${_vtk_json_module}")
  147. _vtk_module_get_module_property("${_vtk_json_module}"
  148. PROPERTY "headers"
  149. VARIABLE _vtk_json_headers)
  150. get_filename_component(_vtk_json_module_dir "${_vtk_json_module_file}" DIRECTORY)
  151. file(RELATIVE_PATH _vtk_json_module_subdir "${CMAKE_SOURCE_DIR}" "${_vtk_json_module_dir}")
  152. string(REPLACE "${CMAKE_SOURCE_DIR}/${_vtk_json_module_subdir}/" "" _vtk_json_headers "${_vtk_json_headers}")
  153. string(REPLACE "${CMAKE_BINARY_DIR}/${_vtk_json_module_subdir}/" "" _vtk_json_headers "${_vtk_json_headers}")
  154. endif ()
  155. string(APPEND _vtk_json_contents "\"${_vtk_json_module}\": {")
  156. string(APPEND _vtk_json_contents "\"library_name\": \"${_vtk_json_library_name}\", ")
  157. string(APPEND _vtk_json_contents "\"description\": \"${_vtk_json_description}\", ")
  158. _vtk_json_bool(_vtk_json_contents "enabled" "TARGET;${_vtk_json_module}")
  159. _vtk_json_bool(_vtk_json_contents "implementable" _vtk_json_implementable)
  160. _vtk_json_bool(_vtk_json_contents "third_party" _vtk_json_third_party)
  161. _vtk_json_bool(_vtk_json_contents "wrap_exclude" _vtk_json_wrap_exclude)
  162. string(APPEND _vtk_json_contents "\"kit\": ${_vtk_json_kit_name}, ")
  163. _vtk_json_string_list(_vtk_json_contents "depends" _vtk_json_depends)
  164. _vtk_json_string_list(_vtk_json_contents "optional_depends" _vtk_json_optional_depends)
  165. _vtk_json_string_list(_vtk_json_contents "private_depends" _vtk_json_private_depends)
  166. _vtk_json_string_list(_vtk_json_contents "implements" _vtk_json_implements)
  167. _vtk_json_string_list(_vtk_json_contents "headers" _vtk_json_headers)
  168. string(APPEND _vtk_json_contents "}, ")
  169. endforeach ()
  170. string(APPEND _vtk_json_contents "}, ")
  171. string(APPEND _vtk_json_contents "\"kits\": {")
  172. foreach (_vtk_json_kit IN LISTS _vtk_json_kits)
  173. set(_vtk_json_library_name "null")
  174. if (TARGET "${_vtk_json_kit}")
  175. get_property(_vtk_json_library
  176. TARGET "${_vtk_json_kit}"
  177. PROPERTY LIBRARY_OUTPUT_NAME)
  178. set(_vtk_json_library_name "\"${_vtk_json_library}\"")
  179. endif ()
  180. string(APPEND _vtk_json_contents "\"${_vtk_json_kit}\": {")
  181. string(APPEND _vtk_json_contents "\"library_name\": ${_vtk_json_library_name}, ")
  182. _vtk_json_bool(_vtk_json_contents "enabled" "TARGET;${_vtk_json_kit}")
  183. string(APPEND _vtk_json_contents "}, ")
  184. endforeach ()
  185. string(APPEND _vtk_json_contents "}, ")
  186. string(APPEND _vtk_json_contents "}")
  187. string(REPLACE ", ]" "]" _vtk_json_contents "${_vtk_json_contents}")
  188. string(REPLACE ", }" "}" _vtk_json_contents "${_vtk_json_contents}")
  189. file(GENERATE
  190. OUTPUT "${_vtk_json_OUTPUT}"
  191. CONTENT "${_vtk_json_contents}")
  192. endfunction ()