sbt/main/actions/Compiler.scala

100 lines
4.3 KiB
Scala
Raw Normal View History

2010-09-04 14:46:36 +02:00
/* sbt -- Simple Build Tool
* Copyright 2010 Mark Harrah
*/
package sbt
import xsbti.{Logger => _,_}
2011-03-02 12:46:28 +01:00
import compiler._
2010-09-04 14:46:36 +02:00
import inc._
import java.io.File
object Compiler
2010-09-04 14:46:36 +02:00
{
2010-09-05 17:12:44 +02:00
val DefaultMaxErrors = 100
def allProblems(inc: Incomplete): Seq[Problem] =
allProblems(inc :: Nil)
def allProblems(incs: Seq[Incomplete]): Seq[Problem] =
problems(Incomplete.allExceptions(incs).toSeq)
def problems(es: Seq[Throwable]): Seq[Problem] =
es flatMap {
case cf: xsbti.CompileFailed => cf.problems
case _ => Nil
}
2011-04-12 04:12:03 +02:00
final case class Inputs(compilers: Compilers, config: Options, incSetup: IncSetup)
final case class Options(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, order: CompileOrder.Value)
final case class IncSetup(analysisMap: Map[File, Analysis], cacheDirectory: File)
final case class Compilers(scalac: AnalyzingCompiler, javac: JavaCompiler)
2010-09-04 14:46:36 +02:00
2011-04-04 03:15:35 +02:00
def inputs(classpath: Seq[File], sources: Seq[File], outputDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, order: CompileOrder.Value)(implicit compilers: Compilers, log: Logger): Inputs =
2010-09-04 14:46:36 +02:00
{
import Path._
val classesDirectory = outputDirectory / "classes"
val cacheDirectory = outputDirectory / "cache"
val augClasspath = classesDirectory.asFile +: classpath
2011-04-04 03:15:35 +02:00
inputs(augClasspath, sources, classesDirectory, options, javacOptions, Map.empty, cacheDirectory, maxErrors, order)
2010-09-04 14:46:36 +02:00
}
2011-04-04 03:15:35 +02:00
def inputs(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], analysisMap: Map[File, Analysis], cacheDirectory: File, maxErrors: Int, order: CompileOrder.Value)(implicit compilers: Compilers, log: Logger): Inputs =
2010-09-04 14:46:36 +02:00
new Inputs(
compilers,
2011-04-04 03:15:35 +02:00
new Options(classpath, sources, classesDirectory, options, javacOptions, maxErrors, order),
new IncSetup(analysisMap, cacheDirectory)
2010-09-04 14:46:36 +02:00
)
2010-09-05 17:12:44 +02:00
2011-05-26 04:44:22 +02:00
def compilers(cpOptions: ClasspathOptions)(implicit app: AppConfiguration, log: Logger): Compilers =
2010-09-05 17:12:44 +02:00
{
val scalaProvider = app.provider.scalaProvider
2011-05-26 04:44:22 +02:00
compilers(ScalaInstance(scalaProvider.version, scalaProvider.launcher), cpOptions)
2010-09-05 17:12:44 +02:00
}
2011-05-26 04:44:22 +02:00
def compilers(instance: ScalaInstance, cpOptions: ClasspathOptions)(implicit app: AppConfiguration, log: Logger): Compilers =
compilers(instance, cpOptions, None)
2010-09-04 14:46:36 +02:00
def compilers(instance: ScalaInstance, cpOptions: ClasspathOptions, javaHome: Option[File])(implicit app: AppConfiguration, log: Logger): Compilers =
2010-09-04 14:46:36 +02:00
{
val javac = directOrFork(instance, cpOptions, javaHome)
2010-09-04 14:46:36 +02:00
compilers(instance, cpOptions, javac)
}
def compilers(instance: ScalaInstance, cpOptions: ClasspathOptions, javac: (Seq[String], Logger) => Int)(implicit app: AppConfiguration, log: Logger): Compilers =
{
val javaCompiler = JavaCompiler.fork(cpOptions, instance)(javac)
compilers(instance, cpOptions, javaCompiler)
}
def compilers(instance: ScalaInstance, cpOptions: ClasspathOptions, javac: JavaCompiler)(implicit app: AppConfiguration, log: Logger): Compilers =
{
val scalac = scalaCompiler(instance, cpOptions)
new Compilers(scalac, javac)
}
def scalaCompiler(instance: ScalaInstance, cpOptions: ClasspathOptions)(implicit app: AppConfiguration, log: Logger): AnalyzingCompiler =
{
val launcher = app.provider.scalaProvider.launcher
val componentManager = new ComponentManager(launcher.globalLock, app.provider.components, log)
new AnalyzingCompiler(instance, componentManager, cpOptions, log)
}
def directOrFork(instance: ScalaInstance, cpOptions: ClasspathOptions, javaHome: Option[File]): JavaCompiler =
if(javaHome.isDefined)
JavaCompiler.fork(cpOptions, instance)(forkJavac(javaHome))
else
JavaCompiler.directOrFork(cpOptions, instance)( forkJavac(None) )
def forkJavac(javaHome: Option[File]): (Seq[String], Logger) => Int =
{
import Path._
val exec = javaHome match { case None => "javac"; case Some(jh) => (jh / "bin" / "javac").absolutePath }
(args: Seq[String], log: Logger) => {
log.debug("Forking javac: " + exec + " " + args.mkString(" "))
Process(exec, args) ! log
}
}
2010-09-04 14:46:36 +02:00
def apply(in: Inputs, log: Logger): Analysis =
2010-09-04 14:46:36 +02:00
{
import in.compilers._
import in.config._
import in.incSetup._
2011-03-19 21:11:03 +01:00
val agg = new AggressiveCompile(cacheDirectory)
2011-04-04 03:15:35 +02:00
agg(scalac, javac, sources, classpath, classesDirectory, options, javacOptions, analysisMap, maxErrors, order)(log)
2010-09-04 14:46:36 +02:00
}
}