-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathJavaToolchainPlugin.scala
123 lines (111 loc) · 3.37 KB
/
JavaToolchainPlugin.scala
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sbt.{Compile, Def, File, _}
import sbt.Keys._
import sbt.plugins.JvmPlugin
import java.nio.file.Paths
import java.util
import java.util.Collections
import scala.util.Properties
import scala.sys.process.Process
/**
* An sbt plugin that automatically adds the Java compiler to the boot classpath
* when necessary.
*/
object JavaToolchainPlugin extends AutoPlugin {
override def trigger = allRequirements
override def requires = JvmPlugin
object autoImport {
lazy val javaToolchainVersion = settingKey[String](
"The version of the Java"
)
lazy val javaToolchainJvmIndex = settingKey[Option[String]](
"The JVM index to use"
)
}
import autoImport._
override lazy val projectSettings: Seq[Def.Setting[_]] = List(
javacOptions ++=
List(
"-target",
"1.8",
"-source",
"1.8",
"-bootclasspath",
java8Bootclasspath()
),
(doc / javacOptions) --= List("-target", "1.8"),
(doc / javacOptions) --= bootclasspathSettings(javaToolchainVersion.value),
(doc / javacOptions) --= List("-g"),
javacOptions ++= bootclasspathSettings(javaToolchainVersion.value),
javaOptions ++= bootclasspathSettings(javaToolchainVersion.value),
fork := true,
javaToolchainVersion := "11",
javaToolchainJvmIndex := None,
javaHome :=
Some(getJavaHome(javaToolchainVersion.value, javaToolchainJvmIndex.value))
)
/**
* For Java 8, we need to manually add the Java compiler to the boot
* classpath.
*
* Newer Java versions include the compiler by default.
*/
private def bootclasspathSettings(version: String): List[String] = {
val home = getJavaHome(version)
val toolsJar: File = home / "lib" / "tools.jar"
// The tools.jar file includes the bytecode for the Java compiler in the com.sun.source package.
// The Java compiler is available by default in Java 9+, so we only need to add tools.jar to the
// bootclasspath for Java 8.
if (version == "8" && toolsJar.isFile) {
List(s"-Xbootclasspath/p:$toolsJar")
} else {
List()
}
}
private def java8Bootclasspath(): String = {
(getJavaHome("8") / "jre" / "lib" / "rt.jar").toString
}
private val javaHomeCache: util.Map[String, File] = Collections
.synchronizedMap(new util.HashMap[String, File]())
private def getJavaHome(
version: String,
jvmIndex: Option[String] = None
): File = {
javaHomeCache.computeIfAbsent(
version,
(v: String) => {
val coursier = Paths.get("bin", "coursier")
val index = jvmIndex
.toList
.flatMap(index => "--jvm-index" :: index :: Nil)
val arguments =
List(
"java",
"-jar",
coursier.toString,
"java-home",
"--jvm",
jvmName(v)
) ++ index
new File(Process(arguments).!!.trim)
}
)
}
private def jvmName(version: String) =
version match {
case "8" if isAppleM1 =>
"zulu:8" // the only Java 8 distribution available for Apple M1 is Zulu
case _ =>
version
}
private def isAppleM1 =
scala.util.Properties.isMac && sys.props("os.arch") == "aarch64"
private def jvmArchitecture: String =
sys.props("os.arch") match {
case "x86_64" =>
"amd64"
case _ if isAppleM1 =>
"arm64"
case other =>
other
}
}