Skip to content

Commit

Permalink
refactor: Extract getCompilationOptions method
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Oct 11, 2023
1 parent ccd23c8 commit 03c5355
Showing 1 changed file with 63 additions and 63 deletions.
126 changes: 63 additions & 63 deletions backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -280,74 +280,14 @@ object Compiler {
)
}

var isFatalWarningsEnabled: Boolean = false
val isFatalWarningsEnabled: Boolean =
compileInputs.scalacOptions.exists(_ == "-Xfatal-warnings")
def getInputs(compilers: Compilers): Inputs = {
val options = getCompilationOptions(compileInputs)
val options = getCompilationOptions(compileInputs, logger, newClassesDir)
val setup = getSetup(compileInputs)
Inputs.of(compilers, options, setup, compileInputs.previousResult)
}

def getCompilationOptions(inputs: CompileInputs): CompileOptions = {
// Sources are all files
val sources = inputs.sources.map(path => converter.toVirtualFile(path.underlying))
val classpath = inputs.classpath.map(path => converter.toVirtualFile(path.underlying))
def existsReleaseSetting = inputs.scalacOptions.exists(opt =>
opt.startsWith("-release") ||
opt.startsWith("--release") ||
opt.startsWith("-java-output-version")
)
def sameHome = inputs.javacBin match {
case Some(bin) => bin.getParent.getParent == JavaRuntime.home
case None => false
}

val scalacOptions = inputs.javacBin.flatMap(binary =>
// <JAVA_HOME>/bin/java
JavaRuntime.getJavaVersionFromJavaHome(binary.getParent.getParent)
) match {
case None => inputs.scalacOptions
case Some(_) if existsReleaseSetting || sameHome => inputs.scalacOptions
case Some(version) =>
try {
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
if (bloopNumVer > numVer) {
inputs.scalacOptions ++ List("-release", numVer.toString())
} else {
logger.warn(
s"Bloop is run with ${JavaRuntime.version} but your code requires $version to compile, " +
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
)
inputs.scalacOptions
}
} catch {
case NonFatal(_) =>
inputs.scalacOptions
}
}

val optionsWithoutFatalWarnings = scalacOptions.flatMap { option =>
if (option != "-Xfatal-warnings") List(option)
else {
if (!isFatalWarningsEnabled) isFatalWarningsEnabled = true
Nil
}
}

// Enable fatal warnings in the reporter if they are enabled in the build
if (isFatalWarningsEnabled)
inputs.reporter.enableFatalWarnings()

CompileOptions
.create()
.withClassesDirectory(newClassesDir)
.withSources(sources)
.withClasspath(classpath)
.withScalacOptions(optionsWithoutFatalWarnings)
.withJavacOptions(inputs.javacOptions)
.withOrder(inputs.compileOrder)
}

def getSetup(compileInputs: CompileInputs): Setup = {
val skip = false
val empty = Array.empty[T2[String, String]]
Expand Down Expand Up @@ -664,6 +604,66 @@ object Compiler {
}
}

private def getCompilationOptions(
inputs: CompileInputs,
logger: Logger,
newClassesDir: Path
): CompileOptions = {
// Sources are all files
val sources = inputs.sources.map(path => converter.toVirtualFile(path.underlying))
val classpath = inputs.classpath.map(path => converter.toVirtualFile(path.underlying))
def existsReleaseSetting = inputs.scalacOptions.exists(opt =>
opt.startsWith("-release") ||
opt.startsWith("--release") ||
opt.startsWith("-java-output-version")
)
def sameHome = inputs.javacBin match {
case Some(bin) => bin.getParent.getParent == JavaRuntime.home
case None => false
}

val scalacOptions = inputs.javacBin.flatMap(binary =>
// <JAVA_HOME>/bin/java
JavaRuntime.getJavaVersionFromJavaHome(binary.getParent.getParent)
) match {
case None => inputs.scalacOptions
case Some(_) if existsReleaseSetting || sameHome => inputs.scalacOptions
case Some(version) =>
try {
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
if (bloopNumVer > numVer) {
inputs.scalacOptions ++ List("-release", numVer.toString())
} else {
logger.warn(
s"Bloop is runing with ${JavaRuntime.version} but your code requires $version to compile, " +
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
)
inputs.scalacOptions
}
} catch {
case NonFatal(_) =>
inputs.scalacOptions
}
}

val optionsWithoutFatalWarnings = scalacOptions.filter(_ != "-Xfatal-warnings")
val isFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length

// Enable fatal warnings in the reporter if they are enabled in the build
if (isFatalWarningsEnabled)
inputs.reporter.enableFatalWarnings()

CompileOptions
.create()
.withClassesDirectory(newClassesDir)
.withSources(sources)
.withClasspath(classpath)
.withScalacOptions(optionsWithoutFatalWarnings)
.withJavacOptions(inputs.javacOptions)
.withOrder(inputs.compileOrder)
}

def toBackgroundTasks(
tasks: List[(AbsolutePath, Reporter, BraveTracer) => Task[Unit]]
): CompileBackgroundTasks = {
Expand Down

0 comments on commit 03c5355

Please sign in to comment.