UseJava.cmake 52 KB

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