Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Modules / UseJava.cmake
1 # - Use Module for Java
2 # This file provides functions for Java. It is assumed that FindJava.cmake
3 # has already been loaded.  See FindJava.cmake for information on how to
4 # load Java into your CMake project.
5 #
6 # add_jar(target_name
7 #         [SOURCES] source1 [source2 ...] [resource1 ...]
8 #         [INCLUDE_JARS jar1 [jar2 ...]]
9 #         [ENTRY_POINT entry]
10 #         [VERSION version]
11 #         [OUTPUT_NAME name]
12 #         [OUTPUT_DIR dir]
13 #        )
14 #
15 # This command creates a <target_name>.jar. It compiles the given source files
16 # (source) and adds the given resource files (resource) to the jar file. If
17 # only resource files are given then just a jar file is created. The list of
18 # include jars are added to the classpath when compiling the java sources and
19 # also to the dependencies of the target. INCLUDE_JARS also accepts other
20 # target names created by add_jar. For backwards compatibility, jar files
21 # listed as sources are ignored (as they have been since the first version of
22 # this module).
23 #
24 # The default OUTPUT_DIR can also be changed by setting the variable
25 # CMAKE_JAVA_TARGET_OUTPUT_DIR.
26 #
27 # Additional instructions:
28 #   To add compile flags to the target you can set these flags with
29 #   the following variable:
30 #
31 #       set(CMAKE_JAVA_COMPILE_FLAGS -nowarn)
32 #
33 #   To add a path or a jar file to the class path you can do this
34 #   with the CMAKE_JAVA_INCLUDE_PATH variable.
35 #
36 #       set(CMAKE_JAVA_INCLUDE_PATH /usr/share/java/shibboleet.jar)
37 #
38 #   To use a different output name for the target you can set it with:
39 #
40 #       add_jar(foobar foobar.java OUTPUT_NAME shibboleet.jar)
41 #
42 #   To use a different output directory than CMAKE_CURRENT_BINARY_DIR
43 #   you can set it with:
44 #
45 #       add_jar(foobar foobar.java OUTPUT_DIR ${PROJECT_BINARY_DIR}/bin)
46 #
47 #   To define an entry point in your jar you can set it with the ENTRY_POINT
48 #   named argument:
49 #
50 #       add_jar(example ENTRY_POINT com/examples/MyProject/Main)
51 #
52 #   To add a VERSION to the target output name you can set it using
53 #   the VERSION named argument to add_jar. This will create a jar file with the
54 #   name shibboleet-1.0.0.jar and will create a symlink shibboleet.jar
55 #   pointing to the jar with the version information.
56 #
57 #       add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
58 #
59 #    If the target is a JNI library, utilize the following commands to
60 #    create a JNI symbolic link:
61 #
62 #       set(CMAKE_JNI_TARGET TRUE)
63 #       add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
64 #       install_jar(shibboleet ${LIB_INSTALL_DIR}/shibboleet)
65 #       install_jni_symlink(shibboleet ${JAVA_LIB_INSTALL_DIR})
66 #
67 #    If a single target needs to produce more than one jar from its
68 #    java source code, to prevent the accumulation of duplicate class
69 #    files in subsequent jars, set/reset CMAKE_JAR_CLASSES_PREFIX prior
70 #    to calling the add_jar() function:
71 #
72 #       set(CMAKE_JAR_CLASSES_PREFIX com/redhat/foo)
73 #       add_jar(foo foo.java)
74 #
75 #       set(CMAKE_JAR_CLASSES_PREFIX com/redhat/bar)
76 #       add_jar(bar bar.java)
77 #
78 # Target Properties:
79 #   The add_jar() functions sets some target properties. You can get these
80 #   properties with the
81 #      get_property(TARGET <target_name> PROPERTY <propery_name>)
82 #   command.
83 #
84 #   INSTALL_FILES      The files which should be installed. This is used by
85 #                      install_jar().
86 #   JNI_SYMLINK        The JNI symlink which should be installed.
87 #                      This is used by install_jni_symlink().
88 #   JAR_FILE           The location of the jar file so that you can include
89 #                      it.
90 #   CLASS_DIR          The directory where the class files can be found. For
91 #                      example to use them with javah.
92 #
93 # find_jar(<VAR>
94 #          name | NAMES name1 [name2 ...]
95 #          [PATHS path1 [path2 ... ENV var]]
96 #          [VERSIONS version1 [version2]]
97 #          [DOC "cache documentation string"]
98 #         )
99 #
100 # This command is used to find a full path to the named jar. A cache
101 # entry named by <VAR> is created to stor the result of this command. If
102 # the full path to a jar is found the result is stored in the variable
103 # and the search will not repeated unless the variable is cleared. If
104 # nothing is found, the result will be <VAR>-NOTFOUND, and the search
105 # will be attempted again next time find_jar is invoked with the same
106 # variable.
107 # The name of the full path to a file that is searched for is specified
108 # by the names listed after NAMES argument. Additional search locations
109 # can be specified after the PATHS argument. If you require special a
110 # version of a jar file you can specify it with the VERSIONS argument.
111 # The argument after DOC will be used for the documentation string in
112 # the cache.
113 #
114 # install_jar(TARGET_NAME DESTINATION)
115 #
116 # This command installs the TARGET_NAME files to the given DESTINATION.
117 # It should be called in the same scope as add_jar() or it will fail.
118 #
119 # install_jni_symlink(TARGET_NAME DESTINATION)
120 #
121 # This command installs the TARGET_NAME JNI symlinks to the given
122 # DESTINATION. It should be called in the same scope as add_jar()
123 # or it will fail.
124 #
125 # create_javadoc(<VAR>
126 #                PACKAGES pkg1 [pkg2 ...]
127 #                [SOURCEPATH <sourcepath>]
128 #                [CLASSPATH <classpath>]
129 #                [INSTALLPATH <install path>]
130 #                [DOCTITLE "the documentation title"]
131 #                [WINDOWTITLE "the title of the document"]
132 #                [AUTHOR TRUE|FALSE]
133 #                [USE TRUE|FALSE]
134 #                [VERSION TRUE|FALSE]
135 #               )
136 #
137 # Create java documentation based on files or packages. For more
138 # details please read the javadoc manpage.
139 #
140 # There are two main signatures for create_javadoc. The first
141 # signature works with package names on a path with source files:
142 #
143 #   Example:
144 #   create_javadoc(my_example_doc
145 #     PACKAGES com.exmaple.foo com.example.bar
146 #     SOURCEPATH "${CMAKE_CURRENT_SOURCE_DIR}"
147 #     CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
148 #     WINDOWTITLE "My example"
149 #     DOCTITLE "<h1>My example</h1>"
150 #     AUTHOR TRUE
151 #     USE TRUE
152 #     VERSION TRUE
153 #   )
154 #
155 # The second signature for create_javadoc works on a given list of
156 # files.
157 #
158 #   create_javadoc(<VAR>
159 #                  FILES file1 [file2 ...]
160 #                  [CLASSPATH <classpath>]
161 #                  [INSTALLPATH <install path>]
162 #                  [DOCTITLE "the documentation title"]
163 #                  [WINDOWTITLE "the title of the document"]
164 #                  [AUTHOR TRUE|FALSE]
165 #                  [USE TRUE|FALSE]
166 #                  [VERSION TRUE|FALSE]
167 #                 )
168 #
169 # Example:
170 #   create_javadoc(my_example_doc
171 #     FILES ${example_SRCS}
172 #     CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
173 #     WINDOWTITLE "My example"
174 #     DOCTITLE "<h1>My example</h1>"
175 #     AUTHOR TRUE
176 #     USE TRUE
177 #     VERSION TRUE
178 #   )
179 #
180 # Both signatures share most of the options. These options are the
181 # same as what you can find in the javadoc manpage. Please look at
182 # the manpage for CLASSPATH, DOCTITLE, WINDOWTITLE, AUTHOR, USE and
183 # VERSION.
184 #
185 # The documentation will be by default installed to
186 #
187 #   ${CMAKE_INSTALL_PREFIX}/share/javadoc/<VAR>
188 #
189 # if you don't set the INSTALLPATH.
190 #
191
192 #=============================================================================
193 # Copyright 2013 OpenGamma Ltd. <graham@opengamma.com>
194 # Copyright 2010-2011 Andreas schneider <asn@redhat.com>
195 # Copyright 2010-2013 Kitware, Inc.
196 #
197 # Distributed under the OSI-approved BSD License (the "License");
198 # see accompanying file Copyright.txt for details.
199 #
200 # This software is distributed WITHOUT ANY WARRANTY; without even the
201 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
202 # See the License for more information.
203 #=============================================================================
204 # (To distribute this file outside of CMake, substitute the full
205 #  License text for the above reference.)
206
207 include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
208
209 function (__java_copy_file src dest comment)
210     add_custom_command(
211         OUTPUT  ${dest}
212         COMMAND cmake -E copy_if_different
213         ARGS    ${src}
214                 ${dest}
215         DEPENDS ${src}
216         COMMENT ${comment})
217 endfunction ()
218
219 # define helper scripts
220 set(_JAVA_CLASS_FILELIST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaClassFilelist.cmake)
221 set(_JAVA_SYMLINK_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaSymlinks.cmake)
222
223 function(add_jar _TARGET_NAME)
224
225     # In CMake < 2.8.12, add_jar used variables which were set prior to calling
226     # add_jar for customizing the behavior of add_jar. In order to be backwards
227     # compatible, check if any of those variables are set, and use them to
228     # initialize values of the named arguments. (Giving the corresponding named
229     # argument will override the value set here.)
230     #
231     # New features should use named arguments only.
232     if(DEFINED CMAKE_JAVA_TARGET_VERSION)
233         set(_add_jar_VERSION "${CMAKE_JAVA_TARGET_VERSION}")
234     endif()
235     if(DEFINED CMAKE_JAVA_TARGET_OUTPUT_DIR)
236         set(_add_jar_OUTPUT_DIR "${CMAKE_JAVA_TARGET_OUTPUT_DIR}")
237     endif()
238     if(DEFINED CMAKE_JAVA_TARGET_OUTPUT_NAME)
239         set(_add_jar_OUTPUT_NAME "${CMAKE_JAVA_TARGET_OUTPUT_NAME}")
240         # reset
241         set(CMAKE_JAVA_TARGET_OUTPUT_NAME)
242     endif()
243     if(DEFINED CMAKE_JAVA_JAR_ENTRY_POINT)
244         set(_add_jar_ENTRY_POINT "${CMAKE_JAVA_JAR_ENTRY_POINT}")
245     endif()
246
247     cmake_parse_arguments(_add_jar
248       ""
249       "VERSION;OUTPUT_DIR;OUTPUT_NAME;ENTRY_POINT"
250       "SOURCES;INCLUDE_JARS"
251       ${ARGN}
252     )
253
254     set(_JAVA_SOURCE_FILES ${_add_jar_SOURCES} ${_add_jar_UNPARSED_ARGUMENTS})
255
256     if (NOT DEFINED _add_jar_OUTPUT_DIR)
257         set(_add_jar_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
258     endif()
259
260     if (_add_jar_ENTRY_POINT)
261         set(_ENTRY_POINT_OPTION e)
262         set(_ENTRY_POINT_VALUE ${_add_jar_ENTRY_POINT})
263     endif ()
264
265     if (LIBRARY_OUTPUT_PATH)
266         set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH})
267     else ()
268         set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${_add_jar_OUTPUT_DIR})
269     endif ()
270
271     set(CMAKE_JAVA_INCLUDE_PATH
272         ${CMAKE_JAVA_INCLUDE_PATH}
273         ${CMAKE_CURRENT_SOURCE_DIR}
274         ${CMAKE_JAVA_OBJECT_OUTPUT_PATH}
275         ${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}
276     )
277
278     if (WIN32 AND NOT CYGWIN AND CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
279         set(CMAKE_JAVA_INCLUDE_FLAG_SEP ";")
280     else ()
281         set(CMAKE_JAVA_INCLUDE_FLAG_SEP ":")
282     endif()
283
284     foreach (JAVA_INCLUDE_DIR ${CMAKE_JAVA_INCLUDE_PATH})
285        set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${JAVA_INCLUDE_DIR}")
286     endforeach()
287
288     set(CMAKE_JAVA_CLASS_OUTPUT_PATH "${_add_jar_OUTPUT_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir")
289
290     set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}.jar")
291     if (_add_jar_OUTPUT_NAME AND _add_jar_VERSION)
292         set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}-${_add_jar_VERSION}.jar")
293         set(_JAVA_TARGET_OUTPUT_LINK "${_add_jar_OUTPUT_NAME}.jar")
294     elseif (_add_jar_VERSION)
295         set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}-${_add_jar_VERSION}.jar")
296         set(_JAVA_TARGET_OUTPUT_LINK "${_TARGET_NAME}.jar")
297     elseif (_add_jar_OUTPUT_NAME)
298         set(_JAVA_TARGET_OUTPUT_NAME "${_add_jar_OUTPUT_NAME}.jar")
299     endif ()
300
301     set(_JAVA_CLASS_FILES)
302     set(_JAVA_COMPILE_FILES)
303     set(_JAVA_DEPENDS)
304     set(_JAVA_COMPILE_DEPENDS)
305     set(_JAVA_RESOURCE_FILES)
306     foreach(_JAVA_SOURCE_FILE ${_JAVA_SOURCE_FILES})
307         get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
308         get_filename_component(_JAVA_FILE ${_JAVA_SOURCE_FILE} NAME_WE)
309         get_filename_component(_JAVA_PATH ${_JAVA_SOURCE_FILE} PATH)
310         get_filename_component(_JAVA_FULL ${_JAVA_SOURCE_FILE} ABSOLUTE)
311
312         if (_JAVA_EXT MATCHES ".java")
313             file(RELATIVE_PATH _JAVA_REL_BINARY_PATH ${_add_jar_OUTPUT_DIR} ${_JAVA_FULL})
314             file(RELATIVE_PATH _JAVA_REL_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${_JAVA_FULL})
315             string(LENGTH ${_JAVA_REL_BINARY_PATH} _BIN_LEN)
316             string(LENGTH ${_JAVA_REL_SOURCE_PATH} _SRC_LEN)
317             if (${_BIN_LEN} LESS ${_SRC_LEN})
318                 set(_JAVA_REL_PATH ${_JAVA_REL_BINARY_PATH})
319             else ()
320                 set(_JAVA_REL_PATH ${_JAVA_REL_SOURCE_PATH})
321             endif ()
322             get_filename_component(_JAVA_REL_PATH ${_JAVA_REL_PATH} PATH)
323
324             list(APPEND _JAVA_COMPILE_FILES ${_JAVA_SOURCE_FILE})
325             set(_JAVA_CLASS_FILE "${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_REL_PATH}/${_JAVA_FILE}.class")
326             set(_JAVA_CLASS_FILES ${_JAVA_CLASS_FILES} ${_JAVA_CLASS_FILE})
327
328         elseif (_JAVA_EXT MATCHES ".jar"
329                 OR _JAVA_EXT MATCHES ".war"
330                 OR _JAVA_EXT MATCHES ".ear"
331                 OR _JAVA_EXT MATCHES ".sar")
332             # Ignored for backward compatibility
333
334         elseif (_JAVA_EXT STREQUAL "")
335             list(APPEND CMAKE_JAVA_INCLUDE_PATH ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}} ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}_CLASSPATH})
336             list(APPEND _JAVA_DEPENDS ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}})
337
338         else ()
339             __java_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/${_JAVA_SOURCE_FILE}
340                              ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE}
341                              "Copying ${_JAVA_SOURCE_FILE} to the build directory")
342             list(APPEND _JAVA_RESOURCE_FILES ${_JAVA_SOURCE_FILE})
343         endif ()
344     endforeach()
345
346     foreach(_JAVA_INCLUDE_JAR ${_add_jar_INCLUDE_JARS})
347         if (TARGET ${_JAVA_INCLUDE_JAR})
348             get_target_property(_JAVA_JAR_PATH ${_JAVA_INCLUDE_JAR} JAR_FILE)
349             if (_JAVA_JAR_PATH)
350                 set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_JAR_PATH}")
351                 list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_JAR_PATH})
352                 list(APPEND _JAVA_DEPENDS ${_JAVA_INCLUDE_JAR})
353                 list(APPEND _JAVA_COMPILE_DEPENDS ${_JAVA_INCLUDE_JAR})
354             else ()
355                 message(SEND_ERROR "add_jar: INCLUDE_JARS target ${_JAVA_INCLUDE_JAR} is not a jar")
356             endif ()
357         else ()
358             set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_INCLUDE_JAR}")
359             list(APPEND CMAKE_JAVA_INCLUDE_PATH "${_JAVA_INCLUDE_JAR}")
360             list(APPEND _JAVA_DEPENDS "${_JAVA_INCLUDE_JAR}")
361             list(APPEND _JAVA_COMPILE_DEPENDS "${_JAVA_INCLUDE_JAR}")
362         endif ()
363     endforeach()
364
365     # create an empty java_class_filelist
366     if (NOT EXISTS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist)
367         file(WRITE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist "")
368     endif()
369
370     if (_JAVA_COMPILE_FILES)
371         # Create the list of files to compile.
372         set(_JAVA_SOURCES_FILE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_sources)
373         string(REPLACE ";" "\"\n\"" _JAVA_COMPILE_STRING "\"${_JAVA_COMPILE_FILES}\"")
374         file(WRITE ${_JAVA_SOURCES_FILE} ${_JAVA_COMPILE_STRING})
375
376         # Compile the java files and create a list of class files
377         add_custom_command(
378             # NOTE: this command generates an artificial dependency file
379             OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
380             COMMAND ${Java_JAVAC_EXECUTABLE}
381                 ${CMAKE_JAVA_COMPILE_FLAGS}
382                 -classpath "${CMAKE_JAVA_INCLUDE_PATH_FINAL}"
383                 -d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
384                 @${_JAVA_SOURCES_FILE}
385             COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
386             DEPENDS ${_JAVA_COMPILE_FILES} ${_JAVA_COMPILE_DEPENDS}
387             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
388             COMMENT "Building Java objects for ${_TARGET_NAME}.jar"
389         )
390         add_custom_command(
391             OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
392             COMMAND ${CMAKE_COMMAND}
393                 -DCMAKE_JAVA_CLASS_OUTPUT_PATH=${CMAKE_JAVA_CLASS_OUTPUT_PATH}
394                 -DCMAKE_JAR_CLASSES_PREFIX="${CMAKE_JAR_CLASSES_PREFIX}"
395                 -P ${_JAVA_CLASS_FILELIST_SCRIPT}
396             DEPENDS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
397             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
398         )
399     endif ()
400
401     # create the jar file
402     set(_JAVA_JAR_OUTPUT_PATH
403       ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_NAME})
404     if (CMAKE_JNI_TARGET)
405         add_custom_command(
406             OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
407             COMMAND ${Java_JAR_EXECUTABLE}
408                 -cf${_ENTRY_POINT_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE}
409                 ${_JAVA_RESOURCE_FILES} @java_class_filelist
410             COMMAND ${CMAKE_COMMAND}
411                 -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
412                 -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
413                 -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
414                 -P ${_JAVA_SYMLINK_SCRIPT}
415             COMMAND ${CMAKE_COMMAND}
416                 -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
417                 -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_JAR_OUTPUT_PATH}
418                 -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
419                 -P ${_JAVA_SYMLINK_SCRIPT}
420             DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
421             WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
422             COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
423         )
424     else ()
425         add_custom_command(
426             OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
427             COMMAND ${Java_JAR_EXECUTABLE}
428                 -cf${_ENTRY_POINT_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE}
429                 ${_JAVA_RESOURCE_FILES} @java_class_filelist
430             COMMAND ${CMAKE_COMMAND}
431                 -D_JAVA_TARGET_DIR=${_add_jar_OUTPUT_DIR}
432                 -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
433                 -D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
434                 -P ${_JAVA_SYMLINK_SCRIPT}
435             WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
436             DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
437             COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
438         )
439     endif ()
440
441     # Add the target and make sure we have the latest resource files.
442     add_custom_target(${_TARGET_NAME} ALL DEPENDS ${_JAVA_JAR_OUTPUT_PATH})
443
444     set_property(
445         TARGET
446             ${_TARGET_NAME}
447         PROPERTY
448             INSTALL_FILES
449                 ${_JAVA_JAR_OUTPUT_PATH}
450     )
451
452     if (_JAVA_TARGET_OUTPUT_LINK)
453         set_property(
454             TARGET
455                 ${_TARGET_NAME}
456             PROPERTY
457                 INSTALL_FILES
458                     ${_JAVA_JAR_OUTPUT_PATH}
459                     ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
460         )
461
462         if (CMAKE_JNI_TARGET)
463             set_property(
464                 TARGET
465                     ${_TARGET_NAME}
466                 PROPERTY
467                     JNI_SYMLINK
468                         ${_add_jar_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
469             )
470         endif ()
471     endif ()
472
473     set_property(
474         TARGET
475             ${_TARGET_NAME}
476         PROPERTY
477             JAR_FILE
478                 ${_JAVA_JAR_OUTPUT_PATH}
479     )
480
481     set_property(
482         TARGET
483             ${_TARGET_NAME}
484         PROPERTY
485             CLASSDIR
486                 ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
487     )
488
489 endfunction()
490
491 function(INSTALL_JAR _TARGET_NAME _DESTINATION)
492     get_property(__FILES
493         TARGET
494             ${_TARGET_NAME}
495         PROPERTY
496             INSTALL_FILES
497     )
498
499     if (__FILES)
500         install(
501             FILES
502                 ${__FILES}
503             DESTINATION
504                 ${_DESTINATION}
505         )
506     else ()
507         message(SEND_ERROR "The target ${_TARGET_NAME} is not known in this scope.")
508     endif ()
509 endfunction()
510
511 function(INSTALL_JNI_SYMLINK _TARGET_NAME _DESTINATION)
512     get_property(__SYMLINK
513         TARGET
514             ${_TARGET_NAME}
515         PROPERTY
516             JNI_SYMLINK
517     )
518
519     if (__SYMLINK)
520         install(
521             FILES
522                 ${__SYMLINK}
523             DESTINATION
524                 ${_DESTINATION}
525         )
526     else ()
527         message(SEND_ERROR "The target ${_TARGET_NAME} is not known in this scope.")
528     endif ()
529 endfunction()
530
531 function (find_jar VARIABLE)
532     set(_jar_names)
533     set(_jar_files)
534     set(_jar_versions)
535     set(_jar_paths
536         /usr/share/java/
537         /usr/local/share/java/
538         ${Java_JAR_PATHS})
539     set(_jar_doc "NOTSET")
540
541     set(_state "name")
542
543     foreach (arg ${ARGN})
544         if (${_state} STREQUAL "name")
545             if (${arg} STREQUAL "VERSIONS")
546                 set(_state "versions")
547             elseif (${arg} STREQUAL "NAMES")
548                 set(_state "names")
549             elseif (${arg} STREQUAL "PATHS")
550                 set(_state "paths")
551             elseif (${arg} STREQUAL "DOC")
552                 set(_state "doc")
553             else ()
554                 set(_jar_names ${arg})
555                 if (_jar_doc STREQUAL "NOTSET")
556                     set(_jar_doc "Finding ${arg} jar")
557                 endif ()
558             endif ()
559         elseif (${_state} STREQUAL "versions")
560             if (${arg} STREQUAL "NAMES")
561                 set(_state "names")
562             elseif (${arg} STREQUAL "PATHS")
563                 set(_state "paths")
564             elseif (${arg} STREQUAL "DOC")
565                 set(_state "doc")
566             else ()
567                 set(_jar_versions ${_jar_versions} ${arg})
568             endif ()
569         elseif (${_state} STREQUAL "names")
570             if (${arg} STREQUAL "VERSIONS")
571                 set(_state "versions")
572             elseif (${arg} STREQUAL "PATHS")
573                 set(_state "paths")
574             elseif (${arg} STREQUAL "DOC")
575                 set(_state "doc")
576             else ()
577                 set(_jar_names ${_jar_names} ${arg})
578                 if (_jar_doc STREQUAL "NOTSET")
579                     set(_jar_doc "Finding ${arg} jar")
580                 endif ()
581             endif ()
582         elseif (${_state} STREQUAL "paths")
583             if (${arg} STREQUAL "VERSIONS")
584                 set(_state "versions")
585             elseif (${arg} STREQUAL "NAMES")
586                 set(_state "names")
587             elseif (${arg} STREQUAL "DOC")
588                 set(_state "doc")
589             else ()
590                 set(_jar_paths ${_jar_paths} ${arg})
591             endif ()
592         elseif (${_state} STREQUAL "doc")
593             if (${arg} STREQUAL "VERSIONS")
594                 set(_state "versions")
595             elseif (${arg} STREQUAL "NAMES")
596                 set(_state "names")
597             elseif (${arg} STREQUAL "PATHS")
598                 set(_state "paths")
599             else ()
600                 set(_jar_doc ${arg})
601             endif ()
602         endif ()
603     endforeach ()
604
605     if (NOT _jar_names)
606         message(FATAL_ERROR "find_jar: No name to search for given")
607     endif ()
608
609     foreach (jar_name ${_jar_names})
610         foreach (version ${_jar_versions})
611             set(_jar_files ${_jar_files} ${jar_name}-${version}.jar)
612         endforeach ()
613         set(_jar_files ${_jar_files} ${jar_name}.jar)
614     endforeach ()
615
616     find_file(${VARIABLE}
617         NAMES   ${_jar_files}
618         PATHS   ${_jar_paths}
619         DOC     ${_jar_doc}
620         NO_DEFAULT_PATH)
621 endfunction ()
622
623 function(create_javadoc _target)
624     set(_javadoc_packages)
625     set(_javadoc_files)
626     set(_javadoc_sourcepath)
627     set(_javadoc_classpath)
628     set(_javadoc_installpath "${CMAKE_INSTALL_PREFIX}/share/javadoc")
629     set(_javadoc_doctitle)
630     set(_javadoc_windowtitle)
631     set(_javadoc_author FALSE)
632     set(_javadoc_version FALSE)
633     set(_javadoc_use FALSE)
634
635     set(_state "package")
636
637     foreach (arg ${ARGN})
638         if (${_state} STREQUAL "package")
639             if (${arg} STREQUAL "PACKAGES")
640                 set(_state "packages")
641             elseif (${arg} STREQUAL "FILES")
642                 set(_state "files")
643             elseif (${arg} STREQUAL "SOURCEPATH")
644                 set(_state "sourcepath")
645             elseif (${arg} STREQUAL "CLASSPATH")
646                 set(_state "classpath")
647             elseif (${arg} STREQUAL "INSTALLPATH")
648                 set(_state "installpath")
649             elseif (${arg} STREQUAL "DOCTITLE")
650                 set(_state "doctitle")
651             elseif (${arg} STREQUAL "WINDOWTITLE")
652                 set(_state "windowtitle")
653             elseif (${arg} STREQUAL "AUTHOR")
654                 set(_state "author")
655             elseif (${arg} STREQUAL "USE")
656                 set(_state "use")
657             elseif (${arg} STREQUAL "VERSION")
658                 set(_state "version")
659             else ()
660                 set(_javadoc_packages ${arg})
661                 set(_state "packages")
662             endif ()
663         elseif (${_state} STREQUAL "packages")
664             if (${arg} STREQUAL "FILES")
665                 set(_state "files")
666             elseif (${arg} STREQUAL "SOURCEPATH")
667                 set(_state "sourcepath")
668             elseif (${arg} STREQUAL "CLASSPATH")
669                 set(_state "classpath")
670             elseif (${arg} STREQUAL "INSTALLPATH")
671                 set(_state "installpath")
672             elseif (${arg} STREQUAL "DOCTITLE")
673                 set(_state "doctitle")
674             elseif (${arg} STREQUAL "WINDOWTITLE")
675                 set(_state "windowtitle")
676             elseif (${arg} STREQUAL "AUTHOR")
677                 set(_state "author")
678             elseif (${arg} STREQUAL "USE")
679                 set(_state "use")
680             elseif (${arg} STREQUAL "VERSION")
681                 set(_state "version")
682             else ()
683                 list(APPEND _javadoc_packages ${arg})
684             endif ()
685         elseif (${_state} STREQUAL "files")
686             if (${arg} STREQUAL "PACKAGES")
687                 set(_state "packages")
688             elseif (${arg} STREQUAL "SOURCEPATH")
689                 set(_state "sourcepath")
690             elseif (${arg} STREQUAL "CLASSPATH")
691                 set(_state "classpath")
692             elseif (${arg} STREQUAL "INSTALLPATH")
693                 set(_state "installpath")
694             elseif (${arg} STREQUAL "DOCTITLE")
695                 set(_state "doctitle")
696             elseif (${arg} STREQUAL "WINDOWTITLE")
697                 set(_state "windowtitle")
698             elseif (${arg} STREQUAL "AUTHOR")
699                 set(_state "author")
700             elseif (${arg} STREQUAL "USE")
701                 set(_state "use")
702             elseif (${arg} STREQUAL "VERSION")
703                 set(_state "version")
704             else ()
705                 list(APPEND _javadoc_files ${arg})
706             endif ()
707         elseif (${_state} STREQUAL "sourcepath")
708             if (${arg} STREQUAL "PACKAGES")
709                 set(_state "packages")
710             elseif (${arg} STREQUAL "FILES")
711                 set(_state "files")
712             elseif (${arg} STREQUAL "CLASSPATH")
713                 set(_state "classpath")
714             elseif (${arg} STREQUAL "INSTALLPATH")
715                 set(_state "installpath")
716             elseif (${arg} STREQUAL "DOCTITLE")
717                 set(_state "doctitle")
718             elseif (${arg} STREQUAL "WINDOWTITLE")
719                 set(_state "windowtitle")
720             elseif (${arg} STREQUAL "AUTHOR")
721                 set(_state "author")
722             elseif (${arg} STREQUAL "USE")
723                 set(_state "use")
724             elseif (${arg} STREQUAL "VERSION")
725                 set(_state "version")
726             else ()
727                 list(APPEND _javadoc_sourcepath ${arg})
728             endif ()
729         elseif (${_state} STREQUAL "classpath")
730             if (${arg} STREQUAL "PACKAGES")
731                 set(_state "packages")
732             elseif (${arg} STREQUAL "FILES")
733                 set(_state "files")
734             elseif (${arg} STREQUAL "SOURCEPATH")
735                 set(_state "sourcepath")
736             elseif (${arg} STREQUAL "INSTALLPATH")
737                 set(_state "installpath")
738             elseif (${arg} STREQUAL "DOCTITLE")
739                 set(_state "doctitle")
740             elseif (${arg} STREQUAL "WINDOWTITLE")
741                 set(_state "windowtitle")
742             elseif (${arg} STREQUAL "AUTHOR")
743                 set(_state "author")
744             elseif (${arg} STREQUAL "USE")
745                 set(_state "use")
746             elseif (${arg} STREQUAL "VERSION")
747                 set(_state "version")
748             else ()
749                 list(APPEND _javadoc_classpath ${arg})
750             endif ()
751         elseif (${_state} STREQUAL "installpath")
752             if (${arg} STREQUAL "PACKAGES")
753                 set(_state "packages")
754             elseif (${arg} STREQUAL "FILES")
755                 set(_state "files")
756             elseif (${arg} STREQUAL "SOURCEPATH")
757                 set(_state "sourcepath")
758             elseif (${arg} STREQUAL "DOCTITLE")
759                 set(_state "doctitle")
760             elseif (${arg} STREQUAL "WINDOWTITLE")
761                 set(_state "windowtitle")
762             elseif (${arg} STREQUAL "AUTHOR")
763                 set(_state "author")
764             elseif (${arg} STREQUAL "USE")
765                 set(_state "use")
766             elseif (${arg} STREQUAL "VERSION")
767                 set(_state "version")
768             else ()
769                 set(_javadoc_installpath ${arg})
770             endif ()
771         elseif (${_state} STREQUAL "doctitle")
772             if (${arg} STREQUAL "PACKAGES")
773                 set(_state "packages")
774             elseif (${arg} STREQUAL "FILES")
775                 set(_state "files")
776             elseif (${arg} STREQUAL "SOURCEPATH")
777                 set(_state "sourcepath")
778             elseif (${arg} STREQUAL "INSTALLPATH")
779                 set(_state "installpath")
780             elseif (${arg} STREQUAL "CLASSPATH")
781                 set(_state "classpath")
782             elseif (${arg} STREQUAL "WINDOWTITLE")
783                 set(_state "windowtitle")
784             elseif (${arg} STREQUAL "AUTHOR")
785                 set(_state "author")
786             elseif (${arg} STREQUAL "USE")
787                 set(_state "use")
788             elseif (${arg} STREQUAL "VERSION")
789                 set(_state "version")
790             else ()
791                 set(_javadoc_doctitle ${arg})
792             endif ()
793         elseif (${_state} STREQUAL "windowtitle")
794             if (${arg} STREQUAL "PACKAGES")
795                 set(_state "packages")
796             elseif (${arg} STREQUAL "FILES")
797                 set(_state "files")
798             elseif (${arg} STREQUAL "SOURCEPATH")
799                 set(_state "sourcepath")
800             elseif (${arg} STREQUAL "CLASSPATH")
801                 set(_state "classpath")
802             elseif (${arg} STREQUAL "INSTALLPATH")
803                 set(_state "installpath")
804             elseif (${arg} STREQUAL "DOCTITLE")
805                 set(_state "doctitle")
806             elseif (${arg} STREQUAL "AUTHOR")
807                 set(_state "author")
808             elseif (${arg} STREQUAL "USE")
809                 set(_state "use")
810             elseif (${arg} STREQUAL "VERSION")
811                 set(_state "version")
812             else ()
813                 set(_javadoc_windowtitle ${arg})
814             endif ()
815         elseif (${_state} STREQUAL "author")
816             if (${arg} STREQUAL "PACKAGES")
817                 set(_state "packages")
818             elseif (${arg} STREQUAL "FILES")
819                 set(_state "files")
820             elseif (${arg} STREQUAL "SOURCEPATH")
821                 set(_state "sourcepath")
822             elseif (${arg} STREQUAL "CLASSPATH")
823                 set(_state "classpath")
824             elseif (${arg} STREQUAL "INSTALLPATH")
825                 set(_state "installpath")
826             elseif (${arg} STREQUAL "DOCTITLE")
827                 set(_state "doctitle")
828             elseif (${arg} STREQUAL "WINDOWTITLE")
829                 set(_state "windowtitle")
830             elseif (${arg} STREQUAL "AUTHOR")
831                 set(_state "author")
832             elseif (${arg} STREQUAL "USE")
833                 set(_state "use")
834             elseif (${arg} STREQUAL "VERSION")
835                 set(_state "version")
836             else ()
837                 set(_javadoc_author ${arg})
838             endif ()
839         elseif (${_state} STREQUAL "use")
840             if (${arg} STREQUAL "PACKAGES")
841                 set(_state "packages")
842             elseif (${arg} STREQUAL "FILES")
843                 set(_state "files")
844             elseif (${arg} STREQUAL "SOURCEPATH")
845                 set(_state "sourcepath")
846             elseif (${arg} STREQUAL "CLASSPATH")
847                 set(_state "classpath")
848             elseif (${arg} STREQUAL "INSTALLPATH")
849                 set(_state "installpath")
850             elseif (${arg} STREQUAL "DOCTITLE")
851                 set(_state "doctitle")
852             elseif (${arg} STREQUAL "WINDOWTITLE")
853                 set(_state "windowtitle")
854             elseif (${arg} STREQUAL "AUTHOR")
855                 set(_state "author")
856             elseif (${arg} STREQUAL "USE")
857                 set(_state "use")
858             elseif (${arg} STREQUAL "VERSION")
859                 set(_state "version")
860             else ()
861                 set(_javadoc_use ${arg})
862             endif ()
863         elseif (${_state} STREQUAL "version")
864             if (${arg} STREQUAL "PACKAGES")
865                 set(_state "packages")
866             elseif (${arg} STREQUAL "FILES")
867                 set(_state "files")
868             elseif (${arg} STREQUAL "SOURCEPATH")
869                 set(_state "sourcepath")
870             elseif (${arg} STREQUAL "CLASSPATH")
871                 set(_state "classpath")
872             elseif (${arg} STREQUAL "INSTALLPATH")
873                 set(_state "installpath")
874             elseif (${arg} STREQUAL "DOCTITLE")
875                 set(_state "doctitle")
876             elseif (${arg} STREQUAL "WINDOWTITLE")
877                 set(_state "windowtitle")
878             elseif (${arg} STREQUAL "AUTHOR")
879                 set(_state "author")
880             elseif (${arg} STREQUAL "USE")
881                 set(_state "use")
882             elseif (${arg} STREQUAL "VERSION")
883                 set(_state "version")
884             else ()
885                 set(_javadoc_version ${arg})
886             endif ()
887         endif ()
888     endforeach ()
889
890     set(_javadoc_builddir ${CMAKE_CURRENT_BINARY_DIR}/javadoc/${_target})
891     set(_javadoc_options -d ${_javadoc_builddir})
892
893     if (_javadoc_sourcepath)
894         set(_start TRUE)
895         foreach(_path ${_javadoc_sourcepath})
896             if (_start)
897                 set(_sourcepath ${_path})
898                 set(_start FALSE)
899             else ()
900                 set(_sourcepath ${_sourcepath}:${_path})
901             endif ()
902         endforeach()
903         set(_javadoc_options ${_javadoc_options} -sourcepath ${_sourcepath})
904     endif ()
905
906     if (_javadoc_classpath)
907         set(_start TRUE)
908         foreach(_path ${_javadoc_classpath})
909             if (_start)
910                 set(_classpath ${_path})
911                 set(_start FALSE)
912             else ()
913                 set(_classpath ${_classpath}:${_path})
914             endif ()
915         endforeach()
916         set(_javadoc_options ${_javadoc_options} -classpath "${_classpath}")
917     endif ()
918
919     if (_javadoc_doctitle)
920         set(_javadoc_options ${_javadoc_options} -doctitle '${_javadoc_doctitle}')
921     endif ()
922
923     if (_javadoc_windowtitle)
924         set(_javadoc_options ${_javadoc_options} -windowtitle '${_javadoc_windowtitle}')
925     endif ()
926
927     if (_javadoc_author)
928         set(_javadoc_options ${_javadoc_options} -author)
929     endif ()
930
931     if (_javadoc_use)
932         set(_javadoc_options ${_javadoc_options} -use)
933     endif ()
934
935     if (_javadoc_version)
936         set(_javadoc_options ${_javadoc_options} -version)
937     endif ()
938
939     add_custom_target(${_target}_javadoc ALL
940         COMMAND ${Java_JAVADOC_EXECUTABLE} ${_javadoc_options}
941                             ${_javadoc_files}
942                             ${_javadoc_packages}
943         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
944     )
945
946     install(
947         DIRECTORY ${_javadoc_builddir}
948         DESTINATION ${_javadoc_installpath}
949     )
950 endfunction()