2010-07-05 18:53:37 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2009, 2010 Mark Harrah
|
|
|
|
|
*/
|
|
|
|
|
package sbt
|
2011-03-02 12:46:28 +01:00
|
|
|
package compiler
|
2009-09-05 21:01:04 +02:00
|
|
|
|
2010-10-23 22:34:22 +02:00
|
|
|
import xsbti.{AnalysisCallback, Logger => xLogger, Reporter}
|
2012-07-10 19:12:39 +02:00
|
|
|
import xsbti.compile.{CachedCompiler, CachedCompilerProvider, DependencyChanges, GlobalsCache, CompileProgress, Output}
|
2009-09-05 21:01:04 +02:00
|
|
|
import java.io.File
|
2009-10-08 03:27:53 +02:00
|
|
|
import java.net.{URL, URLClassLoader}
|
2009-09-05 21:01:04 +02:00
|
|
|
|
|
|
|
|
/** Interface to the Scala compiler that uses the dependency analysis plugin. This class uses the Scala library and compiler
|
2009-09-06 22:05:31 +02:00
|
|
|
* provided by scalaInstance. This class requires a ComponentManager in order to obtain the interface code to scalac and
|
|
|
|
|
* the analysis plugin. Because these call Scala code for a different Scala version than the one used for this class, they must
|
|
|
|
|
* be compiled for the version of Scala being used.*/
|
2012-04-29 00:58:38 +02:00
|
|
|
final class AnalyzingCompiler(val scalaInstance: xsbti.compile.ScalaInstance, val provider: CompilerInterfaceProvider, val cp: xsbti.compile.ClasspathOptions, log: Logger) extends CachedCompilerProvider
|
2009-09-05 21:01:04 +02:00
|
|
|
{
|
2012-04-18 14:07:53 +02:00
|
|
|
def this(scalaInstance: ScalaInstance, provider: CompilerInterfaceProvider, log: Logger) = this(scalaInstance, provider, ClasspathOptions.auto, log)
|
2012-07-10 19:12:39 +02:00
|
|
|
def apply(sources: Seq[File], changes: DependencyChanges, classpath: Seq[File], singleOutput: File, options: Seq[String], callback: AnalysisCallback, maximumErrors: Int, cache: GlobalsCache, log: Logger)
|
2009-09-05 21:01:04 +02:00
|
|
|
{
|
2012-07-10 19:12:39 +02:00
|
|
|
val arguments = (new CompilerArguments(scalaInstance, cp))(Nil, classpath, None, options)
|
|
|
|
|
val output = CompileOutput(singleOutput)
|
2012-08-28 18:01:26 +02:00
|
|
|
compile(sources, changes, arguments, output, callback, new LoggerReporter(maximumErrors, log, p => p), cache, log, None)
|
2012-04-29 00:58:38 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-11 10:15:04 +02:00
|
|
|
def compile(sources: Seq[File], changes: DependencyChanges, options: Seq[String], output: Output, callback: AnalysisCallback, reporter: Reporter, cache: GlobalsCache, log: Logger, progressOpt: Option[CompileProgress]): Unit =
|
2012-04-29 00:58:38 +02:00
|
|
|
{
|
2012-07-10 19:12:39 +02:00
|
|
|
val cached = cache(options.toArray, output, !changes.isEmpty, this, log, reporter)
|
|
|
|
|
val progress = progressOpt getOrElse IgnoreProgress
|
|
|
|
|
compile(sources, changes, callback, log, reporter, progress, cached)
|
2012-04-29 00:58:38 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-10 19:12:39 +02:00
|
|
|
def compile(sources: Seq[File], changes: DependencyChanges, callback: AnalysisCallback, log: Logger, reporter: Reporter, progress: CompileProgress, compiler: CachedCompiler)
|
2012-04-29 00:58:38 +02:00
|
|
|
{
|
|
|
|
|
call("xsbt.CompilerInterface", "run", log)(
|
2012-07-10 19:12:39 +02:00
|
|
|
classOf[Array[File]], classOf[DependencyChanges], classOf[AnalysisCallback], classOf[xLogger], classOf[Reporter], classOf[CompileProgress], classOf[CachedCompiler]) (
|
|
|
|
|
sources.toArray, changes, callback, log, reporter, progress, compiler )
|
2010-06-27 15:16:53 +02:00
|
|
|
}
|
2012-07-10 19:12:39 +02:00
|
|
|
def newCachedCompiler(arguments: Array[String], output: Output, log: xLogger, reporter: Reporter, resident: Boolean): CachedCompiler =
|
|
|
|
|
newCachedCompiler(arguments: Seq[String], output, log, reporter, resident)
|
2010-10-23 22:34:22 +02:00
|
|
|
|
2012-07-10 19:12:39 +02:00
|
|
|
def newCachedCompiler(arguments: Seq[String], output: Output, log: xLogger, reporter: Reporter, resident: Boolean): CachedCompiler =
|
2010-06-27 15:16:53 +02:00
|
|
|
{
|
2012-04-29 00:58:38 +02:00
|
|
|
call("xsbt.CompilerInterface", "newCompiler", log)(
|
2012-07-10 19:12:39 +02:00
|
|
|
classOf[Array[String]], classOf[Output], classOf[xLogger], classOf[Reporter], classOf[Boolean] ) (
|
|
|
|
|
arguments.toArray[String] : Array[String], output, log, reporter, resident: java.lang.Boolean ).
|
2012-04-29 00:58:38 +02:00
|
|
|
asInstanceOf[CachedCompiler]
|
2009-10-03 15:39:16 +02:00
|
|
|
}
|
2010-10-23 22:34:22 +02:00
|
|
|
|
2010-07-05 18:53:37 +02:00
|
|
|
def doc(sources: Seq[File], classpath: Seq[File], outputDirectory: File, options: Seq[String], maximumErrors: Int, log: Logger): Unit =
|
2010-10-23 22:34:22 +02:00
|
|
|
doc(sources, classpath, outputDirectory, options, log, new LoggerReporter(maximumErrors, log))
|
|
|
|
|
def doc(sources: Seq[File], classpath: Seq[File], outputDirectory: File, options: Seq[String], log: Logger, reporter: Reporter): Unit =
|
2009-10-03 15:39:16 +02:00
|
|
|
{
|
2012-07-10 19:12:39 +02:00
|
|
|
val arguments = (new CompilerArguments(scalaInstance, cp))(sources, classpath, Some(outputDirectory), options)
|
2012-04-29 00:58:38 +02:00
|
|
|
call("xsbt.ScaladocInterface", "run", log) (classOf[Array[String]], classOf[xLogger], classOf[Reporter]) (
|
2010-10-23 22:34:22 +02:00
|
|
|
arguments.toArray[String] : Array[String], log, reporter)
|
2009-10-08 03:27:53 +02:00
|
|
|
}
|
2011-10-16 23:27:36 +02:00
|
|
|
def console(classpath: Seq[File], options: Seq[String], initialCommands: String, cleanupCommands: String, log: Logger)(loader: Option[ClassLoader] = None, bindings: Seq[(String, Any)] = Nil): Unit =
|
2009-10-08 03:27:53 +02:00
|
|
|
{
|
2010-05-05 14:30:03 +02:00
|
|
|
val arguments = new CompilerArguments(scalaInstance, cp)
|
2011-03-23 12:06:51 +01:00
|
|
|
val classpathString = CompilerArguments.absString(arguments.finishClasspath(classpath))
|
2012-05-22 04:23:44 +02:00
|
|
|
val bootClasspath = if(cp.autoBoot) arguments.createBootClasspathFor(classpath) else ""
|
2010-09-04 14:16:22 +02:00
|
|
|
val (names, values) = bindings.unzip
|
2012-04-29 00:58:38 +02:00
|
|
|
call("xsbt.ConsoleInterface", "run", log)(
|
2011-10-16 23:27:36 +02:00
|
|
|
classOf[Array[String]], classOf[String], classOf[String], classOf[String], classOf[String], classOf[ClassLoader], classOf[Array[String]], classOf[Array[Any]], classOf[xLogger])(
|
|
|
|
|
options.toArray[String]: Array[String], bootClasspath, classpathString, initialCommands, cleanupCommands, loader.orNull, names.toArray[String], values.toArray[Any], log)
|
2009-10-08 03:27:53 +02:00
|
|
|
}
|
2012-04-18 14:07:53 +02:00
|
|
|
def force(log: Logger): Unit = provider(scalaInstance, log)
|
2012-04-29 00:58:38 +02:00
|
|
|
private def call(interfaceClassName: String, methodName: String, log: Logger)(argTypes: Class[_]*)(args: AnyRef*): AnyRef =
|
2009-10-08 03:27:53 +02:00
|
|
|
{
|
|
|
|
|
val interfaceClass = getInterfaceClass(interfaceClassName, log)
|
2009-10-03 15:39:16 +02:00
|
|
|
val interface = interfaceClass.newInstance.asInstanceOf[AnyRef]
|
2012-04-29 00:58:38 +02:00
|
|
|
val method = interfaceClass.getMethod(methodName, argTypes : _*)
|
2009-10-10 01:12:14 +02:00
|
|
|
try { method.invoke(interface, args: _*) }
|
2012-05-20 00:20:19 +02:00
|
|
|
catch { case e: java.lang.reflect.InvocationTargetException =>
|
|
|
|
|
e.getCause match {
|
|
|
|
|
case c: xsbti.CompileFailed => throw new CompileFailed(c.arguments, c.toString, c.problems)
|
|
|
|
|
case t => throw t
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-03 15:39:16 +02:00
|
|
|
}
|
2010-03-13 19:25:08 +01:00
|
|
|
private[this] def loader =
|
2009-10-03 15:39:16 +02:00
|
|
|
{
|
2012-04-18 14:07:53 +02:00
|
|
|
val interfaceJar = provider(scalaInstance, log)
|
2010-10-23 03:55:16 +02:00
|
|
|
// this goes to scalaInstance.loader for scala classes and the loader of this class for xsbti classes
|
|
|
|
|
val dual = createDualLoader(scalaInstance.loader, getClass.getClassLoader)
|
2010-03-13 19:25:08 +01:00
|
|
|
new URLClassLoader(Array(interfaceJar.toURI.toURL), dual)
|
2009-09-05 21:01:04 +02:00
|
|
|
}
|
2010-07-05 18:53:37 +02:00
|
|
|
private def getInterfaceClass(name: String, log: Logger) = Class.forName(name, true, loader)
|
2009-09-05 21:01:04 +02:00
|
|
|
protected def createDualLoader(scalaLoader: ClassLoader, sbtLoader: ClassLoader): ClassLoader =
|
|
|
|
|
{
|
|
|
|
|
val xsbtiFilter = (name: String) => name.startsWith("xsbti.")
|
|
|
|
|
val notXsbtiFilter = (name: String) => !xsbtiFilter(name)
|
2010-07-05 18:53:37 +02:00
|
|
|
new classpath.DualLoader(scalaLoader, notXsbtiFilter, x => true, sbtLoader, xsbtiFilter, x => false)
|
2009-09-05 21:01:04 +02:00
|
|
|
}
|
|
|
|
|
override def toString = "Analyzing compiler (Scala " + scalaInstance.actualVersion + ")"
|
2012-04-18 14:07:53 +02:00
|
|
|
}
|
2012-04-18 22:01:45 +02:00
|
|
|
object AnalyzingCompiler
|
|
|
|
|
{
|
|
|
|
|
import sbt.IO.{copy, createDirectory, zip, jars, unzip, withTemporaryDirectory}
|
|
|
|
|
|
2012-08-20 21:55:50 +02:00
|
|
|
// Note: The Scala build now depends on some details of this method:
|
|
|
|
|
// https://github.com/jsuereth/scala/commit/3431860048df8d2a381fb85a526097e00154eae0
|
2012-04-18 22:01:45 +02:00
|
|
|
/** Extract sources from source jars, compile them with the xsbti interfaces on the classpath, and package the compiled classes and
|
|
|
|
|
* any resources from the source jars into a final jar.*/
|
|
|
|
|
def compileSources(sourceJars: Iterable[File], targetJar: File, xsbtiJars: Iterable[File], id: String, compiler: RawCompiler, log: Logger)
|
|
|
|
|
{
|
|
|
|
|
val isSource = (f: File) => isSourceName(f.getName)
|
|
|
|
|
def keepIfSource(files: Set[File]): Set[File] = if(files.exists(isSource)) files else Set()
|
|
|
|
|
|
|
|
|
|
withTemporaryDirectory { dir =>
|
|
|
|
|
val extractedSources = (Set[File]() /: sourceJars) { (extracted, sourceJar)=> extracted ++ keepIfSource(unzip(sourceJar, dir)) }
|
|
|
|
|
val (sourceFiles, resources) = extractedSources.partition(isSource)
|
|
|
|
|
withTemporaryDirectory { outputDirectory =>
|
|
|
|
|
log.info("'" + id + "' not yet compiled for Scala " + compiler.scalaInstance.actualVersion + ". Compiling...")
|
|
|
|
|
val start = System.currentTimeMillis
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-05-25 12:00:11 +02:00
|
|
|
compiler(sourceFiles.toSeq, compiler.scalaInstance.libraryJar +: (xsbtiJars.toSeq ++ sourceJars), outputDirectory, "-nowarn" :: Nil)
|
2012-04-18 22:01:45 +02:00
|
|
|
log.info(" Compilation completed in " + (System.currentTimeMillis - start) / 1000.0 + " s")
|
|
|
|
|
}
|
2012-05-20 00:20:19 +02:00
|
|
|
catch { case e: xsbti.CompileFailed => throw new CompileFailed(e.arguments, "Error compiling sbt component '" + id + "'", e.problems) }
|
2012-04-18 22:01:45 +02:00
|
|
|
import sbt.Path._
|
|
|
|
|
copy(resources x rebase(dir, outputDirectory))
|
|
|
|
|
zip((outputDirectory ***) x_! relativeTo(outputDirectory), targetJar)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private def isSourceName(name: String): Boolean = name.endsWith(".scala") || name.endsWith(".java")
|
2012-07-10 19:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private[this] object IgnoreProgress extends CompileProgress {
|
|
|
|
|
def startUnit(phase: String, unitPath: String) {}
|
|
|
|
|
def advance(current: Int, total: Int) = true
|
|
|
|
|
}
|