FindJNI.cmake 11 KB
Newer Older
1 2 3 4 5 6
#.rst:
# FindJNI
# -------
#
# Find JNI java libraries.
#
Ken Martin's avatar
Ken Martin committed
7
# This module finds if Java is installed and determines where the
8
# include files and libraries are.  It also determines what the name of
9 10 11 12
# the library is.  The caller may set variable JAVA_HOME to specify a
# Java installation prefix explicitly.
#
# This module sets the following result variables:
13
#
14
# ::
15
#
16 17 18 19 20 21 22 23
#   JNI_INCLUDE_DIRS      = the include dirs to use
#   JNI_LIBRARIES         = the libraries to use
#   JNI_FOUND             = TRUE if JNI headers and libraries were found.
#   JAVA_AWT_LIBRARY      = the path to the jawt library
#   JAVA_JVM_LIBRARY      = the path to the jvm library
#   JAVA_INCLUDE_PATH     = the include path to jni.h
#   JAVA_INCLUDE_PATH2    = the include path to jni_md.h
#   JAVA_AWT_INCLUDE_PATH = the include path to jawt.h
24

25 26 27 28 29 30 31 32 33 34
#=============================================================================
# Copyright 2001-2009 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.
#=============================================================================
35
# (To distribute this file outside of CMake, substitute the full
36 37
#  License text for the above reference.)

38
# Expand {libarch} occurences to java_libarch subdirectory(-ies) and set ${_var}
39
macro(java_append_library_directories _var)
40
    # Determine java arch-specific library subdir
41 42 43
    # Mostly based on openjdk/jdk/make/common/shared/Platform.gmk as of openjdk
    # 1.6.0_18 + icedtea patches. However, it would be much better to base the
    # guess on the first part of the GNU config.guess platform triplet.
44
    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
45 46 47
      if(CMAKE_LIBRARY_ARCHITECTURE STREQUAL "x86_64-linux-gnux32")
        set(_java_libarch "x32" "amd64" "i386")
      else()
48
        set(_java_libarch "amd64" "i386")
49
      endif()
50 51 52 53 54
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
        set(_java_libarch "i386")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^alpha")
        set(_java_libarch "alpha")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
55
        # Subdir is "arm" for both big-endian (arm) and little-endian (armel).
56 57
        set(_java_libarch "arm")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
58 59
        # mips* machines are bi-endian mostly so processor does not tell
        # endianess of the underlying system.
60
        set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "mips" "mipsel" "mipseb" "mips64" "mips64el" "mipsn32" "mipsn32el")
61
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64le")
62
        set(_java_libarch "ppc64" "ppc64le")
63
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64")
64
        set(_java_libarch "ppc64" "ppc")
65
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
66
        set(_java_libarch "ppc" "ppc64")
67
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sparc")
68
        # Both flavours can run on the same processor
69 70 71 72
        set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "sparc" "sparcv9")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(parisc|hppa)")
        set(_java_libarch "parisc" "parisc64")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^s390")
73
        # s390 binaries can run on s390x machines
74 75 76
        set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "s390" "s390x")
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sh")
        set(_java_libarch "sh")
77
    else()
78
        set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}")
79
    endif()
80 81 82

    # Append default list architectures if CMAKE_SYSTEM_PROCESSOR was empty or
    # system is non-Linux (where the code above has not been well tested)
83 84
    if(NOT _java_libarch OR NOT (CMAKE_SYSTEM_NAME MATCHES "Linux"))
        list(APPEND _java_libarch "i386" "amd64" "ppc")
85
    endif()
86 87 88

    # Sometimes ${CMAKE_SYSTEM_PROCESSOR} is added to the list to prefer
    # current value to a hardcoded list. Remove possible duplicates.
89
    list(REMOVE_DUPLICATES _java_libarch)
90

91 92 93 94 95
    foreach(_path ${ARGN})
        if(_path MATCHES "{libarch}")
            foreach(_libarch ${_java_libarch})
                string(REPLACE "{libarch}" "${_libarch}" _newpath "${_path}")
                list(APPEND ${_var} "${_newpath}")
96 97
            endforeach()
        else()
98
            list(APPEND ${_var} "${_path}")
99 100 101
        endif()
    endforeach()
endmacro()
102

103
include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
104

105 106 107 108 109 110 111 112 113 114 115
# 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()

116 117 118 119 120
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
121
    ${_JAVA_HOME}/lib/{libarch}
