Skip to content

Commit

Permalink
Updated zinc to use sbt 0.13.8 and new java compilers that provide a …
Browse files Browse the repository at this point in the history
…proper log level with their output. See pantsbuild#1557 for details.

Also removed -fork-java option because with the new compilers it's redundant.

Testing Done:
An added test is passing with a local published Zinc.

Bugs closed: 1557, 1573

Reviewed at https://rbcommons.com/s/twitter/r/2248/
  • Loading branch information
fkorotkov authored and fkorotkov committed May 22, 2015
1 parent 2a58a9f commit eaab2d3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/jvm/com/typesafe/sbt/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# zinc dependencies

SBT_REV='0.13.7'
SBT_REV='0.13.8'

jar_library(name = 'compiler-interface',
jars = [jar(org = 'com.typesafe.sbt', name = 'compiler-interface', rev = SBT_REV,
Expand Down
24 changes: 14 additions & 10 deletions src/scala/org/pantsbuild/zinc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.pantsbuild.zinc

import java.io.File
import java.net.URLClassLoader
import sbt.compiler.javac
import sbt.{ ClasspathOptions, CompileOptions, CompileSetup, LoggerReporter, ScalaInstance }
import sbt.compiler.{ AggressiveCompile, AnalyzingCompiler, CompilerCache, CompileOutput, IC }
import sbt.inc.{ Analysis, AnalysisStore, FileBasedStore }
Expand Down Expand Up @@ -54,27 +55,30 @@ object Compiler {
def create(setup: Setup, log: Logger): Compiler = {
val instance = scalaInstance(setup)
val interfaceJar = compilerInterface(setup, instance, log)
val scalac = newScalaCompiler(instance, interfaceJar, log)
val scalac = newScalaCompiler(instance, interfaceJar)
val javac = newJavaCompiler(instance, setup.javaHome, setup.forkJava)
new Compiler(scalac, javac)
}

/**
* Create a new scala compiler.
*/
def newScalaCompiler(instance: ScalaInstance, interfaceJar: File, log: Logger): AnalyzingCompiler = {
IC.newScalaCompiler(instance, interfaceJar, ClasspathOptions.boot, log)
def newScalaCompiler(instance: ScalaInstance, interfaceJar: File): AnalyzingCompiler = {
IC.newScalaCompiler(instance, interfaceJar, ClasspathOptions.boot)
}

/**
* Create a new java compiler.
*/
def newJavaCompiler(instance: ScalaInstance, javaHome: Option[File], fork: Boolean): JavaCompiler = {
val options = ClasspathOptions.javac(false)
if (fork || javaHome.isDefined)
sbt.compiler.JavaCompiler.fork(options, instance)(AggressiveCompile.forkJavac(javaHome))
else
sbt.compiler.JavaCompiler.directOrFork(options, instance)(AggressiveCompile.forkJavac(None))
val compiler =
if (fork || javaHome.isDefined)
javac.JavaCompiler.fork(javaHome)
else
javac.JavaCompiler.local.getOrElse(javac.JavaCompiler.fork(None))

val options = ClasspathOptions.javac(compiler = false)
new javac.JavaCompilerAdapter(compiler, instance, options)
}

/**
Expand Down Expand Up @@ -130,7 +134,7 @@ object Compiler {
* Create the scala instance for the compiler. Includes creating the classloader.
*/
def scalaInstance(setup: Setup): ScalaInstance = {
import setup.{ scalaCompiler, scalaLibrary, scalaExtra}
import setup.{scalaCompiler, scalaExtra, scalaLibrary}
val loader = scalaLoader(scalaLibrary +: scalaCompiler +: scalaExtra)
val version = scalaVersion(loader)
new ScalaInstance(version.getOrElse("unknown"), loader, scalaLibrary, scalaCompiler, scalaExtra, version)
Expand Down Expand Up @@ -201,7 +205,7 @@ class Compiler(scalac: AnalyzingCompiler, javac: JavaCompiler) {
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 getAnalysis: File => Option[Analysis] = analysisMap.get
val aggressive = new AggressiveCompile(cacheFile)
val cp = autoClasspath(classesDirectory, scalac.scalaInstance.allJars, javaOnly, classpath)
val compileOutput = CompileOutput(classesDirectory)
Expand Down
4 changes: 2 additions & 2 deletions src/scala/org/pantsbuild/zinc/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ object Main {
if (cwd.isDefined) Util.setProperties(settings.properties)

val log = Util.logger(settings.quiet, settings.logLevel, settings.color)
val isDebug = (!settings.quiet && settings.logLevel == Level.Debug)
val isDebug = !settings.quiet && settings.logLevel == Level.Debug

// bail out on any command-line option errors
if (!errors.isEmpty) {
if (errors.nonEmpty) {
for (error <- errors) log.error(error)
log.error("See %s -help for information about options" format Setup.Command)
sys.exit(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
java_library(name='compilation_failure_target',
sources=['CompilationFailure.java']
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.pantsbuild.testproject.dummies;

// intentional warning. see JvmExamplesCompileIntegrationTest#test_log_level
import sun.security.x509.X500Name;

/**
* A simple example with an error and a warning to use in tests
**/
public class CompilationFailure {
public static void main(String[] args) {
System.out.println("Hello World!");
// intentional error. see JvmExamplesCompileIntegrationTest#test_log_level
System2.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import unittest

from pants.util.contextutil import temporary_dir
from pants_test.backend.jvm.tasks.jvm_compile.base_compile_integration_test import BaseCompileIT
from pants_test.testutils.compile_strategy_utils import provide_compile_strategies
Expand All @@ -23,7 +25,24 @@ def test_java_tests_zinc_compile(self, strategy):
def test_in_process(self, strategy):
with temporary_dir(root_dir=self.workdir_root()) as workdir:
pants_run = self.run_test_compile(
workdir, 'examples/tests/java/::', strategy, extra_args=['--compile-zinc-java-enabled', '-ldebug']
workdir, 'examples/src/java/org/pantsbuild/example/hello/main', strategy,
extra_args=['--compile-zinc-java-enabled', '-ldebug'], clean_all=True
)
# self.assertTrue('Attempting to call com.sun.tools.javac.api.JavacTool' in pants_run.stdout_data)
self.assertTrue('Attempting to call javac directly' in pants_run.stdout_data)
self.assertFalse('Forking javac' in pants_run.stdout_data)

@unittest.skip("""
Zinc 1.0.3 isn't published yet.
Don't forget to uncomment and replace an assertion in #test_in_process above
by moving to sbt 0.13.8 the output will be a bit different.
""")
@provide_compile_strategies
def test_log_level(self, strategy):
with temporary_dir(root_dir=self.workdir_root()) as workdir:
target = 'testprojects/src/java/org/pantsbuild/testproject/dummies:compilation_failure_target'
pants_run = self.run_test_compile(
workdir, target, strategy, extra_args=['--compile-zinc-java-enabled', '--no-color'], clean_all=True
)
self.assertTrue('[warn] sun.security.x509.X500Name' in pants_run.stdout_data)
self.assertTrue('[error] System2.out' in pants_run.stdout_data)
20 changes: 20 additions & 0 deletions zinc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ Zinc is built using pants:

./pants test zinc/

To build a jar use:

./pants binary.dup --excludes="['rootdoc.txt']" zinc:bin

To consume a jar change zinc target in BUILD.tools:

jar_library(name = 'zinc',
jars = [
jar(org = 'org.pantsbuild', name = 'zinc', rev = 'none', mutable = True,
url = 'file:///Users/fkorotkov/workspace/fkorotkov/pants/dist/zinc.jar'),
],
dependencies=[
':nailgun-server',
'3rdparty/jvm/com/typesafe/sbt:compiler-interface',
'3rdparty/jvm/com/typesafe/sbt:incremental-compiler',
'3rdparty/jvm/com/typesafe/sbt:sbt-interface',
'3rdparty:guava',
'3rdparty:jsr305',
])


Options
-------
Expand Down

0 comments on commit eaab2d3

Please sign in to comment.