diff options
author | Sergiu Deitsch <sergiu.deitsch@gmail.com> | 2022-03-13 14:56:33 (GMT) |
---|---|---|
committer | Sergiu Deitsch <sergiu.deitsch@gmail.com> | 2022-04-04 17:46:32 (GMT) |
commit | 171d45c039eac3427a7ff7c4bbccba9f1414c68d (patch) | |
tree | 0083934798f2b2f88e9f5b37f65f7098efd1f76a /Modules/FindJNI.cmake | |
parent | 35e92ec61913d5fb30cd7ca3df2e1ad8728e7e98 (diff) | |
download | CMake-171d45c039eac3427a7ff7c4bbccba9f1414c68d.zip CMake-171d45c039eac3427a7ff7c4bbccba9f1414c68d.tar.gz CMake-171d45c039eac3427a7ff7c4bbccba9f1414c68d.tar.bz2 |
FindJNI: added components and imported targets
Diffstat (limited to 'Modules/FindJNI.cmake')
-rw-r--r-- | Modules/FindJNI.cmake | 197 |
1 files changed, 172 insertions, 25 deletions
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_<component>_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 <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 +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) |