UseJava.cmake 48 KB

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