vtkModuleWrapJava.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #[==[
  2. @defgroup module-wrapping-java Module Java CMake APIs
  3. #]==]
  4. #[==[
  5. @file vtkModuleWrapJava.cmake
  6. @brief APIs for wrapping modules for Java
  7. #]==]
  8. #[==[
  9. @ingroup module-impl
  10. @brief Generate sources for using a module's classes from Java
  11. This function generates the wrapped sources for a module. It places the list of
  12. generated source files and Java source files in variables named in the second
  13. and third arguments, respectively.
  14. ~~~
  15. _vtk_module_wrap_java_sources(<module> <sources> <classes>)
  16. ~~~
  17. #]==]
  18. function (_vtk_module_wrap_java_sources module sources java_sources)
  19. _vtk_module_get_module_property("${module}"
  20. PROPERTY "exclude_wrap"
  21. VARIABLE _vtk_java_exclude_wrap)
  22. if (_vtk_java_exclude_wrap)
  23. return ()
  24. endif ()
  25. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_vtk_java_library_name}Java")
  26. set(_vtk_java_args_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_vtk_java_library_name}Java/${_vtk_java_library_name}-java.$<CONFIGURATION>.args")
  27. set(_vtk_java_init_data_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_vtk_java_library_name}Java/${_vtk_java_library_name}-java-init.data")
  28. set(_vtk_java_hierarchy_depends "${module}")
  29. _vtk_module_get_module_property("${module}"
  30. PROPERTY "private_depends"
  31. VARIABLE _vtk_java_private_depends)
  32. list(APPEND _vtk_java_hierarchy_depends ${_vtk_java_private_depends})
  33. set(_vtk_java_command_depends)
  34. foreach (_vtk_java_hierarchy_depend IN LISTS _vtk_java_hierarchy_depends)
  35. _vtk_module_get_module_property("${_vtk_java_hierarchy_depend}"
  36. PROPERTY "hierarchy"
  37. VARIABLE _vtk_java_hierarchy_file)
  38. if (_vtk_java_hierarchy_file)
  39. list(APPEND _vtk_java_hierarchy_files "${_vtk_java_hierarchy_file}")
  40. get_property(_vtk_java_is_imported
  41. TARGET "${_vtk_java_hierarchy_depend}"
  42. PROPERTY "IMPORTED")
  43. if (_vtk_java_is_imported OR CMAKE_GENERATOR MATCHES "Ninja")
  44. list(APPEND _vtk_java_command_depends "${_vtk_java_hierarchy_file}")
  45. else ()
  46. _vtk_module_get_module_property("${_vtk_java_hierarchy_depend}"
  47. PROPERTY "library_name"
  48. VARIABLE _vtk_java_hierarchy_library_name)
  49. if (TARGET "${_vtk_java_hierarchy_library_name}-hierarchy")
  50. list(APPEND _vtk_java_command_depends "${_vtk_java_hierarchy_library_name}-hierarchy")
  51. else ()
  52. message(FATAL_ERROR
  53. "The ${_vtk_java_hierarchy_depend} hierarchy file is attached to a non-imported target "
  54. "and a hierarchy target (${_vtk_java_hierarchy_library_name}-hierarchy) is "
  55. "missing.")
  56. endif ()
  57. endif ()
  58. endif ()
  59. endforeach ()
  60. set(_vtk_java_genex_compile_definitions
  61. "$<TARGET_PROPERTY:${_vtk_java_target_name},COMPILE_DEFINITIONS>")
  62. set(_vtk_java_genex_include_directories
  63. "$<TARGET_PROPERTY:${_vtk_java_target_name},INCLUDE_DIRECTORIES>")
  64. file(GENERATE
  65. OUTPUT "${_vtk_java_args_file}"
  66. CONTENT "$<$<BOOL:${_vtk_java_genex_compile_definitions}>:\n-D\'$<JOIN:${_vtk_java_genex_compile_definitions},\'\n-D\'>\'>\n
  67. $<$<BOOL:${_vtk_java_genex_include_directories}>:\n-I\'$<JOIN:${_vtk_java_genex_include_directories},\'\n-I\'>\'>\n
  68. $<$<BOOL:${_vtk_java_hierarchy_files}>:\n--types \'$<JOIN:${_vtk_java_hierarchy_files},\'\n--types \'>\'>\n")
  69. set(_vtk_java_sources)
  70. set(_vtk_java_java_sources)
  71. _vtk_module_get_module_property("${module}"
  72. PROPERTY "headers"
  73. VARIABLE _vtk_java_headers)
  74. set(_vtk_java_classes)
  75. foreach (_vtk_java_header IN LISTS _vtk_java_headers)
  76. get_filename_component(_vtk_java_basename "${_vtk_java_header}" NAME_WE)
  77. list(APPEND _vtk_java_classes
  78. "${_vtk_java_basename}")
  79. # The vtkWrapJava tool has special logic for the `vtkRenderWindow` class.
  80. # This extra logic requires its wrappers to be compiled as ObjC++ code
  81. # instead.
  82. set(_vtk_java_ext "cxx")
  83. if (APPLE AND _vtk_java_basename STREQUAL "vtkRenderWindow")
  84. set(_vtk_java_ext "mm")
  85. endif ()
  86. set(_vtk_java_source_output
  87. "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_vtk_java_library_name}Java/${_vtk_java_basename}Java.${_vtk_java_ext}")
  88. list(APPEND _vtk_java_sources
  89. "${_vtk_java_source_output}")
  90. set(_vtk_java_wrap_target "VTK::WrapJava")
  91. set(_vtk_java_macros_args)
  92. if (TARGET VTKCompileTools::WrapJava)
  93. set(_vtk_java_wrap_target "VTKCompileTools::WrapJava")
  94. if (TARGET VTKCompileTools_macros)
  95. list(APPEND _vtk_java_command_depends
  96. "VTKCompileTools_macros")
  97. list(APPEND _vtk_java_macros_args
  98. -undef
  99. -imacros "${_VTKCompileTools_macros_file}")
  100. endif ()
  101. endif ()
  102. set(_vtk_java_parse_target "VTK::ParseJava")
  103. if (TARGET VTKCompileTools::ParseJava)
  104. set(_vtk_java_parse_target "VTKCompileTools::ParseJava")
  105. endif ()
  106. add_custom_command(
  107. OUTPUT "${_vtk_java_source_output}"
  108. COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
  109. "$<TARGET_FILE:${_vtk_java_wrap_target}>"
  110. "@${_vtk_java_args_file}"
  111. -o "${_vtk_java_source_output}"
  112. "${_vtk_java_header}"
  113. ${_vtk_java_macros_args}
  114. IMPLICIT_DEPENDS
  115. CXX "${_vtk_java_header}"
  116. COMMENT "Generating Java wrapper sources for ${_vtk_java_basename}"
  117. DEPENDS
  118. "${_vtk_java_header}"
  119. "${_vtk_java_args_file}"
  120. "$<TARGET_FILE:${_vtk_java_wrap_target}>"
  121. ${_vtk_java_command_depends})
  122. set(_vtk_java_java_source_output
  123. "${_vtk_java_JAVA_OUTPUT}/${_vtk_java_basename}.java")
  124. list(APPEND _vtk_java_java_sources
  125. "${_vtk_java_java_source_output}")
  126. add_custom_command(
  127. OUTPUT "${_vtk_java_java_source_output}"
  128. COMMAND "${_vtk_java_parse_target}"
  129. "@${_vtk_java_args_file}"
  130. -o "${_vtk_java_java_source_output}"
  131. "${_vtk_java_header}"
  132. ${_vtk_java_macros_args}
  133. IMPLICIT_DEPENDS
  134. CXX "${_vtk_java_header}"
  135. COMMENT "Generating Java sources for ${_vtk_java_basename}"
  136. DEPENDS
  137. "${_vtk_java_header}"
  138. "${_vtk_java_args_file}"
  139. "$<TARGET_FILE:${_vtk_java_parse_target}>"
  140. ${_vtk_java_command_depends})
  141. endforeach ()
  142. set("${sources}"
  143. "${_vtk_java_sources}"
  144. PARENT_SCOPE)
  145. set("${java_sources}"
  146. "${_vtk_java_java_sources}"
  147. PARENT_SCOPE)
  148. endfunction ()
  149. #[==[
  150. @ingroup module-impl
  151. @brief Generate a JNI library for a set of modules
  152. A single JNI library may consist of the Java wrappings of multiple modules.
  153. This is useful for kit-based builds where the modules part of the same kit
  154. belong to the same JNI library as well.
  155. ~~~
  156. _vtk_module_wrap_java_library(<name> <module>...)
  157. ~~~
  158. The first argument is the name of the JNI library. The remaining arguments are
  159. modules to include in the JNI library.
  160. The remaining information it uses is assumed to be provided by the
  161. @ref vtk_module_wrap_java function.
  162. #]==]
  163. function (_vtk_module_wrap_java_library name)
  164. set(_vtk_java_library_sources)
  165. set(_vtk_java_library_java_sources)
  166. set(_vtk_java_library_link_depends)
  167. foreach (_vtk_java_module IN LISTS ARGN)
  168. _vtk_module_get_module_property("${_vtk_java_module}"
  169. PROPERTY "exclude_wrap"
  170. VARIABLE _vtk_java_exclude_wrap)
  171. if (_vtk_java_exclude_wrap)
  172. continue ()
  173. endif ()
  174. _vtk_module_real_target(_vtk_java_target_name "${_vtk_java_module}")
  175. _vtk_module_get_module_property("${_vtk_java_module}"
  176. PROPERTY "library_name"
  177. VARIABLE _vtk_java_library_name)
  178. _vtk_module_wrap_java_sources("${_vtk_java_module}" _vtk_java_sources _vtk_java_java_sources)
  179. list(APPEND _vtk_java_library_sources
  180. ${_vtk_java_sources})
  181. list(APPEND _vtk_java_library_java_sources
  182. ${_vtk_java_java_sources})
  183. _vtk_module_get_module_property("${_vtk_java_module}"
  184. PROPERTY "depends"
  185. VARIABLE _vtk_java_module_depends)
  186. foreach (_vtk_java_module_depend IN LISTS _vtk_java_module_depends)
  187. _vtk_module_get_module_property("${_vtk_java_module_depend}"
  188. PROPERTY "exclude_wrap"
  189. VARIABLE _vtk_java_module_depend_exclude_wrap)
  190. if (_vtk_java_module_depend_exclude_wrap)
  191. continue ()
  192. endif ()
  193. _vtk_module_get_module_property("${_vtk_java_module_depend}"
  194. PROPERTY "library_name"
  195. VARIABLE _vtk_java_depend_library_name)
  196. # XXX(kits): This doesn't work for kits.
  197. list(APPEND _vtk_java_library_link_depends
  198. "${_vtk_java_depend_library_name}Java")
  199. endforeach ()
  200. endforeach ()
  201. if (NOT _vtk_java_library_sources)
  202. return ()
  203. endif ()
  204. if (_vtk_java_library_link_depends)
  205. list(REMOVE_DUPLICATES _vtk_java_library_link_depends)
  206. endif ()
  207. set(_vtk_java_target "${name}Java")
  208. # XXX(java): Should this be a `MODULE`? If not, we should probably export
  209. # these targets, but then we'll need logic akin to the `vtkModuleWrapPython`
  210. # logic for loading wrapped modules from other packages.
  211. add_library("${_vtk_java_target}" SHARED
  212. ${_vtk_java_library_sources})
  213. add_custom_target("${_vtk_java_target}-java-sources"
  214. DEPENDS
  215. ${_vtk_java_library_java_sources})
  216. add_dependencies("${_vtk_java_target}"
  217. "${_vtk_java_target}-java-sources")
  218. if (MINGW)
  219. set_property(TARGET "${_vtk_java_target}"
  220. PROPERTY
  221. PREFIX "")
  222. endif ()
  223. if (APPLE)
  224. set_property(TARGET "${_vtk_java_target}"
  225. PROPERTY
  226. SUFFIX ".jnilib")
  227. endif ()
  228. set_property(TARGET "${_vtk_java_target}"
  229. PROPERTY
  230. "_vtk_module_java_files" "${_vtk_java_library_java_sources}")
  231. if (_vtk_java_JNILIB_DESTINATION)
  232. install(
  233. TARGETS "${_vtk_java_target}"
  234. # Windows
  235. RUNTIME
  236. DESTINATION "${_vtk_java_JNILIB_DESTINATION}"
  237. COMPONENT "${_vtk_java_JNILIB_COMPONENT}"
  238. # Other platforms
  239. LIBRARY
  240. DESTINATION "${_vtk_java_JNILIB_DESTINATION}"
  241. COMPONENT "${_vtk_java_JNILIB_COMPONENT}")
  242. endif ()
  243. vtk_module_autoinit(
  244. MODULES ${ARGN}
  245. TARGETS "${_vtk_java_target}")
  246. target_link_libraries("${_vtk_java_target}"
  247. PRIVATE
  248. ${ARGN}
  249. # XXX(java): If we use modules, remove this.
  250. ${_vtk_java_library_link_depends}
  251. VTK::Java)
  252. endfunction ()
  253. #[==[
  254. @ingroup module-wrapping-java
  255. @brief Wrap a set of modules for use in Java
  256. ~~~
  257. vtk_module_wrap_java(
  258. MODULES <module>...
  259. [WRAPPED_MODULES <varname>]
  260. [JAVA_OUTPUT <destination>])
  261. ~~~
  262. * `MODULES`: (Required) The list of modules to wrap.
  263. * `WRAPPED_MODULES`: (Recommended) Not all modules are wrappable. This
  264. variable will be set to contain the list of modules which were wrapped.
  265. * `JAVA_OUTPUT`: Defaults to
  266. `${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/vtkJava`. Java source files are
  267. written to this directory. After generation, the files may be compiled as
  268. needed.
  269. * `LIBRARY_DESTINATION` (Recommended): If provided, dynamic loader
  270. information will be added to modules for loading dependent libraries.
  271. * `JNILIB_DESTINATION`: Where to install JNI libraries.
  272. * `JNILIB_COMPONENT`: Defaults to `jni`. The install component to use for JNI
  273. libraries.
  274. For each wrapped module, a `<module>Java` target will be created. These targets
  275. will have a `_vtk_module_java_files` property which is the list of generated
  276. Java source files for that target.
  277. For dependency purposes, the `<module>Java-java-sources` target may also be
  278. used.
  279. #]==]
  280. function (vtk_module_wrap_java)
  281. cmake_parse_arguments(_vtk_java
  282. ""
  283. "JAVA_OUTPUT;WRAPPED_MODULES;LIBRARY_DESTINATION;JNILIB_DESTINATION;JNILIB_COMPONENT"
  284. "MODULES"
  285. ${ARGN})
  286. if (_vtk_java_UNPARSED_ARGUMENTS)
  287. message(FATAL_ERROR
  288. "Unparsed arguments for vtk_module_wrap_java: "
  289. "${_vtk_java_UNPARSED_ARGUMENTS}")
  290. endif ()
  291. if (NOT _vtk_java_JAVA_OUTPUT)
  292. set(_vtk_java_JAVA_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/vtkJava")
  293. endif ()
  294. if (NOT _vtk_java_JNILIB_COMPONENT)
  295. set(_vtk_java_JNILIB_COMPONENT "jni")
  296. endif ()
  297. # Set up rpaths
  298. set(CMAKE_BUILD_RPATH_USE_ORIGIN 1)
  299. if (UNIX)
  300. if (APPLE)
  301. set(_vtk_java_origin_rpath_prefix
  302. "@loader_path")
  303. else ()
  304. set(_vtk_java_origin_rpath_prefix
  305. "$ORIGIN")
  306. endif ()
  307. list(APPEND CMAKE_INSTALL_RPATH
  308. # For sibling wrapped modules.
  309. "${_vtk_java_origin_rpath_prefix}")
  310. if (DEFINED _vtk_java_LIBRARY_DESTINATION AND DEFINED _vtk_java_JNILIB_DESTINATION)
  311. file(RELATIVE_PATH _vtk_java_relpath
  312. "/prefix/${_vtk_java_JNILIB_DESTINATION}"
  313. "/prefix/${_vtk_java_LIBRARY_DESTINATION}")
  314. list(APPEND CMAKE_INSTALL_RPATH
  315. # For libraries.
  316. "${_vtk_java_origin_rpath_prefix}/${_vtk_java_relpath}")
  317. endif ()
  318. endif ()
  319. if (DEFINED _vtk_java_JNILIB_DESTINATION)
  320. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${_vtk_java_JNILIB_DESTINATION}")
  321. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${_vtk_java_JNILIB_DESTINATION}")
  322. endif ()
  323. if (NOT _vtk_java_MODULES)
  324. message(WARNING
  325. "No modules were requested for java wrapping.")
  326. return ()
  327. endif ()
  328. set(_vtk_java_all_wrapped_modules)
  329. foreach (_vtk_java_module IN LISTS _vtk_java_MODULES)
  330. _vtk_module_get_module_property("${_vtk_java_module}"
  331. PROPERTY "library_name"
  332. VARIABLE _vtk_java_exclude_wrap)
  333. _vtk_module_get_module_property("${_vtk_java_module}"
  334. PROPERTY "library_name"
  335. VARIABLE _vtk_java_library_name)
  336. _vtk_module_wrap_java_library("${_vtk_java_library_name}" "${_vtk_java_module}")
  337. if (TARGET "${_vtk_java_library_name}Java")
  338. list(APPEND _vtk_java_all_wrapped_modules
  339. "${_vtk_java_module}")
  340. endif ()
  341. endforeach ()
  342. if (NOT _vtk_java_all_wrapped_modules)
  343. message(FATAL_ERROR
  344. "No modules given could be wrapped.")
  345. endif ()
  346. if (DEFINED _vtk_java_WRAPPED_MODULES)
  347. set("${_vtk_java_WRAPPED_MODULES}"
  348. "${_vtk_java_all_wrapped_modules}"
  349. PARENT_SCOPE)
  350. endif ()
  351. endfunction ()