[cmake-developers] [review] add_jar (UseJava) uses cmake_parse_arguments

Brad King brad.king at kitware.com
Tue Mar 26 12:53:57 EDT 2013


On 03/26/2013 12:25 PM, Matthew Woehlke wrote:
> Done. Please note that I re-pushed the branch such that the end result 
> is the same, but it is now broken into two commits, the first one adding 
> just INCLUDE_JARS. So presumably only the first commit is wanted, and 
> the second can be left along until 2.8.12.

Thanks.  I rebased the topic back on the end of the last topic at adeafad4,
force-pushed to the stage, and then merged the first commit to next:

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=87b3412a

For reference I included its net diff since 45c8e4b1 below.

Andreas, Nicolas, can you please confirm this change does not break
your existing uses, and even better that the new interface works as
you expect?

Thanks,
-Brad


diff --git a/Modules/UseJava.cmake b/Modules/UseJava.cmake
index 7573576..2752cf3 100644
--- a/Modules/UseJava.cmake
+++ b/Modules/UseJava.cmake
@@ -3,11 +3,19 @@
 # has already been loaded.  See FindJava.cmake for information on how to
 # load Java into your CMake project.
 #
-# add_jar(TARGET_NAME SRC1 SRC2 .. SRCN RCS1 RCS2 .. RCSN)
-#
-# This command creates a <TARGET_NAME>.jar. It compiles the given source
-# files (SRC) and adds the given resource files (RCS) to the jar file.
-# If only resource files are given then just a jar file is created.
+# add_jar(target_name
+#         [SOURCES] source1 [source2 ...] [resource1 ...]
+#         [INCLUDE_JARS jar1 [jar2 ...]]
+#        )
+#
+# This command creates a <target_name>.jar. It compiles the given source files
+# (source) and adds the given resource files (resource) to the jar file. If
+# only resource files are given then just a jar file is created. The list of
+# include jars are added to the classpath when compiling the java sources and
+# also to the dependencies of the target. INCLUDE_JARS also accepts other
+# target names created by add_jar. For backwards compatibility, jar files
+# listed as sources are ignored (as they have been since the first version of
+# this module).
 #
 # Additional instructions:
 #   To add compile flags to the target you can set these flags with
@@ -190,6 +198,8 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)

+include(CMakeParseArguments)
+
 function (__java_copy_file src dest comment)
     add_custom_command(
         OUTPUT  ${dest}
@@ -205,7 +215,10 @@ set(_JAVA_CLASS_FILELIST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaClassFilelist.c
 set(_JAVA_SYMLINK_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaSymlinks.cmake)

 function(add_jar _TARGET_NAME)
-    set(_JAVA_SOURCE_FILES ${ARGN})
+
+    cmake_parse_arguments(_add_jar "" "" "SOURCES;INCLUDE_JARS" ${ARGN})
+
+    set(_JAVA_SOURCE_FILES ${_add_jar_SOURCES} ${_add_jar_UNPARSED_ARGUMENTS})

     if (NOT DEFINED CMAKE_JAVA_TARGET_OUTPUT_DIR)
       set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
@@ -257,6 +270,7 @@ function(add_jar _TARGET_NAME)
     set(_JAVA_CLASS_FILES)
     set(_JAVA_COMPILE_FILES)
     set(_JAVA_DEPENDS)
+    set(_JAVA_COMPILE_DEPENDS)
     set(_JAVA_RESOURCE_FILES)
     foreach(_JAVA_SOURCE_FILE ${_JAVA_SOURCE_FILES})
         get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
@@ -284,7 +298,7 @@ function(add_jar _TARGET_NAME)
                 OR _JAVA_EXT MATCHES ".war"
                 OR _JAVA_EXT MATCHES ".ear"
                 OR _JAVA_EXT MATCHES ".sar")
-            list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_SOURCE_FILE})
+            # Ignored for backward compatibility

         elseif (_JAVA_EXT STREQUAL "")
             list(APPEND CMAKE_JAVA_INCLUDE_PATH ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}} ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}_CLASSPATH})
@@ -298,6 +312,25 @@ function(add_jar _TARGET_NAME)
         endif ()
     endforeach()

+    foreach(_JAVA_INCLUDE_JAR ${_add_jar_INCLUDE_JARS})
+        if (TARGET ${_JAVA_INCLUDE_JAR})
+            get_target_property(_JAVA_JAR_PATH ${_JAVA_INCLUDE_JAR} JAR_FILE)
+            if (_JAVA_JAR_PATH)
+                set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_JAR_PATH}")
+                list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_JAR_PATH})
+                list(APPEND _JAVA_DEPENDS ${_JAVA_INCLUDE_JAR})
+                list(APPEND _JAVA_COMPILE_DEPENDS ${_JAVA_INCLUDE_JAR})
+            else ()
+                message(SEND_ERROR "add_jar: INCLUDE_JARS target ${_JAVA_INCLUDE_JAR} is not a jar")
+            endif ()
+        else ()
+            set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${_JAVA_INCLUDE_JAR}")
+            list(APPEND CMAKE_JAVA_INCLUDE_PATH "${_JAVA_INCLUDE_JAR}")
+            list(APPEND _JAVA_DEPENDS "${_JAVA_INCLUDE_JAR}")
+            list(APPEND _JAVA_COMPILE_DEPENDS "${_JAVA_INCLUDE_JAR}")
+        endif ()
+    endforeach()
+
     # create an empty java_class_filelist
     if (NOT EXISTS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist)
         file(WRITE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist "")
@@ -314,7 +347,7 @@ function(add_jar _TARGET_NAME)
                 -d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
                 ${_JAVA_COMPILE_FILES}
             COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
-            DEPENDS ${_JAVA_COMPILE_FILES}
+            DEPENDS ${_JAVA_COMPILE_FILES} ${_JAVA_COMPILE_DEPENDS}
             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
             COMMENT "Building Java objects for ${_TARGET_NAME}.jar"
         )



More information about the cmake-developers mailing list