diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-12-19 11:37:53 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-12-19 11:37:53 +0800 |
commit | 5ae99d383b4837eb6db99e4760023e29e40cdd66 (patch) | |
tree | c03743b000230ef266fd29560c2fea9fdd2722c6 /scalalib | |
parent | 044c02639df40e947bf1acfe8f349973f9658e0b (diff) | |
download | mill-5ae99d383b4837eb6db99e4760023e29e40cdd66.tar.gz mill-5ae99d383b4837eb6db99e4760023e29e40cdd66.tar.bz2 mill-5ae99d383b4837eb6db99e4760023e29e40cdd66.zip |
.
Diffstat (limited to 'scalalib')
-rw-r--r-- | scalalib/api/src/ZincWorkerApi.scala | 16 | ||||
-rw-r--r-- | scalalib/src/ScalaModule.scala | 33 | ||||
-rw-r--r-- | scalalib/src/ZincWorkerModule.scala | 44 | ||||
-rw-r--r-- | scalalib/worker/src/ZincWorkerImpl.scala | 91 |
4 files changed, 84 insertions, 100 deletions
diff --git a/scalalib/api/src/ZincWorkerApi.scala b/scalalib/api/src/ZincWorkerApi.scala index c5230ec5..d42be9f3 100644 --- a/scalalib/api/src/ZincWorkerApi.scala +++ b/scalalib/api/src/ZincWorkerApi.scala @@ -3,14 +3,16 @@ package mill.scalalib.api import mill.api.Loose.Agg import mill.api.PathRef import mill.api.JsonFormatters._ - +object ZincWorkerApi{ + type Ctx = mill.api.Ctx.Dest with mill.api.Ctx.Log with mill.api.Ctx.Home +} trait ZincWorkerApi { /** Compile a Java-only project */ def compileJava(upstreamCompileOutput: Seq[CompilationResult], sources: Agg[os.Path], compileClasspath: Agg[os.Path], javacOptions: Seq[String]) - (implicit ctx: mill.api.Ctx): mill.api.Result[CompilationResult] + (implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] /** Compile a mixed Scala/Java or Scala-only project */ def compileMixed(upstreamCompileOutput: Seq[CompilationResult], @@ -18,21 +20,21 @@ trait ZincWorkerApi { compileClasspath: Agg[os.Path], javacOptions: Seq[String], scalaVersion: String, + scalaOrganization: String, scalacOptions: Seq[String], - compilerBridgeSources: os.Path, compilerClasspath: Agg[os.Path], scalacPluginClasspath: Agg[os.Path]) - (implicit ctx: mill.api.Ctx): mill.api.Result[CompilationResult] + (implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] def discoverMainClasses(compilationResult: CompilationResult) - (implicit ctx: mill.api.Ctx): Seq[String] + (implicit ctx: ZincWorkerApi.Ctx): Seq[String] def docJar(scalaVersion: String, - compilerBridgeSources: os.Path, + scalaOrganization: String, compilerClasspath: Agg[os.Path], scalacPluginClasspath: Agg[os.Path], args: Seq[String]) - (implicit ctx: mill.api.Ctx): Boolean + (implicit ctx: ZincWorkerApi.Ctx): Boolean } diff --git a/scalalib/src/ScalaModule.scala b/scalalib/src/ScalaModule.scala index 9d669bf4..5edef350 100644 --- a/scalalib/src/ScalaModule.scala +++ b/scalalib/src/ScalaModule.scala @@ -79,36 +79,7 @@ trait ScalaModule extends JavaModule { outer => def scalaDocOptions = T{ scalacOptions() } - private val Milestone213 = raw"""2.13.(\d+)-M(\d+)""".r - def scalaCompilerBridgeSources = T { - val (scalaVersion0, scalaBinaryVersion0) = scalaVersion() match { - case Milestone213(_, _) => ("2.13.0-M2", "2.13.0-M2") - case _ => (scalaVersion(), mill.scalalib.api.Util.scalaBinaryVersion(scalaVersion())) - } - - val (bridgeDep, bridgeName, bridgeVersion) = - if (isDotty(scalaVersion0)) { - val org = scalaOrganization() - val name = "dotty-sbt-bridge" - val version = scalaVersion() - (ivy"$org:$name:$version", name, version) - } else { - val org = "org.scala-sbt" - val name = "compiler-bridge" - val version = Versions.zinc - (ivy"$org::$name:$version", s"${name}_$scalaBinaryVersion0", version) - } - - resolveDependencies( - repositories, - Lib.depToDependency(_, scalaVersion0, platformSuffix()), - Seq(bridgeDep), - sources = true - ).map(deps => - mill.scalalib.api.Util.grepJar(deps.map(_.path), bridgeName, bridgeVersion, sources = true) - ) - } /** * The local classpath of Scala compiler plugins on-disk; you can add @@ -159,8 +130,8 @@ trait ScalaModule extends JavaModule { outer => compileClasspath().map(_.path), javacOptions(), scalaVersion(), + scalaOrganization(), scalacOptions(), - scalaCompilerBridgeSources(), scalaCompilerClasspath().map(_.path), scalacPluginClasspath().map(_.path), ) @@ -187,7 +158,7 @@ trait ScalaModule extends JavaModule { outer => else { zincWorker.worker().docJar( scalaVersion(), - scalaCompilerBridgeSources(), + scalaOrganization(), scalaCompilerClasspath().map(_.path), scalacPluginClasspath().map(_.path), files ++ options diff --git a/scalalib/src/ZincWorkerModule.scala b/scalalib/src/ZincWorkerModule.scala index 5ca824ce..d787e0ef 100644 --- a/scalalib/src/ZincWorkerModule.scala +++ b/scalalib/src/ZincWorkerModule.scala @@ -4,8 +4,10 @@ import coursier.Cache import coursier.maven.MavenRepository import mill.Agg import mill.T +import mill.api.KeyedLockedCache import mill.define.{Discover, Worker} import mill.scalalib.Lib.resolveDependencies +import mill.scalalib.api.Util.isDotty import mill.util.Loose import mill.util.JsonFormatters._ @@ -41,10 +43,50 @@ trait ZincWorkerModule extends mill.Module{ ) val cls = cl.loadClass("mill.scalalib.worker.ZincWorkerImpl") val instance = cls.getConstructor(classOf[mill.api.Ctx], classOf[Array[String]]) - .newInstance(T.ctx(), compilerInterfaceClasspath().map(_.path.toString).toArray[String]) + .newInstance( + Left(( + T.ctx(), + compilerInterfaceClasspath().map(_.path.toString).toArray[String], + scalaCompilerBridgeSourceJar _ + )), + mill.scalalib.api.Util.grepJar(_, "scala-library", _, sources = false), + mill.scalalib.api.Util.grepJar(_, "scala-compiler", _, sources = false), + new KeyedLockedCache.RandomBoundedCache(1, 1) + ) instance.asInstanceOf[mill.scalalib.api.ZincWorkerApi] } + private val Milestone213 = raw"""2.13.(\d+)-M(\d+)""".r + def scalaCompilerBridgeSourceJar(scalaVersion: String, + scalaOrganization: String) = { + val (scalaVersion0, scalaBinaryVersion0) = scalaVersion match { + case Milestone213(_, _) => ("2.13.0-M2", "2.13.0-M2") + case _ => (scalaVersion, mill.scalalib.api.Util.scalaBinaryVersion(scalaVersion)) + } + + val (bridgeDep, bridgeName, bridgeVersion) = + if (isDotty(scalaVersion0)) { + val org = scalaOrganization + val name = "dotty-sbt-bridge" + val version = scalaVersion + (ivy"$org:$name:$version", name, version) + } else { + val org = "org.scala-sbt" + val name = "compiler-bridge" + val version = Versions.zinc + (ivy"$org::$name:$version", s"${name}_$scalaBinaryVersion0", version) + } + + resolveDependencies( + repositories, + Lib.depToDependency(_, scalaVersion0, ""), + Seq(bridgeDep), + sources = true + ).map(deps => + mill.scalalib.api.Util.grepJar(deps.map(_.path), bridgeName, bridgeVersion, sources = true) + ) + } + def compilerInterfaceClasspath = T{ resolveDependencies( repositories, diff --git a/scalalib/worker/src/ZincWorkerImpl.scala b/scalalib/worker/src/ZincWorkerImpl.scala index 0c1201a9..02623bdd 100644 --- a/scalalib/worker/src/ZincWorkerImpl.scala +++ b/scalalib/worker/src/ZincWorkerImpl.scala @@ -1,18 +1,16 @@ package mill.scalalib.worker -import mill.scalalib.worker._ - import java.io.File import java.util.Optional import mill.api.Loose.Agg -import mill.api.PathRef +import mill.api.{KeyedLockedCache, PathRef} import xsbti.compile.{CompilerCache => _, FileAnalysisStore => _, ScalaInstance => _, _} -import mill.scalalib.api.Util.{isDotty, grepJar, scalaBinaryVersion} +import mill.scalalib.api.Util.{grepJar, isDotty, scalaBinaryVersion} import sbt.internal.inc._ import sbt.internal.util.{ConsoleOut, MainAppender} import sbt.util.LogExchange -import mill.scalalib.api.CompilationResult +import mill.scalalib.api.{CompilationResult, ZincWorkerApi} case class MockedLookup(am: File => Optional[CompileAnalysis]) extends PerClasspathEntryLookup { override def analysis(classpathEntry: File): Optional[CompileAnalysis] = am(classpathEntry) @@ -21,51 +19,14 @@ case class MockedLookup(am: File => Optional[CompileAnalysis]) extends PerClassp Locate.definesClass(classpathEntry) } -trait KeyedCache[T]{ - def withCachedValue[V](key: Long)(f: => T)(f2: T => V): V -} - -object KeyedCache{ - class RandomBoundedCache[T](hotParallelism: Int, coldCacheSize: Int) extends KeyedCache[T]{ - private[this] val random = new scala.util.Random(313373) - val available = new java.util.concurrent.Semaphore(hotParallelism) - - // Awful asymptotic complexity, but our caches are tiny n < 10 so it doesn't matter - var cache = Array.fill[Option[(Long, T)]](coldCacheSize)(None) - - def withCachedValue[V](key: Long)(f: => T)(f2: T => V): V = { - available.acquire() - val pickedValue = synchronized{ - cache.indexWhere(_.exists(_._1 == key)) match { - case -1 => f - case i => - val (k, v) = cache(i).get - cache(i) = None - v - } - } - val result = f2(pickedValue) - synchronized{ - cache.indexWhere(_.isEmpty) match{ - // Random eviction #YOLO - case -1 => cache(random.nextInt(cache.length)) = Some((key, pickedValue)) - case i => cache(i) = Some((key, pickedValue)) - } - } - - available.release() - result - } - } -} - -object ZincWorkerImpl{ - type Ctx = mill.api.Ctx.Dest with mill.api.Ctx.Log with mill.api.Ctx.Home -} -class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], String => os.Path), String => os.Path], - libraryJarNameGrep: (Agg[os.Path], String) => os.Path = grepJar(_, "scala-library", _), - compilerJarNameGrep: (Agg[os.Path], String) => os.Path = grepJar(_, "scala-compiler", _), - compilerCache: KeyedCache[Compilers] = new KeyedCache.RandomBoundedCache(1, 1)) { +class ZincWorkerImpl(compilerBridge: Either[ + (ZincWorkerApi.Ctx, Array[os.Path], (String, String) => os.Path), + String => os.Path + ], + libraryJarNameGrep: (Agg[os.Path], String) => os.Path, + compilerJarNameGrep: (Agg[os.Path], String) => os.Path, + compilerCache: KeyedLockedCache[Compilers]) + extends ZincWorkerApi{ private val ic = new sbt.internal.inc.IncrementalCompilerImpl() lazy val javaOnlyCompilers = { // Keep the classpath as written by the user @@ -88,12 +49,14 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], } def docJar(scalaVersion: String, + scalaOrganization: String, compilerClasspath: Agg[os.Path], scalacPluginClasspath: Agg[os.Path], args: Seq[String]) - (implicit ctx: ZincWorkerImpl.Ctx): Boolean = { + (implicit ctx: ZincWorkerApi.Ctx): Boolean = { withCompilers( scalaVersion, + scalaOrganization, compilerClasspath, scalacPluginClasspath, ) { compilers: Compilers => @@ -105,7 +68,7 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], /** Compile the bridge if it doesn't exist yet and return the output directory. * TODO: Proper invalidation, see #389 */ - def compileZincBridgeIfNeeded(scalaVersion: String, compilerJars: Array[File]): os.Path = { + def compileZincBridgeIfNeeded(scalaVersion: String, scalaOrganization: String, compilerJars: Array[File]): os.Path = { compilerBridge match{ case Right(compiled) => compiled(scalaVersion) case Left((ctx0, compilerBridgeClasspath, srcJars)) => @@ -117,7 +80,7 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], os.makeDir.all(workingDir) os.makeDir.all(compiledDest) - val sourceFolder = mill.api.IO.unpackZip(srcJars(scalaVersion))(workingDir) + val sourceFolder = mill.api.IO.unpackZip(srcJars(scalaVersion, scalaOrganization))(workingDir) val classloader = mill.api.ClassLoader.create(compilerJars.map(_.toURI.toURL), null)(ctx0) val compilerMain = classloader.loadClass( if (isDotty(scalaVersion)) "dotty.tools.dotc.Main" @@ -140,14 +103,14 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], def discoverMainClasses(compilationResult: CompilationResult) - (implicit ctx: ZincWorkerImpl.Ctx): Seq[String] = { + (implicit ctx: ZincWorkerApi.Ctx): Seq[String] = { def toScala[A](o: Optional[A]): Option[A] = if (o.isPresent) Some(o.get) else None toScala(FileAnalysisStore.binary(compilationResult.analysisFile.toIO).get()) .map(_.getAnalysis) .flatMap{ case analysis: Analysis => - Some(analysis.infos.allInfos.values.map(_.getMainClasses).flatten.toSeq.sorted) + Some(analysis.infos.allInfos.values.flatMap(_.getMainClasses).toSeq.sorted) case _ => None } @@ -158,7 +121,7 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], sources: Agg[os.Path], compileClasspath: Agg[os.Path], javacOptions: Seq[String]) - (implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = { + (implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = { compileInternal( upstreamCompileOutput, sources, @@ -174,12 +137,14 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], compileClasspath: Agg[os.Path], javacOptions: Seq[String], scalaVersion: String, + scalaOrganization: String, scalacOptions: Seq[String], compilerClasspath: Agg[os.Path], scalacPluginClasspath: Agg[os.Path]) - (implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = { + (implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = { withCompilers( scalaVersion, + scalaOrganization, compilerClasspath, scalacPluginClasspath, ) {compilers: Compilers => @@ -195,15 +160,19 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], } private def withCompilers[T](scalaVersion: String, + scalaOrganization: String, compilerClasspath: Agg[os.Path], scalacPluginClasspath: Agg[os.Path]) (f: Compilers => T) - (implicit ctx: ZincWorkerImpl.Ctx)= { + (implicit ctx: ZincWorkerApi.Ctx)= { val combinedCompilerClasspath = compilerClasspath ++ scalacPluginClasspath val combinedCompilerJars = combinedCompilerClasspath.toArray.map(_.toIO) - val compiledCompilerBridge = - compileZincBridgeIfNeeded(scalaVersion, compilerClasspath.toArray.map(_.toIO)) + val compiledCompilerBridge = compileZincBridgeIfNeeded( + scalaVersion, + scalaOrganization, + compilerClasspath.toArray.map(_.toIO) + ) val compilerBridgeSig = os.mtime(compiledCompilerBridge) @@ -240,7 +209,7 @@ class ZincWorkerImpl(compilerBridge: Either[(ZincWorkerImpl.Ctx, Array[os.Path], javacOptions: Seq[String], scalacOptions: Seq[String], compilers: Compilers) - (implicit ctx: ZincWorkerImpl.Ctx): mill.api.Result[CompilationResult] = { + (implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = { os.makeDir.all(ctx.dest) println("compileClasspath " + compileClasspath) |