Skip to content

Commit

Permalink
Add callbacks at the end of Dependecy and API phases (in AnalysisCall…
Browse files Browse the repository at this point in the history
…back).

Remove async calls in Dependecy phase.
  • Loading branch information
romanowski committed Jan 25, 2017
1 parent 55a5d38 commit 1edf1f3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
3 changes: 2 additions & 1 deletion internal/compiler-bridge/src/main/scala/xsbt/API.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ final class API(val global: CallbackGlobal) {
override def run(): Unit =
{
val start = System.currentTimeMillis
super.run
super.run()
callback.apiPhaseCompleted()
val stop = System.currentTimeMillis
debuglog("API phase took : " + ((stop - start) / 1000.0) + " s")
}
Expand Down
25 changes: 6 additions & 19 deletions internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package xsbt

import java.io.File
import java.util.concurrent.{ TimeUnit, Executors }

import xsbti.AnalysisCallback
import xsbti.api.DependencyContext
import DependencyContext._

Expand Down Expand Up @@ -39,23 +37,12 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
override def description = "Extracts dependency information"
def name = Dependency.name

val executor = Executors.newFixedThreadPool(1)

override def run(): Unit = {
val start = System.currentTimeMillis
super.run()
// Wait on all callback calls to finish
executor.shutdown()
executor.awaitTermination(20L, TimeUnit.MINUTES)
()
}

private def withCallback(op: AnalysisCallback => Unit): Unit = {
executor.submit(new Runnable {
override def run(): Unit = {
op(callback)
}
})
()
callback.dependencyPhaseCompleted()
val stop = System.currentTimeMillis
debuglog("Dependency phase took : " + ((stop - start) / 1000.0) + " s")
}

def apply(unit: CompilationUnit): Unit = {
Expand Down Expand Up @@ -105,7 +92,7 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
def processDependency(context: DependencyContext)(dep: ClassDependency): Unit = {
val fromClassName = className(dep.from)
def binaryDependency(file: File, onBinaryClassName: String) =
withCallback(_.binaryDependency(file, onBinaryClassName, fromClassName, sourceFile, context))
callback.binaryDependency(file, onBinaryClassName, fromClassName, sourceFile, context)
val onSource = dep.to.sourceFile
if (onSource == null) {
classFile(dep.to) match {
Expand All @@ -120,7 +107,7 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
}
} else if (onSource.file != sourceFile) {
val onClassName = className(dep.to)
withCallback(_.classDependency(onClassName, fromClassName, context))
callback.classDependency(onClassName, fromClassName, context)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public interface AnalysisCallback {
* Do not depend on it, please.
*/
boolean nameHashing();

/** Called at the end of dependency phase. Can be used e.g. to wait on asynchronous tasks. */
void dependencyPhaseCompleted();

/** Called at the end of dependency phase. Can be used e.g. to wait on asynchronous tasks. */
void apiPhaseCompleted();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class TestCallback(override val nameHashing: Boolean = false) extends AnalysisCa
()
}
def problem(category: String, pos: xsbti.Position, message: String, severity: xsbti.Severity, reported: Boolean): Unit = ()

override def dependencyPhaseCompleted(): Unit = {}

override def apiPhaseCompleted(): Unit = {}
}

object TestCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private[sbt] object Analyze {
try { Some(Class.forName(tpe, false, loader)) }
catch { case e: Throwable => errMsg.foreach(msg => log.warn(msg + " : " + e.toString)); None }

val productToClassName = mutable.Set.empty[String]
val classNames = mutable.Set.empty[String]
val sourceToClassFiles = mutable.HashMap[File, Buffer[ClassFile]](
sources zip Seq.fill(sources.size)(new ArrayBuffer[ClassFile]): _*
)
Expand All @@ -53,7 +53,7 @@ private[sbt] object Analyze {
srcClassName match {
case Some(srcClassName) =>
analysis.generatedNonLocalClass(source, newClass, binaryClassName, srcClassName)
productToClassName += srcClassName
classNames += srcClassName
case None => analysis.generatedLocalClass(source, newClass)
}

Expand Down Expand Up @@ -93,7 +93,7 @@ private[sbt] object Analyze {
trapAndLog(log) {
val scalaLikeTypeName = onBinaryName.replace('$', '.')

if (productToClassName.contains(scalaLikeTypeName))
if (classNames.contains(scalaLikeTypeName))
analysis.classDependency(scalaLikeTypeName, fromClassName, context)
else
for (file <- classfilesCache.get(onBinaryName).orElse(loadFromClassloader()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,8 @@ private final class AnalysisCallback(

a.addSource(src, analyzedApis, stamp, info, nonLocalProds, localProds, internalDeps, externalDeps, binDeps)
}

override def dependencyPhaseCompleted(): Unit = {}

override def apiPhaseCompleted(): Unit = {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ class TestAnalysisCallback(
}

def problem(category: String, pos: xsbti.Position, message: String, severity: xsbti.Severity, reported: Boolean): Unit = ()

override def dependencyPhaseCompleted(): Unit = {}

override def apiPhaseCompleted(): Unit = {}
}

0 comments on commit 1edf1f3

Please sign in to comment.