UseJava.cmake 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. # ensure output directory exists
  502. file (MAKE_DIRECTORY "${CMAKE_JAVA_CLASS_OUTPUT_PATH}")
  503. set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}.jar")
  504. if (_add_jar_OUTPUT_NAME AND _add_jar_VERSION)
  505. set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}-${_add_jar_VERSION}.jar")
  506. set(_JAVA_TARGET_OUTPUT_LINK "${_add_jar_OUTPUT_NAME}.jar")
  507. elseif (_add_jar_VERSION)
  508. set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}-${_add_jar_VERSION}.jar")
  509. set(_JAVA_TARGET_OUTPUT_LINK "${_TARGET_NAME}.jar")
  510. elseif (_add_jar_OUTPUT_NAME)
  511. set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}.jar")
  512. endif ()
  513. set(_JAVA_CLASS_FILES)
  514. set(_JAVA_COMPILE_FILES)
  515. set(_JAVA_COMPILE_FILELISTS)
  516. set(_JAVA_DEPENDS)
  517. set(_JAVA_COMPILE_DEPENDS)
  518. set(_JAVA_RESOURCE_FILES)
  519. set(_JAVA_RESOURCE_FILES_RELATIVE)
  520. foreach(_JAVA_SOURCE_FILE ${_JAVA_SOURCE_FILES})
  521. get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
  522. get_filename_component(_JAVA_FILE ${_JAVA_SOURCE_FILE} NAME_WE)
  523. get_filename_component(_JAVA_PATH ${_JAVA_SOURCE_FILE} PATH)
  524. get_filename_component(_JAVA_FULL ${_JAVA_SOURCE_FILE} ABSOLUTE)
  525. if (_JAVA_SOURCE_FILE MATCHES "^@(.+)$")
  526. get_filename_component(_JAVA_FULL ${CMAKE_MATCH_1} ABSOLUTE)
  527. list(APPEND _JAVA_COMPILE_FILELISTS ${_JAVA_FULL})
  528. elseif (_JAVA_EXT MATCHES ".java")
  529. file(RELATIVE_PATH _JAVA_REL_BINARY_PATH ${_add_jar_OUTPUT_DIR} ${_JAVA_FULL})
  530. file(RELATIVE_PATH _JAVA_REL_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${_JAVA_FULL})
  531. string(LENGTH ${_JAVA_REL_BINARY_PATH} _BIN_LEN)
  532. string(LENGTH ${_JAVA_REL_SOURCE_PATH} _SRC_LEN)
  533. if (${_BIN_LEN} LESS ${_SRC_LEN})
  534. set(_JAVA_REL_PATH ${_JAVA_REL_BINARY_PATH})
  535. else ()
  536. set(_JAVA_REL_PATH ${_JAVA_REL_SOURCE_PATH})
  537. endif ()
  538. get_filename_component(_JAVA_REL_PATH ${_JAVA_REL_PATH} PATH)
  539. list(APPEND _JAVA_COMPILE_FILES ${_JAVA_SOURCE_FILE})
  540. set(_JAVA_CLASS_FILE "${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_REL_PATH}/${_JAVA_FILE}.class")
  541. set(_JAVA_CLASS_FILES ${_JAVA_CLASS_FILES} ${_JAVA_CLASS_FILE})
  542. elseif (_JAVA_EXT MATCHES ".jar"
  543. OR _JAVA_EXT MATCHES ".war"
  544. OR _JAVA_EXT MATCHES ".ear"
  545. OR _JAVA_EXT MATCHES ".sar")
  546. # Ignored for backward compatibility
  547. elseif (_JAVA_EXT STREQUAL "")
  548. list(APPEND CMAKE_JAVA_INCLUDE_PATH ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}} ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}_CLASSPATH})
  549. list(APPEND _JAVA_DEPENDS ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}})
  550. else ()
  551. __java_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/${_JAVA_SOURCE_FILE}
  552. ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE}
  553. "Copying ${_JAVA_SOURCE_FILE} to the build directory")
  554. list(APPEND _JAVA_RESOURCE_FILES ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE})
  555. list(APPEND _JAVA_RESOURCE_FILES_RELATIVE ${_JAVA_SOURCE_FILE})
  556. endif ()
  557. endforeach()
  558. foreach(_JAVA_INCLUDE_JAR ${_add_jar_INCLUDE_JARS})
  559. if (TARGET ${_JAVA_INCLUDE_JAR})
  560. get_target_property(_JAVA_JAR_PATH ${_JAVA_INCLUDE_JAR} JAR_FILE)
  561. if (_JAVA_JAR_PATH)
  562. string(APPEND CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_JAR_PATH}")
  563. list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_JAR_PATH})
  564. list(APPEND _JAVA_DEPENDS ${_JAVA_INCLUDE_JAR})
  565. list(APPEND _JAVA_COMPILE_DEPENDS ${_JAVA_INCLUDE_JAR})
  566. else ()
  567. message(SEND_ERROR "add_jar: INCLUDE_JARS target ${_JAVA_INCLUDE_JAR} is not a jar")
  568. endif ()
  569. else ()
  570. string(APPEND CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_INCLUDE_JAR}")
  571. list(APPEND CMAKE_JAVA_INCLUDE_PATH "${_JAVA_INCLUDE_JAR}")
  572. list(APPEND _JAVA_DEPENDS "${_JAVA_INCLUDE_JAR}")
  573. list(APPEND _JAVA_COMPILE_DEPENDS "${_JAVA_INCLUDE_JAR}")
  574. endif ()
  575. endforeach()
  576. if (_JAVA_COMPILE_FILES OR _JAVA_COMPILE_FILELISTS)
  577. set (_JAVA_SOURCES_FILELISTS)
  578. if (_JAVA_COMPILE_FILES)
  579. # Create the list of files to compile.
  580. set(_JAVA_SOURCES_FILE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_sources)
  581. string(REPLACE ";" "\"\n\"" _JAVA_COMPILE_STRING "\"${_JAVA_COMPILE_FILES}\"")
  582. file(WRITE ${_JAVA_SOURCES_FILE} ${_JAVA_COMPILE_STRING})
  583. list (APPEND _JAVA_SOURCES_FILELISTS "@${_JAVA_SOURCES_FILE}")
  584. endif()
  585. if (_JAVA_COMPILE_FILELISTS)
  586. foreach (_JAVA_FILELIST IN LISTS _JAVA_COMPILE_FILELISTS)
  587. list (APPEND _JAVA_SOURCES_FILELISTS "@${_JAVA_FILELIST}")
  588. endforeach()
  589. endif()
  590. # Compile the java files and create a list of class files
  591. add_custom_command(
  592. # NOTE: this command generates an artificial dependency file
  593. OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  594. COMMAND ${Java_JAVAC_EXECUTABLE}
  595. ${CMAKE_JAVA_COMPILE_FLAGS}
  596. -classpath "${CMAKE_JAVA_INCLUDE_PATH_FINAL}"
  597. -d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  598. ${_JAVA_SOURCES_FILELISTS}
  599. COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  600. DEPENDS ${_JAVA_COMPILE_FILES} ${_JAVA_COMPILE_FILELISTS} ${_JAVA_COMPILE_DEPENDS}
  601. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  602. COMMENT "Building Java objects for ${_TARGET_NAME}.jar"
  603. )
  604. add_custom_command(
  605. OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  606. COMMAND ${CMAKE_COMMAND}
  607. -DCMAKE_JAVA_CLASS_OUTPUT_PATH=${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  608. -DCMAKE_JAR_CLASSES_PREFIX="${CMAKE_JAR_CLASSES_PREFIX}"
  609. -P ${_JAVA_CLASS_FILELIST_SCRIPT}
  610. DEPENDS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
  611. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  612. )
  613. else ()
  614. # create an empty java_class_filelist
  615. if (NOT EXISTS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist)
  616. file(WRITE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist "")
  617. endif()
  618. endif ()
  619. # create the jar file
  620. set(_JAVA_JAR_OUTPUT_PATH
  621. ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_NAME})
  622. if (CMAKE_JNI_TARGET)
  623. add_custom_command(
  624. OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
  625. COMMAND ${Java_JAR_EXECUTABLE}
  626. -cf${_ENTRY_POINT_OPTION}${_MANIFEST_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE} ${_MANIFEST_VALUE}
  627. ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
  628. COMMAND ${CMAKE_COMMAND}
  629. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  630. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
  631. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  632. -P ${_JAVA_SYMLINK_SCRIPT}
  633. COMMAND ${CMAKE_COMMAND}
  634. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  635. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_JAR_OUTPUT_PATH}
  636. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  637. -P ${_JAVA_SYMLINK_SCRIPT}
  638. DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  639. WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  640. COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
  641. )
  642. else ()
  643. add_custom_command(
  644. OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
  645. COMMAND ${Java_JAR_EXECUTABLE}
  646. -cf${_ENTRY_POINT_OPTION}${_MANIFEST_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE} ${_MANIFEST_VALUE}
  647. ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
  648. COMMAND ${CMAKE_COMMAND}
  649. -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
  650. -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
  651. -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
  652. -P ${_JAVA_SYMLINK_SCRIPT}
  653. WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  654. DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
  655. COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
  656. )
  657. endif ()
  658. # Add the target and make sure we have the latest resource files.
  659. add_custom_target(${_TARGET_NAME} ALL DEPENDS ${_JAVA_JAR_OUTPUT_PATH})
  660. set_property(
  661. TARGET
  662. ${_TARGET_NAME}
  663. PROPERTY
  664. INSTALL_FILES
  665. ${_JAVA_JAR_OUTPUT_PATH}
  666. )
  667. if (_JAVA_TARGET_OUTPUT_LINK)
  668. set_property(
  669. TARGET
  670. ${_TARGET_NAME}
  671. PROPERTY
  672. INSTALL_FILES
  673. ${_JAVA_JAR_OUTPUT_PATH}
  674. ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
  675. )
  676. if (CMAKE_JNI_TARGET)
  677. set_property(
  678. TARGET
  679. ${_TARGET_NAME}
  680. PROPERTY
  681. JNI_SYMLINK
  682. ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
  683. )
  684. endif ()
  685. endif ()
  686. set_property(
  687. TARGET
  688. ${_TARGET_NAME}
  689. PROPERTY
  690. JAR_FILE
  691. ${_JAVA_JAR_OUTPUT_PATH}
  692. )
  693. set_property(
  694. TARGET
  695. ${_TARGET_NAME}
  696. PROPERTY
  697. CLASSDIR
  698. ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
  699. )
  700. endfunction()
  701. function(INSTALL_JAR _TARGET_NAME)
  702. if (ARGC EQUAL 2)
  703. set (_DESTINATION ${ARGV1})
  704. else()
  705. cmake_parse_arguments(_install_jar
  706. ""
  707. "DESTINATION;COMPONENT"
  708. ""
  709. ${ARGN})
  710. if (_install_jar_DESTINATION)
  711. set (_DESTINATION ${_install_jar_DESTINATION})
  712. else()
  713. message(SEND_ERROR "install_jar: ${_TARGET_NAME}: DESTINATION must be specified.")
  714. endif()
  715. if (_install_jar_COMPONENT)
  716. set (_COMPONENT COMPONENT ${_install_jar_COMPONENT})
  717. endif()
  718. endif()
  719. get_property(__FILES
  720. TARGET
  721. ${_TARGET_NAME}
  722. PROPERTY
  723. INSTALL_FILES
  724. )
  725. set_property(
  726. TARGET
  727. ${_TARGET_NAME}
  728. PROPERTY
  729. INSTALL_DESTINATION
  730. ${_DESTINATION}
  731. )
  732. if (__FILES)
  733. install(
  734. FILES
  735. ${__FILES}
  736. DESTINATION
  737. ${_DESTINATION}
  738. ${_COMPONENT}
  739. )
  740. else ()
  741. message(SEND_ERROR "install_jar: The target ${_TARGET_NAME} is not known in this scope.")
  742. endif ()
  743. endfunction()
  744. function(INSTALL_JNI_SYMLINK _TARGET_NAME)
  745. if (ARGC EQUAL 2)
  746. set (_DESTINATION ${ARGV1})
  747. else()
  748. cmake_parse_arguments(_install_jni_symlink
  749. ""
  750. "DESTINATION;COMPONENT"
  751. ""
  752. ${ARGN})
  753. if (_install_jni_symlink_DESTINATION)
  754. set (_DESTINATION ${_install_jni_symlink_DESTINATION})
  755. else()
  756. message(SEND_ERROR "install_jni_symlink: ${_TARGET_NAME}: DESTINATION must be specified.")
  757. endif()
  758. if (_install_jni_symlink_COMPONENT)
  759. set (_COMPONENT COMPONENT ${_install_jni_symlink_COMPONENT})
  760. endif()
  761. endif()
  762. get_property(__SYMLINK
  763. TARGET
  764. ${_TARGET_NAME}
  765. PROPERTY
  766. JNI_SYMLINK
  767. )
  768. if (__SYMLINK)
  769. install(
  770. FILES
  771. ${__SYMLINK}
  772. DESTINATION
  773. ${_DESTINATION}
  774. ${_COMPONENT}
  775. )
  776. else ()
  777. message(SEND_ERROR "install_jni_symlink: The target ${_TARGET_NAME} is not known in this scope.")
  778. endif ()
  779. endfunction()
  780. function (find_jar VARIABLE)
  781. set(_jar_names)
  782. set(_jar_files)
  783. set(_jar_versions)
  784. set(_jar_paths
  785. /usr/share/java/
  786. /usr/local/share/java/
  787. ${Java_JAR_PATHS})
  788. set(_jar_doc "NOTSET")
  789. set(_state "name")
  790. foreach (arg ${ARGN})
  791. if (${_state} STREQUAL "name")
  792. if (${arg} STREQUAL "VERSIONS")
  793. set(_state "versions")
  794. elseif (${arg} STREQUAL "NAMES")
  795. set(_state "names")
  796. elseif (${arg} STREQUAL "PATHS")
  797. set(_state "paths")
  798. elseif (${arg} STREQUAL "DOC")
  799. set(_state "doc")
  800. else ()
  801. set(_jar_names ${arg})
  802. if (_jar_doc STREQUAL "NOTSET")
  803. set(_jar_doc "Finding ${arg} jar")
  804. endif ()
  805. endif ()
  806. elseif (${_state} STREQUAL "versions")
  807. if (${arg} STREQUAL "NAMES")
  808. set(_state "names")
  809. elseif (${arg} STREQUAL "PATHS")
  810. set(_state "paths")
  811. elseif (${arg} STREQUAL "DOC")
  812. set(_state "doc")
  813. else ()
  814. set(_jar_versions ${_jar_versions} ${arg})
  815. endif ()
  816. elseif (${_state} STREQUAL "names")
  817. if (${arg} STREQUAL "VERSIONS")
  818. set(_state "versions")
  819. elseif (${arg} STREQUAL "PATHS")
  820. set(_state "paths")
  821. elseif (${arg} STREQUAL "DOC")
  822. set(_state "doc")
  823. else ()
  824. set(_jar_names ${_jar_names} ${arg})
  825. if (_jar_doc STREQUAL "NOTSET")
  826. set(_jar_doc "Finding ${arg} jar")
  827. endif ()
  828. endif ()
  829. elseif (${_state} STREQUAL "paths")
  830. if (${arg} STREQUAL "VERSIONS")
  831. set(_state "versions")
  832. elseif (${arg} STREQUAL "NAMES")
  833. set(_state "names")
  834. elseif (${arg} STREQUAL "DOC")
  835. set(_state "doc")
  836. else ()
  837. set(_jar_paths ${_jar_paths} ${arg})
  838. endif ()
  839. elseif (${_state} STREQUAL "doc")
  840. if (${arg} STREQUAL "VERSIONS")
  841. set(_state "versions")
  842. elseif (${arg} STREQUAL "NAMES")
  843. set(_state "names")
  844. elseif (${arg} STREQUAL "PATHS")
  845. set(_state "paths")
  846. else ()
  847. set(_jar_doc ${arg})
  848. endif ()
  849. endif ()
  850. endforeach ()
  851. if (NOT _jar_names)
  852. message(FATAL_ERROR "find_jar: No name to search for given")
  853. endif ()
  854. foreach (jar_name ${_jar_names})
  855. foreach (version ${_jar_versions})
  856. set(_jar_files ${_jar_files} ${jar_name}-${version}.jar)
  857. endforeach ()
  858. set(_jar_files ${_jar_files} ${jar_name}.jar)
  859. endforeach ()
  860. find_file(${VARIABLE}
  861. NAMES ${_jar_files}
  862. PATHS ${_jar_paths}
  863. DOC ${_jar_doc}
  864. NO_DEFAULT_PATH)
  865. endfunction ()
  866. function(create_javadoc _target)
  867. set(_javadoc_packages)
  868. set(_javadoc_files)
  869. set(_javadoc_sourcepath)
  870. set(_javadoc_classpath)
  871. set(_javadoc_installpath "${CMAKE_INSTALL_PREFIX}/share/javadoc")
  872. set(_javadoc_doctitle)
  873. set(_javadoc_windowtitle)
  874. set(_javadoc_author FALSE)
  875. set(_javadoc_version FALSE)
  876. set(_javadoc_use FALSE)
  877. set(_state "package")
  878. foreach (arg ${ARGN})
  879. if (${_state} STREQUAL "package")
  880. if (${arg} STREQUAL "PACKAGES")
  881. set(_state "packages")
  882. elseif (${arg} STREQUAL "FILES")
  883. set(_state "files")
  884. elseif (${arg} STREQUAL "SOURCEPATH")
  885. set(_state "sourcepath")
  886. elseif (${arg} STREQUAL "CLASSPATH")
  887. set(_state "classpath")
  888. elseif (${arg} STREQUAL "INSTALLPATH")
  889. set(_state "installpath")
  890. elseif (${arg} STREQUAL "DOCTITLE")
  891. set(_state "doctitle")
  892. elseif (${arg} STREQUAL "WINDOWTITLE")
  893. set(_state "windowtitle")
  894. elseif (${arg} STREQUAL "AUTHOR")
  895. set(_state "author")
  896. elseif (${arg} STREQUAL "USE")
  897. set(_state "use")
  898. elseif (${arg} STREQUAL "VERSION")
  899. set(_state "version")
  900. else ()
  901. set(_javadoc_packages ${arg})
  902. set(_state "packages")
  903. endif ()
  904. elseif (${_state} STREQUAL "packages")
  905. if (${arg} STREQUAL "FILES")
  906. set(_state "files")
  907. elseif (${arg} STREQUAL "SOURCEPATH")
  908. set(_state "sourcepath")
  909. elseif (${arg} STREQUAL "CLASSPATH")
  910. set(_state "classpath")
  911. elseif (${arg} STREQUAL "INSTALLPATH")
  912. set(_state "installpath")
  913. elseif (${arg} STREQUAL "DOCTITLE")
  914. set(_state "doctitle")
  915. elseif (${arg} STREQUAL "WINDOWTITLE")
  916. set(_state "windowtitle")
  917. elseif (${arg} STREQUAL "AUTHOR")
  918. set(_state "author")
  919. elseif (${arg} STREQUAL "USE")
  920. set(_state "use")
  921. elseif (${arg} STREQUAL "VERSION")
  922. set(_state "version")
  923. else ()
  924. list(APPEND _javadoc_packages ${arg})
  925. endif ()
  926. elseif (${_state} STREQUAL "files")
  927. if (${arg} STREQUAL "PACKAGES")
  928. set(_state "packages")
  929. elseif (${arg} STREQUAL "SOURCEPATH")
  930. set(_state "sourcepath")
  931. elseif (${arg} STREQUAL "CLASSPATH")
  932. set(_state "classpath")
  933. elseif (${arg} STREQUAL "INSTALLPATH")
  934. set(_state "installpath")
  935. elseif (${arg} STREQUAL "DOCTITLE")
  936. set(_state "doctitle")
  937. elseif (${arg} STREQUAL "WINDOWTITLE")
  938. set(_state "windowtitle")
  939. elseif (${arg} STREQUAL "AUTHOR")
  940. set(_state "author")
  941. elseif (${arg} STREQUAL "USE")
  942. set(_state "use")
  943. elseif (${arg} STREQUAL "VERSION")
  944. set(_state "version")
  945. else ()
  946. list(APPEND _javadoc_files ${arg})
  947. endif ()
  948. elseif (${_state} STREQUAL "sourcepath")
  949. if (${arg} STREQUAL "PACKAGES")
  950. set(_state "packages")
  951. elseif (${arg} STREQUAL "FILES")
  952. set(_state "files")
  953. elseif (${arg} STREQUAL "CLASSPATH")
  954. set(_state "classpath")
  955. elseif (${arg} STREQUAL "INSTALLPATH")
  956. set(_state "installpath")
  957. elseif (${arg} STREQUAL "DOCTITLE")
  958. set(_state "doctitle")
  959. elseif (${arg} STREQUAL "WINDOWTITLE")
  960. set(_state "windowtitle")
  961. elseif (${arg} STREQUAL "AUTHOR")
  962. set(_state "author")
  963. elseif (${arg} STREQUAL "USE")
  964. set(_state "use")
  965. elseif (${arg} STREQUAL "VERSION")
  966. set(_state "version")
  967. else ()
  968. list(APPEND _javadoc_sourcepath ${arg})
  969. endif ()
  970. elseif (${_state} STREQUAL "classpath")
  971. if (${arg} STREQUAL "PACKAGES")
  972. set(_state "packages")
  973. elseif (${arg} STREQUAL "FILES")
  974. set(_state "files")
  975. elseif (${arg} STREQUAL "SOURCEPATH")
  976. set(_state "sourcepath")
  977. elseif (${arg} STREQUAL "INSTALLPATH")
  978. set(_state "installpath")
  979. elseif (${arg} STREQUAL "DOCTITLE")
  980. set(_state "doctitle")
  981. elseif (${arg} STREQUAL "WINDOWTITLE")
  982. set(_state "windowtitle")
  983. elseif (${arg} STREQUAL "AUTHOR")
  984. set(_state "author")
  985. elseif (${arg} STREQUAL "USE")
  986. set(_state "use")
  987. elseif (${arg} STREQUAL "VERSION")
  988. set(_state "version")
  989. else ()
  990. list(APPEND _javadoc_classpath ${arg})
  991. endif ()
  992. elseif (${_state} STREQUAL "installpath")
  993. if (${arg} STREQUAL "PACKAGES")
  994. set(_state "packages")
  995. elseif (${arg} STREQUAL "FILES")
  996. set(_state "files")
  997. elseif (${arg} STREQUAL "SOURCEPATH")
  998. set(_state "sourcepath")
  999. elseif (${arg} STREQUAL "DOCTITLE")
  1000. set(_state "doctitle")
  1001. elseif (${arg} STREQUAL "WINDOWTITLE")
  1002. set(_state "windowtitle")
  1003. elseif (${arg} STREQUAL "AUTHOR")
  1004. set(_state "author")
  1005. elseif (${arg} STREQUAL "USE")
  1006. set(_state "use")
  1007. elseif (${arg} STREQUAL "VERSION")
  1008. set(_state "version")
  1009. else ()
  1010. set(_javadoc_installpath ${arg})
  1011. endif ()
  1012. elseif (${_state} STREQUAL "doctitle")
  1013. if (${arg} STREQUAL "PACKAGES")
  1014. set(_state "packages")
  1015. elseif (${arg} STREQUAL "FILES")
  1016. set(_state "files")
  1017. elseif (${arg} STREQUAL "SOURCEPATH")
  1018. set(_state "sourcepath")
  1019. elseif (${arg} STREQUAL "INSTALLPATH")
  1020. set(_state "installpath")
  1021. elseif (${arg} STREQUAL "CLASSPATH")
  1022. set(_state "classpath")
  1023. elseif (${arg} STREQUAL "WINDOWTITLE")
  1024. set(_state "windowtitle")
  1025. elseif (${arg} STREQUAL "AUTHOR")
  1026. set(_state "author")
  1027. elseif (${arg} STREQUAL "USE")
  1028. set(_state "use")
  1029. elseif (${arg} STREQUAL "VERSION")
  1030. set(_state "version")
  1031. else ()
  1032. set(_javadoc_doctitle ${arg})
  1033. endif ()
  1034. elseif (${_state} STREQUAL "windowtitle")
  1035. if (${arg} STREQUAL "PACKAGES")
  1036. set(_state "packages")
  1037. elseif (${arg} STREQUAL "FILES")
  1038. set(_state "files")
  1039. elseif (${arg} STREQUAL "SOURCEPATH")
  1040. set(_state "sourcepath")
  1041. elseif (${arg} STREQUAL "CLASSPATH")
  1042. set(_state "classpath")
  1043. elseif (${arg} STREQUAL "INSTALLPATH")
  1044. set(_state "installpath")
  1045. elseif (${arg} STREQUAL "DOCTITLE")
  1046. set(_state "doctitle")
  1047. elseif (${arg} STREQUAL "AUTHOR")
  1048. set(_state "author")
  1049. elseif (${arg} STREQUAL "USE")
  1050. set(_state "use")
  1051. elseif (${arg} STREQUAL "VERSION")
  1052. set(_state "version")
  1053. else ()
  1054. set(_javadoc_windowtitle ${arg})
  1055. endif ()
  1056. elseif (${_state} STREQUAL "author")
  1057. if (${arg} STREQUAL "PACKAGES")
  1058. set(_state "packages")
  1059. elseif (${arg} STREQUAL "FILES")
  1060. set(_state "files")
  1061. elseif (${arg} STREQUAL "SOURCEPATH")
  1062. set(_state "sourcepath")
  1063. elseif (${arg} STREQUAL "CLASSPATH")
  1064. set(_state "classpath")
  1065. elseif (${arg} STREQUAL "INSTALLPATH")
  1066. set(_state "installpath")
  1067. elseif (${arg} STREQUAL "DOCTITLE")
  1068. set(_state "doctitle")
  1069. elseif (${arg} STREQUAL "WINDOWTITLE")
  1070. set(_state "windowtitle")
  1071. elseif (${arg} STREQUAL "AUTHOR")
  1072. set(_state "author")
  1073. elseif (${arg} STREQUAL "USE")
  1074. set(_state "use")
  1075. elseif (${arg} STREQUAL "VERSION")
  1076. set(_state "version")
  1077. else ()
  1078. set(_javadoc_author ${arg})
  1079. endif ()
  1080. elseif (${_state} STREQUAL "use")
  1081. if (${arg} STREQUAL "PACKAGES")
  1082. set(_state "packages")
  1083. elseif (${arg} STREQUAL "FILES")
  1084. set(_state "files")
  1085. elseif (${arg} STREQUAL "SOURCEPATH")
  1086. set(_state "sourcepath")
  1087. elseif (${arg} STREQUAL "CLASSPATH")
  1088. set(_state "classpath")
  1089. elseif (${arg} STREQUAL "INSTALLPATH")
  1090. set(_state "installpath")
  1091. elseif (${arg} STREQUAL "DOCTITLE")
  1092. set(_state "doctitle")
  1093. elseif (${arg} STREQUAL "WINDOWTITLE")
  1094. set(_state "windowtitle")
  1095. elseif (${arg} STREQUAL "AUTHOR")
  1096. set(_state "author")
  1097. elseif (${arg} STREQUAL "USE")
  1098. set(_state "use")
  1099. elseif (${arg} STREQUAL "VERSION")
  1100. set(_state "version")
  1101. else ()
  1102. set(_javadoc_use ${arg})
  1103. endif ()
  1104. elseif (${_state} STREQUAL "version")
  1105. if (${arg} STREQUAL "PACKAGES")
  1106. set(_state "packages")
  1107. elseif (${arg} STREQUAL "FILES")
  1108. set(_state "files")
  1109. elseif (${arg} STREQUAL "SOURCEPATH")
  1110. set(_state "sourcepath")
  1111. elseif (${arg} STREQUAL "CLASSPATH")
  1112. set(_state "classpath")
  1113. elseif (${arg} STREQUAL "INSTALLPATH")
  1114. set(_state "installpath")
  1115. elseif (${arg} STREQUAL "DOCTITLE")
  1116. set(_state "doctitle")
  1117. elseif (${arg} STREQUAL "WINDOWTITLE")
  1118. set(_state "windowtitle")
  1119. elseif (${arg} STREQUAL "AUTHOR")
  1120. set(_state "author")
  1121. elseif (${arg} STREQUAL "USE")
  1122. set(_state "use")
  1123. elseif (${arg} STREQUAL "VERSION")
  1124. set(_state "version")
  1125. else ()
  1126. set(_javadoc_version ${arg})
  1127. endif ()
  1128. endif ()
  1129. endforeach ()
  1130. set(_javadoc_builddir ${CMAKE_CURRENT_BINARY_DIR}/javadoc/${_target})
  1131. set(_javadoc_options -d ${_javadoc_builddir})
  1132. if (_javadoc_sourcepath)
  1133. set(_start TRUE)
  1134. foreach(_path ${_javadoc_sourcepath})
  1135. if (_start)
  1136. set(_sourcepath ${_path})
  1137. set(_start FALSE)
  1138. else ()
  1139. set(_sourcepath ${_sourcepath}:${_path})
  1140. endif ()
  1141. endforeach()
  1142. set(_javadoc_options ${_javadoc_options} -sourcepath ${_sourcepath})
  1143. endif ()
  1144. if (_javadoc_classpath)
  1145. set(_start TRUE)
  1146. foreach(_path ${_javadoc_classpath})
  1147. if (_start)
  1148. set(_classpath ${_path})
  1149. set(_start FALSE)
  1150. else ()
  1151. set(_classpath ${_classpath}:${_path})
  1152. endif ()
  1153. endforeach()
  1154. set(_javadoc_options ${_javadoc_options} -classpath "${_classpath}")
  1155. endif ()
  1156. if (_javadoc_doctitle)
  1157. set(_javadoc_options ${_javadoc_options} -doctitle '${_javadoc_doctitle}')
  1158. endif ()
  1159. if (_javadoc_windowtitle)
  1160. set(_javadoc_options ${_javadoc_options} -windowtitle '${_javadoc_windowtitle}')
  1161. endif ()
  1162. if (_javadoc_author)
  1163. set(_javadoc_options ${_javadoc_options} -author)
  1164. endif ()
  1165. if (_javadoc_use)
  1166. set(_javadoc_options ${_javadoc_options} -use)
  1167. endif ()
  1168. if (_javadoc_version)
  1169. set(_javadoc_options ${_javadoc_options} -version)
  1170. endif ()
  1171. add_custom_target(${_target}_javadoc ALL
  1172. COMMAND ${Java_JAVADOC_EXECUTABLE} ${_javadoc_options}
  1173. ${_javadoc_files}
  1174. ${_javadoc_packages}
  1175. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  1176. )
  1177. install(
  1178. DIRECTORY ${_javadoc_builddir}
  1179. DESTINATION ${_javadoc_installpath}
  1180. )
  1181. endfunction()
  1182. function (create_javah)
  1183. cmake_parse_arguments(_create_javah
  1184. ""
  1185. "TARGET;GENERATED_FILES;OUTPUT_NAME;OUTPUT_DIR"
  1186. "CLASSES;CLASSPATH;DEPENDS"
  1187. ${ARGN})
  1188. # ckeck parameters
  1189. if (NOT _create_javah_TARGET AND NOT _create_javah_GENERATED_FILES)
  1190. message (FATAL_ERROR "create_javah: TARGET or GENERATED_FILES must be specified.")
  1191. endif()
  1192. if (_create_javah_OUTPUT_NAME AND _create_javah_OUTPUT_DIR)
  1193. message (FATAL_ERROR "create_javah: OUTPUT_NAME and OUTPUT_DIR are mutually exclusive.")
  1194. endif()
  1195. if (NOT _create_javah_CLASSES)
  1196. message (FATAL_ERROR "create_javah: CLASSES is a required parameter.")
  1197. endif()
  1198. set (_output_files)
  1199. if (WIN32 AND NOT CYGWIN AND CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  1200. set(_classpath_sep "$<SEMICOLON>")
  1201. else ()
  1202. set(_classpath_sep ":")
  1203. endif()
  1204. # handle javah options
  1205. set (_javah_options)
  1206. if (_create_javah_CLASSPATH)
  1207. # CLASSPATH can specify directories, jar files or targets created with add_jar command
  1208. set (_classpath)
  1209. foreach (_path IN LISTS _create_javah_CLASSPATH)
  1210. if (TARGET ${_path})
  1211. get_target_property (_jar_path ${_path} JAR_FILE)
  1212. if (_jar_path)
  1213. list (APPEND _classpath "${_jar_path}")
  1214. list (APPEND _create_javah_DEPENDS "${_path}")
  1215. else()
  1216. message(SEND_ERROR "create_javah: CLASSPATH target ${_path} is not a jar.")
  1217. endif()
  1218. elseif (EXISTS "${_path}")
  1219. list (APPEND _classpath "${_path}")
  1220. if (NOT IS_DIRECTORY "${_path}")
  1221. list (APPEND _create_javah_DEPENDS "${_path}")
  1222. endif()
  1223. else()
  1224. message(SEND_ERROR "create_javah: CLASSPATH entry ${_path} does not exist.")
  1225. endif()
  1226. endforeach()
  1227. string (REPLACE ";" "${_classpath_sep}" _classpath "${_classpath}")
  1228. list (APPEND _javah_options -classpath "${_classpath}")
  1229. endif()
  1230. if (_create_javah_OUTPUT_DIR)
  1231. list (APPEND _javah_options -d "${_create_javah_OUTPUT_DIR}")
  1232. endif()
  1233. if (_create_javah_OUTPUT_NAME)
  1234. list (APPEND _javah_options -o "${_create_javah_OUTPUT_NAME}")
  1235. set (_output_files "${_create_javah_OUTPUT_NAME}")
  1236. get_filename_component (_create_javah_OUTPUT_DIR "${_create_javah_OUTPUT_NAME}" DIRECTORY)
  1237. get_filename_component (_create_javah_OUTPUT_DIR "${_create_javah_OUTPUT_DIR}" ABSOLUTE)
  1238. endif()
  1239. if (NOT _create_javah_OUTPUT_DIR)
  1240. set (_create_javah_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  1241. endif()
  1242. if (NOT _create_javah_OUTPUT_NAME)
  1243. # compute output names
  1244. foreach (_class IN LISTS _create_javah_CLASSES)
  1245. string (REPLACE "." "_" _c_header "${_class}")
  1246. set (_c_header "${_create_javah_OUTPUT_DIR}/${_c_header}.h")
  1247. list (APPEND _output_files "${_c_header}")
  1248. endforeach()
  1249. endif()
  1250. # finalize custom command arguments
  1251. if (_create_javah_DEPENDS)
  1252. list (INSERT _create_javah_DEPENDS 0 DEPENDS)
  1253. endif()
  1254. add_custom_command (OUTPUT ${_output_files}
  1255. COMMAND "${Java_JAVAH_EXECUTABLE}" ${_javah_options} -jni ${_create_javah_CLASSES}
  1256. ${_create_javah_DEPENDS}
  1257. WORKING_DIRECTORY ${_create_javah_OUTPUT_DIR}
  1258. COMMENT "Building C header files from classes...")
  1259. if (_create_javah_TARGET)
  1260. add_custom_target (${_create_javah_TARGET} ALL DEPENDS ${_output_files})
  1261. endif()
  1262. if (_create_javah_GENERATED_FILES)
  1263. set (${_create_javah_GENERATED_FILES} ${_output_files} PARENT_SCOPE)
  1264. endif()
  1265. endfunction()
  1266. function(export_jars)
  1267. # Parse and validate arguments
  1268. cmake_parse_arguments(_export_jars
  1269. ""
  1270. "FILE;NAMESPACE"
  1271. "TARGETS"
  1272. ${ARGN}
  1273. )
  1274. if (NOT _export_jars_FILE)
  1275. message(SEND_ERROR "export_jars: FILE must be specified.")
  1276. endif()
  1277. if (NOT _export_jars_TARGETS)
  1278. message(SEND_ERROR "export_jars: TARGETS must be specified.")
  1279. endif()
  1280. set(_jar_NAMESPACE "${_export_jars_NAMESPACE}")
  1281. # Set content of generated exports file
  1282. string(REPLACE ";" " " __targets__ "${_export_jars_TARGETS}")
  1283. set(__targetdefs__ "")
  1284. foreach(_target ${_export_jars_TARGETS})
  1285. get_target_property(_jarpath ${_target} JAR_FILE)
  1286. get_filename_component(_jarpath ${_jarpath} PATH)
  1287. __java_export_jar(__targetdefs__ ${_target} "${_jarpath}")
  1288. endforeach()
  1289. # Generate exports file
  1290. configure_file(
  1291. ${_JAVA_EXPORT_TARGETS_SCRIPT}
  1292. ${_export_jars_FILE}
  1293. @ONLY
  1294. )
  1295. endfunction()
  1296. function(install_jar_exports)
  1297. # Parse and validate arguments
  1298. cmake_parse_arguments(_install_jar_exports
  1299. ""
  1300. "FILE;DESTINATION;COMPONENT;NAMESPACE"
  1301. "TARGETS"
  1302. ${ARGN}
  1303. )
  1304. if (NOT _install_jar_exports_FILE)
  1305. message(SEND_ERROR "install_jar_exports: FILE must be specified.")
  1306. endif()
  1307. if (NOT _install_jar_exports_DESTINATION)
  1308. message(SEND_ERROR "install_jar_exports: DESTINATION must be specified.")
  1309. endif()
  1310. if (NOT _install_jar_exports_TARGETS)
  1311. message(SEND_ERROR "install_jar_exports: TARGETS must be specified.")
  1312. endif()
  1313. set(_jar_NAMESPACE "${_install_jar_exports_NAMESPACE}")
  1314. if (_install_jar_exports_COMPONENT)
  1315. set (_COMPONENT COMPONENT ${_install_jar_exports_COMPONENT})
  1316. endif()
  1317. # Determine relative path from installed export file to install prefix
  1318. if(IS_ABSOLUTE ${_install_jar_exports_DESTINATION})
  1319. file(RELATIVE_PATH _relpath
  1320. ${_install_jar_exports_DESTINATION}
  1321. ${CMAKE_INSTALL_PREFIX}
  1322. )
  1323. else()
  1324. file(RELATIVE_PATH _relpath
  1325. ${CMAKE_INSTALL_PREFIX}/${_install_jar_exports_DESTINATION}
  1326. ${CMAKE_INSTALL_PREFIX}
  1327. )
  1328. endif()
  1329. # Set up unique location for generated exports file
  1330. string(SHA256 _hash "${_install_jar_exports_DESTINATION}")
  1331. set(_tmpdir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/JavaExports/${_hash})
  1332. # Set content of generated exports file
  1333. string(REPLACE ";" " " __targets__ "${_install_jar_exports_TARGETS}")
  1334. set(__targetdefs__ "set(_prefix \${CMAKE_CURRENT_LIST_DIR}/${_relpath})\n\n")
  1335. foreach(_target ${_install_jar_exports_TARGETS})
  1336. get_target_property(_dir ${_target} INSTALL_DESTINATION)
  1337. __java_export_jar(__targetdefs__ ${_target} "\${_prefix}/${_dir}")
  1338. endforeach()
  1339. __java_lcat(__targetdefs__ "\nunset(_prefix)")
  1340. # Generate and install exports file
  1341. configure_file(
  1342. ${_JAVA_EXPORT_TARGETS_SCRIPT}
  1343. ${_tmpdir}/${_install_jar_exports_FILE}
  1344. @ONLY
  1345. )
  1346. install(FILES ${_tmpdir}/${_install_jar_exports_FILE}
  1347. DESTINATION ${_install_jar_exports_DESTINATION}
  1348. ${_COMPONENT})
  1349. endfunction()