FindJava.cmake 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #.rst:
  2. # FindJava
  3. # --------
  4. #
  5. # Find Java
  6. #
  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. #
  11. #
  12. # Specify one or more of the following components as you call this find module. See example below.
  13. #
  14. # ::
  15. #
  16. # Runtime = User just want to execute some Java byte-compiled
  17. # Development = Development tools (java, javac, javah and javadoc), includes Runtime component
  18. # IdlJ = idl compiler for Java
  19. # JarSigner = signer tool for jar
  20. #
  21. #
  22. # This module sets the following result variables:
  23. #
  24. # ::
  25. #
  26. # Java_JAVA_EXECUTABLE = the full path to the Java runtime
  27. # Java_JAVAC_EXECUTABLE = the full path to the Java compiler
  28. # Java_JAVAH_EXECUTABLE = the full path to the Java header generator
  29. # Java_JAVADOC_EXECUTABLE = the full path to the Java documentation generator
  30. # Java_IDLJ_EXECUTABLE = the full path to the Java idl compiler
  31. # Java_JAR_EXECUTABLE = the full path to the Java archiver
  32. # Java_JARSIGNER_EXECUTABLE = the full path to the Java jar signer
  33. # Java_VERSION_STRING = Version of java found, eg. 1.6.0_12
  34. # Java_VERSION_MAJOR = The major version of the package found.
  35. # Java_VERSION_MINOR = The minor version of the package found.
  36. # Java_VERSION_PATCH = The patch version of the package found.
  37. # Java_VERSION_TWEAK = The tweak version of the package found (after '_')
  38. # Java_VERSION = This is set to: $major.$minor.$patch(.$tweak)
  39. #
  40. #
  41. #
  42. # The minimum required version of Java can be specified using the
  43. # standard CMake syntax, e.g. find_package(Java 1.5)
  44. #
  45. # NOTE: ${Java_VERSION} and ${Java_VERSION_STRING} are not guaranteed to
  46. # be identical. For example some java version may return:
  47. # Java_VERSION_STRING = 1.5.0_17 and Java_VERSION = 1.5.0.17
  48. #
  49. # another example is the Java OEM, with: Java_VERSION_STRING = 1.6.0-oem
  50. # and Java_VERSION = 1.6.0
  51. #
  52. # For these components the following variables are set:
  53. #
  54. # ::
  55. #
  56. # Java_FOUND - TRUE if all components are found.
  57. # Java_INCLUDE_DIRS - Full paths to all include dirs.
  58. # Java_LIBRARIES - Full paths to all libraries.
  59. # Java_<component>_FOUND - TRUE if <component> is found.
  60. #
  61. #
  62. #
  63. # Example Usages:
  64. #
  65. # ::
  66. #
  67. # find_package(Java)
  68. # find_package(Java COMPONENTS Runtime)
  69. # find_package(Java COMPONENTS Development)
  70. #=============================================================================
  71. # Copyright 2002-2009 Kitware, Inc.
  72. # Copyright 2009-2011 Mathieu Malaterre <mathieu.malaterre@gmail.com>
  73. #
  74. # Distributed under the OSI-approved BSD License (the "License");
  75. # see accompanying file Copyright.txt for details.
  76. #
  77. # This software is distributed WITHOUT ANY WARRANTY; without even the
  78. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  79. # See the License for more information.
  80. #=============================================================================
  81. # (To distribute this file outside of CMake, substitute the full
  82. # License text for the above reference.)
  83. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
  84. # The HINTS option should only be used for values computed from the system.
  85. set(_JAVA_HINTS)
  86. if(_JAVA_HOME)
  87. list(APPEND _JAVA_HINTS ${_JAVA_HOME}/bin)
  88. endif()
  89. list(APPEND _JAVA_HINTS
  90. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\2.0;JavaHome]/bin"
  91. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/bin"
  92. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/bin"
  93. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/bin"
  94. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/bin"
  95. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/bin"
  96. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/bin"
  97. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/bin"
  98. )
  99. # Hard-coded guesses should still go in PATHS. This ensures that the user
  100. # environment can always override hard guesses.
  101. set(_JAVA_PATHS
  102. /usr/lib/java/bin
  103. /usr/share/java/bin
  104. /usr/local/java/bin
  105. /usr/local/java/share/bin
  106. /usr/java/j2sdk1.4.2_04
  107. /usr/lib/j2sdk1.4-sun/bin
  108. /usr/java/j2sdk1.4.2_09/bin
  109. /usr/lib/j2sdk1.5-sun/bin
  110. /opt/sun-jdk-1.5.0.04/bin
  111. /usr/local/jdk-1.7.0/bin
  112. /usr/local/jdk-1.6.0/bin
  113. )
  114. find_program(Java_JAVA_EXECUTABLE
  115. NAMES java
  116. HINTS ${_JAVA_HINTS}
  117. PATHS ${_JAVA_PATHS}
  118. )
  119. if(Java_JAVA_EXECUTABLE)
  120. execute_process(COMMAND ${Java_JAVA_EXECUTABLE} -version
  121. RESULT_VARIABLE res
  122. OUTPUT_VARIABLE var
  123. ERROR_VARIABLE var # sun-java output to stderr
  124. OUTPUT_STRIP_TRAILING_WHITESPACE
  125. ERROR_STRIP_TRAILING_WHITESPACE)
  126. if( res )
  127. if(var MATCHES "No Java runtime present, requesting install")
  128. set_property(CACHE Java_JAVA_EXECUTABLE
  129. PROPERTY VALUE "Java_JAVA_EXECUTABLE-NOTFOUND")
  130. elseif(${Java_FIND_REQUIRED})
  131. message( FATAL_ERROR "Error executing java -version" )
  132. else()
  133. message( STATUS "Warning, could not run java -version")
  134. endif()
  135. else()
  136. # extract major/minor version and patch level from "java -version" output
  137. # Tested on linux using
  138. # 1. Sun / Sun OEM
  139. # 2. OpenJDK 1.6
  140. # 3. GCJ 1.5
  141. # 4. Kaffe 1.4.2
  142. # 5. OpenJDK 1.7.x on OpenBSD
  143. if(var MATCHES "java version \"([0-9]+\\.[0-9]+\\.[0-9_.]+.*)\"")
  144. # This is most likely Sun / OpenJDK, or maybe GCJ-java compat layer
  145. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  146. elseif(var MATCHES "java full version \"kaffe-([0-9]+\\.[0-9]+\\.[0-9_]+)\"")
  147. # Kaffe style
  148. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  149. elseif(var MATCHES "openjdk version \"([0-9]+\\.[0-9]+\\.[0-9_]+.*)\"")
  150. # OpenJDK ver 1.7.x on OpenBSD
  151. set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
  152. else()
  153. if(NOT Java_FIND_QUIETLY)
  154. message(WARNING "regex not supported: ${var}. Please report")
  155. endif()
  156. endif()
  157. string( REGEX REPLACE "([0-9]+).*" "\\1" Java_VERSION_MAJOR "${Java_VERSION_STRING}" )
  158. string( REGEX REPLACE "[0-9]+\\.([0-9]+).*" "\\1" Java_VERSION_MINOR "${Java_VERSION_STRING}" )
  159. string( REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" Java_VERSION_PATCH "${Java_VERSION_STRING}" )
  160. # warning tweak version can be empty:
  161. string( REGEX REPLACE "[0-9]+\\.[0-9]+\\.[0-9]+[_\\.]?([0-9]*).*$" "\\1" Java_VERSION_TWEAK "${Java_VERSION_STRING}" )
  162. if( Java_VERSION_TWEAK STREQUAL "" ) # check case where tweak is not defined
  163. set(Java_VERSION ${Java_VERSION_MAJOR}.${Java_VERSION_MINOR}.${Java_VERSION_PATCH})
  164. else()
  165. set(Java_VERSION ${Java_VERSION_MAJOR}.${Java_VERSION_MINOR}.${Java_VERSION_PATCH}.${Java_VERSION_TWEAK})
  166. endif()
  167. endif()
  168. endif()
  169. find_program(Java_JAR_EXECUTABLE
  170. NAMES jar
  171. HINTS ${_JAVA_HINTS}
  172. PATHS ${_JAVA_PATHS}
  173. )
  174. find_program(Java_JAVAC_EXECUTABLE
  175. NAMES javac
  176. HINTS ${_JAVA_HINTS}
  177. PATHS ${_JAVA_PATHS}
  178. )
  179. find_program(Java_JAVAH_EXECUTABLE
  180. NAMES javah
  181. HINTS ${_JAVA_HINTS}
  182. PATHS ${_JAVA_PATHS}
  183. )
  184. find_program(Java_JAVADOC_EXECUTABLE
  185. NAMES javadoc
  186. HINTS ${_JAVA_HINTS}
  187. PATHS ${_JAVA_PATHS}
  188. )
  189. find_program(Java_IDLJ_EXECUTABLE
  190. NAMES idlj
  191. HINTS ${_JAVA_HINTS}
  192. PATHS ${_JAVA_PATHS}
  193. )
  194. find_program(Java_JARSIGNER_EXECUTABLE
  195. NAMES jarsigner
  196. HINTS ${_JAVA_HINTS}
  197. PATHS ${_JAVA_PATHS}
  198. )
  199. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  200. if(Java_FIND_COMPONENTS)
  201. set(_JAVA_REQUIRED_VARS)
  202. foreach(component ${Java_FIND_COMPONENTS})
  203. # User just want to execute some Java byte-compiled
  204. If(component STREQUAL "Runtime")
  205. list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE)
  206. if(Java_JAVA_EXECUTABLE)
  207. set(Java_Runtime_FOUND TRUE)
  208. endif()
  209. elseif(component STREQUAL "Development")
  210. list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAVAC_EXECUTABLE
  211. Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE)
  212. if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE
  213. AND Java_JAVAH_EXECUTABLE AND Java_JAVADOC_EXECUTABLE)
  214. set(Java_Development_FOUND TRUE)
  215. endif()
  216. elseif(component STREQUAL "IdlJ")
  217. list(APPEND _JAVA_REQUIRED_VARS Java_IDLJ_EXECUTABLE)
  218. if(Java_IDLJ_EXECUTABLE)
  219. set(Java_IdlJ_FOUND TRUE)
  220. endif()
  221. elseif(component STREQUAL "JarSigner")
  222. list(APPEND _JAVA_REQUIRED_VARS Java_JARSIGNER_EXECUTABLE)
  223. if(Java_JARSIGNER_EXECUTABLE)
  224. set(Java_JarSigner_FOUND TRUE)
  225. endif()
  226. else()
  227. message(FATAL_ERROR "Comp: ${component} is not handled")
  228. endif()
  229. endforeach()
  230. list (REMOVE_DUPLICATES _JAVA_REQUIRED_VARS)
  231. find_package_handle_standard_args(Java
  232. REQUIRED_VARS ${_JAVA_REQUIRED_VARS} HANDLE_COMPONENTS
  233. VERSION_VAR Java_VERSION
  234. )
  235. if(Java_FOUND)
  236. foreach(component ${Java_FIND_COMPONENTS})
  237. set(Java_${component}_FOUND TRUE)
  238. endforeach()
  239. endif()
  240. else()
  241. # Check for Development
  242. find_package_handle_standard_args(Java
  243. REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE
  244. Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE
  245. VERSION_VAR Java_VERSION
  246. )
  247. endif()
  248. mark_as_advanced(
  249. Java_JAVA_EXECUTABLE
  250. Java_JAR_EXECUTABLE
  251. Java_JAVAC_EXECUTABLE
  252. Java_JAVAH_EXECUTABLE
  253. Java_JAVADOC_EXECUTABLE
  254. Java_IDLJ_EXECUTABLE
  255. Java_JARSIGNER_EXECUTABLE
  256. )
  257. # LEGACY
  258. set(JAVA_RUNTIME ${Java_JAVA_EXECUTABLE})
  259. set(JAVA_ARCHIVE ${Java_JAR_EXECUTABLE})
  260. set(JAVA_COMPILE ${Java_JAVAC_EXECUTABLE})