FindJava.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. FindJava
  5. --------
  6. Find Java
  7. This module finds if Java is installed and determines where the
  8. include files and libraries are. The caller may set variable ``JAVA_HOME``
  9. to specify a Java installation prefix explicitly.
  10. See also the :module:`FindJNI` module to find Java Native Interface (JNI).
  11. Specify one or more of the following components as you call this find module. See example below.
  12. ::
  13. Runtime = Java Runtime Environment used to execute Java byte-compiled applications
  14. Development = Development tools (java, javac, javah, jar and javadoc), includes Runtime component
  15. IdlJ = Interface Description Language (IDL) to Java compiler
  16. JarSigner = Signer and verifier tool for Java Archive (JAR) files
  17. This module sets the following result variables:
  18. ::
  19. Java_JAVA_EXECUTABLE = the full path to the Java runtime
  20. Java_JAVAC_EXECUTABLE = the full path to the Java compiler
  21. Java_JAVAH_EXECUTABLE = the full path to the Java header generator
  22. Java_JAVADOC_EXECUTABLE = the full path to the Java documentation generator
  23. Java_IDLJ_EXECUTABLE = the full path to the Java idl compiler
  24. Java_JAR_EXECUTABLE = the full path to the Java archiver
  25. Java_JARSIGNER_EXECUTABLE = the full path to the Java jar signer
  26. Java_VERSION_STRING = Version of java found, eg. 1.6.0_12
  27. Java_VERSION_MAJOR = The major version of the package found.
  28. Java_VERSION_MINOR = The minor version of the package found.
  29. Java_VERSION_PATCH = The patch version of the package found.
  30. Java_VERSION_TWEAK = The tweak version of the package found (after '_')
  31. Java_VERSION = This is set to: $major[.$minor[.$patch[.$tweak]]]
  32. The minimum required version of Java can be specified using the
  33. :command:`find_package` syntax, e.g.
  34. .. code-block:: cmake
  35. find_package(Java 1.8)
  36. NOTE: ``${Java_VERSION}`` and ``${Java_VERSION_STRING}`` are not guaranteed to
  37. be identical. For example some java version may return:
  38. ``Java_VERSION_STRING = 1.8.0_17`` and ``Java_VERSION = 1.8.0.17``
  39. another example is the Java OEM, with: ``Java_VERSION_STRING = 1.8.0-oem``
  40. and ``Java_VERSION = 1.8.0``
  41. For these components the following variables are set:
  42. ::
  43. Java_FOUND - TRUE if all components are found.
  44. Java_<component>_FOUND - TRUE if <component> is found.
  45. Example Usages:
  46. ::
  47. find_package(Java)
  48. find_package(Java 1.8 REQUIRED)
  49. find_package(Java COMPONENTS Runtime)
  50. find_package(Java COMPONENTS Development)
  51. #]=======================================================================]
  52. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
  53. # The HINTS option should only be used for values computed from the system.
  54. set(_JAVA_HINTS)
  55. if(_JAVA_HOME)
  56. list(APPEND _JAVA_HINTS ${_JAVA_HOME}/bin)
  57. endif()
  58. if (WIN32)
  59. macro (_JAVA_GET_INSTALLED_VERSIONS _KIND)
  60. execute_process(COMMAND REG QUERY HKLM\\SOFTWARE\\JavaSoft\\${_KIND}
  61. RESULT_VARIABLE _JAVA_RESULT
  62. OUTPUT_VARIABLE _JAVA_VERSIONS
  63. ERROR_QUIET)
  64. if (NOT _JAVA_RESULT)
  65. string (REGEX MATCHALL "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\JavaSoft\\\\${_KIND}\\\\[0-9.]+" _JAVA_VERSIONS "${_JAVA_VERSIONS}")
  66. if (_JAVA_VERSIONS)
  67. # sort versions. Most recent first
  68. ## handle version 9 apart from other versions to get correct ordering
  69. set (_JAVA_V9 ${_JAVA_VERSIONS})
  70. list (FILTER _JAVA_VERSIONS EXCLUDE REGEX "${_KIND}\\\\9")
  71. list (SORT _JAVA_VERSIONS)
  72. list (REVERSE _JAVA_VERSIONS)
  73. list (FILTER _JAVA_V9 INCLUDE REGEX "${_KIND}\\\\9")
  74. list (SORT _JAVA_V9)
  75. list (REVERSE _JAVA_V9)
  76. list (APPEND _JAVA_VERSIONS ${_JAVA_V9})
  77. foreach (_JAVA_HINT IN LISTS _JAVA_VERSIONS)
  78. list(APPEND _JAVA_HINTS "[${_JAVA_HINT};JavaHome]/bin")
  79. endforeach()
  80. endif()
  81. endif()
  82. endmacro()
  83. # search for installed versions for version 9 and upper
  84. _JAVA_GET_INSTALLED_VERSIONS("JDK")
  85. _JAVA_GET_INSTALLED_VERSIONS("JRE")
  86. list(APPEND _JAVA_HINTS
  87. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/bin"
  88. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/bin"
  89. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/bin"
  90. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/bin"
  91. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/bin"
  92. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/bin"
  93. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/bin"
  94. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.9;JavaHome]/bin"
  95. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.8;JavaHome]/bin"
  96. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.7;JavaHome]/bin"
  97. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.6;JavaHome]/bin"
  98. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.5;JavaHome]/bin"
  99. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.4;JavaHome]/bin"
  100. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.3;JavaHome]/bin"
  101. )
  102. endif()
  103. # Hard-coded guesses should still go in PATHS. This ensures that the user
  104. # environment can always override hard guesses.
  105. set(_JAVA_PATHS
  106. /usr/lib/java/bin
  107. /usr/share/java/bin
  108. /usr/local/java/bin
  109. /usr/local/java/share/bin
  110. /usr/java/j2sdk1.4.2_04
  111. /usr/lib/j2sdk1.4-sun/bin
  112. /usr/java/j2sdk1.4.2_09/bin
  113. /usr/lib/j2sdk1.5-sun/bin
  114. /opt/sun-jdk-1.5.0.04/bin
  115. /usr/local/jdk-1.7.0/bin
  116. /usr/local/jdk-1.6.0/bin
  117. )
  118. find_program(Java_JAVA_EXECUTABLE
  119. NAMES java
  120. HINTS ${_JAVA_HINTS}
  121. PATHS ${_JAVA_PATHS}
  122. )
  123. if(Java_JAVA_EXECUTABLE)
  124. execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -version
  125. RESULT_VARIABLE res
  126. OUTPUT_VARIABLE var
  127. ERROR_VARIABLE var # sun-java output to stderr
  128. OUTPUT_STRIP_TRAILING_WHITESPACE
  129. ERROR_STRIP_TRAILING_WHITESPACE)
  130. if( res )
  131. if(var MATCHES "No Java runtime present, requesting install")
  132. set_property(CACHE Java_JAVA_EXECUTABLE
  133. PROPERTY VALUE "Java_JAVA_EXECUTABLE-NOTFOUND")
  134. elseif(${Java_FIND_REQUIRED})
  135. message( FATAL_ERROR "Error executing java -version" )
  136. else()
  137. message( STATUS "Warning, could not run java -version")
  138. endif()
  139. else()
  140. # Extract version components (up to 4 levels) from "java -version" output.
  141. set(_java_version_regex [[(([0-9]+)(\.([0-9]+)(\.([0-9]+)(_([0-9]+))?)?)?.*)]])
  142. if(var MATCHES "java version \"${_java_version_regex}\"")
  143. # Sun, GCJ, older OpenJDK
  144. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  145. set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
  146. if (CMAKE_MATCH_4)
  147. set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
  148. else()
  149. set(Java_VERSION_MINOR 0)
  150. endif()
  151. if (CMAKE_MATCH_6)
  152. set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
  153. else()
  154. set(Java_VERSION_PATCH 0)
  155. endif()
  156. set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
  157. elseif(var MATCHES "openjdk version \"${_java_version_regex}\"")
  158. # OpenJDK
  159. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  160. set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
  161. if (CMAKE_MATCH_4)
  162. set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
  163. else()
  164. set(Java_VERSION_MINOR 0)
  165. endif()
  166. if (CMAKE_MATCH_6)
  167. set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
  168. else()
  169. set(Java_VERSION_PATCH 0)
  170. endif()
  171. set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
  172. elseif(var MATCHES "openjdk version \"([0-9]+)-[A-Za-z]+\"")
  173. # OpenJDK 9 early access builds or locally built
  174. set(Java_VERSION_STRING "1.${CMAKE_MATCH_1}.0")
  175. set(Java_VERSION_MAJOR "1")
  176. set(Java_VERSION_MINOR "${CMAKE_MATCH_1}")
  177. set(Java_VERSION_PATCH "0")
  178. set(Java_VERSION_TWEAK "")
  179. elseif(var MATCHES "java full version \"kaffe-${_java_version_regex}\"")
  180. # Kaffe style
  181. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  182. set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
  183. set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
  184. set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
  185. set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
  186. else()
  187. if(NOT Java_FIND_QUIETLY)
  188. string(REPLACE "\n" "\n " ver_msg "\n${var}")
  189. message(WARNING "Java version not recognized:${ver_msg}\nPlease report.")
  190. endif()
  191. set(Java_VERSION_STRING "")
  192. set(Java_VERSION_MAJOR "")
  193. set(Java_VERSION_MINOR "")
  194. set(Java_VERSION_PATCH "")
  195. set(Java_VERSION_TWEAK "")
  196. endif()
  197. set(Java_VERSION "${Java_VERSION_MAJOR}")
  198. if(NOT "x${Java_VERSION}" STREQUAL "x")
  199. foreach(c MINOR PATCH TWEAK)
  200. if(NOT "x${Java_VERSION_${c}}" STREQUAL "x")
  201. string(APPEND Java_VERSION ".${Java_VERSION_${c}}")
  202. else()
  203. break()
  204. endif()
  205. endforeach()
  206. endif()
  207. endif()
  208. endif()
  209. find_program(Java_JAR_EXECUTABLE
  210. NAMES jar
  211. HINTS ${_JAVA_HINTS}
  212. PATHS ${_JAVA_PATHS}
  213. )
  214. find_program(Java_JAVAC_EXECUTABLE
  215. NAMES javac
  216. HINTS ${_JAVA_HINTS}
  217. PATHS ${_JAVA_PATHS}
  218. )
  219. find_program(Java_JAVAH_EXECUTABLE
  220. NAMES javah
  221. HINTS ${_JAVA_HINTS}
  222. PATHS ${_JAVA_PATHS}
  223. )
  224. find_program(Java_JAVADOC_EXECUTABLE
  225. NAMES javadoc
  226. HINTS ${_JAVA_HINTS}
  227. PATHS ${_JAVA_PATHS}
  228. )
  229. find_program(Java_IDLJ_EXECUTABLE
  230. NAMES idlj
  231. HINTS ${_JAVA_HINTS}
  232. PATHS ${_JAVA_PATHS}
  233. )
  234. find_program(Java_JARSIGNER_EXECUTABLE
  235. NAMES jarsigner
  236. HINTS ${_JAVA_HINTS}
  237. PATHS ${_JAVA_PATHS}
  238. )
  239. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  240. if(Java_FIND_COMPONENTS)
  241. set(_JAVA_REQUIRED_VARS)
  242. foreach(component ${Java_FIND_COMPONENTS})
  243. # User just want to execute some Java byte-compiled
  244. If(component STREQUAL "Runtime")
  245. list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE)
  246. if(Java_JAVA_EXECUTABLE)
  247. set(Java_Runtime_FOUND TRUE)
  248. endif()
  249. elseif(component STREQUAL "Development")
  250. list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAVAC_EXECUTABLE
  251. Java_JAR_EXECUTABLE Java_JAVADOC_EXECUTABLE)
  252. if(Java_VERSION VERSION_LESS "10")
  253. list(APPEND _JAVA_REQUIRED_VARS Java_JAVAH_EXECUTABLE)
  254. if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE
  255. AND Java_JAVAH_EXECUTABLE AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE)
  256. set(Java_Development_FOUND TRUE)
  257. endif()
  258. else()
  259. if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE
  260. AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE)
  261. set(Java_Development_FOUND TRUE)
  262. endif()
  263. endif()
  264. elseif(component STREQUAL "IdlJ")
  265. list(APPEND _JAVA_REQUIRED_VARS Java_IDLJ_EXECUTABLE)
  266. if(Java_IDLJ_EXECUTABLE)
  267. set(Java_IdlJ_FOUND TRUE)
  268. endif()
  269. elseif(component STREQUAL "JarSigner")
  270. list(APPEND _JAVA_REQUIRED_VARS Java_JARSIGNER_EXECUTABLE)
  271. if(Java_JARSIGNER_EXECUTABLE)
  272. set(Java_JarSigner_FOUND TRUE)
  273. endif()
  274. else()
  275. message(FATAL_ERROR "Comp: ${component} is not handled")
  276. endif()
  277. endforeach()
  278. list (REMOVE_DUPLICATES _JAVA_REQUIRED_VARS)
  279. find_package_handle_standard_args(Java
  280. REQUIRED_VARS ${_JAVA_REQUIRED_VARS} HANDLE_COMPONENTS
  281. VERSION_VAR Java_VERSION
  282. )
  283. if(Java_FOUND)
  284. foreach(component ${Java_FIND_COMPONENTS})
  285. set(Java_${component}_FOUND TRUE)
  286. endforeach()
  287. endif()
  288. else()
  289. # Check for Development
  290. if(Java_VERSION VERSION_LESS "10")
  291. find_package_handle_standard_args(Java
  292. REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE
  293. Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE
  294. VERSION_VAR Java_VERSION_STRING
  295. )
  296. else()
  297. find_package_handle_standard_args(Java
  298. REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE
  299. Java_JAVADOC_EXECUTABLE
  300. VERSION_VAR Java_VERSION_STRING
  301. )
  302. endif()
  303. endif()
  304. mark_as_advanced(
  305. Java_JAVA_EXECUTABLE
  306. Java_JAR_EXECUTABLE
  307. Java_JAVAC_EXECUTABLE
  308. Java_JAVAH_EXECUTABLE
  309. Java_JAVADOC_EXECUTABLE
  310. Java_IDLJ_EXECUTABLE
  311. Java_JARSIGNER_EXECUTABLE
  312. )
  313. # LEGACY
  314. set(JAVA_RUNTIME ${Java_JAVA_EXECUTABLE})
  315. set(JAVA_ARCHIVE ${Java_JAR_EXECUTABLE})
  316. set(JAVA_COMPILE ${Java_JAVAC_EXECUTABLE})