Skip to content

Commit

Permalink
Import zinc to the pants repo, and drop its sbt build.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Howland authored and Stu Hood committed Apr 29, 2015
1 parent a833e89 commit 674c205
Show file tree
Hide file tree
Showing 122 changed files with 450 additions and 2,824 deletions.
18 changes: 18 additions & 0 deletions BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,21 @@ jar_library(name = 'wire-compiler',
jars = [
jar(org = 'com.squareup.wire', name = 'wire-compiler', rev = '1.6.0')
])

# zinc depedencies

SBT_REV='0.13.7'

jar_library(name = 'compiler-interface',
jars = [
jar(org = 'com.typesafe.sbt', name = 'compiler-interface', rev = SBT_REV)
])
jar_library(name = 'incremental-compiler',
jars = [
jar(org = 'com.typesafe.sbt', name = 'incremental-compiler', rev = SBT_REV)
])
jar_library(name = 'sbt-interface',
jars = [
jar(org = 'com.typesafe.sbt', name = 'sbt-interface', rev = SBT_REV)
])

2 changes: 1 addition & 1 deletion build-support/bin/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fi
if [[ "${skip_jvm:-false}" == "false" ]]; then
banner "Running core jvm tests"
(
./pants.pex ${PANTS_ARGS[@]} test tests/java::
./pants.pex ${PANTS_ARGS[@]} test tests/java:: src:: zinc::
) || die "Core jvm test failure"
fi

