mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-22 23:19:58 -06:00
Merge topic 'FindJNI-targets'
00c4f488f2FindJNI: support Android NDK171d45c039FindJNI: added components and imported targets35e92ec619FindJNI: improved description Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: Alex <leha-bot@yandex.ru> Merge-request: !7069
This commit is contained in:
@@ -27,6 +27,7 @@ set(CMake_TEST_FindGTest "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindGTK2 "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIconv "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIntl "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJNI "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJPEG "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJsonCpp "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindLAPACK "All;static=1;Generic" CACHE STRING "")
|
||||
|
||||
@@ -31,6 +31,7 @@ set(CMake_TEST_FindGTest "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindGTK2 "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIconv "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIntl "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJNI "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJPEG "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJsonCpp "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindLAPACK "All;static=1;Generic" CACHE STRING "")
|
||||
|
||||
@@ -30,6 +30,7 @@ set(CMake_TEST_FindGTest "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindGTK2 "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIconv "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindIntl "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJNI "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJPEG "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindJsonCpp "ON" CACHE BOOL "")
|
||||
set(CMake_TEST_FindLAPACK "All;static=1;Generic" CACHE STRING "")
|
||||
|
||||
4
Help/release/dev/FindJNI-targets.rst
Normal file
4
Help/release/dev/FindJNI-targets.rst
Normal file
@@ -0,0 +1,4 @@
|
||||
FindJNI-targets
|
||||
---------------
|
||||
|
||||
* The :module:`FindJNI` module now provides imported targets.
|
||||
@@ -5,28 +5,79 @@
|
||||
FindJNI
|
||||
-------
|
||||
|
||||
Find Java Native Interface (JNI) libraries.
|
||||
Find Java Native Interface (JNI) headers and libraries.
|
||||
|
||||
JNI enables Java code running in a Java Virtual Machine (JVM) to call
|
||||
and be called by native applications and libraries written in other
|
||||
languages such as C, C++.
|
||||
JNI enables Java code running in a Java Virtual Machine (JVM) or Dalvik Virtual
|
||||
Machine (DVM) on Android to call and be called by native applications and
|
||||
libraries written in other languages such as C and C++.
|
||||
|
||||
This module finds if Java is installed and determines where the
|
||||
include files and libraries are. It also determines what the name of
|
||||
the library is. The caller may set variable ``JAVA_HOME`` to specify a
|
||||
Java installation prefix explicitly.
|
||||
|
||||
.. versionadded:: 3.24
|
||||
|
||||
Added imported targets, components ``AWT``, ``JVM``, and Android NDK support.
|
||||
If no components are specified, the module defaults to an empty components
|
||||
list while targeting Android, and all available components otherwise.
|
||||
|
||||
When using Android NDK, the corresponding package version is reported and a
|
||||
specific release can be requested. At Android API level 31 and above, the
|
||||
additional ``NativeHelper`` component can be requested. ``NativeHelper`` is
|
||||
also exposed as an implicit dependency of the ``JVM`` component (only if this
|
||||
does not cause a conflict) which provides a uniform access to JVM functions.
|
||||
|
||||
Imported Targets
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
.. versionadded:: 3.24
|
||||
|
||||
``JNI::JNI``
|
||||
Main JNI target, defined only if ``jni.h`` was found.
|
||||
|
||||
``JNI::AWT``
|
||||
Java AWT Native Interface (JAWT) library, defined only if component ``AWT`` was
|
||||
found.
|
||||
|
||||
``JNI::JVM``
|
||||
Java Virtual Machine (JVM) library, defined only if component ``JVM`` was found.
|
||||
|
||||
``JNI::NativeHelper``
|
||||
When targeting Android API level 31 and above, the import target will provide
|
||||
access to ``libnativehelper.so`` that exposes JVM functions such as
|
||||
``JNI_CreateJavaVM``.
|
||||
|
||||
Result Variables
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
This module sets the following result variables:
|
||||
|
||||
``JNI_INCLUDE_DIRS``
|
||||
the include dirs to use
|
||||
The include directories to use.
|
||||
``JNI_LIBRARIES``
|
||||
the libraries to use (JAWT and JVM)
|
||||
The libraries to use (JAWT and JVM).
|
||||
``JNI_FOUND``
|
||||
TRUE if JNI headers and libraries were found.
|
||||
``TRUE`` if JNI headers and libraries were found.
|
||||
``JNI_<component>_FOUND``
|
||||
.. versionadded:: 3.24
|
||||
|
||||
``TRUE`` if ``<component>`` was found.
|
||||
``JNI_VERSION``
|
||||
Full Android NDK package version (including suffixes such as ``-beta3`` and
|
||||
``-rc1``) or undefined otherwise.
|
||||
``JNI_VERSION_MAJOR``
|
||||
.. versionadded:: 3.24
|
||||
|
||||
Android NDK major version or undefined otherwise.
|
||||
``JNI_VERSION_MINOR``
|
||||
.. versionadded:: 3.24
|
||||
|
||||
Android NDK minor version or undefined otherwise.
|
||||
``JNI_VERSION_PATCH``
|
||||
.. versionadded:: 3.24
|
||||
|
||||
Android NDK patch version or undefined otherwise.
|
||||
|
||||
Cache Variables
|
||||
^^^^^^^^^^^^^^^
|
||||
@@ -34,17 +85,55 @@ Cache Variables
|
||||
The following cache variables are also available to set or use:
|
||||
|
||||
``JAVA_AWT_LIBRARY``
|
||||
the path to the Java AWT Native Interface (JAWT) library
|
||||
The path to the Java AWT Native Interface (JAWT) library.
|
||||
``JAVA_JVM_LIBRARY``
|
||||
the path to the Java Virtual Machine (JVM) library
|
||||
The path to the Java Virtual Machine (JVM) library.
|
||||
``JAVA_INCLUDE_PATH``
|
||||
the include path to jni.h
|
||||
The include path to ``jni.h``.
|
||||
``JAVA_INCLUDE_PATH2``
|
||||
the include path to jni_md.h and jniport.h
|
||||
The include path to machine-dependant headers ``jni_md.h`` and ``jniport.h``.
|
||||
The variable is defined only if ``jni.h`` depends on one of these headers. In
|
||||
contrast, Android NDK ``jni.h`` can be typically used standalone.
|
||||
``JAVA_AWT_INCLUDE_PATH``
|
||||
the include path to jawt.h
|
||||
The include path to ``jawt.h``.
|
||||
#]=======================================================================]
|
||||
|
||||
cmake_policy(PUSH)
|
||||
cmake_policy(SET CMP0057 NEW)
|
||||
|
||||
include(CheckSourceCompiles)
|
||||
include(CMakePushCheckState)
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
if(NOT JNI_FIND_COMPONENTS)
|
||||
if(ANDROID)
|
||||
if(CMAKE_ANDROID_API LESS 31)
|
||||
# There are no components for Android NDK
|
||||
set(JNI_FIND_COMPONENTS)
|
||||
else()
|
||||
set(JNI_FIND_COMPONENTS NativeHelper)
|
||||
set(JNI_FIND_REQUIRED_NativeHelper TRUE)
|
||||
endif()
|
||||
else(ANDROID)
|
||||
set(JNI_FIND_COMPONENTS AWT JVM)
|
||||
# For compatibility purposes, if no components are specified both are
|
||||
# considered required.
|
||||
set(JNI_FIND_REQUIRED_AWT TRUE)
|
||||
set(JNI_FIND_REQUIRED_JVM TRUE)
|
||||
endif()
|
||||
else()
|
||||
# On Android, if JVM was requested we need to find NativeHelper as well which
|
||||
# is an implicit dependency of JVM allowing to provide uniform access to basic
|
||||
# JVM/DVM functionality.
|
||||
if(ANDROID AND CMAKE_ANDROID_API GREATER_EQUAL 31 AND JVM IN_LIST JNI_FIND_COMPONENTS)
|
||||
if(NOT NativeHelper IN_LIST JNI_FIND_COMPONENTS)
|
||||
list(APPEND JNI_FIND_COMPONENTS NativeHelper)
|
||||
# NativeHelper is required only if JVM was requested as such.
|
||||
set(JNI_FIND_REQUIRED_NativeHelper ${JNI_FIND_REQUIRED_JVM})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Expand {libarch} occurrences to java_libarch subdirectory(-ies) and set ${_var}
|
||||
macro(java_append_library_directories _var)
|
||||
# Determine java arch-specific library subdir
|
||||
@@ -328,10 +417,19 @@ set(_JNI_NORMAL_JAWT
|
||||
)
|
||||
|
||||
foreach(search ${_JNI_SEARCHES})
|
||||
find_library(JAVA_JVM_LIBRARY ${_JNI_${search}_JVM})
|
||||
find_library(JAVA_AWT_LIBRARY ${_JNI_${search}_JAWT})
|
||||
if(JAVA_JVM_LIBRARY)
|
||||
break()
|
||||
if(JVM IN_LIST JNI_FIND_COMPONENTS)
|
||||
find_library(JAVA_JVM_LIBRARY ${_JNI_${search}_JVM}
|
||||
DOC "Java Virtual Machine library"
|
||||
)
|
||||
endif(JVM IN_LIST JNI_FIND_COMPONENTS)
|
||||
|
||||
if(AWT IN_LIST JNI_FIND_COMPONENTS)
|
||||
find_library(JAVA_AWT_LIBRARY ${_JNI_${search}_JAWT}
|
||||
DOC "Java AWT Native Interface library"
|
||||
)
|
||||
if(JAVA_JVM_LIBRARY)
|
||||
break()
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
unset(_JNI_SEARCHES)
|
||||
@@ -350,11 +448,46 @@ endif()
|
||||
# add in the include path
|
||||
find_path(JAVA_INCLUDE_PATH jni.h
|
||||
${JAVA_AWT_INCLUDE_DIRECTORIES}
|
||||
DOC "JNI include directory"
|
||||
)
|
||||
|
||||
if(JAVA_INCLUDE_PATH)
|
||||
if(CMAKE_C_COMPILER_LOADED)
|
||||
set(_JNI_CHECK_LANG C)
|
||||
elseif(CMAKE_CXX_COMPILER_LOADED)
|
||||
set(_JNI_CHECK_LANG CXX)
|
||||
else()
|
||||
set(_JNI_CHECK_LANG FALSE)
|
||||
endif()
|
||||
|
||||
# Skip the check if neither C nor CXX is loaded.
|
||||
if(_JNI_CHECK_LANG)
|
||||
cmake_push_check_state(RESET)
|
||||
# The result of the following check is not relevant for the user as
|
||||
# JAVA_INCLUDE_PATH2 will be added to REQUIRED_VARS if necessary.
|
||||
set(CMAKE_REQUIRED_QUIET ON)
|
||||
set(CMAKE_REQUIRED_INCLUDES ${JAVA_INCLUDE_PATH})
|
||||
|
||||
# Determine whether jni.h requires jni_md.h and add JAVA_INCLUDE_PATH2
|
||||
# correspondingly to REQUIRED_VARS
|
||||
check_source_compiles(${_JNI_CHECK_LANG}
|
||||
"
|
||||
#include <jni.h>
|
||||
int main(void) { return 0; }
|
||||
"
|
||||
JNI_INCLUDE_PATH2_OPTIONAL)
|
||||
|
||||
cmake_pop_check_state()
|
||||
else()
|
||||
# If the above check is skipped assume jni_md.h is not needed.
|
||||
set(JNI_INCLUDE_PATH2_OPTIONAL TRUE)
|
||||
endif()
|
||||
|
||||
unset(_JNI_CHECK_LANG)
|
||||
endif()
|
||||
|
||||
find_path(JAVA_INCLUDE_PATH2 NAMES jni_md.h jniport.h
|
||||
PATHS
|
||||
${JAVA_INCLUDE_PATH}
|
||||
PATHS ${JAVA_INCLUDE_PATH}
|
||||
${JAVA_INCLUDE_PATH}/darwin
|
||||
${JAVA_INCLUDE_PATH}/win32
|
||||
${JAVA_INCLUDE_PATH}/linux
|
||||
@@ -364,11 +497,50 @@ find_path(JAVA_INCLUDE_PATH2 NAMES jni_md.h jniport.h
|
||||
${JAVA_INCLUDE_PATH}/hp-ux
|
||||
${JAVA_INCLUDE_PATH}/alpha
|
||||
${JAVA_INCLUDE_PATH}/aix
|
||||
DOC "jni_md.h jniport.h include directory"
|
||||
)
|
||||
|
||||
find_path(JAVA_AWT_INCLUDE_PATH jawt.h
|
||||
${JAVA_INCLUDE_PATH}
|
||||
)
|
||||
if(AWT IN_LIST JNI_FIND_COMPONENTS)
|
||||
find_path(JAVA_AWT_INCLUDE_PATH jawt.h
|
||||
${JAVA_INCLUDE_PATH}
|
||||
DOC "Java AWT Native Interface include directory"
|
||||
)
|
||||
endif()
|
||||
|
||||
if(ANDROID)
|
||||
# Some functions in jni.h (e.g., JNI_GetCreatedJavaVMs) are exported by
|
||||
# libnativehelper.so, however, only when targeting Android API level >= 31.
|
||||
find_library(JAVA_NativeHelper_LIBRARY NAMES nativehelper
|
||||
DOC "Android nativehelper library"
|
||||
)
|
||||
endif()
|
||||
|
||||
# Set found components
|
||||
if(JAVA_AWT_INCLUDE_PATH AND JAVA_AWT_LIBRARY)
|
||||
set(JNI_AWT_FOUND TRUE)
|
||||
else()
|
||||
set(JNI_AWT_FOUND FALSE)
|
||||
endif()
|
||||
|
||||
# JVM is available even on Android referencing the nativehelper library
|
||||
if(JAVA_JVM_LIBRARY)
|
||||
set(JNI_JVM_FOUND TRUE)
|
||||
else(JAVA_JVM_LIBRARY)
|
||||
set(JNI_JVM_FOUND FALSE)
|
||||
endif()
|
||||
|
||||
if(JAVA_NativeHelper_LIBRARY)
|
||||
# Alias JAVA_JVM_LIBRARY to JAVA_NativeHelper_LIBRARY
|
||||
if(NOT JAVA_JVM_LIBRARY)
|
||||
set(JAVA_JVM_LIBRARY "${JAVA_NativeHelper_LIBRARY}" CACHE FILEPATH
|
||||
"Alias to nativehelper library" FORCE)
|
||||
# Make JVM component available
|
||||
set(JNI_JVM_FOUND TRUE)
|
||||
endif()
|
||||
set(JNI_NativeHelper_FOUND TRUE)
|
||||
else()
|
||||
set(JNI_NativeHelper_FOUND FALSE)
|
||||
endif()
|
||||
|
||||
# Restore CMAKE_FIND_FRAMEWORK
|
||||
if(DEFINED _JNI_CMAKE_FIND_FRAMEWORK)
|
||||
@@ -378,12 +550,35 @@ else()
|
||||
unset(CMAKE_FIND_FRAMEWORK)
|
||||
endif()
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(JNI DEFAULT_MSG JAVA_AWT_LIBRARY
|
||||
JAVA_JVM_LIBRARY
|
||||
JAVA_INCLUDE_PATH
|
||||
JAVA_INCLUDE_PATH2
|
||||
JAVA_AWT_INCLUDE_PATH)
|
||||
if(ANDROID)
|
||||
# Extract NDK version from source.properties in the NDK root
|
||||
set(JAVA_SOURCE_PROPERTIES_FILE ${CMAKE_ANDROID_NDK}/source.properties)
|
||||
|
||||
if(EXISTS ${JAVA_SOURCE_PROPERTIES_FILE})
|
||||
file(READ ${JAVA_SOURCE_PROPERTIES_FILE} NDK_VERSION_CONTENTS)
|
||||
string (REGEX REPLACE
|
||||
".*Pkg\\.Revision = (([0-9]+)\\.([0-9]+)\\.([0-9]+)([^\n]+)?).*" "\\1"
|
||||
JNI_VERSION "${NDK_VERSION_CONTENTS}")
|
||||
set(JNI_VERSION_MAJOR ${CMAKE_MATCH_1})
|
||||
set(JNI_VERSION_MINOR ${CMAKE_MATCH_2})
|
||||
set(JNI_VERSION_PATCH ${CMAKE_MATCH_3})
|
||||
set(JNI_VERSION_COMPONENTS 3)
|
||||
|
||||
set(JNI_FPHSA_ARGS VERSION_VAR JNI_VERSION HANDLE_VERSION_RANGE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(JNI_REQUIRED_VARS JAVA_INCLUDE_PATH)
|
||||
|
||||
if(NOT JNI_INCLUDE_PATH2_OPTIONAL)
|
||||
list(APPEND JNI_REQUIRED_VARS JAVA_INCLUDE_PATH2)
|
||||
endif()
|
||||
|
||||
find_package_handle_standard_args(JNI
|
||||
REQUIRED_VARS ${JNI_REQUIRED_VARS}
|
||||
${JNI_FPHSA_ARGS}
|
||||
HANDLE_COMPONENTS
|
||||
)
|
||||
|
||||
mark_as_advanced(
|
||||
JAVA_AWT_LIBRARY
|
||||
@@ -393,13 +588,93 @@ mark_as_advanced(
|
||||
JAVA_INCLUDE_PATH2
|
||||
)
|
||||
|
||||
set(JNI_LIBRARIES
|
||||
${JAVA_AWT_LIBRARY}
|
||||
${JAVA_JVM_LIBRARY}
|
||||
)
|
||||
set(JNI_LIBRARIES)
|
||||
|
||||
set(JNI_INCLUDE_DIRS
|
||||
${JAVA_INCLUDE_PATH}
|
||||
${JAVA_INCLUDE_PATH2}
|
||||
${JAVA_AWT_INCLUDE_PATH}
|
||||
)
|
||||
foreach(component IN LISTS JNI_FIND_COMPONENTS)
|
||||
if(JNI_${component}_FOUND)
|
||||
list(APPEND JNI_LIBRARIES ${JAVA_${component}_LIBRARY})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(JNI_INCLUDE_DIRS ${JAVA_INCLUDE_PATH})
|
||||
|
||||
if(NOT JNI_INCLUDE_PATH2_OPTIONAL)
|
||||
list(APPEND JNI_INCLUDE_DIRS ${JAVA_INCLUDE_PATH2})
|
||||
endif()
|
||||
|
||||
if(JNI_FIND_REQUIRED_AWT)
|
||||
list(APPEND JNI_INCLUDE_DIRS ${JAVA_AWT_INCLUDE_PATH})
|
||||
endif()
|
||||
|
||||
if(JNI_FOUND)
|
||||
if(NOT TARGET JNI::JNI)
|
||||
add_library(JNI::JNI IMPORTED INTERFACE)
|
||||
endif()
|
||||
|
||||
set_property(TARGET JNI::JNI PROPERTY INTERFACE_INCLUDE_DIRECTORIES
|
||||
${JAVA_INCLUDE_PATH})
|
||||
|
||||
if(JNI_NativeHelper_FOUND)
|
||||
if(NOT TARGET JNI::NativeHelper)
|
||||
add_library(JNI::NativeHelper IMPORTED UNKNOWN)
|
||||
endif()
|
||||
|
||||
set_property(TARGET JNI::NativeHelper PROPERTY INTERFACE_LINK_LIBRARIES
|
||||
JNI::JNI)
|
||||
set_property(TARGET JNI::NativeHelper PROPERTY IMPORTED_LOCATION
|
||||
${JAVA_NativeHelper_LIBRARY})
|
||||
endif()
|
||||
|
||||
if(NOT JNI_INCLUDE_PATH2_OPTIONAL AND JAVA_INCLUDE_PATH2)
|
||||
set_property(TARGET JNI::JNI APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
|
||||
${JAVA_INCLUDE_PATH2})
|
||||
endif()
|
||||
|
||||
if(JNI_AWT_FOUND)
|
||||
if(NOT TARGET JNI::AWT)
|
||||
add_library(JNI::AWT IMPORTED UNKNOWN)
|
||||
endif(NOT TARGET JNI::AWT)
|
||||
|
||||
set_property(TARGET JNI::AWT PROPERTY INTERFACE_INCLUDE_DIRECTORIES
|
||||
${JAVA_AWT_INCLUDE_PATH})
|
||||
set_property(TARGET JNI::AWT PROPERTY IMPORTED_LOCATION
|
||||
${JAVA_AWT_LIBRARY})
|
||||
set_property(TARGET JNI::AWT PROPERTY INTERFACE_LINK_LIBRARIES JNI::JNI)
|
||||
endif()
|
||||
|
||||
if(JNI_JVM_FOUND OR JNI_NativeHelper_FOUND)
|
||||
# If Android nativehelper is available but not the JVM library, we still
|
||||
# define the JNI::JVM target but only declare JNI::NativeHelper as an
|
||||
# interface link library of the former. This provides a uniform access to
|
||||
# fundamental JVM functionality regardless of whether JVM or DVM is used. At
|
||||
# the same time, this allows the user to detect whenever exclusively
|
||||
# nativehelper functionality is available.
|
||||
if(NOT TARGET JNI::JVM)
|
||||
if(JAVA_JVM_LIBRARY AND NOT JAVA_JVM_LIBRARY STREQUAL JAVA_NativeHelper_LIBRARY)
|
||||
# JAVA_JVM_LIBRARY is not an alias of JAVA_NativeHelper_LIBRARY
|
||||
add_library(JNI::JVM IMPORTED UNKNOWN)
|
||||
else()
|
||||
add_library(JNI::JVM IMPORTED INTERFACE)
|
||||
endif()
|
||||
endif(NOT TARGET JNI::JVM)
|
||||
|
||||
set_property(TARGET JNI::JVM PROPERTY INTERFACE_LINK_LIBRARIES JNI::JNI)
|
||||
get_property(_JNI_JVM_TYPE TARGET JNI::JVM PROPERTY TYPE)
|
||||
|
||||
if(NOT _JNI_JVM_TYPE STREQUAL "INTERFACE_LIBRARY")
|
||||
set_property(TARGET JNI::JVM PROPERTY IMPORTED_LOCATION
|
||||
${JAVA_JVM_LIBRARY})
|
||||
else()
|
||||
# We declare JNI::NativeHelper a dependency of JNI::JVM only if the latter
|
||||
# was not initially found. If the solely theoretical situation occurs
|
||||
# where both libraries are available, we want to avoid any potential
|
||||
# errors that can occur due to duplicate symbols.
|
||||
set_property(TARGET JNI::JVM APPEND PROPERTY INTERFACE_LINK_LIBRARIES
|
||||
JNI::NativeHelper)
|
||||
endif()
|
||||
|
||||
unset(_JNI_JVM_TYPE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
cmake_policy(POP)
|
||||
|
||||
@@ -1455,6 +1455,7 @@ if(BUILD_TESTING)
|
||||
ICU
|
||||
Intl
|
||||
Jasper
|
||||
JNI
|
||||
JPEG
|
||||
JsonCpp
|
||||
LAPACK
|
||||
|
||||
7
Tests/FindJNI/AWT/AWTTgt.java
Normal file
7
Tests/FindJNI/AWT/AWTTgt.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class AWTTgt
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("AWTTgt");
|
||||
}
|
||||
}
|
||||
7
Tests/FindJNI/AWT/AWTVar.java
Normal file
7
Tests/FindJNI/AWT/AWTVar.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class AWTVar
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("AWTVar");
|
||||
}
|
||||
}
|
||||
31
Tests/FindJNI/AWT/CMakeLists.txt
Normal file
31
Tests/FindJNI/AWT/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
cmake_minimum_required (VERSION 3.23)
|
||||
project (TestAWT)
|
||||
|
||||
find_package (JNI REQUIRED COMPONENTS AWT)
|
||||
find_package (Java REQUIRED COMPONENTS Runtime Development)
|
||||
|
||||
add_library (AWTTgt MODULE awt.cxx)
|
||||
target_link_libraries (AWTTgt PRIVATE JNI::AWT)
|
||||
|
||||
add_library (AWTVar MODULE awt.cxx)
|
||||
target_include_directories (AWTVar PRIVATE ${JNI_INCLUDE_DIRS})
|
||||
target_link_libraries (AWTVar PRIVATE ${JNI_LIBRARIES})
|
||||
|
||||
enable_testing ()
|
||||
|
||||
foreach (test AWTTgt AWTVar)
|
||||
add_test (NAME Compile${test} COMMAND ${Java_JAVAC_EXECUTABLE}
|
||||
${TestAWT_SOURCE_DIR}/${test}.java)
|
||||
add_test (NAME Remove${test} COMMAND ${CMAKE_COMMAND} -E remove
|
||||
${TestAWT_SOURCE_DIR}/${test}.class)
|
||||
|
||||
add_test (NAME ${test} COMMAND ${Java_JAVA_EXECUTABLE}
|
||||
-cp ${TestAWT_SOURCE_DIR}
|
||||
-Djava.library.path=$<TARGET_FILE_DIR:${test}>
|
||||
${test}
|
||||
)
|
||||
|
||||
set_tests_properties (Compile${test} PROPERTIES FIXTURES_SETUP Init${test})
|
||||
set_tests_properties (Remove${test} PROPERTIES FIXTURES_CLEANUP Init${test})
|
||||
set_tests_properties (${test} PROPERTIES FIXTURES_REQUIRED Init${test})
|
||||
endforeach ()
|
||||
23
Tests/FindJNI/AWT/awt.cxx
Normal file
23
Tests/FindJNI/AWT/awt.cxx
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <jawt.h>
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
|
||||
{
|
||||
void* tmp;
|
||||
|
||||
if (vm->GetEnv(&tmp, JNI_VERSION_1_2) != JNI_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
JAWT awt;
|
||||
awt.version = JAWT_VERSION_1_3;
|
||||
|
||||
if (JAWT_GetAWT(static_cast<JNIEnv*>(tmp), &awt) == JNI_FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return JNI_VERSION_1_2;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
|
||||
{
|
||||
}
|
||||
12
Tests/FindJNI/CMakeLists.txt
Normal file
12
Tests/FindJNI/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
foreach(test IN ITEMS Minimal JVM AWT)
|
||||
add_test(NAME FindJNI.${test} COMMAND
|
||||
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
|
||||
--build-and-test
|
||||
"${CMake_SOURCE_DIR}/Tests/FindJNI/${test}"
|
||||
"${CMake_BINARY_DIR}/Tests/FindJNI/${test}"
|
||||
${build_generator_args}
|
||||
--build-project Test${test}
|
||||
--build-options ${build_options}
|
||||
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
|
||||
)
|
||||
endforeach()
|
||||
31
Tests/FindJNI/JVM/CMakeLists.txt
Normal file
31
Tests/FindJNI/JVM/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
cmake_minimum_required (VERSION 3.23)
|
||||
project (TestJVM)
|
||||
|
||||
find_package (JNI REQUIRED COMPONENTS JVM)
|
||||
find_package (Java REQUIRED COMPONENTS Runtime Development)
|
||||
|
||||
add_library (JVMTgt MODULE jvm.cxx)
|
||||
target_link_libraries (JVMTgt PRIVATE JNI::JVM)
|
||||
|
||||
add_library (JVMVar MODULE jvm.cxx)
|
||||
target_include_directories (JVMVar PRIVATE ${JNI_INCLUDE_DIRS})
|
||||
target_link_libraries (JVMVar PRIVATE ${JNI_LIBRARIES})
|
||||
|
||||
enable_testing ()
|
||||
|
||||
foreach (test JVMTgt JVMVar)
|
||||
add_test (NAME Compile${test} COMMAND ${Java_JAVAC_EXECUTABLE}
|
||||
${TestJVM_SOURCE_DIR}/${test}.java)
|
||||
add_test (NAME Remove${test} COMMAND ${CMAKE_COMMAND} -E remove
|
||||
${TestJVM_SOURCE_DIR}/${test}.class)
|
||||
|
||||
add_test (NAME ${test} COMMAND ${Java_JAVA_EXECUTABLE}
|
||||
-cp ${TestJVM_SOURCE_DIR}
|
||||
-Djava.library.path=$<TARGET_FILE_DIR:${test}>
|
||||
${test}
|
||||
)
|
||||
|
||||
set_tests_properties (Compile${test} PROPERTIES FIXTURES_SETUP Init${test})
|
||||
set_tests_properties (Remove${test} PROPERTIES FIXTURES_CLEANUP Init${test})
|
||||
set_tests_properties (${test} PROPERTIES FIXTURES_REQUIRED Init${test})
|
||||
endforeach ()
|
||||
7
Tests/FindJNI/JVM/JVMTgt.java
Normal file
7
Tests/FindJNI/JVM/JVMTgt.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class JVMTgt
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("JVMTgt");
|
||||
}
|
||||
}
|
||||
7
Tests/FindJNI/JVM/JVMVar.java
Normal file
7
Tests/FindJNI/JVM/JVMVar.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class JVMVar
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("JVMVar");
|
||||
}
|
||||
}
|
||||
15
Tests/FindJNI/JVM/jvm.cxx
Normal file
15
Tests/FindJNI/JVM/jvm.cxx
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <jni.h>
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
|
||||
{
|
||||
jsize n = 0;
|
||||
if (JNI_GetCreatedJavaVMs(nullptr, 0, &n) != JNI_OK || n <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return JNI_VERSION_1_2;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
|
||||
{
|
||||
}
|
||||
35
Tests/FindJNI/Minimal/CMakeLists.txt
Normal file
35
Tests/FindJNI/Minimal/CMakeLists.txt
Normal file
@@ -0,0 +1,35 @@
|
||||
cmake_minimum_required (VERSION 3.23)
|
||||
project (TestMinimal)
|
||||
|
||||
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
|
||||
|
||||
find_package (JNI REQUIRED)
|
||||
find_package (Java REQUIRED COMPONENTS Runtime Development)
|
||||
|
||||
add_library (MinimalTgt MODULE minimal.cxx)
|
||||
target_link_libraries (MinimalTgt PRIVATE JNI::JNI)
|
||||
|
||||
add_library (MinimalVar MODULE minimal.cxx)
|
||||
target_include_directories (MinimalVar PRIVATE ${JNI_INCLUDE_DIRS})
|
||||
target_link_libraries (MinimalVar PRIVATE ${JNI_LIBRARIES})
|
||||
|
||||
enable_testing ()
|
||||
|
||||
foreach (test MinimalTgt MinimalVar)
|
||||
add_test (NAME Compile${test} COMMAND ${Java_JAVAC_EXECUTABLE}
|
||||
${TestMinimal_SOURCE_DIR}/${test}.java)
|
||||
add_test (NAME Remove${test} COMMAND ${CMAKE_COMMAND} -E remove
|
||||
${TestMinimal_SOURCE_DIR}/${test}.class)
|
||||
|
||||
add_test (NAME ${test} COMMAND ${Java_JAVA_EXECUTABLE}
|
||||
-cp ${TestMinimal_SOURCE_DIR}
|
||||
-Djava.library.path=$<TARGET_FILE_DIR:${test}>
|
||||
${test}
|
||||
)
|
||||
|
||||
set_tests_properties (Compile${test} PROPERTIES FIXTURES_SETUP Init${test})
|
||||
set_tests_properties (Remove${test} PROPERTIES FIXTURES_CLEANUP Init${test})
|
||||
set_tests_properties (${test} PROPERTIES FIXTURES_REQUIRED Init${test})
|
||||
|
||||
set_tests_properties (${test} PROPERTIES PASS_REGULAR_EXPRESSION "^FindJNI\n$")
|
||||
endforeach ()
|
||||
7
Tests/FindJNI/Minimal/MinimalTgt.java
Normal file
7
Tests/FindJNI/Minimal/MinimalTgt.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class MinimalTgt
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("MinimalTgt");
|
||||
}
|
||||
}
|
||||
7
Tests/FindJNI/Minimal/MinimalVar.java
Normal file
7
Tests/FindJNI/Minimal/MinimalVar.java
Normal file
@@ -0,0 +1,7 @@
|
||||
class MinimalVar
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.loadLibrary("MinimalVar");
|
||||
}
|
||||
}
|
||||
38
Tests/FindJNI/Minimal/minimal.cxx
Normal file
38
Tests/FindJNI/Minimal/minimal.cxx
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <cassert>
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
|
||||
{
|
||||
void* tmp;
|
||||
if (vm->GetEnv(&tmp, JNI_VERSION_1_2) != JNI_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The following lines do:
|
||||
//
|
||||
// System.out.println("FindJNI");
|
||||
//
|
||||
JNIEnv* env = static_cast<JNIEnv*>(tmp);
|
||||
|
||||
jclass clzS = env->FindClass("java/lang/System");
|
||||
jclass clzP = env->FindClass("java/io/PrintStream");
|
||||
|
||||
jfieldID outF = env->GetStaticFieldID(clzS, "out", "Ljava/io/PrintStream;");
|
||||
jobject out = env->GetStaticObjectField(clzS, outF);
|
||||
jmethodID println =
|
||||
env->GetMethodID(clzP, "println", "(Ljava/lang/String;)V");
|
||||
|
||||
env->CallVoidMethod(out, println, env->NewStringUTF("FindJNI"));
|
||||
|
||||
return JNI_VERSION_1_2;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
|
||||
{
|
||||
void* env;
|
||||
jint result = vm->GetEnv(&env, JNI_VERSION_1_2);
|
||||
assert(result == JNI_OK);
|
||||
|
||||
assert(static_cast<JNIEnv*>(env)->GetVersion() == JNI_VERSION_1_2);
|
||||
}
|
||||
Reference in New Issue
Block a user