UseJava.cmake 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. UseJava
  5. -------
  6. Use Module for Java
  7. This file provides functions for Java. It is assumed that
  8. :module:`FindJava` has already been loaded. See :module:`FindJava` for
  9. information on how to load Java into your CMake project.
  10. Creating And Installing JARs
  11. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. .. code-block:: cmake
  13. add_jar(<target_name>
  14. [SOURCES] <source1> [<source2>...] [<resource1>...]
  15. [INCLUDE_JARS <jar1> [<jar2>...]]
  16. [ENTRY_POINT <entry>]
  17. [VERSION <version>]
  18. [OUTPUT_NAME <name>]
  19. [OUTPUT_DIR <dir>]
  20. [GENERATE_NATIVE_HEADERS <target> [DESTINATION <dir>]]
  21. )
  22. This command creates a ``<target_name>.jar``. It compiles the given
  23. ``<source>`` files and adds the given ``<resource>`` files to
  24. the jar file. Source files can be java files or listing files
  25. (prefixed by ``@``). If only resource files are given then just a jar file
  26. is created. The list of ``INCLUDE_JARS`` are added to the classpath when
  27. compiling the java sources and also to the dependencies of the target.
  28. ``INCLUDE_JARS`` also accepts other target names created by ``add_jar()``.
  29. For backwards compatibility, jar files listed as sources are ignored (as
  30. they have been since the first version of this module).
  31. The default ``OUTPUT_DIR`` can also be changed by setting the variable
  32. ``CMAKE_JAVA_TARGET_OUTPUT_DIR``.
  33. Optionally, using option ``GENERATE_NATIVE_HEADERS``, native header files can
  34. be generated for methods declared as native. These files provide the
  35. connective glue that allow your Java and C code to interact. An INTERFACE
  36. target will be created for an easy usage of generated files. Sub-option
  37. ``DESTINATION`` can be used to specify the output directory for generated
  38. header files.
  39. ``GENERATE_NATIVE_HEADERS`` option requires, at least, version 1.8 of the JDK.
  40. The ``add_jar()`` function sets the following target properties on
  41. ``<target_name>``:
  42. ``INSTALL_FILES``
  43. The files which should be installed. This is used by ``install_jar()``.
  44. ``JNI_SYMLINK``
  45. The JNI symlink which should be installed. This is used by
  46. ``install_jni_symlink()``.
  47. ``JAR_FILE``
  48. The location of the jar file so that you can include it.
  49. ``CLASSDIR``
  50. The directory where the class files can be found. For example to use them
  51. with ``javah``.
  52. .. code-block:: cmake
  53. install_jar(<target_name> <destination>)
  54. install_jar(<target_name> DESTINATION <destination> [COMPONENT <component>])
  55. This command installs the ``<target_name>`` files to the given
  56. ``<destination>``. It should be called in the same scope as ``add_jar()`` or
  57. it will fail.
  58. The ``install_jar()`` function sets the ``INSTALL_DESTINATION`` target
  59. property on jars so installed. This property holds the ``<destination>`` as
  60. described above, and is used by ``install_jar_exports()``. You can get this
  61. information with :command:`get_property` and the ``INSTALL_DESTINATION``
  62. property key.
  63. .. code-block:: cmake
  64. install_jni_symlink(<target_name> <destination>)
  65. install_jni_symlink(<target_name> DESTINATION <destination> [COMPONENT <component>])
  66. This command installs the ``<target_name>`` JNI symlinks to the given
  67. ``<destination>``. It should be called in the same scope as ``add_jar()`` or
  68. it will fail.
  69. .. code-block:: cmake
  70. install_jar_exports(TARGETS <jars>...
  71. [NAMESPACE <namespace>]
  72. FILE <filename>
  73. DESTINATION <destination> [COMPONENT <component>])
  74. This command installs a target export file ``<filename>`` for the named jar
  75. targets to the given ``<destination>`` directory. Its function is similar to
  76. that of :command:`install(EXPORTS)`.
  77. .. code-block:: cmake
  78. export_jars(TARGETS <jars>...
  79. [NAMESPACE <namespace>]
  80. FILE <filename>)
  81. This command writes a target export file ``<filename>`` for the named ``<jars>``
  82. targets. Its function is similar to that of :command:`export`.
  83. Examples
  84. """"""""
  85. To add compile flags to the target you can set these flags with the following
  86. variable:
  87. .. code-block:: cmake
  88. set(CMAKE_JAVA_COMPILE_FLAGS -nowarn)
  89. To add a path or a jar file to the class path you can do this with the
  90. ``CMAKE_JAVA_INCLUDE_PATH`` variable.
  91. .. code-block:: cmake
  92. set(CMAKE_JAVA_INCLUDE_PATH /usr/share/java/shibboleet.jar)
  93. To use a different output name for the target you can set it with:
  94. .. code-block:: cmake
  95. add_jar(foobar foobar.java OUTPUT_NAME shibboleet.jar)
  96. To use a different output directory than ``CMAKE_CURRENT_BINARY_DIR`` you can
  97. set it with:
  98. .. code-block:: cmake
  99. add_jar(foobar foobar.java OUTPUT_DIR ${PROJECT_BINARY_DIR}/bin)
  100. To define an entry point in your jar you can set it with the ``ENTRY_POINT``
  101. named argument:
  102. .. code-block:: cmake
  103. add_jar(example ENTRY_POINT com/examples/MyProject/Main)
  104. To define a custom manifest for the jar, you can set it with the ``MANIFEST``
  105. named argument:
  106. .. code-block:: cmake
  107. add_jar(example MANIFEST /path/to/manifest)
  108. To add a version to the target output name you can set it using the ``VERSION``
  109. named argument to ``add_jar()``. The following example will create a jar file
  110. with the name ``shibboleet-1.0.0.jar`` and will create a symlink
  111. ``shibboleet.jar`` pointing to the jar with the version information.
  112. .. code-block:: cmake
  113. add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
  114. If the target is a JNI library, utilize the following commands to
  115. create a JNI symbolic link:
  116. .. code-block:: cmake
  117. set(CMAKE_JNI_TARGET TRUE)
  118. add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
  119. install_jar(shibboleet ${LIB_INSTALL_DIR}/shibboleet)
  120. install_jni_symlink(shibboleet ${JAVA_LIB_INSTALL_DIR})
  121. If a single target needs to produce more than one jar from its
  122. java source code, to prevent the accumulation of duplicate class
  123. files in subsequent jars, set/reset ``CMAKE_JAR_CLASSES_PREFIX`` prior
  124. to calling the ``add_jar()`` function:
  125. .. code-block:: cmake
  126. set(CMAKE_JAR_CLASSES_PREFIX com/redhat/foo)
  127. add_jar(foo foo.java)
  128. set(CMAKE_JAR_CLASSES_PREFIX com/redhat/bar)
  129. add_jar(bar bar.java)
  130. For an optimum usage of option ``GENERATE_NATIVE_HEADERS``, it is recommended to
  131. include module JNI before any call to ``add_jar()``. The produced target for
  132. native headers can then be used to compile C/C++ sources with the
  133. :command:`target_link_libraries` command.
  134. .. code-block:: cmake
  135. find_package(JNI)
  136. add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native)
  137. add_library(bar bar.cpp)
  138. target_link_libraries(bar PRIVATE foo-native)
  139. Finding JARs
  140. ^^^^^^^^^^^^
  141. .. code-block:: cmake
  142. find_jar(<VAR>
  143. <name> | NAMES <name1> [<name2>...]
  144. [PATHS <path1> [<path2>... ENV <var>]]
  145. [VERSIONS <version1> [<version2>]]
  146. [DOC "cache documentation string"]
  147. )
  148. This command is used to find a full path to the named jar. A cache
  149. entry named by ``<VAR>`` is created to store the result of this command.
  150. If the full path to a jar is found the result is stored in the
  151. variable and the search will not repeated unless the variable is
  152. cleared. If nothing is found, the result will be ``<VAR>-NOTFOUND``, and
  153. the search will be attempted again next time ``find_jar()`` is invoked with
  154. the same variable. The name of the full path to a file that is
  155. searched for is specified by the names listed after ``NAMES`` argument.
  156. Additional search locations can be specified after the ``PATHS`` argument.
  157. If you require special a version of a jar file you can specify it with
  158. the ``VERSIONS`` argument. The argument after ``DOC`` will be used for the
  159. documentation string in the cache.
  160. Javadoc
  161. ^^^^^^^
  162. The ``create_javadoc()`` command can be used to create java documentation
  163. based on files or packages. For more details please read the javadoc manpage.
  164. There are two main signatures for ``create_javadoc()``. The first signature
  165. works with package names on a path with source files.
  166. .. code-block:: cmake
  167. create_javadoc(<VAR>
  168. PACKAGES <pkg1> [<pkg2>...]
  169. [SOURCEPATH <sourcepath>]
  170. [CLASSPATH <classpath>]
  171. [INSTALLPATH <install path>]
  172. [DOCTITLE "the documentation title"]
  173. [WINDOWTITLE "the title of the document"]
  174. [AUTHOR TRUE|FALSE]
  175. [USE TRUE|FALSE]
  176. [VERSION TRUE|FALSE]
  177. )
  178. For example:
  179. .. code-block:: cmake
  180. create_javadoc(my_example_doc
  181. PACKAGES com.example.foo com.example.bar
  182. SOURCEPATH "${CMAKE_CURRENT_SOURCE_DIR}"
  183. CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
  184. WINDOWTITLE "My example"
  185. DOCTITLE "<h1>My example</h1>"
  186. AUTHOR TRUE
  187. USE TRUE
  188. VERSION TRUE
  189. )
  190. The second signature for ``create_javadoc()`` works on a given list of
  191. files.
  192. .. code-block:: cmake
  193. create_javadoc(<VAR>
  194. FILES <file1> [<file2>...]
  195. [CLASSPATH <classpath>]
  196. [INSTALLPATH <install path>]
  197. [DOCTITLE "the documentation title"]
  198. [WINDOWTITLE "the title of the document"]
  199. [AUTHOR TRUE|FALSE]
  200. [USE TRUE|FALSE]
  201. [VERSION TRUE|FALSE]
  202. )
  203. For example:
  204. .. code-block:: cmake
  205. create_javadoc(my_example_doc
  206. FILES ${example_SRCS}
  207. CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
  208. WINDOWTITLE "My example"
  209. DOCTITLE "<h1>My example</h1>"
  210. AUTHOR TRUE
  211. USE TRUE
  212. VERSION TRUE
  213. )
  214. Both signatures share most of the options. These options are the same
  215. as what you can find in the javadoc manpage. Please look at the
  216. manpage for ``CLASSPATH``, ``DOCTITLE``, ``WINDOWTITLE``, ``AUTHOR``, ``USE``
  217. and ``VERSION``.
  218. If you don't set the ``INSTALLPATH``, then by default the documentation will
  219. be installed to :
  220. ::
  221. ${CMAKE_INSTALL_PREFIX}/share/javadoc/<VAR>
  222. Header Generation
  223. ^^^^^^^^^^^^^^^^^
  224. .. code-block:: cmake
  225. create_javah(TARGET <target> | GENERATED_FILES <VAR>
  226. CLASSES <class>...
  227. [CLASSPATH <classpath>...]
  228. [DEPENDS <depend>...]
  229. [OUTPUT_NAME <path>|OUTPUT_DIR <path>]
  230. )
  231. Create C header files from java classes. These files provide the connective glue
  232. that allow your Java and C code to interact.
  233. .. deprecated:: 3.11
  234. .. note::
  235. This command will no longer be supported starting with version 10 of the JDK
  236. due to the `suppression of javah tool <http://openjdk.java.net/jeps/313>`_.
  237. The ``add_jar(GENERATE_NATIVE_HEADERS)`` command should be used instead.
  238. There are two main signatures for ``create_javah()``. The first signature
  239. returns generated files through variable specified by the ``GENERATED_FILES``
  240. option. For example:
  241. .. code-block:: cmake
  242. create_javah(GENERATED_FILES files_headers
  243. CLASSES org.cmake.HelloWorld
  244. CLASSPATH hello.jar
  245. )
  246. The second signature for ``create_javah()`` creates a target which encapsulates
  247. header files generation. E.g.
  248. .. code-block:: cmake
  249. create_javah(TARGET target_headers
  250. CLASSES org.cmake.HelloWorld
  251. CLASSPATH hello.jar
  252. )
  253. Both signatures share same options.
  254. ``CLASSES <class>...``
  255. Specifies Java classes used to generate headers.
  256. ``CLASSPATH <classpath>...``
  257. Specifies various paths to look up classes. Here .class files, jar files or
  258. targets created by command add_jar can be used.
  259. ``DEPENDS <depend>...``
  260. Targets on which the javah target depends.
  261. ``OUTPUT_NAME <path>``
  262. Concatenates the resulting header files for all the classes listed by option
  263. ``CLASSES`` into ``<path>``. Same behavior as option ``-o`` of javah tool.
  264. ``OUTPUT_DIR <path>``
  265. Sets the directory where the header files will be generated. Same behavior
  266. as option ``-d`` of javah tool. If not specified,
  267. :variable:`CMAKE_CURRENT_BINARY_DIR` is used as the output directory.
  268. #]=======================================================================]
  269. function (__java_copy_file src dest comment)
  270. add_custom_command(
  271. OUTPUT ${dest}
  272. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  273. ARGS ${src}
  274. ${dest}
  275. DEPENDS ${src}
  276. COMMENT ${comment})
  277. endfunction ()
  278. function(__java_lcat VAR)
  279. foreach(_line IN LISTS ARGN)
  280. string(APPEND ${VAR} "${_line}\n")
  281. endforeach()
  282. set(${VAR} "${${VAR}}" PARENT_SCOPE)
  283. endfunction()
  284. function(__java_export_jar VAR TARGET PATH)
  285. get_target_property(_jarpath ${TARGET} JAR_FILE)
  286. get_filename_component(_jarname ${_jarpath} NAME)
  287. set(_target "${_jar_NAMESPACE}${TARGET}")
  288. __java_lcat(${VAR}
  289. "# Create imported target ${_target}"
  290. "add_library(${_target} IMPORTED STATIC)"
  291. "set_target_properties(${_target} PROPERTIES"
  292. " IMPORTED_LOCATION \"${PATH}/${_jarname}\""
  293. " JAR_FILE \"${PATH}/${_jarname}\")"
  294. ""
  295. )
  296. set(${VAR} "${${VAR}}" PARENT_SCOPE)
  297. endfunction()
  298. # define helper scripts
  299. set(_JAVA_EXPORT_TARGETS_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/javaTargets.cmake.in)
  300. set(_JAVA_CLASS_FILELIST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaClassFilelist.cmake)
  301. set(_JAVA_SYMLINK_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaSymlinks.cmake)
  302. function(add_jar _TARGET_NAME)
  303. cmake_parse_arguments(_add_jar
  304. ""
  305. "VERSION;OUTPUT_DIR;OUTPUT_NAME;ENTRY_POINT;MANIFEST"
  306. "SOURCES;INCLUDE_JARS;GENERATE_NATIVE_HEADERS"
  307. ${ARGN}
  308. )
  309. # In CMake < 2.8.12, add_jar used variables which were set prior to calling
  310. # add_jar for customizing the behavior of add_jar. In order to be backwards
  311. # compatible, check if any of those variables are set, and use them to
  312. # initialize values of the named arguments. (Giving the corresponding named
  313. # argument will override the value set here.)
  314. #
  315. # New features should use named arguments only.
  316. if(NOT DEFINED _add_jar_VERSION AND DEFINED CMAKE_JAVA_TARGET_VERSION)
  317. set(_add_jar_VERSION "${CMAKE_JAVA_TARGET_VERSION}")
  318. endif()
  319. if(NOT DEFINED _add_jar_OUTPUT_DIR AND DEFINED CMAKE_JAVA_TARGET_OUTPUT_DIR)
  320. set(_add_jar_OUTPUT_DIR "${CMAKE_JAVA_TARGET_OUTPUT_DIR}")
  321. endif()
  322. if(NOT DEFINED _add_jar_OUTPUT_NAME AND DEFINED CMAKE_JAVA_TARGET_OUTPUT_NAME)
  323. set(_add_jar_OUTPUT_NAME "${CMAKE_JAVA_TARGET_OUTPUT_NAME}")
  324. # reset
  325. set(CMAKE_JAVA_TARGET_OUTPUT_NAME)
  326. endif()
  327. if(NOT DEFINED _add_jar_ENTRY_POINT AND DEFINED CMAKE_JAVA_JAR_ENTRY_POINT)
  328. set(_add_jar_ENTRY_POINT "${CMAKE_JAVA_JAR_ENTRY_POINT}")
  329. endif()
  330. set(_JAVA_SOURCE_FILES ${_add_jar_SOURCES} ${_add_jar_UNPARSED_ARGUMENTS})
  331. if (NOT DEFINED _add_jar_OUTPUT_DIR)
  332. set(_add_jar_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  333. else()
  334. get_filename_component(_add_jar_OUTPUT_DIR ${_add_jar_OUTPUT_DIR} ABSOLUTE)
  335. endif()
  336. # ensure output directory exists
  337. file (MAKE_DIRECTORY "${_add_jar_OUTPUT_DIR}")
  338. if (_add_jar_ENTRY_POINT)
  339. set(_ENTRY_POINT_OPTION e)
  340. set(_ENTRY_POINT_VALUE ${_add_jar_ENTRY_POINT})
  341. endif ()
  342. if (_add_jar_MANIFEST)
  343. set(_MANIFEST_OPTION m)
  344. get_filename_component (_MANIFEST_VALUE "${_add_jar_MANIFEST}" ABSOLUTE)
  345. endif ()
  346. unset (_GENERATE_NATIVE_HEADERS)
  347. if (_add_jar_GENERATE_NATIVE_HEADERS)
  348. # Raise an error if JDK version is less than 1.8 because javac -h is not supported
  349. # by earlier versions.
  350. if (Java_VERSION VERSION_LESS 1.8)
  351. message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS is not supported with this version of Java.")
  352. endif()
  353. cmake_parse_arguments (_add_jar_GENERATE_NATIVE_HEADERS "" "DESTINATION" "" ${_add_jar_GENERATE_NATIVE_HEADERS})
  354. if (NOT _add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS)
  355. message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS: missing required argument.")
  356. endif()
  357. list (LENGTH _add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS length)
  358. if (length GREATER 1)
  359. list (REMOVE_AT _add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS 0)
  360. message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS: ${_add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS}: unexpected argument(s).")
  361. endif()
  362. if (NOT _add_jar_GENERATE_NATIVE_HEADERS_DESTINATION)
  363. set (_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir/native_headers")
  364. endif()
  365. set (_GENERATE_NATIVE_HEADERS_TARGET ${_add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS})
  366. set (_GENERATE_NATIVE_HEADERS_OUTPUT_DIR "${_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION}")
  367. set (_GENERATE_NATIVE_HEADERS -h "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}")
  368. endif()
  369. if (LIBRARY_OUTPUT_PATH)
  370. set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH})
  371. else ()
  372. set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${_add_jar_OUTPUT_DIR})
  373. endif ()
  374. set(CMAKE_JAVA_INCLUDE_PATH
  375. ${CMAKE_JAVA_INCLUDE_PATH}
  376. ${CMAKE_CURRENT_SOURCE_DIR}
  377. ${CMAKE_JAVA_OBJECT_OUTPUT_PATH}
  378. ${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}
  379. )
  380. if (CMAKE_HOST_WIN32 AND NOT CYGWIN AND CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  381. set(CMAKE_JAVA_INCLUDE_FLAG_SEP ";")
  382. else ()
  383. set(CMAKE_JAVA_INCLUDE_FLAG_SEP ":")
  384. endif()
  385. foreach (JAVA_INCLUDE_DIR IN LISTS CMAKE_JAVA_INCLUDE_PATH)
  386. string(APPEND CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_FLAG_SEP}${JAVA_INCLUDE_DIR}")
  387. endforeach()
  388. set(CMAKE_JAVA_CLASS_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir")
  389. set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}.jar")
  390. if (_add_jar_OUTPUT_NAME AND _add_jar_VERSION)
  391. set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}-${_add_jar_VERSION}.jar")
  392. set(_JAVA_TARGET_OUTPUT_LINK "${_add_jar_OUTPUT_NAME}.jar")
  393. elseif (_add_jar_VERSION)
  394. set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}-${_add_jar_VERSION}.jar")
  395. set(_JAVA_TARGET_OUTPUT_LINK "${_TARGET_NAME}.jar")
  396. elseif (_add_jar_OUTPUT_NAME)
  397. set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}.jar")
  398. endif ()
  399. set(_JAVA_CLASS_FILES)
  400. set(_JAVA_COMPILE_FILES)
  401. set(_JAVA_COMPILE_FILELISTS)
  402. set(_JAVA_DEPENDS)
  403. set(_JAVA_COMPILE_DEPENDS)
  404. set(_JAVA_RESOURCE_FILES)
  405. set(_JAVA_RESOURCE_FILES_RELATIVE)
  406. foreach(_JAVA_SOURCE_FILE IN LISTS _JAVA_SOURCE_FILES)
  407. get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
  408. get_filename_component(_JAVA_FILE ${_JAVA_SOURCE_FILE} NAME_WE)
  409. get_filename_component(_JAVA_PATH ${_JAVA_SOURCE_FILE} PATH)
  410. get_filename_component(_JAVA_FULL ${_JAVA_SOURCE_FILE} ABSOLUTE)
  411. if (_JAVA_SOURCE_FILE MATCHES "^@(.+)$")
  412. get_filename_component(_JAVA_FULL ${CMAKE_MATCH_1} ABSOLUTE)
  413. list(APPEND _JAVA_COMPILE_FILELISTS ${_JAVA_FULL})
  414. elseif (_JAVA_EXT MATCHES ".java")
  415. file(RELATIVE_PATH _JAVA_REL_BINARY_PATH ${CMAKE_CURRENT_BINARY_DIR} ${_JAVA_FULL})
  416. file(RELATIVE_PATH _JAVA_REL_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${_JAVA_FULL})
  417. string(LENGTH ${_JAVA_REL_BINARY_PATH} _BIN_LEN)
  418. string(LENGTH ${_JAVA_REL_SOURCE_PATH} _SRC_LEN)
  419. if (_BIN_LEN LESS _SRC_LEN)
  420. set(_JAVA_REL_PATH ${_JAVA_REL_BINARY_PATH})
  421. else ()
  422. set(_JAVA_REL_PATH ${_JAVA_REL_SOURCE_PATH})
  423. endif ()
  424. get_filename_component(_JAVA_REL_PATH ${_JAVA_REL_PATH} PATH)
  425. list(APPEND _JAVA_COMPILE_FILES ${_JAVA_SOURCE_FILE})
  426. set(_JAVA_CLASS_FILE "${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_REL_PATH}/${_JAVA_FILE}.class")
  427. set(_JAVA_CLASS_FILES ${_JAVA_CLASS_FILES} ${_JAVA_CLASS_FILE})
  428. elseif (_JAVA_EXT MATCHES ".jar"
  429. OR _JAVA_EXT MATCHES ".war"
  430. OR _JAVA_EXT MATCHES ".ear"
  431. OR _JAVA_EXT MATCHES ".sar")
  432. # Ignored for backward compatibility
  433. elseif (_JAVA_EXT STREQUAL "")
  434. list(APPEND CMAKE_JAVA_INCLUDE_PATH ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}} ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}_CLASSPATH})
  435. list(APPEND _JAVA_DEPENDS ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}})
  436. else ()
  437. __java_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/${_JAVA_SOURCE_FILE}
  438. ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE}
  439. "Copying ${_JAVA_SOURCE_FILE} to the build directory")
  440. list(APPEND _JAVA_RESOURCE_FILES ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE})
  441. list(APPEND _JAVA_RESOURCE_FILES_RELATIVE ${_JAVA_SOURCE_FILE})
  442. endif ()
  443. endforeach()
  444. foreach(_JAVA_INCLUDE_JAR IN LISTS _add_jar_INCLUDE_JARS)
  445. if (TARGET ${_JAVA_INCLUDE_JAR})
  446. get_target_property(_JAVA_JAR_PATH ${_JAVA_INCLUDE_JAR} JAR_FILE)
  447. if (_JAVA_JAR_PATH)
  448. string(APPEND CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_JAR_PATH}")
  449. list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_JAR_PATH})
  450. list(APPEND _JAVA_DEPENDS ${_JAVA_INCLUDE_JAR})
  451. list(APPEND _JAVA_COMPILE_DEPENDS ${_JAVA_JAR_PATH})
  452. else ()
  453. message(SEND_ERROR "add_jar: INCLUDE_JARS target ${_JAVA_INCLUDE_JAR} is not a jar")
  454. endif ()
  455. else ()
  456. string(APPEND CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_INCLUDE_JAR}")
  457. list(APPEND CMAKE_JAVA_INCLUDE_PATH "${_JAVA_INCLUDE_JAR}")
  458. list(APPEND _JAVA_DEPENDS "${_JAVA_INCLUDE_JAR}")
  459. list(APPEND _JAVA_COMPILE_DEPENDS "${_JAVA_INCLUDE_JAR}")
  460. endif ()
  461. endforeach()
  462. if (_JAVA_COMPILE_FILES OR _JAVA_COMPILE_FILELISTS)
  463. set (_JAVA_SOURCES_FILELISTS)
  464. if (_JAVA_COMPILE_FILES)
  465. # Create the list of files to compile.
  466. set(_JAVA_SOURCES_FILE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_sources)
  467. string(REPLACE ";" "\"\n\"" _JAVA_COMPILE_STRING "\"${_JAVA_COMPILE_FILES}\"")
  468. file(WRITE ${_JAVA_SOURCES_FILE} ${_JAVA_COMPILE_STRING})
  469. list (APPEND _JAVA_SOURCES_FILELISTS "@${_JAVA_SOURCES_FILE}")
  470. endif()
  471. if (_JAVA_COMPILE_FILELISTS)
  472. foreach (_JAVA_FILELIST IN LISTS _JAVA_COMPILE_FILELISTS)
  473. list (APPEND _JAVA_SOURCES_FILELISTS "@${_JAVA_FILELIST}")
  474. endforeach()
  475. endif()
  476. # Compile the java files and create a list of class files
  477. add_custom_command(
  478. # NOTE: this command generates an artificial dependency file
  479. OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  480. COMMAND ${Java_JAVAC_EXECUTABLE}
  481. ${CMAKE_JAVA_COMPILE_FLAGS}
  482. -classpath "${CMAKE_JAVA_INCLUDE_PATH_FINAL}"
  483. -d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  484. ${_GENERATE_NATIVE_HEADERS}
  485. ${_JAVA_SOURCES_FILELISTS}
  486. COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  487. DEPENDS ${_JAVA_COMPILE_FILES} ${_JAVA_COMPILE_FILELISTS} ${_JAVA_COMPILE_DEPENDS}
  488. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  489. COMMENT "Building Java objects for ${_TARGET_NAME}.jar"
  490. )
  491. add_custom_command(
  492. OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  493. COMMAND ${CMAKE_COMMAND}
  494. -DCMAKE_JAVA_CLASS_OUTPUT_PATH=${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  495. -DCMAKE_JAR_CLASSES_PREFIX="${CMAKE_JAR_CLASSES_PREFIX}"
  496. -P ${_JAVA_CLASS_FILELIST_SCRIPT}
  497. DEPENDS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  498. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  499. )
  500. else ()
  501. # create an empty java_class_filelist
  502. if (NOT EXISTS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist)
  503. file(WRITE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist "")
  504. endif()
  505. endif ()
  506. # create the jar file
  507. set(_JAVA_JAR_OUTPUT_PATH
  508. "${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_NAME}")
  509. if (CMAKE_JNI_TARGET)
  510. add_custom_command(
  511. OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
  512. COMMAND ${Java_JAR_EXECUTABLE}
  513. -cf${_ENTRY_POINT_OPTION}${_MANIFEST_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE} ${_MANIFEST_VALUE}
  514. ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
  515. COMMAND ${CMAKE_COMMAND}
  516. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  517. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
  518. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  519. -P ${_JAVA_SYMLINK_SCRIPT}
  520. COMMAND ${CMAKE_COMMAND}
  521. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  522. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_JAR_OUTPUT_PATH}
  523. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  524. -P ${_JAVA_SYMLINK_SCRIPT}
  525. DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  526. WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  527. COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
  528. )
  529. else ()
  530. add_custom_command(
  531. OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
  532. COMMAND ${Java_JAR_EXECUTABLE}
  533. -cf${_ENTRY_POINT_OPTION}${_MANIFEST_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE} ${_MANIFEST_VALUE}
  534. ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
  535. COMMAND ${CMAKE_COMMAND}
  536. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  537. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
  538. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  539. -P ${_JAVA_SYMLINK_SCRIPT}
  540. WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  541. DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  542. COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
  543. )
  544. endif ()
  545. # Add the target and make sure we have the latest resource files.
  546. add_custom_target(${_TARGET_NAME} ALL DEPENDS ${_JAVA_JAR_OUTPUT_PATH})
  547. set_property(
  548. TARGET
  549. ${_TARGET_NAME}
  550. PROPERTY
  551. INSTALL_FILES
  552. ${_JAVA_JAR_OUTPUT_PATH}
  553. )
  554. if (_JAVA_TARGET_OUTPUT_LINK)
  555. set_property(
  556. TARGET
  557. ${_TARGET_NAME}
  558. PROPERTY
  559. INSTALL_FILES
  560. ${_JAVA_JAR_OUTPUT_PATH}
  561. ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
  562. )
  563. if (CMAKE_JNI_TARGET)
  564. set_property(
  565. TARGET
  566. ${_TARGET_NAME}
  567. PROPERTY
  568. JNI_SYMLINK
  569. ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
  570. )
  571. endif ()
  572. endif ()
  573. set_property(
  574. TARGET
  575. ${_TARGET_NAME}
  576. PROPERTY
  577. JAR_FILE
  578. ${_JAVA_JAR_OUTPUT_PATH}
  579. )
  580. set_property(
  581. TARGET
  582. ${_TARGET_NAME}
  583. PROPERTY
  584. CLASSDIR
  585. ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  586. )
  587. if (_GENERATE_NATIVE_HEADERS)
  588. # create an INTERFACE library encapsulating include directory for generated headers
  589. add_library (${_GENERATE_NATIVE_HEADERS_TARGET} INTERFACE)
  590. target_include_directories (${_GENERATE_NATIVE_HEADERS_TARGET} INTERFACE
  591. "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}"
  592. ${JNI_INCLUDE_DIRS})
  593. # this INTERFACE library depends on jar generation
  594. add_dependencies (${_GENERATE_NATIVE_HEADERS_TARGET} ${_TARGET_NAME})
  595. set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES
  596. "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}")
  597. endif()
  598. endfunction()
  599. function(INSTALL_JAR _TARGET_NAME)
  600. if (ARGC EQUAL 2)
  601. set (_DESTINATION ${ARGV1})
  602. else()
  603. cmake_parse_arguments(_install_jar
  604. ""
  605. "DESTINATION;COMPONENT"
  606. ""
  607. ${ARGN})
  608. if (_install_jar_DESTINATION)
  609. set (_DESTINATION ${_install_jar_DESTINATION})
  610. else()
  611. message(SEND_ERROR "install_jar: ${_TARGET_NAME}: DESTINATION must be specified.")
  612. endif()
  613. if (_install_jar_COMPONENT)
  614. set (_COMPONENT COMPONENT ${_install_jar_COMPONENT})
  615. endif()
  616. endif()
  617. get_property(__FILES
  618. TARGET
  619. ${_TARGET_NAME}
  620. PROPERTY
  621. INSTALL_FILES
  622. )
  623. set_property(
  624. TARGET
  625. ${_TARGET_NAME}
  626. PROPERTY
  627. INSTALL_DESTINATION
  628. ${_DESTINATION}
  629. )
  630. if (__FILES)
  631. install(
  632. FILES
  633. ${__FILES}
  634. DESTINATION
  635. ${_DESTINATION}
  636. ${_COMPONENT}
  637. )
  638. else ()
  639. message(SEND_ERROR "install_jar: The target ${_TARGET_NAME} is not known in this scope.")
  640. endif ()
  641. endfunction()
  642. function(INSTALL_JNI_SYMLINK _TARGET_NAME)
  643. if (ARGC EQUAL 2)
  644. set (_DESTINATION ${ARGV1})
  645. else()
  646. cmake_parse_arguments(_install_jni_symlink
  647. ""
  648. "DESTINATION;COMPONENT"
  649. ""
  650. ${ARGN})
  651. if (_install_jni_symlink_DESTINATION)
  652. set (_DESTINATION ${_install_jni_symlink_DESTINATION})
  653. else()
  654. message(SEND_ERROR "install_jni_symlink: ${_TARGET_NAME}: DESTINATION must be specified.")
  655. endif()
  656. if (_install_jni_symlink_COMPONENT)
  657. set (_COMPONENT COMPONENT ${_install_jni_symlink_COMPONENT})
  658. endif()
  659. endif()
  660. get_property(__SYMLINK
  661. TARGET
  662. ${_TARGET_NAME}
  663. PROPERTY
  664. JNI_SYMLINK
  665. )
  666. if (__SYMLINK)
  667. install(
  668. FILES
  669. ${__SYMLINK}
  670. DESTINATION
  671. ${_DESTINATION}
  672. ${_COMPONENT}
  673. )
  674. else ()
  675. message(SEND_ERROR "install_jni_symlink: The target ${_TARGET_NAME} is not known in this scope.")
  676. endif ()
  677. endfunction()
  678. function (find_jar VARIABLE)
  679. set(_jar_names)
  680. set(_jar_files)
  681. set(_jar_versions)
  682. set(_jar_paths
  683. /usr/share/java/
  684. /usr/local/share/java/
  685. ${Java_JAR_PATHS})
  686. set(_jar_doc "NOTSET")
  687. set(_state "name")
  688. foreach (arg IN LISTS ARGN)
  689. if (_state STREQUAL "name")
  690. if (arg STREQUAL "VERSIONS")
  691. set(_state "versions")
  692. elseif (arg STREQUAL "NAMES")
  693. set(_state "names")
  694. elseif (arg STREQUAL "PATHS")
  695. set(_state "paths")
  696. elseif (arg STREQUAL "DOC")
  697. set(_state "doc")
  698. else ()
  699. set(_jar_names ${arg})
  700. if (_jar_doc STREQUAL "NOTSET")
  701. set(_jar_doc "Finding ${arg} jar")
  702. endif ()
  703. endif ()
  704. elseif (_state STREQUAL "versions")
  705. if (arg STREQUAL "NAMES")
  706. set(_state "names")
  707. elseif (arg STREQUAL "PATHS")
  708. set(_state "paths")
  709. elseif (arg STREQUAL "DOC")
  710. set(_state "doc")
  711. else ()
  712. set(_jar_versions ${_jar_versions} ${arg})
  713. endif ()
  714. elseif (_state STREQUAL "names")
  715. if (arg STREQUAL "VERSIONS")
  716. set(_state "versions")
  717. elseif (arg STREQUAL "PATHS")
  718. set(_state "paths")
  719. elseif (arg STREQUAL "DOC")
  720. set(_state "doc")
  721. else ()
  722. set(_jar_names ${_jar_names} ${arg})
  723. if (_jar_doc STREQUAL "NOTSET")
  724. set(_jar_doc "Finding ${arg} jar")
  725. endif ()
  726. endif ()
  727. elseif (_state STREQUAL "paths")
  728. if (arg STREQUAL "VERSIONS")
  729. set(_state "versions")
  730. elseif (arg STREQUAL "NAMES")
  731. set(_state "names")
  732. elseif (arg STREQUAL "DOC")
  733. set(_state "doc")
  734. else ()
  735. set(_jar_paths ${_jar_paths} ${arg})
  736. endif ()
  737. elseif (_state STREQUAL "doc")
  738. if (arg STREQUAL "VERSIONS")
  739. set(_state "versions")
  740. elseif (arg STREQUAL "NAMES")
  741. set(_state "names")
  742. elseif (arg STREQUAL "PATHS")
  743. set(_state "paths")
  744. else ()
  745. set(_jar_doc ${arg})
  746. endif ()
  747. endif ()
  748. endforeach ()
  749. if (NOT _jar_names)
  750. message(FATAL_ERROR "find_jar: No name to search for given")
  751. endif ()
  752. foreach (jar_name IN LISTS _jar_names)
  753. foreach (version IN LISTS _jar_versions)
  754. set(_jar_files ${_jar_files} ${jar_name}-${version}.jar)
  755. endforeach ()
  756. set(_jar_files ${_jar_files} ${jar_name}.jar)
  757. endforeach ()
  758. find_file(${VARIABLE}
  759. NAMES ${_jar_files}
  760. PATHS ${_jar_paths}
  761. DOC ${_jar_doc}
  762. NO_DEFAULT_PATH)
  763. endfunction ()
  764. function(create_javadoc _target)
  765. set(_javadoc_packages)
  766. set(_javadoc_files)
  767. set(_javadoc_sourcepath)
  768. set(_javadoc_classpath)
  769. set(_javadoc_installpath "${CMAKE_INSTALL_PREFIX}/share/javadoc")
  770. set(_javadoc_doctitle)
  771. set(_javadoc_windowtitle)
  772. set(_javadoc_author FALSE)
  773. set(_javadoc_version FALSE)
  774. set(_javadoc_use FALSE)
  775. set(_state "package")
  776. foreach (arg IN LISTS ARGN)
  777. if (_state STREQUAL "package")
  778. if (arg STREQUAL "PACKAGES")
  779. set(_state "packages")
  780. elseif (arg STREQUAL "FILES")
  781. set(_state "files")
  782. elseif (arg STREQUAL "SOURCEPATH")
  783. set(_state "sourcepath")
  784. elseif (arg STREQUAL "CLASSPATH")
  785. set(_state "classpath")
  786. elseif (arg STREQUAL "INSTALLPATH")
  787. set(_state "installpath")
  788. elseif (arg STREQUAL "DOCTITLE")
  789. set(_state "doctitle")
  790. elseif (arg STREQUAL "WINDOWTITLE")
  791. set(_state "windowtitle")
  792. elseif (arg STREQUAL "AUTHOR")
  793. set(_state "author")
  794. elseif (arg STREQUAL "USE")
  795. set(_state "use")
  796. elseif (arg STREQUAL "VERSION")
  797. set(_state "version")
  798. else ()
  799. set(_javadoc_packages ${arg})
  800. set(_state "packages")
  801. endif ()
  802. elseif (_state STREQUAL "packages")
  803. if (arg STREQUAL "FILES")
  804. set(_state "files")
  805. elseif (arg STREQUAL "SOURCEPATH")
  806. set(_state "sourcepath")
  807. elseif (arg STREQUAL "CLASSPATH")
  808. set(_state "classpath")
  809. elseif (arg STREQUAL "INSTALLPATH")
  810. set(_state "installpath")
  811. elseif (arg STREQUAL "DOCTITLE")
  812. set(_state "doctitle")
  813. elseif (arg STREQUAL "WINDOWTITLE")
  814. set(_state "windowtitle")
  815. elseif (arg STREQUAL "AUTHOR")
  816. set(_state "author")
  817. elseif (arg STREQUAL "USE")
  818. set(_state "use")
  819. elseif (arg STREQUAL "VERSION")
  820. set(_state "version")
  821. else ()
  822. list(APPEND _javadoc_packages ${arg})
  823. endif ()
  824. elseif (_state STREQUAL "files")
  825. if (arg STREQUAL "PACKAGES")
  826. set(_state "packages")
  827. elseif (arg STREQUAL "SOURCEPATH")
  828. set(_state "sourcepath")
  829. elseif (arg STREQUAL "CLASSPATH")
  830. set(_state "classpath")
  831. elseif (arg STREQUAL "INSTALLPATH")
  832. set(_state "installpath")
  833. elseif (arg STREQUAL "DOCTITLE")
  834. set(_state "doctitle")
  835. elseif (arg STREQUAL "WINDOWTITLE")
  836. set(_state "windowtitle")
  837. elseif (arg STREQUAL "AUTHOR")
  838. set(_state "author")
  839. elseif (arg STREQUAL "USE")
  840. set(_state "use")
  841. elseif (arg STREQUAL "VERSION")
  842. set(_state "version")
  843. else ()
  844. list(APPEND _javadoc_files ${arg})
  845. endif ()
  846. elseif (_state STREQUAL "sourcepath")
  847. if (arg STREQUAL "PACKAGES")
  848. set(_state "packages")
  849. elseif (arg STREQUAL "FILES")
  850. set(_state "files")
  851. elseif (arg STREQUAL "CLASSPATH")
  852. set(_state "classpath")
  853. elseif (arg STREQUAL "INSTALLPATH")
  854. set(_state "installpath")
  855. elseif (arg STREQUAL "DOCTITLE")
  856. set(_state "doctitle")
  857. elseif (arg STREQUAL "WINDOWTITLE")
  858. set(_state "windowtitle")
  859. elseif (arg STREQUAL "AUTHOR")
  860. set(_state "author")
  861. elseif (arg STREQUAL "USE")
  862. set(_state "use")
  863. elseif (arg STREQUAL "VERSION")
  864. set(_state "version")
  865. else ()
  866. list(APPEND _javadoc_sourcepath ${arg})
  867. endif ()
  868. elseif (_state STREQUAL "classpath")
  869. if (arg STREQUAL "PACKAGES")
  870. set(_state "packages")
  871. elseif (arg STREQUAL "FILES")
  872. set(_state "files")
  873. elseif (arg STREQUAL "SOURCEPATH")
  874. set(_state "sourcepath")
  875. elseif (arg STREQUAL "INSTALLPATH")
  876. set(_state "installpath")
  877. elseif (arg STREQUAL "DOCTITLE")
  878. set(_state "doctitle")
  879. elseif (arg STREQUAL "WINDOWTITLE")
  880. set(_state "windowtitle")
  881. elseif (arg STREQUAL "AUTHOR")
  882. set(_state "author")
  883. elseif (arg STREQUAL "USE")
  884. set(_state "use")
  885. elseif (arg STREQUAL "VERSION")
  886. set(_state "version")
  887. else ()
  888. list(APPEND _javadoc_classpath ${arg})
  889. endif ()
  890. elseif (_state STREQUAL "installpath")
  891. if (arg STREQUAL "PACKAGES")
  892. set(_state "packages")
  893. elseif (arg STREQUAL "FILES")
  894. set(_state "files")
  895. elseif (arg STREQUAL "SOURCEPATH")
  896. set(_state "sourcepath")
  897. elseif (arg STREQUAL "DOCTITLE")
  898. set(_state "doctitle")
  899. elseif (arg STREQUAL "WINDOWTITLE")
  900. set(_state "windowtitle")
  901. elseif (arg STREQUAL "AUTHOR")
  902. set(_state "author")
  903. elseif (arg STREQUAL "USE")
  904. set(_state "use")
  905. elseif (arg STREQUAL "VERSION")
  906. set(_state "version")
  907. else ()
  908. set(_javadoc_installpath ${arg})
  909. endif ()
  910. elseif (_state STREQUAL "doctitle")
  911. if (${arg} STREQUAL "PACKAGES")
  912. set(_state "packages")
  913. elseif (arg STREQUAL "FILES")
  914. set(_state "files")
  915. elseif (arg STREQUAL "SOURCEPATH")
  916. set(_state "sourcepath")
  917. elseif (arg STREQUAL "INSTALLPATH")
  918. set(_state "installpath")
  919. elseif (arg STREQUAL "CLASSPATH")
  920. set(_state "classpath")
  921. elseif (arg STREQUAL "WINDOWTITLE")
  922. set(_state "windowtitle")
  923. elseif (arg STREQUAL "AUTHOR")
  924. set(_state "author")
  925. elseif (arg STREQUAL "USE")
  926. set(_state "use")
  927. elseif (arg STREQUAL "VERSION")
  928. set(_state "version")
  929. else ()
  930. set(_javadoc_doctitle ${arg})
  931. endif ()
  932. elseif (_state STREQUAL "windowtitle")
  933. if (${arg} STREQUAL "PACKAGES")
  934. set(_state "packages")
  935. elseif (arg STREQUAL "FILES")
  936. set(_state "files")
  937. elseif (arg STREQUAL "SOURCEPATH")
  938. set(_state "sourcepath")
  939. elseif (arg STREQUAL "CLASSPATH")
  940. set(_state "classpath")
  941. elseif (arg STREQUAL "INSTALLPATH")
  942. set(_state "installpath")
  943. elseif (arg STREQUAL "DOCTITLE")
  944. set(_state "doctitle")
  945. elseif (arg STREQUAL "AUTHOR")
  946. set(_state "author")
  947. elseif (arg STREQUAL "USE")
  948. set(_state "use")
  949. elseif (arg STREQUAL "VERSION")
  950. set(_state "version")
  951. else ()
  952. set(_javadoc_windowtitle ${arg})
  953. endif ()
  954. elseif (_state STREQUAL "author")
  955. if (arg STREQUAL "PACKAGES")
  956. set(_state "packages")
  957. elseif (arg STREQUAL "FILES")
  958. set(_state "files")
  959. elseif (arg STREQUAL "SOURCEPATH")
  960. set(_state "sourcepath")
  961. elseif (arg STREQUAL "CLASSPATH")
  962. set(_state "classpath")
  963. elseif (arg STREQUAL "INSTALLPATH")
  964. set(_state "installpath")
  965. elseif (arg STREQUAL "DOCTITLE")
  966. set(_state "doctitle")
  967. elseif (arg STREQUAL "WINDOWTITLE")
  968. set(_state "windowtitle")
  969. elseif (arg STREQUAL "AUTHOR")
  970. set(_state "author")
  971. elseif (arg STREQUAL "USE")
  972. set(_state "use")
  973. elseif (arg STREQUAL "VERSION")
  974. set(_state "version")
  975. else ()
  976. set(_javadoc_author ${arg})
  977. endif ()
  978. elseif (_state STREQUAL "use")
  979. if (arg STREQUAL "PACKAGES")
  980. set(_state "packages")
  981. elseif (arg STREQUAL "FILES")
  982. set(_state "files")
  983. elseif (arg STREQUAL "SOURCEPATH")
  984. set(_state "sourcepath")
  985. elseif (arg STREQUAL "CLASSPATH")
  986. set(_state "classpath")
  987. elseif (arg STREQUAL "INSTALLPATH")
  988. set(_state "installpath")
  989. elseif (arg STREQUAL "DOCTITLE")
  990. set(_state "doctitle")
  991. elseif (arg STREQUAL "WINDOWTITLE")
  992. set(_state "windowtitle")
  993. elseif (arg STREQUAL "AUTHOR")
  994. set(_state "author")
  995. elseif (arg STREQUAL "USE")
  996. set(_state "use")
  997. elseif (arg STREQUAL "VERSION")
  998. set(_state "version")
  999. else ()
  1000. set(_javadoc_use ${arg})
  1001. endif ()
  1002. elseif (_state STREQUAL "version")
  1003. if (arg STREQUAL "PACKAGES")
  1004. set(_state "packages")
  1005. elseif (arg STREQUAL "FILES")
  1006. set(_state "files")
  1007. elseif (arg STREQUAL "SOURCEPATH")
  1008. set(_state "sourcepath")
  1009. elseif (arg STREQUAL "CLASSPATH")
  1010. set(_state "classpath")
  1011. elseif (arg STREQUAL "INSTALLPATH")
  1012. set(_state "installpath")
  1013. elseif (arg STREQUAL "DOCTITLE")
  1014. set(_state "doctitle")
  1015. elseif (arg STREQUAL "WINDOWTITLE")
  1016. set(_state "windowtitle")
  1017. elseif (arg STREQUAL "AUTHOR")
  1018. set(_state "author")
  1019. elseif (arg STREQUAL "USE")
  1020. set(_state "use")
  1021. elseif (arg STREQUAL "VERSION")
  1022. set(_state "version")
  1023. else ()
  1024. set(_javadoc_version ${arg})
  1025. endif ()
  1026. endif ()
  1027. endforeach ()
  1028. set(_javadoc_builddir ${CMAKE_CURRENT_BINARY_DIR}/javadoc/${_target})
  1029. set(_javadoc_options -d ${_javadoc_builddir})
  1030. if (_javadoc_sourcepath)
  1031. set(_start TRUE)
  1032. foreach(_path IN LISTS _javadoc_sourcepath)
  1033. if (_start)
  1034. set(_sourcepath ${_path})
  1035. set(_start FALSE)
  1036. else ()
  1037. set(_sourcepath ${_sourcepath}:${_path})
  1038. endif ()
  1039. endforeach()
  1040. set(_javadoc_options ${_javadoc_options} -sourcepath ${_sourcepath})
  1041. endif ()
  1042. if (_javadoc_classpath)
  1043. set(_start TRUE)
  1044. foreach(_path IN LISTS _javadoc_classpath)
  1045. if (_start)
  1046. set(_classpath ${_path})
  1047. set(_start FALSE)
  1048. else ()
  1049. set(_classpath ${_classpath}:${_path})
  1050. endif ()
  1051. endforeach()
  1052. set(_javadoc_options ${_javadoc_options} -classpath "${_classpath}")
  1053. endif ()
  1054. if (_javadoc_doctitle)
  1055. set(_javadoc_options ${_javadoc_options} -doctitle '${_javadoc_doctitle}')
  1056. endif ()
  1057. if (_javadoc_windowtitle)
  1058. set(_javadoc_options ${_javadoc_options} -windowtitle '${_javadoc_windowtitle}')
  1059. endif ()
  1060. if (_javadoc_author)
  1061. set(_javadoc_options ${_javadoc_options} -author)
  1062. endif ()
  1063. if (_javadoc_use)
  1064. set(_javadoc_options ${_javadoc_options} -use)
  1065. endif ()
  1066. if (_javadoc_version)
  1067. set(_javadoc_options ${_javadoc_options} -version)
  1068. endif ()
  1069. add_custom_target(${_target}_javadoc ALL
  1070. COMMAND ${Java_JAVADOC_EXECUTABLE} ${_javadoc_options}
  1071. ${_javadoc_files}
  1072. ${_javadoc_packages}
  1073. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  1074. )
  1075. install(
  1076. DIRECTORY ${_javadoc_builddir}
  1077. DESTINATION ${_javadoc_installpath}
  1078. )
  1079. endfunction()
  1080. function (create_javah)
  1081. if (Java_VERSION VERSION_GREATER_EQUAL 10)
  1082. message (FATAL_ERROR "create_javah: not supported with this Java version. Use add_jar(GENERATE_NATIVE_HEADERS) instead.")
  1083. elseif (Java_VERSION VERSION_GREATER_EQUAL 1.8)
  1084. message (DEPRECATION "create_javah: this command will no longer be supported starting with version 10 of JDK. Update your project by using command add_jar(GENERATE_NATIVE_HEADERS) instead.")
  1085. endif()
  1086. cmake_parse_arguments(_create_javah
  1087. ""
  1088. "TARGET;GENERATED_FILES;OUTPUT_NAME;OUTPUT_DIR"
  1089. "CLASSES;CLASSPATH;DEPENDS"
  1090. ${ARGN})
  1091. # ckeck parameters
  1092. if (NOT _create_javah_TARGET AND NOT _create_javah_GENERATED_FILES)
  1093. message (FATAL_ERROR "create_javah: TARGET or GENERATED_FILES must be specified.")
  1094. endif()
  1095. if (_create_javah_OUTPUT_NAME AND _create_javah_OUTPUT_DIR)
  1096. message (FATAL_ERROR "create_javah: OUTPUT_NAME and OUTPUT_DIR are mutually exclusive.")
  1097. endif()
  1098. if (NOT _create_javah_CLASSES)
  1099. message (FATAL_ERROR "create_javah: CLASSES is a required parameter.")
  1100. endif()
  1101. set (_output_files)
  1102. if (WIN32 AND NOT CYGWIN AND CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  1103. set(_classpath_sep "$<SEMICOLON>")
  1104. else ()
  1105. set(_classpath_sep ":")
  1106. endif()
  1107. # handle javah options
  1108. set (_javah_options)
  1109. if (_create_javah_CLASSPATH)
  1110. # CLASSPATH can specify directories, jar files or targets created with add_jar command
  1111. set (_classpath)
  1112. foreach (_path IN LISTS _create_javah_CLASSPATH)
  1113. if (TARGET ${_path})
  1114. get_target_property (_jar_path ${_path} JAR_FILE)
  1115. if (_jar_path)
  1116. list (APPEND _classpath "${_jar_path}")
  1117. list (APPEND _create_javah_DEPENDS "${_path}")
  1118. else()
  1119. message(SEND_ERROR "create_javah: CLASSPATH target ${_path} is not a jar.")
  1120. endif()
  1121. elseif (EXISTS "${_path}")
  1122. list (APPEND _classpath "${_path}")
  1123. if (NOT IS_DIRECTORY "${_path}")
  1124. list (APPEND _create_javah_DEPENDS "${_path}")
  1125. endif()
  1126. else()
  1127. message(SEND_ERROR "create_javah: CLASSPATH entry ${_path} does not exist.")
  1128. endif()
  1129. endforeach()
  1130. string (REPLACE ";" "${_classpath_sep}" _classpath "${_classpath}")
  1131. list (APPEND _javah_options -classpath "${_classpath}")
  1132. endif()
  1133. if (_create_javah_OUTPUT_DIR)
  1134. list (APPEND _javah_options -d "${_create_javah_OUTPUT_DIR}")
  1135. endif()
  1136. if (_create_javah_OUTPUT_NAME)
  1137. list (APPEND _javah_options -o "${_create_javah_OUTPUT_NAME}")
  1138. set (_output_files "${_create_javah_OUTPUT_NAME}")
  1139. get_filename_component (_create_javah_OUTPUT_DIR "${_create_javah_OUTPUT_NAME}" DIRECTORY)
  1140. get_filename_component (_create_javah_OUTPUT_DIR "${_create_javah_OUTPUT_DIR}" ABSOLUTE)
  1141. endif()
  1142. if (NOT _create_javah_OUTPUT_DIR)
  1143. set (_create_javah_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  1144. endif()
  1145. if (NOT _create_javah_OUTPUT_NAME)
  1146. # compute output names
  1147. foreach (_class IN LISTS _create_javah_CLASSES)
  1148. string (REPLACE "." "_" _c_header "${_class}")
  1149. set (_c_header "${_create_javah_OUTPUT_DIR}/${_c_header}.h")
  1150. list (APPEND _output_files "${_c_header}")
  1151. endforeach()
  1152. endif()
  1153. # finalize custom command arguments
  1154. if (_create_javah_DEPENDS)
  1155. list (INSERT _create_javah_DEPENDS 0 DEPENDS)
  1156. endif()
  1157. add_custom_command (OUTPUT ${_output_files}
  1158. COMMAND "${Java_JAVAH_EXECUTABLE}" ${_javah_options} -jni ${_create_javah_CLASSES}
  1159. ${_create_javah_DEPENDS}
  1160. WORKING_DIRECTORY ${_create_javah_OUTPUT_DIR}
  1161. COMMENT "Building C header files from classes...")
  1162. if (_create_javah_TARGET)
  1163. add_custom_target (${_create_javah_TARGET} ALL DEPENDS ${_output_files})
  1164. endif()
  1165. if (_create_javah_GENERATED_FILES)
  1166. set (${_create_javah_GENERATED_FILES} ${_output_files} PARENT_SCOPE)
  1167. endif()
  1168. endfunction()
  1169. function(export_jars)
  1170. # Parse and validate arguments
  1171. cmake_parse_arguments(_export_jars
  1172. ""
  1173. "FILE;NAMESPACE"
  1174. "TARGETS"
  1175. ${ARGN}
  1176. )
  1177. if (NOT _export_jars_FILE)
  1178. message(SEND_ERROR "export_jars: FILE must be specified.")
  1179. endif()
  1180. if (NOT _export_jars_TARGETS)
  1181. message(SEND_ERROR "export_jars: TARGETS must be specified.")
  1182. endif()
  1183. set(_jar_NAMESPACE "${_export_jars_NAMESPACE}")
  1184. # Set content of generated exports file
  1185. string(REPLACE ";" " " __targets__ "${_export_jars_TARGETS}")
  1186. set(__targetdefs__ "")
  1187. foreach(_target IN LISTS _export_jars_TARGETS)
  1188. get_target_property(_jarpath ${_target} JAR_FILE)
  1189. get_filename_component(_jarpath ${_jarpath} PATH)
  1190. __java_export_jar(__targetdefs__ ${_target} "${_jarpath}")
  1191. endforeach()
  1192. # Generate exports file
  1193. configure_file(
  1194. ${_JAVA_EXPORT_TARGETS_SCRIPT}
  1195. ${_export_jars_FILE}
  1196. @ONLY
  1197. )
  1198. endfunction()
  1199. function(install_jar_exports)
  1200. # Parse and validate arguments
  1201. cmake_parse_arguments(_install_jar_exports
  1202. ""
  1203. "FILE;DESTINATION;COMPONENT;NAMESPACE"
  1204. "TARGETS"
  1205. ${ARGN}
  1206. )
  1207. if (NOT _install_jar_exports_FILE)
  1208. message(SEND_ERROR "install_jar_exports: FILE must be specified.")
  1209. endif()
  1210. if (NOT _install_jar_exports_DESTINATION)
  1211. message(SEND_ERROR "install_jar_exports: DESTINATION must be specified.")
  1212. endif()
  1213. if (NOT _install_jar_exports_TARGETS)
  1214. message(SEND_ERROR "install_jar_exports: TARGETS must be specified.")
  1215. endif()
  1216. set(_jar_NAMESPACE "${_install_jar_exports_NAMESPACE}")
  1217. if (_install_jar_exports_COMPONENT)
  1218. set (_COMPONENT COMPONENT ${_install_jar_exports_COMPONENT})
  1219. endif()
  1220. # Determine relative path from installed export file to install prefix
  1221. if(IS_ABSOLUTE "${_install_jar_exports_DESTINATION}")
  1222. file(RELATIVE_PATH _relpath
  1223. ${_install_jar_exports_DESTINATION}
  1224. ${CMAKE_INSTALL_PREFIX}
  1225. )
  1226. else()
  1227. file(RELATIVE_PATH _relpath
  1228. ${CMAKE_INSTALL_PREFIX}/${_install_jar_exports_DESTINATION}
  1229. ${CMAKE_INSTALL_PREFIX}
  1230. )
  1231. endif()
  1232. # Set up unique location for generated exports file
  1233. string(SHA256 _hash "${_install_jar_exports_DESTINATION}")
  1234. set(_tmpdir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/JavaExports/${_hash})
  1235. # Set content of generated exports file
  1236. string(REPLACE ";" " " __targets__ "${_install_jar_exports_TARGETS}")
  1237. set(__targetdefs__ "set(_prefix \${CMAKE_CURRENT_LIST_DIR}/${_relpath})\n\n")
  1238. foreach(_target IN LISTS _install_jar_exports_TARGETS)
  1239. get_target_property(_dir ${_target} INSTALL_DESTINATION)
  1240. __java_export_jar(__targetdefs__ ${_target} "\${_prefix}/${_dir}")
  1241. endforeach()
  1242. __java_lcat(__targetdefs__ "\nunset(_prefix)")
  1243. # Generate and install exports file
  1244. configure_file(
  1245. ${_JAVA_EXPORT_TARGETS_SCRIPT}
  1246. ${_tmpdir}/${_install_jar_exports_FILE}
  1247. @ONLY
  1248. )
  1249. install(FILES ${_tmpdir}/${_install_jar_exports_FILE}
  1250. DESTINATION ${_install_jar_exports_DESTINATION}
  1251. ${_COMPONENT})
  1252. endfunction()