aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/extend/os/mac/requirements/java_requirement.rb
blob: 2a48f02bd85b437064b3aa2a7de5bb4c158f01a0 (plain)
1
2
3
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
class JavaRequirement < Requirement
  env do
    env_java_common
    env_oracle_jdk || env_apple
  end

  # A strict Java 8 requirement (1.8) should prompt the user to install
  # the legacy java8 cask because the current version, Java 9, is not
  # completely backwards compatible, and contains breaking changes such as
  # strong encapsulation of JDK-internal APIs and a modified version scheme
  # (9.0 not 1.9).
  def cask
    if @version.nil? || @version.to_s.end_with?("+") ||
       @version.to_f >= JAVA_CASK_MAP.keys.max.to_f
      JAVA_CASK_MAP.fetch(JAVA_CASK_MAP.keys.max)
    else
      JAVA_CASK_MAP.fetch("1.8")
    end
  end

  private

  JAVA_CASK_MAP = {
    "1.8" => "caskroom/versions/java8",
    "9.0" => "java",
  }.freeze

  def possible_javas
    javas = []
    javas << Pathname.new(ENV["JAVA_HOME"])/"bin/java" if ENV["JAVA_HOME"]
    javas << java_home_cmd
    which_java = which("java")
    # /usr/bin/java is a stub on macOS
    javas << which_java if which_java.to_s != "/usr/bin/java"
    javas
  end

  def java_home_cmd
    return nil unless File.executable?("/usr/libexec/java_home")
    args = %w[--failfast]
    args << "--version" << @version.to_s if @version
    java_home = Utils.popen_read("/usr/libexec/java_home", *args).chomp
    return nil unless $CHILD_STATUS.success?
    Pathname.new(java_home)/"bin/java"
  end

  def env_apple
    ENV.append_to_cflags "-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/"
  end

  def oracle_java_os
    :darwin
  end
end