FindJNI.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindJNI
  5. -------
  6. Find Java Native Interface (JNI) libraries.
  7. JNI enables Java code running in a Java Virtual Machine (JVM) to call
  8. and be called by native applications and libraries written in other
  9. languages such as C, C++.
  10. This module finds if Java is installed and determines where the
  11. include files and libraries are. It also determines what the name of
  12. the library is. The caller may set variable ``JAVA_HOME`` to specify a
  13. Java installation prefix explicitly.
  14. Result Variables
  15. ^^^^^^^^^^^^^^^^
  16. This module sets the following result variables:
  17. ``JNI_INCLUDE_DIRS``
  18. the include dirs to use
  19. ``JNI_LIBRARIES``
  20. the libraries to use (JAWT and JVM)
  21. ``JNI_FOUND``
  22. TRUE if JNI headers and libraries were found.
  23. Cache Variables
  24. ^^^^^^^^^^^^^^^
  25. The following cache variables are also available to set or use:
  26. ``JAVA_AWT_LIBRARY``
  27. the path to the Java AWT Native Interface (JAWT) library
  28. ``JAVA_JVM_LIBRARY``
  29. the path to the Java Virtual Machine (JVM) library
  30. ``JAVA_INCLUDE_PATH``
  31. the include path to jni.h
  32. ``JAVA_INCLUDE_PATH2``
  33. the include path to jni_md.h and jniport.h
  34. ``JAVA_AWT_INCLUDE_PATH``
  35. the include path to jawt.h
  36. #]=======================================================================]
  37. # Expand {libarch} occurrences to java_libarch subdirectory(-ies) and set ${_var}
  38. macro(java_append_library_directories _var)
  39. # Determine java arch-specific library subdir
  40. # Mostly based on openjdk/jdk/make/common/shared/Platform.gmk as of openjdk
  41. # 1.6.0_18 + icedtea patches. However, it would be much better to base the
  42. # guess on the first part of the GNU config.guess platform triplet.
  43. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  44. if(CMAKE_LIBRARY_ARCHITECTURE STREQUAL "x86_64-linux-gnux32")
  45. set(_java_libarch "x32" "amd64" "i386")
  46. else()
  47. set(_java_libarch "amd64" "i386")
  48. endif()
  49. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
  50. set(_java_libarch "i386")
  51. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^alpha")
  52. set(_java_libarch "alpha")
  53. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
  54. # Subdir is "arm" for both big-endian (arm) and little-endian (armel).
  55. set(_java_libarch "arm" "aarch32")
  56. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
  57. # mips* machines are bi-endian mostly so processor does not tell
  58. # endianness of the underlying system.
  59. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}"
  60. "mips" "mipsel" "mipseb" "mipsr6" "mipsr6el"
  61. "mips64" "mips64el" "mips64r6" "mips64r6el"
  62. "mipsn32" "mipsn32el" "mipsn32r6" "mipsn32r6el")
  63. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64le")
  64. set(_java_libarch "ppc64" "ppc64le")
  65. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64")
  66. set(_java_libarch "ppc64" "ppc")
  67. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
  68. set(_java_libarch "ppc" "ppc64")
  69. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sparc")
  70. # Both flavours can run on the same processor
  71. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "sparc" "sparcv9")
  72. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(parisc|hppa)")
  73. set(_java_libarch "parisc" "parisc64")
  74. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^s390")
  75. # s390 binaries can run on s390x machines
  76. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}" "s390" "s390x")
  77. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sh")
  78. set(_java_libarch "sh")
  79. else()
  80. set(_java_libarch "${CMAKE_SYSTEM_PROCESSOR}")
  81. endif()
  82. # Append default list architectures if CMAKE_SYSTEM_PROCESSOR was empty or
  83. # system is non-Linux (where the code above has not been well tested)
  84. if(NOT _java_libarch OR NOT (CMAKE_SYSTEM_NAME MATCHES "Linux"))
  85. list(APPEND _java_libarch "i386" "amd64" "ppc")
  86. endif()
  87. # Sometimes ${CMAKE_SYSTEM_PROCESSOR} is added to the list to prefer
  88. # current value to a hardcoded list. Remove possible duplicates.
  89. list(REMOVE_DUPLICATES _java_libarch)
  90. foreach(_path ${ARGN})
  91. if(_path MATCHES "{libarch}")
  92. foreach(_libarch ${_java_libarch})
  93. string(REPLACE "{libarch}" "${_libarch}" _newpath "${_path}")
  94. if(EXISTS ${_newpath})
  95. list(APPEND ${_var} "${_newpath}")
  96. endif()
  97. endforeach()
  98. else()
  99. if(EXISTS ${_path})
  100. list(APPEND ${_var} "${_path}")
  101. endif()
  102. endif()
  103. endforeach()
  104. endmacro()
  105. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
  106. # Save CMAKE_FIND_FRAMEWORK
  107. if(DEFINED CMAKE_FIND_FRAMEWORK)
  108. set(_JNI_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
  109. else()
  110. unset(_JNI_CMAKE_FIND_FRAMEWORK)
  111. endif()
  112. if(_JAVA_HOME_EXPLICIT)
  113. set(CMAKE_FIND_FRAMEWORK NEVER)
  114. endif()
  115. set(JAVA_AWT_LIBRARY_DIRECTORIES)
  116. if(_JAVA_HOME)
  117. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES
  118. ${_JAVA_HOME}/jre/lib/{libarch}
  119. ${_JAVA_HOME}/jre/lib
  120. ${_JAVA_HOME}/lib/{libarch}
  121. ${_JAVA_HOME}/lib
  122. ${_JAVA_HOME}
  123. )
  124. endif()
  125. if (WIN32)
  126. set (_JNI_HINTS)
  127. execute_process(COMMAND REG QUERY HKLM\\SOFTWARE\\JavaSoft\\JDK
  128. RESULT_VARIABLE _JNI_RESULT
  129. OUTPUT_VARIABLE _JNI_VERSIONS
  130. ERROR_QUIET)
  131. if (NOT _JNI_RESULT)
  132. string (REGEX MATCHALL "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\JavaSoft\\\\JDK\\\\[0-9.]+" _JNI_VERSIONS "${_JNI_VERSIONS}")
  133. if (_JNI_VERSIONS)
  134. # sort versions. Most recent first
  135. ## handle version 9 apart from other versions to get correct ordering
  136. set (_JNI_V9 ${_JNI_VERSIONS})
  137. list (FILTER _JNI_VERSIONS EXCLUDE REGEX "JDK\\\\9")
  138. list (SORT _JNI_VERSIONS)
  139. list (REVERSE _JNI_VERSIONS)
  140. list (FILTER _JNI_V9 INCLUDE REGEX "JDK\\\\9")
  141. list (SORT _JNI_V9)
  142. list (REVERSE _JNI_V9)
  143. list (APPEND _JNI_VERSIONS ${_JNI_V9})
  144. foreach (_JNI_HINT IN LISTS _JNI_VERSIONS)
  145. list(APPEND _JNI_HINTS "[${_JNI_HINT};JavaHome]")
  146. endforeach()
  147. endif()
  148. endif()
  149. foreach (_JNI_HINT IN LISTS _JNI_HINTS)
  150. list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES "${_JNI_HINT}/lib")
  151. endforeach()
  152. get_filename_component(java_install_version
  153. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit;CurrentVersion]" NAME)
  154. list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES
  155. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/lib"
  156. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/lib"
  157. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/lib"
  158. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/lib"
  159. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/lib"
  160. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/lib"
  161. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/lib"
  162. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/lib"
  163. )
  164. endif()
  165. set(_JNI_JAVA_DIRECTORIES_BASE
  166. /usr/lib/jvm/java
  167. /usr/lib/java
  168. /usr/lib/jvm
  169. /usr/local/lib/java
  170. /usr/local/share/java
  171. /usr/lib/j2sdk1.4-sun
  172. /usr/lib/j2sdk1.5-sun
  173. /opt/sun-jdk-1.5.0.04
  174. /usr/lib/jvm/java-6-sun
  175. /usr/lib/jvm/java-1.5.0-sun
  176. /usr/lib/jvm/java-6-sun-1.6.0.00 # can this one be removed according to #8821 ? Alex
  177. /usr/lib/jvm/java-6-openjdk
  178. /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0 # fedora
  179. # Debian specific paths for default JVM
  180. /usr/lib/jvm/default-java
  181. # Arch Linux specific paths for default JVM
  182. /usr/lib/jvm/default
  183. # Ubuntu specific paths for default JVM
  184. /usr/lib/jvm/java-11-openjdk-{libarch} # Ubuntu 18.04 LTS
  185. /usr/lib/jvm/java-8-openjdk-{libarch} # Ubuntu 15.10
  186. /usr/lib/jvm/java-7-openjdk-{libarch} # Ubuntu 15.10
  187. /usr/lib/jvm/java-6-openjdk-{libarch} # Ubuntu 15.10
  188. # OpenBSD specific paths for default JVM
  189. /usr/local/jdk-1.7.0
  190. /usr/local/jre-1.7.0
  191. /usr/local/jdk-1.6.0
  192. /usr/local/jre-1.6.0
  193. # SuSE specific paths for default JVM
  194. /usr/lib64/jvm/java
  195. /usr/lib64/jvm/jre
  196. )
  197. set(_JNI_JAVA_AWT_LIBRARY_TRIES)
  198. set(_JNI_JAVA_INCLUDE_TRIES)
  199. foreach(_java_dir IN LISTS _JNI_JAVA_DIRECTORIES_BASE)
  200. list(APPEND _JNI_JAVA_AWT_LIBRARY_TRIES
  201. ${_java_dir}/jre/lib/{libarch}
  202. ${_java_dir}/jre/lib
  203. ${_java_dir}/lib/{libarch}
  204. ${_java_dir}/lib
  205. ${_java_dir}
  206. )
  207. list(APPEND _JNI_JAVA_INCLUDE_TRIES
  208. ${_java_dir}/include
  209. )
  210. endforeach()
  211. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_LIBRARY_DIRECTORIES
  212. ${_JNI_JAVA_AWT_LIBRARY_TRIES}
  213. )
  214. set(JAVA_JVM_LIBRARY_DIRECTORIES)
  215. foreach(dir ${JAVA_AWT_LIBRARY_DIRECTORIES})
  216. list(APPEND JAVA_JVM_LIBRARY_DIRECTORIES
  217. "${dir}"
  218. "${dir}/client"
  219. "${dir}/server"
  220. # IBM SDK, Java Technology Edition, specific paths
  221. "${dir}/j9vm"
  222. "${dir}/default"
  223. )
  224. endforeach()
  225. set(JAVA_AWT_INCLUDE_DIRECTORIES)
  226. if(_JAVA_HOME)
  227. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES ${_JAVA_HOME}/include)
  228. endif()
  229. if (WIN32)
  230. foreach (_JNI_HINT IN LISTS _JNI_HINTS)
  231. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "${_JNI_HINT}/include")
  232. endforeach()
  233. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES
  234. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/include"
  235. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/include"
  236. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/include"
  237. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/include"
  238. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/include"
  239. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/include"
  240. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/include"
  241. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\${java_install_version};JavaHome]/include"
  242. )
  243. endif()
  244. JAVA_APPEND_LIBRARY_DIRECTORIES(JAVA_AWT_INCLUDE_DIRECTORIES
  245. ${_JNI_JAVA_INCLUDE_TRIES}
  246. )
  247. foreach(JAVA_PROG "${JAVA_RUNTIME}" "${JAVA_COMPILE}" "${JAVA_ARCHIVE}")
  248. get_filename_component(jpath "${JAVA_PROG}" PATH)
  249. foreach(JAVA_INC_PATH ../include ../java/include ../share/java/include)
  250. if(EXISTS ${jpath}/${JAVA_INC_PATH})
  251. list(APPEND JAVA_AWT_INCLUDE_DIRECTORIES "${jpath}/${JAVA_INC_PATH}")
  252. endif()
  253. endforeach()
  254. foreach(JAVA_LIB_PATH
  255. ../lib ../jre/lib ../jre/lib/i386
  256. ../java/lib ../java/jre/lib ../java/jre/lib/i386
  257. ../share/java/lib ../share/java/jre/lib ../share/java/jre/lib/i386)
  258. if(EXISTS ${jpath}/${JAVA_LIB_PATH})
  259. list(APPEND JAVA_AWT_LIBRARY_DIRECTORIES "${jpath}/${JAVA_LIB_PATH}")
  260. endif()
  261. endforeach()
  262. endforeach()
  263. if(APPLE)
  264. if(CMAKE_FIND_FRAMEWORK STREQUAL "ONLY")
  265. set(_JNI_SEARCHES FRAMEWORK)
  266. elseif(CMAKE_FIND_FRAMEWORK STREQUAL "NEVER")
  267. set(_JNI_SEARCHES NORMAL)
  268. elseif(CMAKE_FIND_FRAMEWORK STREQUAL "LAST")
  269. set(_JNI_SEARCHES NORMAL FRAMEWORK)
  270. else()
  271. set(_JNI_SEARCHES FRAMEWORK NORMAL)
  272. endif()
  273. set(_JNI_FRAMEWORK_JVM NAMES JavaVM)
  274. set(_JNI_FRAMEWORK_JAWT "${_JNI_FRAMEWORK_JVM}")
  275. else()
  276. set(_JNI_SEARCHES NORMAL)
  277. endif()
  278. set(_JNI_NORMAL_JVM
  279. NAMES jvm
  280. PATHS ${JAVA_JVM_LIBRARY_DIRECTORIES}
  281. )
  282. set(_JNI_NORMAL_JAWT
  283. NAMES jawt
  284. PATHS ${JAVA_AWT_LIBRARY_DIRECTORIES}
  285. )
  286. foreach(search ${_JNI_SEARCHES})
  287. find_library(JAVA_JVM_LIBRARY ${_JNI_${search}_JVM})
  288. find_library(JAVA_AWT_LIBRARY ${_JNI_${search}_JAWT})
  289. if(JAVA_JVM_LIBRARY)
  290. break()
  291. endif()
  292. endforeach()
  293. unset(_JNI_SEARCHES)
  294. unset(_JNI_FRAMEWORK_JVM)
  295. unset(_JNI_FRAMEWORK_JAWT)
  296. unset(_JNI_NORMAL_JVM)
  297. unset(_JNI_NORMAL_JAWT)
  298. # Find headers matching the library.
  299. if("${JAVA_JVM_LIBRARY};${JAVA_AWT_LIBRARY};" MATCHES "(/JavaVM.framework|-framework JavaVM);")
  300. set(CMAKE_FIND_FRAMEWORK ONLY)
  301. else()
  302. set(CMAKE_FIND_FRAMEWORK NEVER)
  303. endif()
  304. # add in the include path
  305. find_path(JAVA_INCLUDE_PATH jni.h
  306. ${JAVA_AWT_INCLUDE_DIRECTORIES}
  307. )
  308. find_path(JAVA_INCLUDE_PATH2 NAMES jni_md.h jniport.h
  309. PATHS
  310. ${JAVA_INCLUDE_PATH}
  311. ${JAVA_INCLUDE_PATH}/darwin
  312. ${JAVA_INCLUDE_PATH}/win32
  313. ${JAVA_INCLUDE_PATH}/linux
  314. ${JAVA_INCLUDE_PATH}/freebsd
  315. ${JAVA_INCLUDE_PATH}/openbsd
  316. ${JAVA_INCLUDE_PATH}/solaris
  317. ${JAVA_INCLUDE_PATH}/hp-ux
  318. ${JAVA_INCLUDE_PATH}/alpha
  319. ${JAVA_INCLUDE_PATH}/aix
  320. )
  321. find_path(JAVA_AWT_INCLUDE_PATH jawt.h
  322. ${JAVA_INCLUDE_PATH}
  323. )
  324. # Restore CMAKE_FIND_FRAMEWORK
  325. if(DEFINED _JNI_CMAKE_FIND_FRAMEWORK)
  326. set(CMAKE_FIND_FRAMEWORK ${_JNI_CMAKE_FIND_FRAMEWORK})
  327. unset(_JNI_CMAKE_FIND_FRAMEWORK)
  328. else()
  329. unset(CMAKE_FIND_FRAMEWORK)
  330. endif()
  331. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  332. FIND_PACKAGE_HANDLE_STANDARD_ARGS(JNI DEFAULT_MSG JAVA_AWT_LIBRARY
  333. JAVA_JVM_LIBRARY
  334. JAVA_INCLUDE_PATH
  335. JAVA_INCLUDE_PATH2
  336. JAVA_AWT_INCLUDE_PATH)
  337. mark_as_advanced(
  338. JAVA_AWT_LIBRARY
  339. JAVA_JVM_LIBRARY
  340. JAVA_AWT_INCLUDE_PATH
  341. JAVA_INCLUDE_PATH
  342. JAVA_INCLUDE_PATH2
  343. )
  344. set(JNI_LIBRARIES
  345. ${JAVA_AWT_LIBRARY}
  346. ${JAVA_JVM_LIBRARY}
  347. )
  348. set(JNI_INCLUDE_DIRS
  349. ${JAVA_INCLUDE_PATH}
  350. ${JAVA_INCLUDE_PATH2}
  351. ${JAVA_AWT_INCLUDE_PATH}
  352. )