summaryrefslogtreecommitdiffstats
path: root/Modules/UseJava.cmake
diff options
context:
space:
mode:
authorMatthew Woehlke <matthew.woehlke@kitware.com>2016-05-09 18:59:58 (GMT)
committerBrad King <brad.king@kitware.com>2016-06-07 12:50:35 (GMT)
commit5341c0d84af50c2ac5e4041885ab96f8d157ddfe (patch)
tree8ea8229c3c841426982556af557b8dada321a87e /Modules/UseJava.cmake
parentd91ec04402bce94cb29662e0de548438876d86d5 (diff)
downloadCMake-5341c0d84af50c2ac5e4041885ab96f8d157ddfe.zip
CMake-5341c0d84af50c2ac5e4041885ab96f8d157ddfe.tar.gz
CMake-5341c0d84af50c2ac5e4041885ab96f8d157ddfe.tar.bz2
UseJava: Add infrastructure to export targets
Add additional functions to UseJava.cmake to create "exported" targets, similar to those of C/C++ libraries. In support of this, ensure that the JAR_FILE property of jar targets is always an absolute path.
Diffstat (limited to 'Modules/UseJava.cmake')
-rw-r--r--Modules/UseJava.cmake153
1 files changed, 153 insertions, 0 deletions
diff --git a/Modules/UseJava.cmake b/Modules/UseJava.cmake
index 7b3416e..6644bd4 100644
--- a/Modules/UseJava.cmake
+++ b/Modules/UseJava.cmake
@@ -217,6 +217,19 @@
# This command installs the TARGET_NAME files to the given DESTINATION.
# It should be called in the same scope as add_jar() or it will fail.
#
+# Target Properties:
+#
+# ::
+#
+# The install_jar() function sets the INSTALL_DESTINATION target property
+# on jars so installed. This property holds the DESTINATION as described
+# above, and is used by install_jar_exports(). You can get this property
+# with the
+# get_property(TARGET <target_name> PROPERTY INSTALL_DESTINATION)
+# command.
+#
+#
+#
# ::
#
# install_jni_symlink(target_name destination)
@@ -228,6 +241,24 @@
#
# ::
#
+# install_jar_exports(TARGETS jar1 [jar2 ...]
+# FILE export_filename
+# DESTINATION destination [COMPONENT component])
+#
+# This command installs a target export file export_filename for the named jar
+# targets to the given DESTINATION. Its function is similar to that of
+# install(EXPORTS).
+#
+# ::
+#
+# export_jars(TARGETS jar1 [jar2 ...]
+# FILE export_filename)
+#
+# This command writes a target export file export_filename for the named jar
+# targets. Its function is similar to that of export().
+#
+# ::
+#
# create_javadoc(<VAR>
# PACKAGES pkg1 [pkg2 ...]
# [SOURCEPATH <sourcepath>]
@@ -396,7 +427,29 @@ function (__java_copy_file src dest comment)
COMMENT ${comment})
endfunction ()
+function(__java_lcat VAR)
+ foreach(_line ${ARGN})
+ set(${VAR} "${${VAR}}${_line}\n")
+ endforeach()
+
+ set(${VAR} "${${VAR}}" PARENT_SCOPE)
+endfunction()
+
+function(__java_export_jar VAR TARGET PATH)
+ get_target_property(_jarpath ${TARGET} JAR_FILE)
+ get_filename_component(_jarname ${_jarpath} NAME)
+ __java_lcat(${VAR}
+ "# Create imported target ${TARGET}"
+ "add_custom_target(${TARGET})"
+ "set_target_properties(${TARGET} PROPERTIES"
+ " JAR_FILE \"${PATH}/${_jarname}\")"
+ ""
+ )
+ set(${VAR} "${${VAR}}" PARENT_SCOPE)
+endfunction()
+
# define helper scripts
+set(_JAVA_EXPORT_TARGETS_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/javaTargets.cmake.in)
set(_JAVA_CLASS_FILELIST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaClassFilelist.cmake)
set(_JAVA_SYMLINK_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/UseJavaSymlinks.cmake)
@@ -435,6 +488,8 @@ function(add_jar _TARGET_NAME)
if (NOT DEFINED _add_jar_OUTPUT_DIR)
set(_add_jar_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
+ else()
+ get_filename_component(_add_jar_OUTPUT_DIR ${_add_jar_OUTPUT_DIR} ABSOLUTE)
endif()
if (_add_jar_ENTRY_POINT)
@@ -716,6 +771,13 @@ function(INSTALL_JAR _TARGET_NAME)
PROPERTY
INSTALL_FILES
)
+ set_property(
+ TARGET
+ ${_TARGET_NAME}
+ PROPERTY
+ INSTALL_DESTINATION
+ ${_DESTINATION}
+ )
if (__FILES)
install(
@@ -1288,3 +1350,94 @@ function (create_javah)
set (${_create_javah_GENERATED_FILES} ${_output_files} PARENT_SCOPE)
endif()
endfunction()
+
+function(export_jars)
+ # Parse and validate arguments
+ cmake_parse_arguments(_export_jars
+ ""
+ "FILE"
+ "TARGETS"
+ ${ARGN}
+ )
+ if (NOT _export_jars_FILE)
+ message(SEND_ERROR "export_jars: FILE must be specified.")
+ endif()
+ if (NOT _export_jars_TARGETS)
+ message(SEND_ERROR "export_jars: TARGETS must be specified.")
+ endif()
+
+ # Set content of generated exports file
+ string(REPLACE ";" " " __targets__ "${_export_jars_TARGETS}")
+ set(__targetdefs__ "")
+ foreach(_target ${_export_jars_TARGETS})
+ get_target_property(_jarpath ${_target} JAR_FILE)
+ get_filename_component(_jarpath ${_jarpath} PATH)
+ __java_export_jar(__targetdefs__ ${_target} "${_jarpath}")
+ endforeach()
+
+ # Generate exports file
+ configure_file(
+ ${_JAVA_EXPORT_TARGETS_SCRIPT}
+ ${_export_jars_FILE}
+ @ONLY
+ )
+endfunction()
+
+function(install_jar_exports)
+ # Parse and validate arguments
+ cmake_parse_arguments(_install_jar_exports
+ ""
+ "FILE;DESTINATION;COMPONENT"
+ "TARGETS"
+ ${ARGN}
+ )
+ if (NOT _install_jar_exports_FILE)
+ message(SEND_ERROR "install_jar_exports: FILE must be specified.")
+ endif()
+ if (NOT _install_jar_exports_DESTINATION)
+ message(SEND_ERROR "install_jar_exports: DESTINATION must be specified.")
+ endif()
+ if (NOT _install_jar_exports_TARGETS)
+ message(SEND_ERROR "install_jar_exports: TARGETS must be specified.")
+ endif()
+
+ if (_install_jar_exports_COMPONENT)
+ set (_COMPONENT COMPONENT ${_install_jar_exports_COMPONENT})
+ endif()
+
+ # Determine relative path from installed export file to install prefix
+ if(IS_ABSOLUTE ${_install_jar_exports_DESTINATION})
+ file(RELATIVE_PATH _relpath
+ ${_install_jar_exports_DESTINATION}
+ ${CMAKE_INSTALL_PREFIX}
+ )
+ else()
+ file(RELATIVE_PATH _relpath
+ ${CMAKE_INSTALL_PREFIX}/${_install_jar_exports_DESTINATION}
+ ${CMAKE_INSTALL_PREFIX}
+ )
+ endif()
+
+ # Set up unique location for generated exports file
+ string(SHA256 _hash "${_install_jar_exports_DESTINATION}")
+ set(_tmpdir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/JavaExports/${_hash})
+
+ # Set content of generated exports file
+ string(REPLACE ";" " " __targets__ "${_install_jar_exports_TARGETS}")
+ set(__targetdefs__ "set(_prefix \${CMAKE_CURRENT_LIST_DIR}/${_relpath})\n\n")
+ foreach(_target ${_install_jar_exports_TARGETS})
+ get_target_property(_dir ${_target} INSTALL_DESTINATION)
+ __java_export_jar(__targetdefs__ ${_target} "\${_prefix}/${_dir}")
+ endforeach()
+ __java_lcat(__targetdefs__ "\nunset(_prefix)")
+
+ # Generate and install exports file
+ configure_file(
+ ${_JAVA_EXPORT_TARGETS_SCRIPT}
+ ${_tmpdir}/${_install_jar_exports_FILE}
+ @ONLY
+ )
+ install(FILES ${_tmpdir}/${_install_jar_exports_FILE}
+ DESTINATION ${_install_jar_exports_DESTINATION}
+ ${_COMPONENT})
+endfunction()