122 123 124 125
    ${_JAVA_HOME}/lib
    ${_JAVA_HOME}
    )
endif()
126
get_filename_component(java_install_version
127
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit;CurrentVersion]" NAME)
128

129
list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES
130 131
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/lib"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/lib"
132
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/lib"
133 134
  )
JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES
135 136
  /usr/lib
  /usr/local/lib
137
  /usr/lib/jvm/java/lib
138
  /usr/lib/java/jre/lib/{libarch}
139
  /usr/lib/jvm/jre/lib/{libarch}
140 141 142 143 144 145 146 147
  /usr/local/lib/java/jre/lib/{libarch}
  /usr/local/share/java/jre/lib/{libarch}
  /usr/lib/j2sdk1.4-sun/jre/lib/{libarch}
  /usr/lib/j2sdk1.5-sun/jre/lib/{libarch}
  /opt/sun-jdk-1.5.0.04/jre/lib/{libarch}
  /usr/lib/jvm/java-6-sun/jre/lib/{libarch}
  /usr/lib/jvm/java-1.5.0-sun/jre/lib/{libarch}
  /usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/{libarch}       # can this one be removed according to #8821 ? Alex
148
  /usr/lib/jvm/java-6-openjdk/jre/lib/{libarch}
149
  /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/{libarch}        # fedora
150 151 152 153
  # Debian specific paths for default JVM
  /usr/lib/jvm/default-java/jre/lib/{libarch}
  /usr/lib/jvm/default-java/jre/lib
  /usr/lib/jvm/default-java/lib
154 155 156 157 158
  # OpenBSD specific paths for default JVM
  /usr/local/jdk-1.7.0/jre/lib/{libarch}
  /usr/local/jre-1.7.0/lib/{libarch}
  /usr/local/jdk-1.6.0/jre/lib/{libarch}
  /usr/local/jre-1.6.0/lib/{libarch}
159 160 161
  # SuSE specific paths for default JVM
  /usr/lib64/jvm/java/jre/lib/{libarch}
  /usr/lib64/jvm/jre/lib/{libarch}
162
  )
163

164 165
set(JAVA_JVM_LIBRARY_DIRECTORIES)
foreach(dir ${JAVA_AWT_LIBRARY_DIRECTORIES})
166
  list(APPEND JAVA_JVM_LIBRARY_DIRECTORIES
Andy Cedilnik's avatar
Andy Cedilnik committed
167 168 169
    "${dir}"
    "${dir}/client"
    "${dir}/server"
170 171 172
    # IBM SDK, Java Technology Edition, specific paths
    "${dir}/j9vm"
    "${dir}/default"
Andy Cedilnik's avatar
Andy Cedilnik committed
173
    )
174
endforeach()
Andy Cedilnik's avatar
Andy Cedilnik committed
175

176 177 178 179 180
set(JAVA_AWT_INCLUDE_DIRECTORIES)
if(_JAVA_HOME)
  list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES ${_JAVA_HOME}/include)
endif()
list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES
181 182
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/include"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/include"
183
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/include"
184
  /usr/include
Ken Martin's avatar
Ken Martin committed
185
  /usr/local/include
Andy Cedilnik's avatar
Andy Cedilnik committed
186
  /usr/lib/java/include
187
  /usr/local/lib/java/include
188
  /usr/lib/jvm/java/include
189 190 191
  /usr/lib/jvm/java-6-sun/include
  /usr/lib/jvm/java-1.5.0-sun/include
  /usr/lib/jvm/java-6-sun-1.6.0.00/include       # can this one be removed according to #8821 ? Alex
192
  /usr/lib/jvm/java-6-openjdk/include
Andy Cedilnik's avatar
Andy Cedilnik committed
193
  /usr/local/share/java/include
Bill Hoffman's avatar
Bill Hoffman committed
194
  /usr/lib/j2sdk1.4-sun/include
Bill Hoffman's avatar
Bill Hoffman committed
195
  /usr/lib/j2sdk1.5-sun/include
Bill Hoffman's avatar
Bill Hoffman committed
196
  /opt/sun-jdk-1.5.0.04/include
197 198
  # Debian specific path for default JVM
  /usr/lib/jvm/default-java/include
199 200 201
  # OpenBSD specific path for default JVM
  /usr/local/jdk-1.7.0/include
  /usr/local/jdk-1.6.0/include
