FindJava.cmake 12.5 KB
Newer Older
1 2 3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#[=======================================================================[.rst:
FindJava
--------

Find Java

This module finds if Java is installed and determines where the
include files and libraries are.  The caller may set variable ``JAVA_HOME``
to specify a Java installation prefix explicitly.

See also the :module:`FindJNI` module to find Java Native Interface (JNI).

Specify one or more of the following components as you call this find module. See example below.

::

  Runtime     = Java Runtime Environment used to execute Java byte-compiled applications
  Development = Development tools (java, javac, javah, jar and javadoc), includes Runtime component
  IdlJ        = Interface Description Language (IDL) to Java compiler
  JarSigner   = Signer and verifier tool for Java Archive (JAR) files


This module sets the following result variables:

::

  Java_JAVA_EXECUTABLE      = the full path to the Java runtime
  Java_JAVAC_EXECUTABLE     = the full path to the Java compiler
  Java_JAVAH_EXECUTABLE     = the full path to the Java header generator
  Java_JAVADOC_EXECUTABLE   = the full path to the Java documentation generator
  Java_IDLJ_EXECUTABLE      = the full path to the Java idl compiler
  Java_JAR_EXECUTABLE       = the full path to the Java archiver
  Java_JARSIGNER_EXECUTABLE = the full path to the Java jar signer
  Java_VERSION_STRING       = Version of java found, eg. 1.6.0_12
  Java_VERSION_MAJOR        = The major version of the package found.
  Java_VERSION_MINOR        = The minor version of the package found.
  Java_VERSION_PATCH        = The patch version of the package found.
  Java_VERSION_TWEAK        = The tweak version of the package found (after '_')
  Java_VERSION              = This is set to: $major[.$minor[.$patch[.$tweak]]]



The minimum required version of Java can be specified using the
:command:`find_package` syntax, e.g.

.. code-block:: cmake

  find_package(Java 1.8)

NOTE: ``${Java_VERSION}`` and ``${Java_VERSION_STRING}`` are not guaranteed to
be identical.  For example some java version may return:
``Java_VERSION_STRING = 1.8.0_17`` and ``Java_VERSION = 1.8.0.17``

another example is the Java OEM, with: ``Java_VERSION_STRING = 1.8.0-oem``
and ``Java_VERSION = 1.8.0``

For these components the following variables are set:

::

  Java_FOUND                    - TRUE if all components are found.
  Java_<component>_FOUND        - TRUE if <component> is found.



Example Usages:

::

  find_package(Java)
  find_package(Java 1.8 REQUIRED)
  find_package(Java COMPONENTS Runtime)
  find_package(Java COMPONENTS Development)
#]=======================================================================]
78

79
include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindJavaCommon.cmake)
80

81
# The HINTS option should only be used for values computed from the system.
82 83 84 85
set(_JAVA_HINTS)
if(_JAVA_HOME)
  list(APPEND _JAVA_HINTS ${_JAVA_HOME}/bin)
endif()
86 87 88 89 90 91 92
if (WIN32)
  macro (_JAVA_GET_INSTALLED_VERSIONS _KIND)
    execute_process(COMMAND REG QUERY HKLM\\SOFTWARE\\JavaSoft\\${_KIND} /f "." /k
      RESULT_VARIABLE _JAVA_RESULT
      OUTPUT_VARIABLE _JAVA_VERSIONS
      ERROR_QUIET)
    if (NOT  _JAVA_RESULT)
93 94 95 96 97 98 99 100 101 102 103 104 105
      string (REGEX MATCHALL "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\JavaSoft\\\\${_KIND}\\\\[0-9.]+" _JAVA_VERSIONS "${_JAVA_VERSIONS}")
      if (_JAVA_VERSIONS)
        # sort versions. Most recent first
        ## handle version 9 apart from other versions to get correct ordering
        set (_JAVA_V9 ${_JAVA_VERSIONS})
        list (FILTER _JAVA_VERSIONS EXCLUDE REGEX "${_KIND}\\\\9")
        list (SORT _JAVA_VERSIONS)
        list (REVERSE _JAVA_VERSIONS)
        list (FILTER _JAVA_V9 INCLUDE REGEX "${_KIND}\\\\9")
        list (SORT _JAVA_V9)
        list (REVERSE _JAVA_V9)
        list (APPEND _JAVA_VERSIONS ${_JAVA_V9})
        foreach (_JAVA_HINT IN LISTS _JAVA_VERSIONS)
106
          list(APPEND _JAVA_HINTS "[${_JAVA_HINT};JavaHome]/bin")
107 108
        endforeach()
      endif()