Expand Down
9 changes: 9 additions & 0 deletions src/scala/org/pantsbuild/zinc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scala_library(name='zinc',
dependencies=[
'//:compiler-interface',
'//:incremental-compiler',
'//:nailgun-server',
'//:sbt-interface',
],
sources=globs('*.scala')
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2012 Typesafe, Inc. <http://www.typesafe.com>
*/

package com.typesafe.zinc
package org.pantsbuild.zinc

import java.lang.ref.SoftReference
import java.util.{ LinkedHashMap, Map => JMap }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
* Copyright (C) 2012 Typesafe, Inc. <http://www.typesafe.com>
*/

package com.typesafe.zinc
package org.pantsbuild.zinc

import java.io.File
import java.net.URLClassLoader
import sbt.{ ClasspathOptions, CompileOptions, CompileSetup, LoggerReporter, ScalaInstance }
import sbt.compiler.{ AggressiveCompile, AnalyzingCompiler, CompilerCache, CompileOutput, IC }
import sbt.inc.{ Analysis, AnalysisStore, FileBasedStore }

import sbt.Path._
import xsbti.compile.{ JavaCompiler, GlobalsCache }
import sbt.compiler.{AggressiveCompile, AnalyzingCompiler, CompileOutput, CompilerCache, IC}
import sbt.inc.{Analysis, AnalysisStore, FileBasedStore}
import sbt.{ClasspathOptions, CompileOptions, CompileSetup, LoggerReporter, ScalaInstance}
import xsbti.Logger
import xsbti.compile.{GlobalsCache, JavaCompiler}

object Compiler {
val CompilerInterfaceId = "compiler-interface"
Expand All @@ -32,27 +33,24 @@ object Compiler {
* a cache miss, the underlying file will yield a valid Analysis.
*/
val analysisCache = Cache[FileFPrint, Option[(Analysis, CompileSetup)]](Setup.Defaults.analysisCacheLimit)

/**
* Java API for creating compiler.
*/
def getOrCreate(setup: Setup, log: Logger): Compiler = apply(setup, log)
/**
* Get or create a zinc compiler based on compiler setup.
*/
def apply(setup: Setup, log: Logger): Compiler = {
compilerCache.get(setup)(create(setup, log))
}

/**
* Java API for creating compiler.
*/
def getOrCreate(setup: Setup, log: Logger): Compiler = apply(setup, log)

/**
* Create a new zinc compiler based on compiler setup.
*/
def create(setup: Setup, log: Logger): Compiler = {
val instance = scalaInstance(setup)
val instance = scalaInstance(setup)
val interfaceJar = compilerInterface(setup, instance, log)
val scalac = newScalaCompiler(instance, interfaceJar, log)
val javac = newJavaCompiler(instance, setup.javaHome, setup.forkJava)
val scalac = newScalaCompiler(instance, interfaceJar, log)
val javac = newJavaCompiler(instance, setup.javaHome, setup.forkJava)
new Compiler(scalac, javac)
}

Expand All @@ -73,70 +71,26 @@ object Compiler {
else
sbt.compiler.JavaCompiler.directOrFork(options, instance)(AggressiveCompile.forkJavac(None))
}

/**
* Create new globals cache.
*/
def createResidentCache(maxCompilers: Int): GlobalsCache = {
if (maxCompilers <= 0) CompilerCache.fresh else CompilerCache(maxCompilers)
}

/**
* Create an analysis store backed by analysisCache.
*/
def analysisStore(cacheFile: File): AnalysisStore = {
val fileStore = AnalysisStore.cached(FileBasedStore(cacheFile))

val fprintStore = new AnalysisStore {
def set(analysis: Analysis, setup: CompileSetup) {
fileStore.set(analysis, setup)
FileFPrint.fprint(cacheFile) foreach { analysisCache.put(_, Some((analysis, setup))) }
}
def get(): Option[(Analysis, CompileSetup)] = {
FileFPrint.fprint(cacheFile) flatMap { fprint => analysisCache.get(fprint)(fileStore.get) }
}
}

AnalysisStore.sync(AnalysisStore.cached(fprintStore))
}

/**
* Get an analysis, lookup by cache file.
*/
def analysis(cacheFile: File): Analysis = {
analysisStore(cacheFile).get map (_._1) getOrElse Analysis.Empty
}

/**
* Check whether an analysis is empty.
*/
def analysisIsEmpty(cacheFile: File): Boolean = {
analysis(cacheFile) eq Analysis.Empty
}

/**
* Create the scala instance for the compiler. Includes creating the classloader.
*/
def scalaInstance(setup: Setup): ScalaInstance = {
import setup.{ scalaCompiler, scalaLibrary, scalaExtra}
val loader = scalaLoader(scalaLibrary +: scalaCompiler +: scalaExtra)
val loader = scalaLoader(setup.scalaLibrary +: setup.scalaCompiler +: setup.scalaExtra)
val version = scalaVersion(loader)
new ScalaInstance(version.getOrElse("unknown"), loader, scalaLibrary, scalaCompiler, scalaExtra, version)
new ScalaInstance(version.getOrElse("unknown"), loader, setup.scalaLibrary, setup.scalaCompiler,
setup.scalaExtra, version)
}

/**
* Create a new classloader with the root loader as parent (to avoid zinc itself being included).
*/
def scalaLoader(jars: Seq[File]) = new URLClassLoader(toURLs(jars), sbt.classpath.ClasspathUtilities.rootLoader)

/**
* Get the actual scala version from the compiler.properties in a classloader.
* The classloader should only contain one version of scala.
*/
def scalaVersion(scalaLoader: ClassLoader): Option[String] = {
Util.propertyFromResource("compiler.properties", "version.number", scalaLoader)
}

/**
* Get the compiler interface for this compiler setup. Compile it if not already cached.
*/
Expand All @@ -149,8 +103,45 @@ object Compiler {
}
interfaceJar
}

def interfaceId(scalaVersion: String) = CompilerInterfaceId + "-" + scalaVersion + "-" + JavaClassVersion
/**
* Create new globals cache.
*/
def createResidentCache(maxCompilers: Int): GlobalsCache = {
if (maxCompilers <= 0) CompilerCache.fresh else CompilerCache(maxCompilers)
}
/**
* Check whether an analysis is empty.
*/
def analysisIsEmpty(cacheFile: File): Boolean = {
analysis(cacheFile) eq Analysis.Empty
}
/**
* Get an analysis, lookup by cache file.
*/
def analysis(cacheFile: File): Analysis = {
analysisStore(cacheFile).get map (_._1) getOrElse Analysis.Empty
}
/**
* Create an analysis store backed by analysisCache.
*/
def analysisStore(cacheFile: File): AnalysisStore = {
val fileStore = AnalysisStore.cached(FileBasedStore(cacheFile))

val fprintStore = new AnalysisStore {
def set(analysis: Analysis, setup: CompileSetup) {
fileStore.set(analysis, setup)
FileFPrint.fprint(cacheFile) foreach {
analysisCache.put(_, Some((analysis, setup)))
}
}
def get(): Option[(Analysis, CompileSetup)] = {
FileFPrint.fprint(cacheFile) flatMap { fprint => analysisCache.get(fprint)(fileStore.get) }
}
}

AnalysisStore.sync(AnalysisStore.cached(fprintStore))
}
}

/**
Expand All @@ -160,24 +151,24 @@ class Compiler(scalac: AnalyzingCompiler, javac: JavaCompiler) {

/**
* Run a compile. The resulting analysis is also cached in memory.
* Note: This variant automatically contructs an error-reporter.
* Note: This variant automatically contructs an error-reporter.
*/
def compile(inputs: Inputs)(log: Logger): Analysis = compile(inputs, None)(log)

/**
* Run a compile. The resulting analysis is also cached in memory.
*
* Note: This variant automatically contructs an error-reporter.
* Note: This variant automatically contructs an error-reporter.
*/
def compile(inputs: Inputs, cwd: Option[File])(log: Logger): Analysis = {
val maxErrors = 100
val maxErrors = 100
compile(inputs, cwd, new LoggerReporter(maxErrors, log, identity))(log)
}

/**
* Run a compile. The resulting analysis is also cached in memory.
*
* Note: This variant does not report progress updates
*
* Note: This variant does not report progress updates
*/
def compile(inputs: Inputs, cwd: Option[File], reporter: xsbti.Reporter)(log: Logger): Analysis = {
compile(inputs, cwd, reporter, progress = None)(log)
Expand All @@ -186,34 +177,42 @@ class Compiler(scalac: AnalyzingCompiler, javac: JavaCompiler) {
/**
* Run a compile. The resulting analysis is also cached in memory.
*/
def compile(inputs: Inputs, cwd: Option[File], reporter: xsbti.Reporter, progress: Option[xsbti.compile.CompileProgress])(log: Logger): Analysis = {
import inputs._
if (forceClean && Compiler.analysisIsEmpty(cacheFile)) Util.cleanAllClasses(classesDirectory)
val getAnalysis: File => Option[Analysis] = analysisMap.get _
val aggressive = new AggressiveCompile(cacheFile)
val cp = autoClasspath(classesDirectory, scalac.scalaInstance.allJars, javaOnly, classpath)
val compileOutput = CompileOutput(classesDirectory)
val globalsCache = Compiler.residentCache
val skip = false
val incOpts = incOptions.options
val compileSetup = new CompileSetup(compileOutput, new CompileOptions(scalacOptions, javacOptions), scalac.scalaInstance.actualVersion, compileOrder, incOpts.nameHashing)
val analysisStore = Compiler.analysisStore(cacheFile)
val analysis = aggressive.compile1(sources, cp, compileSetup, progress, analysisStore, getAnalysis, definesClass, scalac, javac, reporter, skip, globalsCache, incOpts)(log)
if (mirrorAnalysis) {
SbtAnalysis.printRelations(analysis, Some(new File(cacheFile.getPath() + ".relations")), cwd)
def compile(inputs: Inputs, cwd: Option[File], reporter: xsbti.Reporter,
progress: Option[xsbti.compile.CompileProgress])(log: Logger): Analysis = {
if (inputs.forceClean && Compiler.analysisIsEmpty(inputs.cacheFile))
Util.cleanAllClasses(inputs.classesDirectory)
val getAnalysis: File => Option[Analysis] = inputs.analysisMap.get _
val aggressive = new AggressiveCompile(inputs.cacheFile)
val cp = autoClasspath(inputs.classesDirectory, scalac.scalaInstance.allJars,
inputs.javaOnly, inputs.classpath)
val compileOutput = CompileOutput(inputs.classesDirectory)
val globalsCache = Compiler.residentCache
val skip = false
val incOpts = inputs.incOptions.options
val compileSetup = new CompileSetup(compileOutput,
new CompileOptions(inputs.scalacOptions, inputs.javacOptions),
scalac.scalaInstance.actualVersion, inputs.compileOrder, incOpts.nameHashing)
val analysisStore = Compiler.analysisStore(inputs.cacheFile)
val analysis = aggressive.compile1(inputs.sources, cp, compileSetup, progress, analysisStore,
getAnalysis, inputs.definesClass, scalac, javac, reporter, skip, globalsCache, incOpts)(log)
if (inputs.mirrorAnalysis) {
SbtAnalysis.printRelations(analysis,
Some(new File(inputs.cacheFile.getPath() + ".relations")), cwd)
}
SbtAnalysis.printOutputs(analysis, outputRelations, outputProducts, cwd, classesDirectory)
SbtAnalysis.printOutputs(analysis, inputs.outputRelations, inputs.outputProducts, cwd,
inputs.classesDirectory)
analysis
}

/**
* Automatically add the output directory and scala library to the classpath.
*/
def autoClasspath(classesDirectory: File, allScalaJars: Seq[File], javaOnly: Boolean, classpath: Seq[File]): Seq[File] = {
def autoClasspath(classesDirectory: File, allScalaJars: Seq[File], javaOnly: Boolean,
classpath: Seq[File]): Seq[File] = {
if (javaOnly) classesDirectory +: classpath
else Setup.splitScala(allScalaJars) match {
case Some(scalaJars) => classesDirectory +: scalaJars.library +: classpath
case None => classesDirectory +: classpath
case None => classesDirectory +: classpath
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2012 Typesafe, Inc. <http://www.typesafe.com>
*/

package com.typesafe.zinc
package org.pantsbuild.zinc

import java.io.{FileNotFoundException, FileInputStream, InputStream, File}
import java.security.MessageDigest
Expand Down
Loading

0 comments on commit 674c205

Please sign in to comment.