Skip to content

Commit

Permalink
Compile Java with Zinc
Browse files Browse the repository at this point in the history
Added --compile-zinc-java-enabled to enable compilation of java targets with zinc.

Eventually we want to refactor everything around to choose a java compiler globally and hopefully retire jmake. For now as a proof of the concept added just a flag to enable it so everyone can test before futher changes.

Testing Done:
ci.sh passes: https://travis-ci.org/fkorotkov/pants/builds/61095655

Reviewed at https://rbcommons.com/s/twitter/r/2156/
  • Loading branch information
fkorotkov authored and stuhood committed May 4, 2015
1 parent 10bc515 commit 21d3dcd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/python/pants/backend/jvm/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from pants.backend.jvm.tasks.junit_run import JUnitRun
from pants.backend.jvm.tasks.jvm_compile.java.apt_compile import AptCompile
from pants.backend.jvm.tasks.jvm_compile.java.java_compile import JavaCompile
from pants.backend.jvm.tasks.jvm_compile.scala.scala_compile import ScalaCompile
from pants.backend.jvm.tasks.jvm_compile.scala.scala_compile import (JavaZincCompile,
ScalaZincCompile)
from pants.backend.jvm.tasks.jvm_run import JvmRun
from pants.backend.jvm.tasks.nailgun_task import NailgunKillall
from pants.backend.jvm.tasks.scala_repl import ScalaRepl
Expand Down Expand Up @@ -119,12 +120,13 @@ def register_goals():
# however if the JavaCompile group member were registered earlier, it would claim the ScalaLibrary
# targets with mixed source sets leaving those targets un-compiled by scalac and resulting in
# systemic compile errors.
jvm_compile.add_member(ScalaCompile)
jvm_compile.add_member(ScalaZincCompile)

# Its important we add AptCompile before JavaCompile since it 1st selector wins and apt code is a
# subset of java code
jvm_compile.add_member(AptCompile)

jvm_compile.add_member(JavaZincCompile)
jvm_compile.add_member(JavaCompile)

task(name='jvm', action=jvm_compile).install('compile').with_description('Compile source code.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,12 @@
from pants.option.options import Options


class ScalaCompile(JvmCompile):
_language = 'scala'
_file_suffix = '.scala'
class ZincCompile(JvmCompile):
_supports_concurrent_execution = True

@classmethod
def get_args_default(cls, bootstrap_option_values):
return ('-S-encoding', '-SUTF-8','-S-g:vars')

@classmethod
def get_warning_args_default(cls):
return ('-S-deprecation', '-S-unchecked')

@classmethod
def get_no_warning_args_default(cls):
return ('-S-nowarn',)

@classmethod
def register_options(cls, register):
super(ScalaCompile, cls).register_options(register)
super(ZincCompile, cls).register_options(register)
# Note: Used in ZincUtils.
# TODO: Revisit this. It's unintuitive for ZincUtils to reach back into the task for options.
register('--plugins', action='append', help='Use these scalac plugins.')
Expand All @@ -44,7 +30,7 @@ def register_options(cls, register):
ZincUtils.register_options(register, cls.register_jvm_tool)

def __init__(self, *args, **kwargs):
super(ScalaCompile, self).__init__(*args, **kwargs)
super(ZincCompile, self).__init__(*args, **kwargs)

# Set up the zinc utils.
color = self.get_options().colors
Expand Down Expand Up @@ -97,3 +83,36 @@ def extra_products(self, target):
def compile(self, args, classpath, sources, classes_output_dir, upstream_analysis, analysis_file):
return self._zinc_utils.compile(args, classpath, sources,
classes_output_dir, analysis_file, upstream_analysis)

class ScalaZincCompile(ZincCompile):
_language = 'scala'
_file_suffix = '.scala'

@classmethod
def get_args_default(cls, bootstrap_option_values):
return ('-S-encoding', '-SUTF-8','-S-g:vars')

@classmethod
def get_warning_args_default(cls):
return ('-S-deprecation', '-S-unchecked')

@classmethod
def get_no_warning_args_default(cls):
return ('-S-nowarn',)

class JavaZincCompile(ZincCompile):
_language = 'java'
_file_suffix = '.java'

@classmethod
def name(cls):
# Use a different name from 'java' so options from JMake version won't interfere.
return "zinc-java"

@classmethod
def register_options(cls, register):
super(JavaZincCompile, cls).register_options(register)
register('--enabled', action='store_true', default=False, help='Use zinc to compile Java targets')

def select(self, target):
return self.get_options().enabled and super(JavaZincCompile, self).select(target)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class BaseCompileIT(PantsRunIntegrationTest):
@contextmanager
def do_test_compile(self, target, strategy,
expected_files=None, iterations=2, expect_failure=False):
expected_files=None, iterations=2, expect_failure=False, extra_args=None):
"""Runs a configurable number of iterations of compilation for the given target.
By default, runs twice to shake out errors related to noops.
Expand All @@ -29,7 +29,7 @@ def do_test_compile(self, target, strategy,
'--compile-java-strategy={}'.format(strategy),
'--compile-scala-strategy={}'.format(strategy),
target,
]
] + (extra_args if extra_args else [])
# Clean-all on the first iteration.
if i == 0:
args.insert(0, 'clean-all')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ python_tests(
]
)


python_tests(
name='apt_compile_integration',
sources=['test_apt_compile_integration.py'],
Expand All @@ -26,6 +25,14 @@ python_tests(
]
)

python_tests(
name='java_zinc_compile_integration',
sources=['test_java_zinc_compile_integration.py'],
dependencies=[
'tests/python/pants_test/backend/jvm/tasks/jvm_compile:base_compile_integration_test',
]
)

python_tests(
name='java_compile_integration',
sources=['test_java_compile_integration.py'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants_test.backend.jvm.tasks.jvm_compile.base_compile_integration_test import BaseCompileIT
from pants_test.backend.jvm.tasks.jvm_compile.utils import provide_compile_strategies


class JvmExamplesCompileIntegrationTest(BaseCompileIT):
@provide_compile_strategies
def test_java_src_zinc_compile(self, strategy):
self.do_test_compile('examples/src/java/::', strategy, extra_args='--compile-zinc-java-enabled')

@provide_compile_strategies
def test_java_tests_zinc_compile(self, strategy):
self.do_test_compile('examples/tests/java/::', strategy, extra_args='--compile-zinc-java-enabled')

0 comments on commit 21d3dcd

Please sign in to comment.