Clean up Java version related build properties #26662

This commit is contained in:
Helena Edelson 2019-04-16 00:10:42 -07:00 committed by Johan Andrén
parent 3811da6509
commit 5576c233d0
5 changed files with 91 additions and 92 deletions

View file

@ -19,8 +19,6 @@ object AkkaBuild {
val parallelExecutionByDefault = false // TODO: enable this once we're sure it does not break things
val jdkVersion = sys.props("java.specification.version")
lazy val buildSettings = Dependencies.Versions ++ Seq(
organization := "com.typesafe.akka",
// use the same value as in the build scope, so it can be overriden by stampVersion
@ -100,7 +98,7 @@ object AkkaBuild {
// invocation of 'ByteBuffer.clear()' in EnvelopeBuffer.class with 'javap -c': it should refer to
// "java/nio/ByteBuffer.clear:()Ljava/nio/Buffer" and not "java/nio/ByteBuffer.clear:()Ljava/nio/ByteBuffer":
scalacOptions in Compile ++= (
if (System.getProperty("java.version").startsWith("1."))
if (JavaVersion.isJdk8)
Seq("-target:jvm-1.8")
else
if (scalaBinaryVersion.value == "2.11")
@ -111,18 +109,8 @@ object AkkaBuild {
scalacOptions in Compile ++= (if (allWarnings) Seq("-deprecation") else Nil),
scalacOptions in Test := (scalacOptions in Test).value.filterNot(opt =>
opt == "-Xlog-reflective-calls" || opt.contains("genjavadoc")),
javacOptions in compile ++= DefaultJavacOptions ++ (
if (System.getProperty("java.version").startsWith("1."))
Seq()
else
Seq("-source", "8", "-target", "8", "-bootclasspath", CrossJava.Keys.fullJavaHomes.value("8") + "/jre/lib/rt.jar")
),
javacOptions in test ++= DefaultJavacOptions ++ (
if (System.getProperty("java.version").startsWith("1."))
Seq()
else
Seq("-source", "8", "-target", "8", "-bootclasspath", CrossJava.Keys.fullJavaHomes.value("8") + "/jre/lib/rt.jar")
),
javacOptions in compile ++= DefaultJavacOptions ++ JavaVersion.sourceAndTarget(CrossJava.Keys.fullJavaHomes.value("8")),
javacOptions in test ++= DefaultJavacOptions ++ JavaVersion.sourceAndTarget(CrossJava.Keys.fullJavaHomes.value("8")),
javacOptions in compile ++= (if (allWarnings) Seq("-Xlint:deprecation") else Nil),
javacOptions in doc ++= Seq(),
@ -234,7 +222,7 @@ object AkkaBuild {
javacOptions in compile ++= Seq("-Xdoclint:none"),
javacOptions in test ++= Seq("-Xdoclint:none"),
javacOptions in doc ++= {
if (jdkVersion == "1.8") Seq("-Xdoclint:none")
if (JavaVersion.isJdk8) Seq("-Xdoclint:none")
else Seq("-Xdoclint:none", "--ignore-source-errors")
}
)

View file

@ -52,11 +52,13 @@ object AkkaDisciplinePlugin extends AutoPlugin with ScalafixSupport {
!VersionNumber(scalaVersion.value).matchesSemVer(SemanticSelector("<=2.11.1"))
})
val silencerVersion = "1.3.1"
lazy val silencerSettings = Seq(
libraryDependencies ++= Seq(
compilerPlugin("com.github.ghik" %% "silencer-plugin" % silencerVersion),
"com.github.ghik" %% "silencer-lib" % silencerVersion % Provided))
lazy val silencerSettings = {
val silencerVersion = "1.3.1"
Seq(
libraryDependencies ++= Seq(
compilerPlugin("com.github.ghik" %% "silencer-plugin" % silencerVersion),
"com.github.ghik" %% "silencer-lib" % silencerVersion % Provided))
}
lazy val disciplineSettings =
scalaFixSettings ++

View file

@ -6,18 +6,18 @@ package akka
import java.io.File
import akka.CrossJava.nullBlank
import sbt._
import scala.annotation.tailrec
import scala.collection.immutable.ListMap
import sbt._
import sbt.librarymanagement.SemanticSelector
import sbt.librarymanagement.VersionNumber
import akka.CrossJava.nullBlank
/*
* Tools for discovering different Java versions,
* will be in sbt 1.3.0 (https://github.com/sbt/sbt/pull/4139 et al)
* but until that time replicated here
*/
case class JavaVersion(numbers: Vector[Long], vendor: Option[String]) {
def numberStr: String = numbers.mkString(".")
@ -30,10 +30,26 @@ case class JavaVersion(numbers: Vector[Long], vendor: Option[String]) {
}
}
object JavaVersion {
val specificationVersion: String = sys.props("java.specification.version")
val version: String = sys.props("java.version")
def isJdk8: Boolean =
VersionNumber(specificationVersion).matchesSemVer(SemanticSelector(s"=1.8"))
val isJdk11orHigher: Boolean =
VersionNumber(specificationVersion).matchesSemVer(SemanticSelector(">=11"))
def apply(version: String): JavaVersion = CrossJava.parseJavaVersion(version)
def apply(numbers: Vector[Long], vendor: String): JavaVersion = new JavaVersion(numbers, Option(vendor))
}
def notOnJdk8[T](values: Seq[T]): Seq[T] = if (isJdk8) Seq.empty[T] else values
def sourceAndTarget(fullJavaHome: File): Seq[String] =
if (isJdk8) Seq.empty
else Seq("-source", "8", "-target", "8", "-bootclasspath", fullJavaHome + "/jre/lib/rt.jar")
}
object CrossJava {
object Keys {
@ -47,8 +63,7 @@ object CrossJava {
val crossJavaSettings = Seq(
discoveredJavaHomes := CrossJava.discoverJavaHomes,
javaHomes := ListMap.empty,
fullJavaHomes := CrossJava.expandJavaHomes(discoveredJavaHomes.value ++ javaHomes.value),
)
fullJavaHomes := CrossJava.expandJavaHomes(discoveredJavaHomes.value ++ javaHomes.value))
// parses jabaa style version number adopt@1.8
def parseJavaVersion(version: String): JavaVersion = {
@ -70,7 +85,7 @@ object CrossJava {
}
def discoverJavaHomes: ListMap[String, File] = {
ListMap(JavaDiscoverConfig.configs flatMap { _.javaHomes } sortWith (versionOrder): _*)
ListMap(JavaDiscoverConfig.configs.flatMap { _.javaHomes }.sortWith(versionOrder): _*)
}
sealed trait JavaDiscoverConf {
@ -104,31 +119,28 @@ object CrossJava {
class LinuxDiscoverConfig(base: File) extends JavaDiscoverConf {
def javaHomes: Vector[(String, File)] =
wrapNull(base.list())
.collect {
case dir@JavaHomeDir(_, m, n) => JavaVersion(nullBlank(m) + n).toString -> (base / dir)
}
wrapNull(base.list()).collect {
case dir @ JavaHomeDir(_, m, n) => JavaVersion(nullBlank(m) + n).toString -> (base / dir)
}
}
class MacOsDiscoverConfig extends JavaDiscoverConf {
val base: File = file("/Library") / "Java" / "JavaVirtualMachines"
def javaHomes: Vector[(String, File)] =
wrapNull(base.list())
.collect {
case dir@JavaHomeDir(_, m, n) =>
JavaVersion(nullBlank(m) + n).toString -> (base / dir / "Contents" / "Home")
}
wrapNull(base.list()).collect {
case dir @ JavaHomeDir(_, m, n) =>
JavaVersion(nullBlank(m) + n).toString -> (base / dir / "Contents" / "Home")
}
}
class WindowsDiscoverConfig extends JavaDiscoverConf {
val base: File = file("C://Program Files/Java")
def javaHomes: Vector[(String, File)] =
wrapNull(base.list())
.collect {
case dir@JavaHomeDir(_, m, n) => JavaVersion(nullBlank(m) + n).toString -> (base / dir)
}
wrapNull(base.list()).collect {
case dir @ JavaHomeDir(_, m, n) => JavaVersion(nullBlank(m) + n).toString -> (base / dir)
}
}
// See https://github.com/shyiko/jabba
@ -137,25 +149,25 @@ object CrossJava {
val JavaHomeDir = """([\w\-]+)\@(1\.)?([0-9]+).*""".r
def javaHomes: Vector[(String, File)] =
wrapNull(base.list())
.collect {
case dir@JavaHomeDir(_, m, n) =>
val v = JavaVersion(nullBlank(m) + n).toString
if ((base / dir / "Contents" / "Home").exists) v -> (base / dir / "Contents" / "Home")
else v -> (base / dir)
}
wrapNull(base.list()).collect {
case dir @ JavaHomeDir(_, m, n) =>
val v = JavaVersion(nullBlank(m) + n).toString
if ((base / dir / "Contents" / "Home").exists) v -> (base / dir / "Contents" / "Home")
else v -> (base / dir)
}
}
class JavaHomeDiscoverConfig extends JavaDiscoverConf {
def javaHomes: Vector[(String, File)] =
sys.env.get("JAVA_HOME")
sys.env
.get("JAVA_HOME")
.map(new java.io.File(_))
.filter(_.exists())
.flatMap { javaHome =>
val base = javaHome.getParentFile
javaHome.getName match {
case dir@JavaHomeDir(_, m, n) => Some(JavaVersion(nullBlank(m) + n).toString -> (base / dir))
case _ => None
case dir @ JavaHomeDir(_, m, n) => Some(JavaVersion(nullBlank(m) + n).toString -> (base / dir))
case _ => None
}
}
.toVector
@ -167,21 +179,19 @@ object CrossJava {
new LinuxDiscoverConfig(file("/usr") / "lib" / "jvm"),
new MacOsDiscoverConfig,
new WindowsDiscoverConfig,
new JavaHomeDiscoverConfig,
)
new JavaHomeDiscoverConfig)
}
def nullBlank(s: String): String =
if (s eq null) ""
else s
def nullBlank(s: String): String =
if (s eq null) ""
else s
// expand Java versions to 1-20 to 1.x, and vice versa to accept both "1.8" and "8"
private val oneDot = Map((1L to 20L).toVector flatMap { i =>
private val oneDot = Map((1L to 20L).toVector.flatMap { i =>
Vector(Vector(i) -> Vector(1L, i), Vector(1L, i) -> Vector(i))
}: _*)
def expandJavaHomes(hs: Map[String, File]): Map[String, File] =
hs flatMap {
hs.flatMap {
case (k, v) =>
val jv = JavaVersion(k)
if (oneDot.contains(jv.numbers))

View file

@ -45,10 +45,17 @@ object Scaladoc extends AutoPlugin {
def scaladocOptions(ver: String, base: File): List[String] = {
val urlString = GitHub.url(ver) + "/€{FILE_PATH}.scala"
val opts = List("-implicits", "-groups", "-doc-source-url", urlString, "-sourcepath", base.getAbsolutePath,
"-doc-title", "Akka",
"-doc-version", ver
)
val opts = List(
"-implicits",
"-groups",
"-doc-source-url",
urlString,
"-sourcepath",
base.getAbsolutePath,
"-doc-title",
"Akka",
"-doc-version",
ver)
CliOptions.scaladocDiagramsEnabled.ifTrue("-diagrams").toList ::: opts
}
@ -123,17 +130,10 @@ object UnidocRoot extends AutoPlugin {
.getOrElse(sbtunidoc.ScalaUnidocPlugin)
val akkaSettings = UnidocRoot.CliOptions.genjavadocEnabled
.ifTrue(
Seq(
javacOptions in (JavaUnidoc, unidoc) := {
if (AkkaBuild.jdkVersion == "1.8") Seq("-Xdoclint:none")
else Seq(
"-Xdoclint:none",
"--frames",
"--ignore-source-errors",
"--no-module-directories")
}
))
.ifTrue(Seq(javacOptions in (JavaUnidoc, unidoc) := {
if (JavaVersion.isJdk8) Seq("-Xdoclint:none")
else Seq("-Xdoclint:none", "--frames", "--ignore-source-errors", "--no-module-directories")
}))
.getOrElse(Nil)
override lazy val projectSettings = {
@ -166,8 +166,8 @@ object BootstrapGenjavadoc extends AutoPlugin {
override def requires =
UnidocRoot.CliOptions.genjavadocEnabled
.ifTrue {
val onJdk8 = System.getProperty("java.version").startsWith("1.")
require(!onJdk8, "Javadoc generation requires at least jdk 11")
// require 11, fail fast for 8, 9, 10
require(JavaVersion.isJdk11orHigher, "Javadoc generation requires at least jdk 11")
sbtunidoc.GenJavadocPlugin
}
.getOrElse(plugins.JvmPlugin)

View file

@ -8,40 +8,39 @@ import sbt._
import sbt.Keys._
object Jdk9 extends AutoPlugin {
import JavaVersion.notOnJdk8
lazy val CompileJdk9 = config("CompileJdk9").extend(Compile)
def notOnScala211[T](scalaBinaryVersion: String, values: Seq[T]): Seq[T] = scalaBinaryVersion match {
case "2.11" => Seq()
case _ => values
case _ => values
}
def notOnJdk8[T](values: Seq[T]): Seq[T] =
if (System.getProperty("java.version").startsWith("1.")) Seq()
else values
val SCALA_SOURCE_DIRECTORY = "scala-jdk-9"
val SCALA_TEST_SOURCE_DIRECTORY = "scala-jdk9-only"
val JAVA_SOURCE_DIRECTORY = "java-jdk-9"
val JAVA_TEST_SOURCE_DIRECTORY = "java-jdk9-only"
val compileJdk9Settings = Seq(
// following the scala-2.12, scala-sbt-1.0, ... convention
unmanagedSourceDirectories := notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq(
(Compile / sourceDirectory).value / SCALA_SOURCE_DIRECTORY,
(Compile / sourceDirectory).value / JAVA_SOURCE_DIRECTORY
))),
scalacOptions := AkkaBuild.DefaultScalacOptions ++ notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq("-release", "11"))),
javacOptions := AkkaBuild.DefaultJavacOptions ++ notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq("--release", "11")))
)
unmanagedSourceDirectories := notOnJdk8(
notOnScala211(
scalaBinaryVersion.value,
Seq(
(Compile / sourceDirectory).value / SCALA_SOURCE_DIRECTORY,
(Compile / sourceDirectory).value / JAVA_SOURCE_DIRECTORY))),
scalacOptions := AkkaBuild.DefaultScalacOptions ++ notOnJdk8(
notOnScala211(scalaBinaryVersion.value, Seq("-release", "11"))),
javacOptions := AkkaBuild.DefaultJavacOptions ++ notOnJdk8(
notOnScala211(scalaBinaryVersion.value, Seq("--release", "11"))))
val compileSettings = Seq(
// It might have been more 'neat' to add the jdk9 products to the jar via packageBin/mappings, but that doesn't work with the OSGi plugin,
// so we add them to the fullClasspath instead.
// Compile / packageBin / mappings
// ++= (CompileJdk9 / products).value.flatMap(Path.allSubpaths),
Compile / fullClasspath ++= (CompileJdk9 / exportedProducts).value
)
Compile / fullClasspath ++= (CompileJdk9 / exportedProducts).value)
override def trigger = noTrigger
override def projectConfigurations = Seq(CompileJdk9)