diff options
author | Sergiu Deitsch <sergiu.deitsch@gmail.com> | 2022-03-18 16:59:53 (GMT) |
---|---|---|
committer | Sergiu Deitsch <sergiu.deitsch@gmail.com> | 2022-04-05 21:56:28 (GMT) |
commit | 00c4f488f2f15af08a4168fbcb59b5919d2e5565 (patch) | |
tree | c99d5a98bb35685978d02c0517fe4f5b4a2ea6c7 | |
parent | 171d45c039eac3427a7ff7c4bbccba9f1414c68d (diff) | |
download | CMake-00c4f488f2f15af08a4168fbcb59b5919d2e5565.zip CMake-00c4f488f2f15af08a4168fbcb59b5919d2e5565.tar.gz CMake-00c4f488f2f15af08a4168fbcb59b5919d2e5565.tar.bz2 |
FindJNI: support Android NDK
-rw-r--r-- | Modules/FindJNI.cmake | 156 |
1 files changed, 142 insertions, 14 deletions
diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index bd9b2e1..96f136a 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -7,9 +7,9 @@ FindJNI 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 @@ -17,7 +17,16 @@ 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`` and ``JVM``. + + 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 ^^^^^^^^^^^^^^^^ @@ -34,6 +43,11 @@ Imported Targets ``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 ^^^^^^^^^^^^^^^^ @@ -48,6 +62,23 @@ This module sets the following result variables: ``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 ^^^^^^^^^^^^^^^ @@ -61,7 +92,8 @@ The following cache variables are also available to set or use: The include path to ``jni.h``. ``JAVA_INCLUDE_PATH2`` 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. + 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``. #]=======================================================================] @@ -74,11 +106,32 @@ include(CMakePushCheckState) include(FindPackageHandleStandardArgs) if(NOT JNI_FIND_COMPONENTS) - 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) + 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} @@ -454,6 +507,14 @@ if(AWT IN_LIST JNI_FIND_COMPONENTS) ) 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) @@ -461,12 +522,26 @@ 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) set(CMAKE_FIND_FRAMEWORK ${_JNI_CMAKE_FIND_FRAMEWORK}) @@ -475,6 +550,24 @@ else() unset(CMAKE_FIND_FRAMEWORK) endif() +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) @@ -521,6 +614,17 @@ if(JNI_FOUND) 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}) @@ -538,14 +642,38 @@ if(JNI_FOUND) set_property(TARGET JNI::AWT PROPERTY INTERFACE_LINK_LIBRARIES JNI::JNI) endif() - if(JNI_JVM_FOUND) + 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) - add_library(JNI::JVM IMPORTED UNKNOWN) + 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 IMPORTED_LOCATION - ${JAVA_JVM_LIBRARY}) 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() |