2023-01-08 17:13:31 +08:00
/*
* Licensed to the Apache Software Foundation ( ASF ) under one or more
* license agreements ; and to You under the Apache License , version 2.0 :
*
* https : //www.apache.org/licenses/LICENSE-2.0
*
2023-06-22 14:19:26 +01:00
* This file is part of the Apache Pekko project , which was derived from Akka .
2023-01-08 17:13:31 +08:00
*/
2019-10-03 05:02:40 +02:00
/*
2022-02-04 12:36:44 +01:00
* Copyright ( C ) 2009 - 2022 Lightbend Inc . < https : //www.lightbend.com>
2019-10-03 05:02:40 +02:00
*/
import java.io.File
import sbt._
import sbt.librarymanagement.SemanticSelector
import sbt.librarymanagement.VersionNumber
object JdkOptions extends AutoPlugin {
object autoImport {
2019-11-08 15:59:49 +01:00
val jdk8home = settingKey [ String ] ( "JDK 8 home. Only needs to be set when it cannot be auto-detected by sbt" )
2020-08-05 13:12:29 +01:00
val targetSystemJdk = settingKey [ Boolean ] (
"Target the system JDK instead of building against JDK 8. When this is enabled resulting artifacts may not work on JDK 8!" )
2019-10-03 05:02:40 +02:00
}
import autoImport._
val specificationVersion : String = sys . props ( "java.specification.version" )
val isJdk8 : Boolean =
VersionNumber ( specificationVersion ) . matchesSemVer ( SemanticSelector ( s" =1.8 " ) )
val isJdk11orHigher : Boolean =
VersionNumber ( specificationVersion ) . matchesSemVer ( SemanticSelector ( ">=11" ) )
2022-01-27 16:14:29 +01:00
val isJdk17orHigher : Boolean =
VersionNumber ( specificationVersion ) . matchesSemVer ( SemanticSelector ( ">=17" ) )
val versionSpecificJavaOptions =
if ( isJdk17orHigher ) {
// for aeron
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" : :
// for LevelDB
"--add-opens=java.base/java.nio=ALL-UNNAMED" : : Nil
} else Nil
2019-10-03 05:02:40 +02:00
def notOnJdk8 [ T ] ( values : Seq [ T ] ) : Seq [ T ] = if ( isJdk8 ) Seq . empty [ T ] else values
2020-08-05 13:12:29 +01:00
def targetJdkScalacOptions (
targetSystemJdk : Boolean ,
jdk8home : Option [ File ] ,
2021-10-25 08:18:22 +02:00
fullJavaHomes : Map [ String , File ] ,
scalaVersion : String ) : Seq [ String ] =
2019-10-03 05:02:40 +02:00
selectOptions (
targetSystemJdk ,
2019-11-08 15:59:49 +01:00
jdk8home ,
2019-10-03 05:02:40 +02:00
fullJavaHomes ,
2023-03-21 13:52:14 +01:00
Seq ( if ( scalaVersion . startsWith ( "3." ) ) "-Xtarget:8" else "release:8" ) ,
( java8home : File ) => Seq ( "-release" , "8" ) )
2020-08-05 13:12:29 +01:00
def targetJdkJavacOptions (
targetSystemJdk : Boolean ,
jdk8home : Option [ File ] ,
fullJavaHomes : Map [ String , File ] ) : Seq [ String ] =
2019-10-03 05:02:40 +02:00
selectOptions (
targetSystemJdk ,
2019-11-08 15:59:49 +01:00
jdk8home ,
2019-10-03 05:02:40 +02:00
fullJavaHomes ,
Nil ,
// '-release 8' would be a neater option here, but is currently not an
2023-03-21 13:52:14 +01:00
// option because it doesn't provide access to `sun.misc.Unsafe` https://github.com/akka/akka/issues/27079
2020-08-05 13:12:29 +01:00
( java8home : File ) => Seq ( "-source" , "8" , "-target" , "8" , "-bootclasspath" , java8home + "/jre/lib/rt.jar" ) )
2019-10-03 05:02:40 +02:00
2020-08-05 13:12:29 +01:00
private def selectOptions (
targetSystemJdk : Boolean ,
jdk8home : Option [ File ] ,
fullJavaHomes : Map [ String , File ] ,
jdk8options : Seq [ String ] ,
jdk11options : File => Seq [ String ] ) : Seq [ String ] =
2019-10-03 05:02:40 +02:00
if ( targetSystemJdk )
Nil
else if ( isJdk8 )
jdk8options
2020-08-05 13:12:29 +01:00
else
jdk8home . orElse ( fullJavaHomes . get ( "8" ) ) match {
case Some ( java8home ) =>
jdk11options ( java8home )
case None =>
throw new MessageOnlyException (
2023-05-24 11:05:51 +02:00
"A JDK 8 installation was not found, but is required to build Apache Pekko. To manually specify a JDK 8 installation, set the JAVA_8_HOME environment variable to its path or use the \"set every jdk8home := \\\"/path/to/jdk\\\" sbt command. If you have no JDK 8 installation, target your system JDK with the \"set every targetSystemJdk := true\" sbt command, but beware resulting artifacts will not work on JDK 8" )
2020-08-05 13:12:29 +01:00
}
2019-10-03 05:02:40 +02:00
2020-08-05 13:12:29 +01:00
val targetJdkSettings = Seq ( targetSystemJdk : = false , jdk8home : = sys . env . get ( "JAVA_8_HOME" ) . getOrElse ( "" ) )
2019-10-03 05:02:40 +02:00
}