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 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( lazy val buildSettings = Dependencies.Versions ++ Seq(
organization := "com.typesafe.akka", organization := "com.typesafe.akka",
// use the same value as in the build scope, so it can be overriden by stampVersion // 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 // 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": // "java/nio/ByteBuffer.clear:()Ljava/nio/Buffer" and not "java/nio/ByteBuffer.clear:()Ljava/nio/ByteBuffer":
scalacOptions in Compile ++= ( scalacOptions in Compile ++= (
if (System.getProperty("java.version").startsWith("1.")) if (JavaVersion.isJdk8)
Seq("-target:jvm-1.8") Seq("-target:jvm-1.8")
else else
if (scalaBinaryVersion.value == "2.11") if (scalaBinaryVersion.value == "2.11")
@ -111,18 +109,8 @@ object AkkaBuild {
scalacOptions in Compile ++= (if (allWarnings) Seq("-deprecation") else Nil), scalacOptions in Compile ++= (if (allWarnings) Seq("-deprecation") else Nil),
scalacOptions in Test := (scalacOptions in Test).value.filterNot(opt => scalacOptions in Test := (scalacOptions in Test).value.filterNot(opt =>
opt == "-Xlog-reflective-calls" || opt.contains("genjavadoc")), opt == "-Xlog-reflective-calls" || opt.contains("genjavadoc")),
javacOptions in compile ++= DefaultJavacOptions ++ ( javacOptions in compile ++= DefaultJavacOptions ++ JavaVersion.sourceAndTarget(CrossJava.Keys.fullJavaHomes.value("8")),
if (System.getProperty("java.version").startsWith("1.")) javacOptions in test ++= DefaultJavacOptions ++ JavaVersion.sourceAndTarget(CrossJava.Keys.fullJavaHomes.value("8")),
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 ++= (if (allWarnings) Seq("-Xlint:deprecation") else Nil), javacOptions in compile ++= (if (allWarnings) Seq("-Xlint:deprecation") else Nil),
javacOptions in doc ++= Seq(), javacOptions in doc ++= Seq(),
@ -234,7 +222,7 @@ object AkkaBuild {
javacOptions in compile ++= Seq("-Xdoclint:none"), javacOptions in compile ++= Seq("-Xdoclint:none"),
javacOptions in test ++= Seq("-Xdoclint:none"), javacOptions in test ++= Seq("-Xdoclint:none"),
javacOptions in doc ++= { javacOptions in doc ++= {
if (jdkVersion == "1.8") Seq("-Xdoclint:none") if (JavaVersion.isJdk8) Seq("-Xdoclint:none")
else Seq("-Xdoclint:none", "--ignore-source-errors") 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")) !VersionNumber(scalaVersion.value).matchesSemVer(SemanticSelector("<=2.11.1"))
}) })
val silencerVersion = "1.3.1" lazy val silencerSettings = {
lazy val silencerSettings = Seq( val silencerVersion = "1.3.1"
libraryDependencies ++= Seq( Seq(
compilerPlugin("com.github.ghik" %% "silencer-plugin" % silencerVersion), libraryDependencies ++= Seq(
"com.github.ghik" %% "silencer-lib" % silencerVersion % Provided)) compilerPlugin("com.github.ghik" %% "silencer-plugin" % silencerVersion),
"com.github.ghik" %% "silencer-lib" % silencerVersion % Provided))
}
lazy val disciplineSettings = lazy val disciplineSettings =
scalaFixSettings ++ scalaFixSettings ++

View file

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

View file

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

View file

@ -8,40 +8,39 @@ import sbt._
import sbt.Keys._ import sbt.Keys._
object Jdk9 extends AutoPlugin { object Jdk9 extends AutoPlugin {
import JavaVersion.notOnJdk8
lazy val CompileJdk9 = config("CompileJdk9").extend(Compile) lazy val CompileJdk9 = config("CompileJdk9").extend(Compile)
def notOnScala211[T](scalaBinaryVersion: String, values: Seq[T]): Seq[T] = scalaBinaryVersion match { def notOnScala211[T](scalaBinaryVersion: String, values: Seq[T]): Seq[T] = scalaBinaryVersion match {
case "2.11" => Seq() 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_SOURCE_DIRECTORY = "scala-jdk-9"
val SCALA_TEST_SOURCE_DIRECTORY = "scala-jdk9-only" val SCALA_TEST_SOURCE_DIRECTORY = "scala-jdk9-only"
val JAVA_SOURCE_DIRECTORY = "java-jdk-9" val JAVA_SOURCE_DIRECTORY = "java-jdk-9"
val JAVA_TEST_SOURCE_DIRECTORY = "java-jdk9-only" val JAVA_TEST_SOURCE_DIRECTORY = "java-jdk9-only"
val compileJdk9Settings = Seq( val compileJdk9Settings = Seq(
// following the scala-2.12, scala-sbt-1.0, ... convention // following the scala-2.12, scala-sbt-1.0, ... convention
unmanagedSourceDirectories := notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq( unmanagedSourceDirectories := notOnJdk8(
(Compile / sourceDirectory).value / SCALA_SOURCE_DIRECTORY, notOnScala211(
(Compile / sourceDirectory).value / JAVA_SOURCE_DIRECTORY scalaBinaryVersion.value,
))), Seq(
scalacOptions := AkkaBuild.DefaultScalacOptions ++ notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq("-release", "11"))), (Compile / sourceDirectory).value / SCALA_SOURCE_DIRECTORY,
javacOptions := AkkaBuild.DefaultJavacOptions ++ notOnJdk8(notOnScala211(scalaBinaryVersion.value, Seq("--release", "11"))) (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( 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, // 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. // so we add them to the fullClasspath instead.
// Compile / packageBin / mappings // Compile / packageBin / mappings
// ++= (CompileJdk9 / products).value.flatMap(Path.allSubpaths), // ++= (CompileJdk9 / products).value.flatMap(Path.allSubpaths),
Compile / fullClasspath ++= (CompileJdk9 / exportedProducts).value Compile / fullClasspath ++= (CompileJdk9 / exportedProducts).value)
)
override def trigger = noTrigger override def trigger = noTrigger
override def projectConfigurations = Seq(CompileJdk9) override def projectConfigurations = Seq(CompileJdk9)