From 4b3614b25a10bf558b3687cee5bac012779866e3 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 29 Jan 2014 13:28:43 -0500 Subject: Find{Java,JNI}: Use ENV{JAVA_HOME} first when set Use list(APPEND) instead of set() to append to lists. When ENV{JAVA_HOME} is set explicitly place values based on it at the front their lists. --- Modules/FindJNI.cmake | 35 ++++++++++++++++++++--------------- Modules/FindJava.cmake | 9 +++++++-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index f1cb57e..aef28d1 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -91,22 +91,26 @@ macro(java_append_library_directories _var) endforeach() endmacro() +file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _JAVA_HOME) + +set(JAVA_AWT_LIBRARY_DIRECTORIES) +if(_JAVA_HOME) + JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES + ${_JAVA_HOME}/jre/lib/{libarch} + ${_JAVA_HOME}/jre/lib + ${_JAVA_HOME}/lib + ${_JAVA_HOME} + ) +endif() get_filename_component(java_install_version "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit;CurrentVersion]" NAME) -set(JAVA_AWT_LIBRARY_DIRECTORIES +list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/lib" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/lib" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/lib" ) - -file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _JAVA_HOME) - JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES - ${_JAVA_HOME}/jre/lib/{libarch} - ${_JAVA_HOME}/jre/lib - ${_JAVA_HOME}/lib - ${_JAVA_HOME} /usr/lib /usr/local/lib /usr/lib/jvm/java/lib @@ -135,20 +139,21 @@ JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES set(JAVA_JVM_LIBRARY_DIRECTORIES) foreach(dir ${JAVA_AWT_LIBRARY_DIRECTORIES}) - set(JAVA_JVM_LIBRARY_DIRECTORIES - ${JAVA_JVM_LIBRARY_DIRECTORIES} + list(APPEND JAVA_JVM_LIBRARY_DIRECTORIES "${dir}" "${dir}/client" "${dir}/server" ) endforeach() - -set(JAVA_AWT_INCLUDE_DIRECTORIES +set(JAVA_AWT_INCLUDE_DIRECTORIES) +if(_JAVA_HOME) + list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES ${_JAVA_HOME}/include) +endif() +list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/include" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/include" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/include" - ${_JAVA_HOME}/include /usr/include /usr/local/include /usr/lib/java/include @@ -173,7 +178,7 @@ foreach(JAVA_PROG "${JAVA_RUNTIME}" "${JAVA_COMPILE}" "${JAVA_ARCHIVE}") get_filename_component(jpath "${JAVA_PROG}" PATH) foreach(JAVA_INC_PATH ../include ../java/include ../share/java/include) if(EXISTS ${jpath}/${JAVA_INC_PATH}) - set(JAVA_AWT_INCLUDE_DIRECTORIES ${JAVA_AWT_INCLUDE_DIRECTORIES} "${jpath}/${JAVA_INC_PATH}") + list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "${jpath}/${JAVA_INC_PATH}") endif() endforeach() foreach(JAVA_LIB_PATH @@ -181,7 +186,7 @@ foreach(JAVA_PROG "${JAVA_RUNTIME}" "${JAVA_COMPILE}" "${JAVA_ARCHIVE}") ../java/lib ../java/jre/lib ../java/jre/lib/i386 ../share/java/lib ../share/java/jre/lib ../share/java/jre/lib/i386) if(EXISTS ${jpath}/${JAVA_LIB_PATH}) - set(JAVA_AWT_LIBRARY_DIRECTORIES ${JAVA_AWT_LIBRARY_DIRECTORIES} "${jpath}/${JAVA_LIB_PATH}") + list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES "${jpath}/${JAVA_LIB_PATH}") endif() endforeach() endforeach() diff --git a/Modules/FindJava.cmake b/Modules/FindJava.cmake index e35fc1d..36624a4 100644 --- a/Modules/FindJava.cmake +++ b/Modules/FindJava.cmake @@ -67,8 +67,14 @@ # (To distribute this file outside of CMake, substitute the full # License text for the above reference.) +file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _JAVA_HOME) + # The HINTS option should only be used for values computed from the system. -set(_JAVA_HINTS +set(_JAVA_HINTS) +if(_JAVA_HOME) + list(APPEND _JAVA_HINTS ${_JAVA_HOME}/bin) +endif() +list(APPEND _JAVA_HINTS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\2.0;JavaHome]/bin" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/bin" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/bin" @@ -77,7 +83,6 @@ set(_JAVA_HINTS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/bin" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/bin" "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/bin" - $ENV{JAVA_HOME}/bin ) # Hard-coded guesses should still go in PATHS. This ensures that the user # environment can always override hard guesses. -- cgit v0.12 From fcd66a7f7b944f95372386332381a11c3b8b1508 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 29 Jan 2014 13:47:51 -0500 Subject: Find{Java,JNI}: Consider JAVA_HOME before ENV{JAVA_HOME} Add a helper module CMakeFindJavaCommon shared between FindJava and FindJNI to select a JAVA_HOME value. Prefer a CMake variable or cache entry first, then an environment variable. --- Modules/CMakeFindJavaCommon.cmake | 30 ++++++++++++++++++++++++++++++ Modules/FindJNI.cmake | 7 +++++-- Modules/FindJava.cmake | 8 +++++--- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 Modules/CMakeFindJavaCommon.cmake diff --git a/Modules/CMakeFindJavaCommon.cmake b/Modules/CMakeFindJavaCommon.cmake new file mode 100644 index 0000000..fd52689 --- /dev/null +++ b/Modules/CMakeFindJavaCommon.cmake @@ -0,0 +1,30 @@ + +#============================================================================= +# Copyright 2013-2014 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# Do not include this module directly from code outside CMake! +set(_JAVA_HOME "") +if(JAVA_HOME AND IS_DIRECTORY "${JAVA_HOME}") + set(_JAVA_HOME "${JAVA_HOME}") + set(_JAVA_HOME_EXPLICIT 1) +else() + set(_ENV_JAVA_HOME "") + if(DEFINED ENV{JAVA_HOME}) + file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _ENV_JAVA_HOME) + endif() + if(_ENV_JAVA_HOME AND IS_DIRECTORY "${_ENV_JAVA_HOME}") + set(_JAVA_HOME "${_ENV_JAVA_HOME}") + set(_JAVA_HOME_EXPLICIT 1) + endif() + unset(_ENV_JAVA_HOME) +endif() diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index aef28d1..ccb5191 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -6,7 +6,10 @@ # # 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. This code sets the following variables: +# the library is. The caller may set variable JAVA_HOME to specify a +# Java installation prefix explicitly. +# +# This module sets the following result variables: # # :: # @@ -91,7 +94,7 @@ macro(java_append_library_directories _var) endforeach() endmacro() -file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _JAVA_HOME) +include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake) set(JAVA_AWT_LIBRARY_DIRECTORIES) if(_JAVA_HOME) diff --git a/Modules/FindJava.cmake b/Modules/FindJava.cmake index 36624a4..a488c46 100644 --- a/Modules/FindJava.cmake +++ b/Modules/FindJava.cmake @@ -5,8 +5,10 @@ # Find Java # # This module finds if Java is installed and determines where the -# include files and libraries are. This code sets the following -# variables: +# include files and libraries are. The caller may set variable JAVA_HOME +# to specify a Java installation prefix explicitly. +# +# This module sets the following result variables: # # :: # @@ -67,7 +69,7 @@ # (To distribute this file outside of CMake, substitute the full # License text for the above reference.) -file(TO_CMAKE_PATH "$ENV{JAVA_HOME}" _JAVA_HOME) +include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake) # The HINTS option should only be used for values computed from the system. set(_JAVA_HINTS) -- cgit v0.12 From 70560ee1466ffc73878afa7ad34937bf154903f6 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 29 Jan 2014 13:51:01 -0500 Subject: FindJNI: Overhaul finding JDK libraries and headers on OS X Drop the hard-coded framework paths and "-framework JavaJM" on APPLE. Save and restore the value of CMAKE_FIND_FRAMEWORK. Use its value to choose and order between a normal search for jvm&jawt and a framework search for JavaVM. When JAVA_HOME is set explicitly by the user then force a normal search by setting CMAKE_FIND_FRAMEWORK to NEVER. Once the library is found, set CMAKE_FIND_FRAMEWORK to help find_path locate the matching headers (framework v. normal). --- Modules/FindJNI.cmake | 92 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake index ccb5191..669e3e2 100644 --- a/Modules/FindJNI.cmake +++ b/Modules/FindJNI.cmake @@ -96,6 +96,17 @@ endmacro() include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake) +# Save CMAKE_FIND_FRAMEWORK +if(DEFINED CMAKE_FIND_FRAMEWORK) + set(_JNI_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK}) +else() + unset(_JNI_CMAKE_FIND_FRAMEWORK) +endif() + +if(_JAVA_HOME_EXPLICIT) + set(CMAKE_FIND_FRAMEWORK NEVER) +endif() + set(JAVA_AWT_LIBRARY_DIRECTORIES) if(_JAVA_HOME) JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES @@ -195,48 +206,49 @@ foreach(JAVA_PROG "${JAVA_RUNTIME}" "${JAVA_COMPILE}" "${JAVA_ARCHIVE}") endforeach() if(APPLE) - if(EXISTS ~/Library/Frameworks/JavaVM.framework) - set(JAVA_HAVE_FRAMEWORK 1) - endif() - if(EXISTS /Library/Frameworks/JavaVM.framework) - set(JAVA_HAVE_FRAMEWORK 1) + if(CMAKE_FIND_FRAMEWORK STREQUAL "ONLY") + set(_JNI_SEARCHES FRAMEWORK) + elseif(CMAKE_FIND_FRAMEWORK STREQUAL "NEVER") + set(_JNI_SEARCHES NORMAL) + elseif(CMAKE_FIND_FRAMEWORK STREQUAL "LAST") + set(_JNI_SEARCHES NORMAL FRAMEWORK) + else() + set(_JNI_SEARCHES FRAMEWORK NORMAL) endif() - if(EXISTS /System/Library/Frameworks/JavaVM.framework) - set(JAVA_HAVE_FRAMEWORK 1) - endif() - - if(JAVA_HAVE_FRAMEWORK) - if(NOT JAVA_AWT_LIBRARY) - set (JAVA_AWT_LIBRARY "-framework JavaVM" CACHE FILEPATH "Java Frameworks" FORCE) - endif() + set(_JNI_FRAMEWORK_JVM NAMES JavaVM) + set(_JNI_FRAMEWORK_JAWT "${_JNI_FRAMEWORK_JVM}") +else() + set(_JNI_SEARCHES NORMAL) +endif() - if(NOT JAVA_JVM_LIBRARY) - set (JAVA_JVM_LIBRARY "-framework JavaVM" CACHE FILEPATH "Java Frameworks" FORCE) - endif() +set(_JNI_NORMAL_JVM + NAMES jvm + PATHS ${JAVA_JVM_LIBRARY_DIRECTORIES} + ) - if(NOT JAVA_AWT_INCLUDE_PATH) - if(EXISTS /System/Library/Frameworks/JavaVM.framework/Headers/jawt.h) - set (JAVA_AWT_INCLUDE_PATH "/System/Library/Frameworks/JavaVM.framework/Headers" CACHE FILEPATH "jawt.h location" FORCE) - endif() - endif() +set(_JNI_NORMAL_JAWT + NAMES jawt + PATHS ${JAVA_AWT_LIBRARY_DIRECTORIES} + ) - # If using "-framework JavaVM", prefer its headers *before* the others in - # JAVA_AWT_INCLUDE_DIRECTORIES... (*prepend* to the list here) - # - set(JAVA_AWT_INCLUDE_DIRECTORIES - ~/Library/Frameworks/JavaVM.framework/Headers - /Library/Frameworks/JavaVM.framework/Headers - /System/Library/Frameworks/JavaVM.framework/Headers - ${JAVA_AWT_INCLUDE_DIRECTORIES} - ) +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() endif() +endforeach() +unset(_JNI_SEARCHES) +unset(_JNI_FRAMEWORK_JVM) +unset(_JNI_FRAMEWORK_JAWT) +unset(_JNI_NORMAL_JVM) +unset(_JNI_NORMAL_JAWT) + +# Find headers matching the library. +if("${JAVA_JVM_LIBRARY};${JAVA_AWT_LIBRARY};" MATCHES "(/JavaVM.framework|-framework JavaVM);") + set(CMAKE_FIND_FRAMEWORK ONLY) else() - find_library(JAVA_AWT_LIBRARY jawt - PATHS ${JAVA_AWT_LIBRARY_DIRECTORIES} - ) - find_library(JAVA_JVM_LIBRARY NAMES jvm JavaVM - PATHS ${JAVA_JVM_LIBRARY_DIRECTORIES} - ) + set(CMAKE_FIND_FRAMEWORK NEVER) endif() # add in the include path @@ -260,6 +272,14 @@ find_path(JAVA_AWT_INCLUDE_PATH jawt.h ${JAVA_INCLUDE_PATH} ) +# Restore CMAKE_FIND_FRAMEWORK +if(DEFINED _JNI_CMAKE_FIND_FRAMEWORK) + set(CMAKE_FIND_FRAMEWORK ${_JNI_CMAKE_FIND_FRAMEWORK}) + unset(_JNI_CMAKE_FIND_FRAMEWORK) +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) -- cgit v0.12 From dd72b19f861d6c39796fdf9950f585f0368348a9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 29 Jan 2014 13:51:32 -0500 Subject: Find{Java,JNI}: Use /usr/libexec/java_home on OS X If no JAVA_HOME or ENV{JAVA_HOME} is set then try running this tool to get the system default Java home. Suggested-by: Larry Shaffer --- Modules/CMakeFindJavaCommon.cmake | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Modules/CMakeFindJavaCommon.cmake b/Modules/CMakeFindJavaCommon.cmake index fd52689..fcf0389 100644 --- a/Modules/CMakeFindJavaCommon.cmake +++ b/Modules/CMakeFindJavaCommon.cmake @@ -25,6 +25,17 @@ else() if(_ENV_JAVA_HOME AND IS_DIRECTORY "${_ENV_JAVA_HOME}") set(_JAVA_HOME "${_ENV_JAVA_HOME}") set(_JAVA_HOME_EXPLICIT 1) + else() + set(_CMD_JAVA_HOME "") + if(APPLE AND EXISTS /usr/libexec/java_home) + execute_process(COMMAND /usr/libexec/java_home + OUTPUT_VARIABLE _CMD_JAVA_HOME OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + if(_CMD_JAVA_HOME AND IS_DIRECTORY "${_CMD_JAVA_HOME}") + set(_JAVA_HOME "${_CMD_JAVA_HOME}") + set(_JAVA_HOME_EXPLICIT 0) + endif() + unset(_CMD_JAVA_HOME) endif() unset(_ENV_JAVA_HOME) endif() -- cgit v0.12