202 203
  # SuSE specific paths for default JVM
  /usr/lib64/jvm/java/include
204 205
  )

206 207 208 209
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})
210
      list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "${jpath}/${JAVA_INC_PATH}")
211 212
    endif()
  endforeach()
213
  foreach(JAVA_LIB_PATH
214 215
    ../lib ../jre/lib ../jre/lib/i386
    ../java/lib ../java/jre/lib ../java/jre/lib/i386
Andy Cedilnik's avatar
Andy Cedilnik committed
216
    ../share/java/lib ../share/java/jre/lib ../share/java/jre/lib/i386)
217
    if(EXISTS ${jpath}/${JAVA_LIB_PATH})
218
      list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES "${jpath}/${JAVA_LIB_PATH}")
219 220 221
    endif()
  endforeach()
endforeach()
Andy Cedilnik's avatar
Andy Cedilnik committed
222

223
if(APPLE)
224 225 226 227 228 229 230 231
  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)
232
  endif()
233 234 235 236 237
  set(_JNI_FRAMEWORK_JVM NAMES JavaVM)
  set(_JNI_FRAMEWORK_JAWT "${_JNI_FRAMEWORK_JVM}")
else()
  set(_JNI_SEARCHES NORMAL)
endif()
Bill Hoffman's avatar
Bill Hoffman committed
238

239 240 241 242
set(_JNI_NORMAL_JVM
  NAMES jvm
  PATHS ${JAVA_JVM_LIBRARY_DIRECTORIES}
  )
Bill Hoffman's avatar
Bill Hoffman committed
243

244 245 246 247
set(_JNI_NORMAL_JAWT
  NAMES jawt
  PATHS ${JAVA_AWT_LIBRARY_DIRECTORIES}
  )
Bill Hoffman's avatar
Bill Hoffman committed
248

249 250 251 252 253
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()
254
  endif()
255 256 257 258 259 260 261 262 263 264
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)
265
else()
266
  set(CMAKE_FIND_FRAMEWORK NEVER)
267
endif()
268

269
# add in the include path
270
find_path(JAVA_INCLUDE_PATH jni.h
271
  ${JAVA_AWT_INCLUDE_DIRECTORIES}
Ken Martin's avatar
Ken Martin committed
272 273
)

274 275
find_path(JAVA_INCLUDE_PATH2 NAMES jni_md.h jniport.h
  PATHS
276
  ${JAVA_INCLUDE_PATH}
277
  ${JAVA_INCLUDE_PATH}/darwin
Ken Martin's avatar
Ken Martin committed
278 279
  ${JAVA_INCLUDE_PATH}/win32
  ${JAVA_INCLUDE_PATH}/linux
280
  ${JAVA_INCLUDE_PATH}/freebsd
281
  ${JAVA_INCLUDE_PATH}/openbsd
282
  ${JAVA_INCLUDE_PATH}/solaris
283 284
  ${JAVA_INCLUDE_PATH}/hp-ux
  ${JAVA_INCLUDE_PATH}/alpha
285
  ${JAVA_INCLUDE_PATH}/aix
Ken Martin's avatar
Ken Martin committed
286
)
Ken Martin's avatar
Ken Martin committed
287

288
find_path(JAVA_AWT_INCLUDE_PATH jawt.h
Bill Hoffman's avatar
Bill Hoffman committed
289 290
  ${JAVA_INCLUDE_PATH}
)
291

292 293 294 295 296 297 298 299
# 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()

300
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
301 302 303
FIND_PACKAGE_HANDLE_STANDARD_ARGS(JNI  DEFAULT_MSG  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY
                                                    JAVA_INCLUDE_PATH  JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)

304
mark_as_advanced(
305
  JAVA_AWT_LIBRARY
Andy Cedilnik's avatar
Andy Cedilnik committed
306
  JAVA_JVM_LIBRARY
307 308 309 310
  JAVA_AWT_INCLUDE_PATH
  JAVA_INCLUDE_PATH
  JAVA_INCLUDE_PATH2
)
311

312
set(JNI_LIBRARIES
313 314 315 316
  ${JAVA_AWT_LIBRARY}
  ${JAVA_JVM_LIBRARY}
)

317
set(JNI_INCLUDE_DIRS
318 319 320 321 322
  ${JAVA_INCLUDE_PATH}
  ${JAVA_INCLUDE_PATH2}
  ${JAVA_AWT_INCLUDE_PATH}
)