FindJNI.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #.rst:
  2. # FindJNI
  3. # -------
  4. #
  5. # Find JNI java libraries.
  6. #
  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. # the library is. The caller may set variable JAVA_HOME to specify a
  10. # Java installation prefix explicitly.
  11. #
  12. # This module sets the following result variables:
  13. #
  14. # ::
  15. #
  16. # JNI_INCLUDE_DIRS = the include dirs to use
  17. # JNI_LIBRARIES = the libraries to use
  18. # JNI_FOUND = TRUE if JNI headers and libraries were found.
  19. # JAVA_AWT_LIBRARY = the path to the jawt library
  20. # JAVA_JVM_LIBRARY = the path to the jvm library
  21. # JAVA_INCLUDE_PATH = the include path to jni.h
  22. # JAVA_INCLUDE_PATH2 = the include path to jni_md.h
  23. # JAVA_AWT_INCLUDE_PATH = the include path to jawt.h
  24. #=============================================================================
  25. # Copyright 2001-2009 Kitware, Inc.
  26. #
  27. # Distributed under the OSI-approved BSD License (the "License");
  28. # see accompanying file Copyright.txt for details.
  29. #
  30. # This software is distributed WITHOUT ANY WARRANTY; without even the
  31. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. # See the License for more information.
  33. #=============================================================================
  34. # (To distribute this file outside of CMake, substitute the full
  35. # License text for the above reference.)
  36. # Expand {libarch} occurences to java_libarch subdirectory(-ies) and set ${_var}
  37. macro(java_append_library_directories _var)
  38. # Determine java arch-specific library subdir
  39. # Mostly based on openjdk/jdk/make/common/shared/Platform.gmk as of openjdk
  40. # 1.6.0_18 + icedtea patches. However, it would be much better to base the
  41. # guess on the first part of the GNU config.guess platform triplet.
  42. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  43. if(CMAKE_LIBRARY_ARCHITECTURE STREQUAL "x86_64-linux-gnux32")
  44. set(_java_libarch "x32" "amd64" "i386")
  45. else()
  46. set(_java_libarch "amd64" "i386")
  47. endif()
  48. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
  49. set(_java_libarch "i386")
  50. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^alpha")
  51. set(_java_libarch "alpha")
  52. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
  53. # Subdir is "arm" for both big-endian (arm) and little-endian (armel).
  54. set(_java_libarch "arm")
  55. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
  56. # mips* machines are bi-endian mostly so processor does not tell
  57. # endianess of the underlying system.
  58. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "mips" "mipsel" "mipseb" "mips64" "mips64el" "mipsn32" "mipsn32el")
  59. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64le")
  60. set(_java_libarch "ppc64" "ppc64le")
  61. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64")
  62. set(_java_libarch "ppc64" "ppc")
  63. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
  64. set(_java_libarch "ppc" "ppc64")
  65. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sparc")
  66. # Both flavours can run on the same processor
  67. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "sparc" "sparcv9")
  68. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(parisc|hppa)")
  69. set(_java_libarch "parisc" "parisc64")
  70. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^s390")
  71. # s390 binaries can run on s390x machines
  72. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "s390" "s390x")
  73. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sh")
  74. set(_java_libarch "sh")
  75. else()
  76. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}")
  77. endif()
  78. # Append default list architectures if CMAKE_SYSTEM_PROCESSOR was empty or
  79. # system is non-Linux (where the code above has not been well tested)
  80. if(NOT _java_libarch OR NOT (CMAKE_SYSTEM_NAME MATCHES "Linux"))
  81. list(APPEND _java_libarch "i386" "amd64" "ppc")
  82. endif()
  83. # Sometimes ${CMAKE_SYSTEM_PROCESSOR} is added to the list to prefer
  84. # current value to a hardcoded list. Remove possible duplicates.
  85. list(REMOVE_DUPLICATES _java_libarch)
  86. foreach(_path ${ARGN})
  87. if(_path MATCHES "{libarch}")
  88. foreach(_libarch ${_java_libarch})
  89. string(REPLACE "{libarch}" "${_libarch}" _newpath "${_path}")
  90. if(EXISTS ${_newpath})
  91. list(APPEND ${_var} "${_newpath}")
  92. endif()
  93. endforeach()
  94. else()
  95. if(EXISTS ${_path})
  96. list(APPEND ${_var} "${_path}")
  97. endif()
  98. endif()
  99. endforeach()
  100. endmacro()
  101. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
  102. # Save CMAKE_FIND_FRAMEWORK
  103. if(DEFINED CMAKE_FIND_FRAMEWORK)
  104. set(_JNI_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
  105. else()
  106. unset(_JNI_CMAKE_FIND_FRAMEWORK)
  107. endif()
  108. if(_JAVA_HOME_EXPLICIT)
  109. set(CMAKE_FIND_FRAMEWORK NEVER)
  110. endif()
  111. set(JAVA_AWT_LIBRARY_DIRECTORIES)
  112. if(_JAVA_HOME)
  113. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES
  114. ${_JAVA_HOME}/jre/lib/{libarch}
  115. ${_JAVA_HOME}/jre/lib
  116. ${_JAVA_HOME}/lib/{libarch}
  117. ${_JAVA_HOME}/lib
  118. ${_JAVA_HOME}
  119. )
  120. endif()
  121. get_filename_component(java_install_version
  122. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit;CurrentVersion]" NAME)
  123. list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES
  124. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/lib"
  125. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/lib"
  126. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/lib"
  127. )
  128. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES
  129. /usr/lib
  130. /usr/local/lib
  131. /usr/lib/jvm/java/lib
  132. /usr/lib/java/jre/lib/{libarch}
  133. /usr/lib/jvm/jre/lib/{libarch}
  134. /usr/local/lib/java/jre/lib/{libarch}
  135. /usr/local/share/java/jre/lib/{libarch}
  136. /usr/lib/j2sdk1.4-sun/jre/lib/{libarch}
  137. /usr/lib/j2sdk1.5-sun/jre/lib/{libarch}
  138. /opt/sun-jdk-1.5.0.04/jre/lib/{libarch}
  139. /usr/lib/jvm/java-6-sun/jre/lib/{libarch}
  140. /usr/lib/jvm/java-1.5.0-sun/jre/lib/{libarch}
  141. /usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/{libarch} # can this one be removed according to #8821 ? Alex
  142. /usr/lib/jvm/java-6-openjdk/jre/lib/{libarch}
  143. /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/{libarch} # fedora
  144. # Debian specific paths for default JVM
  145. /usr/lib/jvm/default-java/jre/lib/{libarch}
  146. /usr/lib/jvm/default-java/jre/lib
  147. /usr/lib/jvm/default-java/lib
  148. # Ubuntu specific paths for default JVM
  149. /usr/lib/jvm/java-8-openjdk-{libarch}/jre/lib/{libarch} # Ubuntu 15.10
  150. /usr/lib/jvm/java-7-openjdk-{libarch}/jre/lib/{libarch} # Ubuntu 15.10
  151. /usr/lib/jvm/java-6-openjdk-{libarch}/jre/lib/{libarch} # Ubuntu 15.10
  152. # OpenBSD specific paths for default JVM
  153. /usr/local/jdk-1.7.0/jre/lib/{libarch}
  154. /usr/local/jre-1.7.0/lib/{libarch}
  155. /usr/local/jdk-1.6.0/jre/lib/{libarch}
  156. /usr/local/jre-1.6.0/lib/{libarch}
  157. # SuSE specific paths for default JVM
  158. /usr/lib64/jvm/java/jre/lib/{libarch}
  159. /usr/lib64/jvm/jre/lib/{libarch}
  160. )
  161. set(JAVA_JVM_LIBRARY_DIRECTORIES)
  162. foreach(dir ${JAVA_AWT_LIBRARY_DIRECTORIES})
  163. list(APPEND JAVA_JVM_LIBRARY_DIRECTORIES
  164. "${dir}"
  165. "${dir}/client"
  166. "${dir}/server"
  167. # IBM SDK, Java Technology Edition, specific paths
  168. "${dir}/j9vm"
  169. "${dir}/default"
  170. )
  171. endforeach()
  172. set(JAVA_AWT_INCLUDE_DIRECTORIES)
  173. if(_JAVA_HOME)
  174. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES ${_JAVA_HOME}/include)
  175. endif()
  176. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES
  177. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/include"
  178. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/include"
  179. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/include"
  180. )
  181. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_INCLUDE_DIRECTORIES
  182. /usr/include
  183. /usr/local/include
  184. /usr/lib/java/include
  185. /usr/local/lib/java/include
  186. /usr/lib/jvm/java/include
  187. /usr/lib/jvm/java-6-sun/include
  188. /usr/lib/jvm/java-1.5.0-sun/include
  189. /usr/lib/jvm/java-6-sun-1.6.0.00/include # can this one be removed according to #8821 ? Alex
  190. /usr/lib/jvm/java-6-openjdk/include
  191. /usr/lib/jvm/java-8-openjdk-{libarch}/include # ubuntu 15.10
  192. /usr/lib/jvm/java-7-openjdk-{libarch}/include # ubuntu 15.10
  193. /usr/lib/jvm/java-6-openjdk-{libarch}/include # ubuntu 15.10
  194. /usr/local/share/java/include
  195. /usr/lib/j2sdk1.4-sun/include
  196. /usr/lib/j2sdk1.5-sun/include
  197. /opt/sun-jdk-1.5.0.04/include
  198. # Debian specific path for default JVM
  199. /usr/lib/jvm/default-java/include
  200. # OpenBSD specific path for default JVM
  201. /usr/local/jdk-1.7.0/include
  202. /usr/local/jdk-1.6.0/include
  203. # SuSE specific paths for default JVM
  204. /usr/lib64/jvm/java/include
  205. )
  206. foreach(JAVA_PROG "${JAVA_RUNTIME}" "${JAVA_COMPILE}" "${JAVA_ARCHIVE}")
  207. get_filename_component(jpath "${JAVA_PROG}" PATH)
  208. foreach(JAVA_INC_PATH ../include ../java/include ../share/java/include)
  209. if(EXISTS ${jpath}/${JAVA_INC_PATH})
  210. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "${jpath}/${JAVA_INC_PATH}")
  211. endif()
  212. endforeach()
  213. foreach(JAVA_LIB_PATH
  214. ../lib ../jre/lib ../jre/lib/i386
  215. ../java/lib ../java/jre/lib ../java/jre/lib/i386
  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. endif()
  220. endforeach()
  221. endforeach()
  222. if(APPLE)
  223. if(CMAKE_FIND_FRAMEWORK STREQUAL "ONLY")
  224. set(_JNI_SEARCHES FRAMEWORK)
  225. elseif(CMAKE_FIND_FRAMEWORK STREQUAL "NEVER")
  226. set(_JNI_SEARCHES NORMAL)
  227. elseif(CMAKE_FIND_FRAMEWORK STREQUAL "LAST")
  228. set(_JNI_SEARCHES NORMAL FRAMEWORK)
  229. else()
  230. set(_JNI_SEARCHES FRAMEWORK NORMAL)
  231. endif()
  232. set(_JNI_FRAMEWORK_JVM NAMES JavaVM)
  233. set(_JNI_FRAMEWORK_JAWT "${_JNI_FRAMEWORK_JVM}")
  234. else()
  235. set(_JNI_SEARCHES NORMAL)
  236. endif()
  237. set(_JNI_NORMAL_JVM
  238. NAMES jvm
  239. PATHS ${JAVA_JVM_LIBRARY_DIRECTORIES}
  240. )
  241. set(_JNI_NORMAL_JAWT
  242. NAMES jawt
  243. PATHS ${JAVA_AWT_LIBRARY_DIRECTORIES}
  244. )
  245. foreach(search ${_JNI_SEARCHES})
  246. find_library(JAVA_JVM_LIBRARY ${_JNI_${search}_JVM})
  247. find_library(JAVA_AWT_LIBRARY ${_JNI_${search}_JAWT})
  248. if(JAVA_JVM_LIBRARY)
  249. break()
  250. endif()
  251. endforeach()
  252. unset(_JNI_SEARCHES)
  253. unset(_JNI_FRAMEWORK_JVM)
  254. unset(_JNI_FRAMEWORK_JAWT)
  255. unset(_JNI_NORMAL_JVM)
  256. unset(_JNI_NORMAL_JAWT)
  257. # Find headers matching the library.
  258. if("${JAVA_JVM_LIBRARY};${JAVA_AWT_LIBRARY};" MATCHES "(/JavaVM.framework|-framework JavaVM);")
  259. set(CMAKE_FIND_FRAMEWORK ONLY)
  260. else()
  261. set(CMAKE_FIND_FRAMEWORK NEVER)
  262. endif()
  263. # add in the include path
  264. find_path(JAVA_INCLUDE_PATH jni.h
  265. ${JAVA_AWT_INCLUDE_DIRECTORIES}
  266. )
  267. find_path(JAVA_INCLUDE_PATH2 NAMES jni_md.h jniport.h
  268. PATHS
  269. ${JAVA_INCLUDE_PATH}
  270. ${JAVA_INCLUDE_PATH}/darwin
  271. ${JAVA_INCLUDE_PATH}/win32
  272. ${JAVA_INCLUDE_PATH}/linux
  273. ${JAVA_INCLUDE_PATH}/freebsd
  274. ${JAVA_INCLUDE_PATH}/openbsd
  275. ${JAVA_INCLUDE_PATH}/solaris
  276. ${JAVA_INCLUDE_PATH}/hp-ux
  277. ${JAVA_INCLUDE_PATH}/alpha
  278. ${JAVA_INCLUDE_PATH}/aix
  279. )
  280. find_path(JAVA_AWT_INCLUDE_PATH jawt.h
  281. ${JAVA_INCLUDE_PATH}
  282. )
  283. # Restore CMAKE_FIND_FRAMEWORK
  284. if(DEFINED _JNI_CMAKE_FIND_FRAMEWORK)
  285. set(CMAKE_FIND_FRAMEWORK ${_JNI_CMAKE_FIND_FRAMEWORK})
  286. unset(_JNI_CMAKE_FIND_FRAMEWORK)
  287. else()
  288. unset(CMAKE_FIND_FRAMEWORK)
  289. endif()
  290. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  291. FIND_PACKAGE_HANDLE_STANDARD_ARGS(JNI DEFAULT_MSG JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY
  292. JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
  293. mark_as_advanced(
  294. JAVA_AWT_LIBRARY
  295. JAVA_JVM_LIBRARY
  296. JAVA_AWT_INCLUDE_PATH
  297. JAVA_INCLUDE_PATH
  298. JAVA_INCLUDE_PATH2
  299. )
  300. set(JNI_LIBRARIES
  301. ${JAVA_AWT_LIBRARY}
  302. ${JAVA_JVM_LIBRARY}
  303. )
  304. set(JNI_INCLUDE_DIRS
  305. ${JAVA_INCLUDE_PATH}
  306. ${JAVA_INCLUDE_PATH2}
  307. ${JAVA_AWT_INCLUDE_PATH}
  308. )