109 110 111 112 113 114 115 116
    endif()
  endmacro()

  # search for installed versions for version 9 and upper
  _JAVA_GET_INSTALLED_VERSIONS("JDK")
  _JAVA_GET_INSTALLED_VERSIONS("JRE")

  list(APPEND _JAVA_HINTS
117 118 119 120 121 122 123
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/bin"
124 125 126
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.9;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.8;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.7;JavaHome]/bin"
127
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.6;JavaHome]/bin"
128 129 130
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.5;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.4;JavaHome]/bin"
  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.3;JavaHome]/bin"
131
  )
132 133
endif()

134 135
# Hard-coded guesses should still go in PATHS. This ensures that the user
# environment can always override hard guesses.
136
set(_JAVA_PATHS
137 138 139
  /usr/lib/java/bin
  /usr/share/java/bin
  /usr/local/java/bin
140
  /usr/local/java/share/bin
141
  /usr/java/j2sdk1.4.2_04
Bill Hoffman's avatar
Bill Hoffman committed
142
  /usr/lib/j2sdk1.4-sun/bin
Andy Cedilnik's avatar
Andy Cedilnik committed
143
  /usr/java/j2sdk1.4.2_09/bin
144
  /usr/lib/j2sdk1.5-sun/bin
145
  /opt/sun-jdk-1.5.0.04/bin
146 147
  /usr/local/jdk-1.7.0/bin
  /usr/local/jdk-1.6.0/bin
148
  )
149
find_program(Java_JAVA_EXECUTABLE
150
  NAMES java
151 152
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
153 154
)

155 156
if(Java_JAVA_EXECUTABLE)
    execute_process(COMMAND ${Java_JAVA_EXECUTABLE} -version
157 158 159 160 161
      RESULT_VARIABLE res
      OUTPUT_VARIABLE var
      ERROR_VARIABLE var # sun-java output to stderr
      OUTPUT_STRIP_TRAILING_WHITESPACE
      ERROR_STRIP_TRAILING_WHITESPACE)
162
    if( res )
163 164 165 166
      if(var MATCHES "No Java runtime present, requesting install")
        set_property(CACHE Java_JAVA_EXECUTABLE
          PROPERTY VALUE "Java_JAVA_EXECUTABLE-NOTFOUND")
      elseif(${Java_FIND_REQUIRED})
167 168
        message( FATAL_ERROR "Error executing java -version" )
      else()
169
        message( STATUS "Warning, could not run java -version")
170 171
      endif()
    else()
172 173 174 175
      # Extract version components (up to 4 levels) from "java -version" output.
      set(_java_version_regex [[(([0-9]+)(\.([0-9]+)(\.([0-9]+)(_([0-9]+))?)?)?.*)]])
      if(var MATCHES "java version \"${_java_version_regex}\"")
        # Sun, GCJ, older OpenJDK
176
        set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
177
        set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
178
        if (CMAKE_MATCH_4)
179
        set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
180 181 182 183
        else()
          set(Java_VERSION_MINOR 0)
        endif()
        if (CMAKE_MATCH_6)
184
        set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
185 186 187
        else()
          set(Java_VERSION_PATCH 0)
        endif()
188 189 190 191 192
        set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
      elseif(var MATCHES "openjdk version \"${_java_version_regex}\"")
        # OpenJDK
        set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
        set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
193
        if (CMAKE_MATCH_4)
194
        set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
195 196 197 198
        else()
          set(Java_VERSION_MINOR 0)
        endif()
        if (CMAKE_MATCH_6)
199
        set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
200 201 202
        else()
          set(Java_VERSION_PATCH 0)
        endif()
203
        set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
204
      elseif(var MATCHES "openjdk version \"([0-9]+)-[A-Za-z]+\"")
205 206
        # OpenJDK 9 early access builds or locally built
        set(Java_VERSION_STRING "1.${CMAKE_MATCH_1}.0")
207 208 209 210 211
        set(Java_VERSION_MAJOR "1")
        set(Java_VERSION_MINOR "${CMAKE_MATCH_1}")
        set(Java_VERSION_PATCH "0")
        set(Java_VERSION_TWEAK "")
      elseif(var MATCHES "java full version \"kaffe-${_java_version_regex}\"")
212
        # Kaffe style
213
        set(Java_VERSION_STRING "${CMAKE_MATCH_1}")
214 215 216 217
        set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}")
        set(Java_VERSION_MINOR "${CMAKE_MATCH_4}")
        set(Java_VERSION_PATCH "${CMAKE_MATCH_6}")
        set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}")
218 219
      else()
        if(NOT Java_FIND_QUIETLY)
220 221
          string(REPLACE "\n" "\n  " ver_msg "\n${var}")
          message(WARNING "Java version not recognized:${ver_msg}\nPlease report.")
222
        endif()
223 224 225 226 227
        set(Java_VERSION_STRING "")
        set(Java_VERSION_MAJOR "")
        set(Java_VERSION_MINOR "")
        set(Java_VERSION_PATCH "")
        set(Java_VERSION_TWEAK "")
228
      endif()
