From 35e92ec61913d5fb30cd7ca3df2e1ad8728e7e98 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Tue, 15 Mar 2022 19:52:56 +0100 Subject: FindJNI: improved description --- Modules/FindJNI.cmake | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index e4f60b3..e1e5584 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -5,7 +5,7 @@ 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 @@ -22,11 +22,11 @@ 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. Cache Variables ^^^^^^^^^^^^^^^ @@ -34,15 +34,15 @@ 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 ``jni_md.h`` and ``jniport.h``. ``JAVA_AWT_INCLUDE_PATH`` - the include path to jawt.h + The include path to ``jawt.h``. #]=======================================================================] # Expand {libarch} occurrences to java_libarch subdirectory(-ies) and set ${_var} -- cgit v0.12 From 171d45c039eac3427a7ff7c4bbccba9f1414c68d Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sun, 13 Mar 2022 15:56:33 +0100 Subject: FindJNI: added components and imported targets --- .gitlab/ci/configure_debian10_aarch64_ninja.cmake | 1 + .gitlab/ci/configure_debian10_ninja.cmake | 1 + .gitlab/ci/configure_fedora35_makefiles.cmake | 1 + Help/release/dev/FindJNI-targets.rst | 4 + Modules/FindJNI.cmake | 197 +++++++++++++++++++--- Tests/CMakeLists.txt | 1 + Tests/FindJNI/AWT/AWTTgt.java | 7 + Tests/FindJNI/AWT/AWTVar.java | 7 + Tests/FindJNI/AWT/CMakeLists.txt | 31 ++++ Tests/FindJNI/AWT/awt.cxx | 23 +++ Tests/FindJNI/CMakeLists.txt | 12 ++ Tests/FindJNI/JVM/CMakeLists.txt | 31 ++++ Tests/FindJNI/JVM/JVMTgt.java | 7 + Tests/FindJNI/JVM/JVMVar.java | 7 + Tests/FindJNI/JVM/jvm.cxx | 15 ++ Tests/FindJNI/Minimal/CMakeLists.txt | 35 ++++ Tests/FindJNI/Minimal/MinimalTgt.java | 7 + Tests/FindJNI/Minimal/MinimalVar.java | 7 + Tests/FindJNI/Minimal/minimal.cxx | 38 +++++ 19 files changed, 407 insertions(+), 25 deletions(-) create mode 100644 Help/release/dev/FindJNI-targets.rst create mode 100644 Tests/FindJNI/AWT/AWTTgt.java create mode 100644 Tests/FindJNI/AWT/AWTVar.java create mode 100644 Tests/FindJNI/AWT/CMakeLists.txt create mode 100644 Tests/FindJNI/AWT/awt.cxx create mode 100644 Tests/FindJNI/CMakeLists.txt create mode 100644 Tests/FindJNI/JVM/CMakeLists.txt create mode 100644 Tests/FindJNI/JVM/JVMTgt.java create mode 100644 Tests/FindJNI/JVM/JVMVar.java create mode 100644 Tests/FindJNI/JVM/jvm.cxx create mode 100644 Tests/FindJNI/Minimal/CMakeLists.txt create mode 100644 Tests/FindJNI/Minimal/MinimalTgt.java create mode 100644 Tests/FindJNI/Minimal/MinimalVar.java create mode 100644 Tests/FindJNI/Minimal/minimal.cxx diff --git a/.gitlab/ci/configure_debian10_aarch64_ninja.cmake b/.gitlab/ci/configure_debian10_aarch64_ninja.cmake index 5431ab7..dcb7f82 100644 --- a/.gitlab/ci/configure_debian10_aarch64_ninja.cmake +++ b/.gitlab/ci/configure_debian10_aarch64_ninja.cmake @@ -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 "") diff --git a/.gitlab/ci/configure_debian10_ninja.cmake b/.gitlab/ci/configure_debian10_ninja.cmake index 8cf3835..6cf19e3 100644 --- a/.gitlab/ci/configure_debian10_ninja.cmake +++ b/.gitlab/ci/configure_debian10_ninja.cmake @@ -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 "") diff --git a/.gitlab/ci/configure_fedora35_makefiles.cmake b/.gitlab/ci/configure_fedora35_makefiles.cmake index 91ed55b..6d892d3 100644 --- a/.gitlab/ci/configure_fedora35_makefiles.cmake +++ b/.gitlab/ci/configure_fedora35_makefiles.cmake @@ -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 "") diff --git a/Help/release/dev/FindJNI-targets.rst b/Help/release/dev/FindJNI-targets.rst new file mode 100644 index 0000000..76e390f --- /dev/null +++ b/Help/release/dev/FindJNI-targets.rst @@ -0,0 +1,4 @@ +FindJNI-targets +--------------- + +* The :module:`FindJNI` module now provides imported targets. diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index e1e5584..bd9b2e1 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -16,6 +16,24 @@ 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`` and ``JVM``. + +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. + Result Variables ^^^^^^^^^^^^^^^^ @@ -27,6 +45,8 @@ This module sets the following result variables: The libraries to use (JAWT and JVM). ``JNI_FOUND`` ``TRUE`` if JNI headers and libraries were found. +``JNI__FOUND`` + .. versionadded:: 3.24 Cache Variables ^^^^^^^^^^^^^^^ @@ -40,11 +60,27 @@ The following cache variables are also available to set or use: ``JAVA_INCLUDE_PATH`` 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. ``JAVA_AWT_INCLUDE_PATH`` 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) + 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() + # 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 +364,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 +395,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 +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 +444,28 @@ 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() + +# Set found components +if(JAVA_AWT_INCLUDE_PATH AND JAVA_AWT_LIBRARY) + set(JNI_AWT_FOUND TRUE) +else() + set(JNI_AWT_FOUND FALSE) +endif() + +if(JAVA_JVM_LIBRARY) + set(JNI_JVM_FOUND TRUE) +else(JAVA_JVM_LIBRARY) + set(JNI_JVM_FOUND FALSE) +endif() # Restore CMAKE_FIND_FRAMEWORK if(DEFINED _JNI_CMAKE_FIND_FRAMEWORK) @@ -378,12 +475,17 @@ 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) +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 +495,58 @@ 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(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) + if(NOT TARGET JNI::JVM) + add_library(JNI::JVM IMPORTED UNKNOWN) + 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) + endif() +endif() + +cmake_policy(POP) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 3ce9b48..ffc30fe 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1455,6 +1455,7 @@ if(BUILD_TESTING) ICU Intl Jasper + JNI JPEG JsonCpp LAPACK diff --git a/Tests/FindJNI/AWT/AWTTgt.java b/Tests/FindJNI/AWT/AWTTgt.java new file mode 100644 index 0000000..dce37f3 --- /dev/null +++ b/Tests/FindJNI/AWT/AWTTgt.java @@ -0,0 +1,7 @@ +class AWTTgt +{ + public static void main(String[] args) + { + System.loadLibrary("AWTTgt"); + } +} diff --git a/Tests/FindJNI/AWT/AWTVar.java b/Tests/FindJNI/AWT/AWTVar.java new file mode 100644 index 0000000..a918c28 --- /dev/null +++ b/Tests/FindJNI/AWT/AWTVar.java @@ -0,0 +1,7 @@ +class AWTVar +{ + public static void main(String[] args) + { + System.loadLibrary("AWTVar"); + } +} diff --git a/Tests/FindJNI/AWT/CMakeLists.txt b/Tests/FindJNI/AWT/CMakeLists.txt new file mode 100644 index 0000000..bdf49ea --- /dev/null +++ b/Tests/FindJNI/AWT/CMakeLists.txt @@ -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=$ + ${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 () diff --git a/Tests/FindJNI/AWT/awt.cxx b/Tests/FindJNI/AWT/awt.cxx new file mode 100644 index 0000000..64e7652 --- /dev/null +++ b/Tests/FindJNI/AWT/awt.cxx @@ -0,0 +1,23 @@ +#include + +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(tmp), &awt) == JNI_FALSE) { + return -1; + } + + return JNI_VERSION_1_2; +} + +extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) +{ +} diff --git a/Tests/FindJNI/CMakeLists.txt b/Tests/FindJNI/CMakeLists.txt new file mode 100644 index 0000000..0a0f756 --- /dev/null +++ b/Tests/FindJNI/CMakeLists.txt @@ -0,0 +1,12 @@ +foreach(test IN ITEMS Minimal JVM AWT) + add_test(NAME FindJNI.${test} COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --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 $ + ) +endforeach() diff --git a/Tests/FindJNI/JVM/CMakeLists.txt b/Tests/FindJNI/JVM/CMakeLists.txt new file mode 100644 index 0000000..cead6ae --- /dev/null +++ b/Tests/FindJNI/JVM/CMakeLists.txt @@ -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=$ + ${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 () diff --git a/Tests/FindJNI/JVM/JVMTgt.java b/Tests/FindJNI/JVM/JVMTgt.java new file mode 100644 index 0000000..aa5dbb8 --- /dev/null +++ b/Tests/FindJNI/JVM/JVMTgt.java @@ -0,0 +1,7 @@ +class JVMTgt +{ + public static void main(String[] args) + { + System.loadLibrary("JVMTgt"); + } +} diff --git a/Tests/FindJNI/JVM/JVMVar.java b/Tests/FindJNI/JVM/JVMVar.java new file mode 100644 index 0000000..0e84a6f --- /dev/null +++ b/Tests/FindJNI/JVM/JVMVar.java @@ -0,0 +1,7 @@ +class JVMVar +{ + public static void main(String[] args) + { + System.loadLibrary("JVMVar"); + } +} diff --git a/Tests/FindJNI/JVM/jvm.cxx b/Tests/FindJNI/JVM/jvm.cxx new file mode 100644 index 0000000..f79ccce --- /dev/null +++ b/Tests/FindJNI/JVM/jvm.cxx @@ -0,0 +1,15 @@ +#include + +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) +{ +} diff --git a/Tests/FindJNI/Minimal/CMakeLists.txt b/Tests/FindJNI/Minimal/CMakeLists.txt new file mode 100644 index 0000000..f499c0a --- /dev/null +++ b/Tests/FindJNI/Minimal/CMakeLists.txt @@ -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=$ + ${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 () diff --git a/Tests/FindJNI/Minimal/MinimalTgt.java b/Tests/FindJNI/Minimal/MinimalTgt.java new file mode 100644 index 0000000..d9c98b5 --- /dev/null +++ b/Tests/FindJNI/Minimal/MinimalTgt.java @@ -0,0 +1,7 @@ +class MinimalTgt +{ + public static void main(String[] args) + { + System.loadLibrary("MinimalTgt"); + } +} diff --git a/Tests/FindJNI/Minimal/MinimalVar.java b/Tests/FindJNI/Minimal/MinimalVar.java new file mode 100644 index 0000000..9f05c74 --- /dev/null +++ b/Tests/FindJNI/Minimal/MinimalVar.java @@ -0,0 +1,7 @@ +class MinimalVar +{ + public static void main(String[] args) + { + System.loadLibrary("MinimalVar"); + } +} diff --git a/Tests/FindJNI/Minimal/minimal.cxx b/Tests/FindJNI/Minimal/minimal.cxx new file mode 100644 index 0000000..4423613 --- /dev/null +++ b/Tests/FindJNI/Minimal/minimal.cxx @@ -0,0 +1,38 @@ +#include + +#include + +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(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(env)->GetVersion() == JNI_VERSION_1_2); +} -- cgit v0.12 From 00c4f488f2f15af08a4168fbcb59b5919d2e5565 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Fri, 18 Mar 2022 17:59:53 +0100 Subject: FindJNI: support Android NDK --- Modules/FindJNI.cmake | 156 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file 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__FOUND`` .. versionadded:: 3.24 + ``TRUE`` if ```` 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() -- cgit v0.12