From 53235626a2b7d2c07e0e50339a4039a4f5e03001 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Tue, 29 Dec 2020 17:13:15 +0100 Subject: UseJava: enable exportation of native headers target --- .../dev/Java-export-native_headers-target.rst | 6 +++ Modules/UseJava.cmake | 55 ++++++++++++++++++++-- Tests/CMakeLists.txt | 3 ++ Tests/JavaNativeHeaders/CMakeLists.txt | 9 +++- Tests/JavaNativeHeaders/Import/CMakeLists.txt | 19 ++++++++ 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 Help/release/dev/Java-export-native_headers-target.rst create mode 100644 Tests/JavaNativeHeaders/Import/CMakeLists.txt diff --git a/Help/release/dev/Java-export-native_headers-target.rst b/Help/release/dev/Java-export-native_headers-target.rst new file mode 100644 index 0000000..13b6dfb --- /dev/null +++ b/Help/release/dev/Java-export-native_headers-target.rst @@ -0,0 +1,6 @@ +Java-export-native_headers-target +--------------------------------- + +* ``add_jar()`` command, from :module:`UseJava` module, gains new capabilities + for ``GENERATE_NATIVE_HEADERS`` option. These capabilities facilitate the + exportation of the generated target. diff --git a/Modules/UseJava.cmake b/Modules/UseJava.cmake index a5f8a08..120a54c 100644 --- a/Modules/UseJava.cmake +++ b/Modules/UseJava.cmake @@ -23,7 +23,8 @@ Creating And Installing JARs [VERSION ] [OUTPUT_NAME ] [OUTPUT_DIR ] - [GENERATE_NATIVE_HEADERS [DESTINATION ]] + [GENERATE_NATIVE_HEADERS + [DESTINATION (|INSTALL [BUILD ])]] ) This command creates a ``.jar``. It compiles the given @@ -37,7 +38,7 @@ For backwards compatibility, jar files listed as sources are ignored (as they have been since the first version of this module). .. versionadded:: 3.4 - Support fot response files (prefixed by ``@``) in the ``SOURCES`` list. + Support for response files (prefixed by ``@``) in the ``SOURCES`` list. The default ``OUTPUT_DIR`` can also be changed by setting the variable ``CMAKE_JAVA_TARGET_OUTPUT_DIR``. @@ -52,6 +53,12 @@ The default ``OUTPUT_DIR`` can also be changed by setting the variable ``GENERATE_NATIVE_HEADERS`` option requires, at least, version 1.8 of the JDK. +.. versionadded:: 3.20 + ``DESTINATION`` sub-option now supports the possibility to specify different + output directories for ``BUILD`` and ``INSTALL`` steps. This is required to + export the interface target generated by ``GENERATE_NATIVE_HEADERS`` option. + If ``BUILD`` directory is not specified, a default directory will be used. + The ``add_jar()`` function sets the following target properties on ````: @@ -66,6 +73,11 @@ The ``add_jar()`` function sets the following target properties on The directory where the class files can be found. For example to use them with ``javah``. +.. versionadded:: 3.20 + The target generated by option ``GENERATE_NATIVE_HEADERS`` has the property + ``NATIVE_HEADERS_DIRECTORY`` which specify the directory holding the native + headers. + .. code-block:: cmake install_jar( ) @@ -217,6 +229,18 @@ native headers can then be used to compile C/C++ sources with the add_library(bar bar.cpp) target_link_libraries(bar PRIVATE foo-native) +.. versionadded:: 3.20 + It is now possible to export the target generated by + ``GENERATE_NATIVE_HEADERS`` option. + + .. code-block:: cmake + + add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native + DESTINATION INSTALL include) + install(TARGETS foo-native EXPORT native) + install(DIRECTORY "$/" + DESTINATION include) + install(EXPORT native DESTINATION /to/export NAMESPACE foo) Finding JARs ^^^^^^^^^^^^ @@ -493,7 +517,10 @@ function(add_jar _TARGET_NAME) if (Java_VERSION VERSION_LESS 1.8) message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS is not supported with this version of Java.") endif() - cmake_parse_arguments (_add_jar_GENERATE_NATIVE_HEADERS "" "DESTINATION" "" ${_add_jar_GENERATE_NATIVE_HEADERS}) + + unset (_GENERATE_NATIVE_HEADERS_OUTPUT_DESC) + + cmake_parse_arguments (_add_jar_GENERATE_NATIVE_HEADERS "" "" "DESTINATION" ${_add_jar_GENERATE_NATIVE_HEADERS}) if (NOT _add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS) message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS: missing required argument.") endif() @@ -504,11 +531,30 @@ function(add_jar _TARGET_NAME) endif() if (NOT _add_jar_GENERATE_NATIVE_HEADERS_DESTINATION) set (_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir/native_headers") + else() + list (LENGTH _add_jar_GENERATE_NATIVE_HEADERS_DESTINATION length) + if (NOT length EQUAL 1) + cmake_parse_arguments (_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION "" "BUILD;INSTALL" "" "${_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION}") + if (_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_UNPARSED_ARGUMENTS) + message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS: DESTINATION: ${_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_UNPARSED_ARGUMENTS}: unexpected argument(s).") + endif() + if (NOT _add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_INSTALL) + message (FATAL_ERROR "ADD_JAR: GENERATE_NATIVE_HEADERS: DESTINATION: INSTALL sub-option is required.") + endif() + if (NOT _add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_BUILD) + set(_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_BUILD "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir/native_headers") + endif() + set(_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION "${_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION_BUILD}") + set(_GENERATE_NATIVE_HEADERS_OUTPUT_DESC "$" "$") + endif() endif() set (_GENERATE_NATIVE_HEADERS_TARGET ${_add_jar_GENERATE_NATIVE_HEADERS_UNPARSED_ARGUMENTS}) set (_GENERATE_NATIVE_HEADERS_OUTPUT_DIR "${_add_jar_GENERATE_NATIVE_HEADERS_DESTINATION}") set (_GENERATE_NATIVE_HEADERS -h "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}") + if(NOT _GENERATE_NATIVE_HEADERS_OUTPUT_DESC) + set(_GENERATE_NATIVE_HEADERS_OUTPUT_DESC "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}") + endif() endif() if (LIBRARY_OUTPUT_PATH) @@ -762,8 +808,9 @@ function(add_jar _TARGET_NAME) # create an INTERFACE library encapsulating include directory for generated headers add_library (${_GENERATE_NATIVE_HEADERS_TARGET} INTERFACE) target_include_directories (${_GENERATE_NATIVE_HEADERS_TARGET} INTERFACE - "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}" + "${_GENERATE_NATIVE_HEADERS_OUTPUT_DESC}" ${JNI_INCLUDE_DIRS}) + set_property(TARGET ${_GENERATE_NATIVE_HEADERS_TARGET} PROPERTY NATIVE_HEADERS_DIRECTORY "${_GENERATE_NATIVE_HEADERS_OUTPUT_DIR}") # this INTERFACE library depends on jar generation add_dependencies (${_GENERATE_NATIVE_HEADERS_TARGET} ${_TARGET_NAME}) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 2a59d66..74fca5c 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -3394,6 +3394,9 @@ if(BUILD_TESTING) ${build_generator_args} --build-project helloJavaNativeHeaders --build-run-dir "${CMake_BINARY_DIR}/Tests/JavaNativeHeaders/" + --build-target install + --build-options + "-DCMAKE_INSTALL_PREFIX:PATH=${CMake_BINARY_DIR}/Tests/JavaNativeHeaders/Install" --test-command ${CMAKE_CTEST_COMMAND} -V -C $) list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/JavaNativeHeaders") endif() diff --git a/Tests/JavaNativeHeaders/CMakeLists.txt b/Tests/JavaNativeHeaders/CMakeLists.txt index 2023d25..f3cc89d 100644 --- a/Tests/JavaNativeHeaders/CMakeLists.txt +++ b/Tests/JavaNativeHeaders/CMakeLists.txt @@ -11,7 +11,7 @@ include (UseJava) # JNI support find_package(JNI) -add_jar(B1 D.java GENERATE_NATIVE_HEADERS D1-native) +add_jar(B1 D.java GENERATE_NATIVE_HEADERS D1-native DESTINATION INSTALL include) add_jar(E1 E.java GENERATE_NATIVE_HEADERS E1-native) add_jar(hello4 HelloWorld3.java) @@ -19,6 +19,13 @@ add_jar(hello4 HelloWorld3.java) add_library(D SHARED D.cpp E.cpp) target_link_libraries (D PRIVATE D1-native E1-native) +install(TARGETS D1-native EXPORT native) +install(DIRECTORY "$/" DESTINATION include) +install(EXPORT native DESTINATION "${CMAKE_INSTALL_PREFIX}" NAMESPACE D1::) + add_test (NAME Java.NativeHeaders COMMAND "${Java_JAVA_EXECUTABLE}" -Djava.library.path=$ -classpath hello4.jar HelloWorld3) + +add_test (NAME Java.ImportNativeHeaders + COMMAND "${CMAKE_COMMAND}" "-DNATIVE_HEADERS_IMPORT_DIR=${CMAKE_INSTALL_PREFIX}" -S "${CMAKE_CURRENT_SOURCE_DIR}/Import" -B "${CMAKE_CURRENT_BINARY_DIR}/Import") diff --git a/Tests/JavaNativeHeaders/Import/CMakeLists.txt b/Tests/JavaNativeHeaders/Import/CMakeLists.txt new file mode 100644 index 0000000..5486da9 --- /dev/null +++ b/Tests/JavaNativeHeaders/Import/CMakeLists.txt @@ -0,0 +1,19 @@ +project(ImportJavaNativeHeaders LANGUAGES NONE) + +cmake_minimum_required (VERSION 3.19...3.20) +set(CMAKE_VERBOSE_MAKEFILE 1) + +include(${NATIVE_HEADERS_IMPORT_DIR}/native.cmake) + +if(NOT TARGET D1::D1-native) + message(FATAL_ERROR "Target 'D1::D1-native' not found.") +endif() + +get_property(incs TARGET D1::D1-native PROPERTY INTERFACE_INCLUDE_DIRECTORIES) +if (NOT incs MATCHES "${NATIVE_HEADERS_IMPORT_DIR}/include") + message(FATAL_ERROR "Target 'D1::D1-native', property 'INTERFACE_INCLUDE_DIRECTORIES' badly defined: ${incs}.") +endif() + +if (NOT EXISTS "${NATIVE_HEADERS_IMPORT_DIR}/include/D.h") + message(FATAL_ERROR "file '${NATIVE_HEADERS_IMPORT_DIR}/include/D.h' not found.") +endif() -- cgit v0.12