Repository: buildr Updated Branches: refs/heads/master c0c415558 -> 749acfe1e
Bring back zinc compiler, using the latest version and a runner class Project: http://git-wip-us.apache.org/repos/asf/buildr/repo Commit: http://git-wip-us.apache.org/repos/asf/buildr/commit/749acfe1 Tree: http://git-wip-us.apache.org/repos/asf/buildr/tree/749acfe1 Diff: http://git-wip-us.apache.org/repos/asf/buildr/diff/749acfe1 Branch: refs/heads/master Commit: 749acfe1ec6f24bd4064c4942d0ebcd6394caf89 Parents: c0c4155 Author: Antoine Toulme <[email protected]> Authored: Mon Aug 15 23:37:02 2016 -0700 Committer: Antoine Toulme <[email protected]> Committed: Mon Aug 15 23:37:02 2016 -0700 ---------------------------------------------------------------------- doc/languages.textile | 34 ++++++++++ lib/buildr/scala/compiler.rb | 55 ++++++++++++++-- .../scala/org/apache/buildr/ZincRunner.class | Bin 0 -> 1162 bytes .../scala/org/apache/buildr/ZincRunner.java | 37 +++++++++++ spec/scala/compiler_spec.rb | 62 ++++++++++++++++++- 5 files changed, 183 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/buildr/blob/749acfe1/doc/languages.textile ---------------------------------------------------------------------- diff --git a/doc/languages.textile b/doc/languages.textile index ec1b985..09252e2 100644 --- a/doc/languages.textile +++ b/doc/languages.textile @@ -238,6 +238,8 @@ h4. Fast Scala Compiler You may use @fsc@, the Fast Scala Compiler, which submits compilation jobs to a compilation daemon, by setting the environment variable @USE_FSC@ to @yes@. Note that @fsc@ _may_ cache class libraries -- don't forget to run @fsc -reset@ if you upgrade a library. +(Note @fsc@ is not compatible with @zinc@ incremental compilation.) + h4. Rebuild detection *Scala 2.7* @@ -268,6 +270,38 @@ To avoid unusual behavior, compiler-level change detection is disabled whenever *Scala 2.9 and later* +Starting with Buildr 1.4.8, Buildr integrates with the "Zinc":https://github.com/typesafehub/zinc incremental compilation wrapper for @scalac@. Incremental compilation can be enabled 3 ways, + +1) By setting the compiler's option directly, + +{% highlight ruby %} +compile.using :incremental => true + +compile.options.incremental = true # same as above +{% endhighlight %} + +Note that this won't enable incremental compilation for both @compile@ and @test.compile@, you would have to set options on both. For this reason, it's recommended that you set the option on the project instead (see below). + +2) By setting the project's @scalac_options.incremental@, + +{% highlight ruby %} +project.scalac_options.incremental = true +{% endhighlight %} + +3) By setting the global @scalac.incremental@ option, + +in your @buildfile@: + +{% highlight ruby %} +Buildr.settings.build['scalac.incremental'] = true +{% endhighlight %} + +or in your @build.yaml@: + +{% highlight yaml %} +scalac.incremental: true +{% endhighlight %} + h4. Support for different Scala versions Buildr defaults to the latest stable Scala version available at the time of the release if neither @SCALA_HOME@ nor the @scala.version@ build property are set. http://git-wip-us.apache.org/repos/asf/buildr/blob/749acfe1/lib/buildr/scala/compiler.rb ---------------------------------------------------------------------- diff --git a/lib/buildr/scala/compiler.rb b/lib/buildr/scala/compiler.rb index 54bbafe..feb127c 100644 --- a/lib/buildr/scala/compiler.rb +++ b/lib/buildr/scala/compiler.rb @@ -87,7 +87,7 @@ module Buildr::Scala # * :other -- Array of options to pass to the Scalac compiler as is, e.g. -Xprint-types class Scalac < Buildr::Compiler::Base - DEFAULT_ZINC_VERSION = '1.0.0-X1' + DEFAULT_ZINC_VERSION = '0.3.12' DEFAULT_SBT_VERSION = '0.13.12' DEFAULT_JLINE_VERSION = '2.14.2' @@ -121,7 +121,9 @@ module Buildr::Scala REQUIRES.artifacts.map(&:to_s) end - scala_dependencies.compact + zinc_dependencies = ZINC_REQUIRES.artifacts.map(&:to_s) + + (scala_dependencies + zinc_dependencies).compact end def use_fsc @@ -159,8 +161,10 @@ module Buildr::Scala end ZINC_REQUIRES = ArtifactNamespace.for(self) do |ns| + zinc_version = Buildr.settings.build['zinc.version'] || DEFAULT_ZINC_VERSION sbt_version = Buildr.settings.build['sbt.version'] || DEFAULT_SBT_VERSION jline_version = Buildr.settings.build['jline.version'] || DEFAULT_JLINE_VERSION + ns.zinc! "com.typesafe.zinc:zinc:jar:>=#{zinc_version}" ns.sbt_interface! "com.typesafe.sbt:sbt-interface:jar:>=#{sbt_version}" ns.incremental! "com.typesafe.sbt:incremental-compiler:jar:>=#{sbt_version}" ns.compiler_interface_sources! "com.typesafe.sbt:compiler-interface:jar:sources:>=#{sbt_version}" @@ -194,7 +198,11 @@ module Buildr::Scala end def compile(sources, target, dependencies) #:nodoc: - compile_with_scalac(sources, target, dependencies) + if zinc? + compile_with_zinc(sources, target, dependencies) + else + compile_with_scalac(sources, target, dependencies) + end end def compile_with_scalac(sources, target, dependencies) #:nodoc: @@ -249,6 +257,37 @@ module Buildr::Scala end end + def compile_with_zinc(sources, target, dependencies) #:nodoc: + + dependencies.unshift target + + cmd_args = [] + cmd_args << '-sbt-interface' << REQUIRES.sbt_interface.artifact + cmd_args << '-compiler-interface' << REQUIRES.compiler_interface_sources.artifact + cmd_args << '-scala-library' << dependencies.find { |d| d =~ /scala-library/ } + cmd_args << '-scala-compiler' << dependencies.find { |d| d =~ /scala-compiler/ } + cmd_args << '-scala-extra' << dependencies.find { |d| d =~ /scala-reflect/ } + cmd_args << '-classpath' << (dependencies + [ File.join(File.dirname(__FILE__)) ]).join(File::PATH_SEPARATOR) + source_paths = sources.select { |source| File.directory?(source) } + cmd_args << '-Ssourcepath' << ("-S" + source_paths.join(File::PATH_SEPARATOR)) unless source_paths.empty? + cmd_args << '-d' << File.expand_path(target) + cmd_args += scalac_args + cmd_args << "-debug" if trace?(:scalac) + + cmd_args.map!(&:to_s) + + cmd_args += files_from_sources(sources) + + unless Buildr.application.options.dryrun + trace((['org.apache.buildr.ZincRunner'] + cmd_args).join(' ')) + begin + Java::Commands.java 'org.apache.buildr.ZincRunner', *(cmd_args + [{ :classpath => Scalac.dependencies + [ File.join(File.dirname(__FILE__)) ]}]) + rescue => e + fail "Zinc compiler crashed:\n#{e.inspect}\n#{e.backtrace.join("\n")}" + end + end + end + protected # :nodoc: see Compiler:Base @@ -293,6 +332,10 @@ module Buildr::Scala private + def zinc? + (options[:incremental] || @project.scalac_options.incremental || (Buildr.settings.build['scalac.incremental'].to_s == "true")) + end + def count(file, pattern) count = 0 File.open(file, "r") do |infile| @@ -322,7 +365,11 @@ module Buildr::Scala args << "-optimise" if options[:optimise] args << "-target:jvm-" + options[:target].to_s if options[:target] args += Array(options[:other]) - args + if zinc? + args.map { |arg| "-S" + arg } + Array(options[:zinc_options]) + else + args + end end end http://git-wip-us.apache.org/repos/asf/buildr/blob/749acfe1/lib/buildr/scala/org/apache/buildr/ZincRunner.class ---------------------------------------------------------------------- diff --git a/lib/buildr/scala/org/apache/buildr/ZincRunner.class b/lib/buildr/scala/org/apache/buildr/ZincRunner.class new file mode 100644 index 0000000..a60c7eb Binary files /dev/null and b/lib/buildr/scala/org/apache/buildr/ZincRunner.class differ http://git-wip-us.apache.org/repos/asf/buildr/blob/749acfe1/lib/buildr/scala/org/apache/buildr/ZincRunner.java ---------------------------------------------------------------------- diff --git a/lib/buildr/scala/org/apache/buildr/ZincRunner.java b/lib/buildr/scala/org/apache/buildr/ZincRunner.java new file mode 100644 index 0000000..e8a6407 --- /dev/null +++ b/lib/buildr/scala/org/apache/buildr/ZincRunner.java @@ -0,0 +1,37 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.buildr; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class ZincRunner { + + public static void main(String[] args) { + try { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Class clazz = loader.loadClass("com.typesafe.zinc.Main$"); + Field singleton = clazz.getField("MODULE$"); + Object instance = singleton.get(null); + Method main = clazz.getMethod("main", String[].class); + main.invoke(instance, new Object[] { args }); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} http://git-wip-us.apache.org/repos/asf/buildr/blob/749acfe1/spec/scala/compiler_spec.rb ---------------------------------------------------------------------- diff --git a/spec/scala/compiler_spec.rb b/spec/scala/compiler_spec.rb index 2c5206c..f28bbe6 100644 --- a/spec/scala/compiler_spec.rb +++ b/spec/scala/compiler_spec.rb @@ -272,7 +272,31 @@ share_as :ScalacCompiler_CommonOptions do end end -describe 'scala compiler options' do + +describe 'scala compiler 2.8 options' do + + it_should_behave_like ScalacCompiler_CommonOptions + + def compile_task + @compile_task ||= define('foo').compile.using(:scalac) + end + + def scalac_args + compile_task.instance_eval { @compiler }.send(:scalac_args) + end + + it 'should use -g argument when debug option is true' do + compile_task.using(:debug=>true) + scalac_args.should include('-g') + end + + it 'should not use -g argument when debug option is false' do + compile_task.using(:debug=>false) + scalac_args.should_not include('-g') + end +end if Buildr::Scala.version?(2.8) + +describe 'scala compiler 2.9 options' do it_should_behave_like ScalacCompiler_CommonOptions @@ -304,5 +328,41 @@ describe 'scala compiler options' do scalac_args.should_not include('-g') end +end if Buildr::Scala.version?(2.9) + +describe 'zinc compiler (enabled through Buildr.settings)' do + before :each do + Buildr.settings.build['scalac.incremental'] = true + end + + it 'should compile with zinc' do + write 'src/main/scala/com/example/Test.scala', 'package com.example; class Test { val i = 1 }' + project = define('foo') + compile_task = project.compile.using(:scalac) + compiler = compile_task.instance_eval { @compiler } + compiler.send(:zinc?).should eql(true) + compiler.should_receive(:compile_with_zinc).once + compile_task.invoke + end + + it_should_behave_like ScalacCompiler + + after :each do + Buildr.settings.build['scalac.incremental'] = nil + end + end +describe 'zinc compiler (enabled through project.scala_options)' do + + it 'should compile with zinc' do + write 'src/main/scala/com/example/Test.scala', 'package com.example; class Test { val i = 1 }' + project = define('foo') + project.scalac_options.incremental = true + compile_task = project.compile.using(:scalac) + compiler = compile_task.instance_eval { @compiler } + compiler.send(:zinc?).should eql(true) + compiler.should_receive(:compile_with_zinc).once + compile_task.invoke + end +end \ No newline at end of file