229 230 231 232 233 234 235 236 237
      set(Java_VERSION "${Java_VERSION_MAJOR}")
      if(NOT "x${Java_VERSION}" STREQUAL "x")
        foreach(c MINOR PATCH TWEAK)
          if(NOT "x${Java_VERSION_${c}}" STREQUAL "x")
            string(APPEND Java_VERSION ".${Java_VERSION_${c}}")
          else()
            break()
          endif()
        endforeach()
238
      endif()
239
    endif()
240

241
endif()
242 243


244
find_program(Java_JAR_EXECUTABLE
245
  NAMES jar
246 247
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
248 249
)

250
find_program(Java_JAVAC_EXECUTABLE
251
  NAMES javac
252 253
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
254 255
)

256
find_program(Java_JAVAH_EXECUTABLE
257 258 259 260 261
  NAMES javah
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
)

262
find_program(Java_JAVADOC_EXECUTABLE
263 264 265 266 267
  NAMES javadoc
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
)

268 269 270 271 272 273 274 275 276 277 278 279
find_program(Java_IDLJ_EXECUTABLE
  NAMES idlj
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
)

find_program(Java_JARSIGNER_EXECUTABLE
  NAMES jarsigner
  HINTS ${_JAVA_HINTS}
  PATHS ${_JAVA_PATHS}
)

280
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
281
if(Java_FIND_COMPONENTS)
282
  set(_JAVA_REQUIRED_VARS)
283 284
  foreach(component ${Java_FIND_COMPONENTS})
    # User just want to execute some Java byte-compiled
285 286 287 288 289
    If(component STREQUAL "Runtime")
      list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE)
      if(Java_JAVA_EXECUTABLE)
        set(Java_Runtime_FOUND TRUE)
      endif()
290
    elseif(component STREQUAL "Development")
291
      list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAVAC_EXECUTABLE
292
                                      Java_JAR_EXECUTABLE Java_JAVADOC_EXECUTABLE)
293
      if(Java_VERSION VERSION_LESS "10")
294 295
        list(APPEND _JAVA_REQUIRED_VARS Java_JAVAH_EXECUTABLE)
        if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE
296
            AND Java_JAVAH_EXECUTABLE AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE)
297 298 299 300
          set(Java_Development_FOUND TRUE)
        endif()
      else()
        if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE
301
            AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE)
302 303
          set(Java_Development_FOUND TRUE)
        endif()
304 305 306
      endif()
    elseif(component STREQUAL "IdlJ")
      list(APPEND _JAVA_REQUIRED_VARS Java_IDLJ_EXECUTABLE)
307 308
      if(Java_IDLJ_EXECUTABLE)
        set(Java_IdlJ_FOUND TRUE)
309 310 311
      endif()
    elseif(component STREQUAL "JarSigner")
      list(APPEND _JAVA_REQUIRED_VARS Java_JARSIGNER_EXECUTABLE)
312
      if(Java_JARSIGNER_EXECUTABLE)
313 314
        set(Java_JarSigner_FOUND TRUE)
      endif()
315 316 317
    else()
      message(FATAL_ERROR "Comp: ${component} is not handled")
    endif()
318
  endforeach()
319 320 321 322 323 324 325 326 327 328
  list (REMOVE_DUPLICATES _JAVA_REQUIRED_VARS)
  find_package_handle_standard_args(Java
    REQUIRED_VARS ${_JAVA_REQUIRED_VARS} HANDLE_COMPONENTS
    VERSION_VAR Java_VERSION
    )
  if(Java_FOUND)
    foreach(component ${Java_FIND_COMPONENTS})
      set(Java_${component}_FOUND TRUE)
    endforeach()
  endif()
329
else()
330
  # Check for Development
331
  if(Java_VERSION VERSION_LESS "10")
332 333 334 335 336 337 338 339 340 341 342 343
    find_package_handle_standard_args(Java
      REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE
                    Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE
      VERSION_VAR Java_VERSION_STRING
      )
  else()
    find_package_handle_standard_args(Java
      REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE
                    Java_JAVADOC_EXECUTABLE
      VERSION_VAR Java_VERSION_STRING
      )
  endif()
344 345
endif()

346

347
mark_as_advanced(
348 349 350
  Java_JAVA_EXECUTABLE
  Java_JAR_EXECUTABLE
  Java_JAVAC_EXECUTABLE
351 352
  Java_JAVAH_EXECUTABLE
  Java_JAVADOC_EXECUTABLE
353 354
  Java_IDLJ_EXECUTABLE
  Java_JARSIGNER_EXECUTABLE
355
  )
356 357

# LEGACY
358 359 360
set(JAVA_RUNTIME ${Java_JAVA_EXECUTABLE})
set(JAVA_ARCHIVE ${Java_JAR_EXECUTABLE})
set(JAVA_COMPILE ${Java_JAVAC_EXECUTABLE})