You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by to...@apache.org on 2010/07/09 09:14:54 UTC

svn commit: r962429 - in /buildr/trunk: CHANGELOG lib/buildr/java/ecj.rb spec/java/ecj_spec.rb

Author: toulmean
Date: Fri Jul  9 07:14:53 2010
New Revision: 962429

URL: http://svn.apache.org/viewvc?rev=962429&view=rev
Log:
fix for BUILDR-317, ecj compiler

Added:
    buildr/trunk/lib/buildr/java/ecj.rb
    buildr/trunk/spec/java/ecj_spec.rb
Modified:
    buildr/trunk/CHANGELOG

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=962429&r1=962428&r2=962429&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Fri Jul  9 07:14:53 2010
@@ -4,6 +4,7 @@
 * Fixed: BUILDR-203 Compiler guessing very inefficient
 * Fixed: BUILDR-225 ArchiveTask#merge, not according to doc
 * Fixed: BUILDR-256 Automatically installing gems aborts rspec test runner (Rhett Sutphin)
+* Fixed: BUILDR-317 ecj compiler
 * Fixed: BUILDR-342 The jruby gem installer invokes the removed Gem.manage_gems function (Rhett Sutphin)
 * Fixed: BUILDR-436 release task should only replace "-SNAPSHOT" (spec from Jean-Philippe Caruana)
 * Fixed: BUILDR-464 Improve the versioning of Buildr (Rhett Sutphin)

Added: buildr/trunk/lib/buildr/java/ecj.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/java/ecj.rb?rev=962429&view=auto
==============================================================================
--- buildr/trunk/lib/buildr/java/ecj.rb (added)
+++ buildr/trunk/lib/buildr/java/ecj.rb Fri Jul  9 07:14:53 2010
@@ -0,0 +1,79 @@
+# 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.
+
+module Buildr
+  module Compiler
+    
+    class Ecj < Javac
+      
+      OPTIONS = Buildr::Compiler::Javac::OPTIONS
+      
+      class << self
+        
+        def applies_to?(project, task)
+          return false
+        end
+        
+      end
+      
+      specify :language=>:java, :sources => 'java', :source_ext => 'java',
+      :target=>'classes', :target_ext=>'class', :packaging=>:jar
+
+      
+      
+      def compile(sources, target, dependencies) #:nodoc:
+        check_options options, OPTIONS
+        cmd_args = []
+        # tools.jar contains the Java compiler.
+        dependencies << Java.tools_jar if Java.tools_jar
+        cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR) unless dependencies.empty?
+        source_paths = sources.select { |source| File.directory?(source) }
+        cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty?
+        cmd_args << '-d' << File.expand_path(target)
+        cmd_args += ecj_args
+        cmd_args += files_from_sources(sources)
+        unless Buildr.application.options.dryrun
+          trace((%w[javac -classpath org.eclipse.jdt.internal.compiler.batch.Main] + cmd_args).join(' '))
+          Java.load
+          Java.org.eclipse.jdt.internal.compiler.batch.Main.compile(cmd_args.join(" ")) or
+          fail 'Failed to compile, see errors above'
+        end
+      end
+
+      private
+
+      # See arg list here: http://publib.boulder.ibm.com/infocenter/rsahelp/v7r0m0/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm
+      def ecj_args #:nodoc:
+        args = []  
+        args << '-warn:none' unless options[:warnings]
+        args << '-verbose' if Buildr.application.options.trace
+        args << '-g' if options[:debug]
+        args << '-deprecation' if options[:deprecation]
+        args << '-source' << options[:source].to_s if options[:source]
+        args << '-target' << options[:target].to_s if options[:target]
+        case options[:lint]
+        when Array  then args << "-Xlint:#{options[:lint].join(',')}"
+        when String then args << "-Xlint:#{options[:lint]}"
+        when true   then args << '-Xlint'
+        end
+        args + Array(options[:other])
+      end
+    end
+  end
+end
+
+Java.classpath << "org.eclipse.jdt.core.compiler:ecj:jar:3.5.1"
+# Adding ecj before javac
+Buildr::Compiler.compilers.unshift Buildr::Compiler::Ecj
\ No newline at end of file

Added: buildr/trunk/spec/java/ecj_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/java/ecj_spec.rb?rev=962429&view=auto
==============================================================================
--- buildr/trunk/spec/java/ecj_spec.rb (added)
+++ buildr/trunk/spec/java/ecj_spec.rb Fri Jul  9 07:14:53 2010
@@ -0,0 +1,115 @@
+# 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.
+
+require File.join(File.dirname(__FILE__), '../spec_helpers')
+
+
+
+describe Buildr::Compiler::Ecj do
+
+  before(:all) do
+    #Make ecj appear as a compiler that applies:
+    class Buildr::Compiler::Ecj
+      class << self
+
+        def applies_to?(project, task)
+          paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
+          paths.flatten!
+          ext_glob = Array(source_ext).join(',')
+
+          paths.each { |path| 
+            Find.find(path) {|found|
+              if (!File.directory?(found)) && found.match(/.*\.#{Array(source_ext).join('|')}/)
+                return true
+              end
+              } if File.exist? path
+            }
+            false
+          end
+        end
+      end
+    end
+
+    it "should be the default Java compiler once loaded" do
+      write 'src/main/java/Foo.java', 'public class Foo {}'
+    foo = define('foo')
+    foo.compile.compiler.should == :ecj
+  end
+
+  describe "should compile a Java project just in the same way javac does" do  
+    javac_spec = File.read(File.join(File.dirname(__FILE__), "compiler_spec.rb"))
+    javac_spec = javac_spec.match(Regexp.escape("require File.join(File.dirname(__FILE__), '../spec_helpers')\n")).post_match
+    javac_spec.gsub!("javac", "ecj")
+    javac_spec.gsub!("nowarn", "warn:none")
+    eval(javac_spec)
+  end
+
+  # Redirect the java error ouput, yielding so you can do something while it is
+  # and returning the content of the error buffer.
+  #
+  def redirect_java_err
+    pending "RJB doesn't support well instantiating a class that has several constructors"  RUBY_PLATFORM =~ /java/
+    err = Java.java.io.ByteArrayOutputStream.new
+    original_err = Java.java.lang.System.err
+    begin
+      printStream = Java.java.io.PrintStream
+      print = printStream.new(err)
+      Java.java.lang.System.setErr(print)
+      yield
+    ensure
+      Java.java.lang.System.setErr(original_err)
+    end
+    err.toString
+  end
+
+  it "should not issue warnings for type casting when warnings are set to warn:none, by default" do
+    write "src/main/java/Main.java", "import java.util.List; public class Main {public List get() {return null;}}"
+    foo = define("foo") {
+      compile.options.source = "1.5"
+      compile.options.target = "1.5"
+    }
+    redirect_java_err { foo.compile.invoke }.should_not match(/WARNING/)
+  end
+
+  it "should issue warnings for type casting when warnings are set" do
+    write "src/main/java/Main.java", "import java.util.List; public class Main {public List get() {return null;}}"
+    foo = define("foo") {
+      compile.options.source = "1.5"
+      compile.options.target = "1.5"
+      compile.options.warnings = true
+    }
+    redirect_java_err { foo.compile.invoke }.should match(/WARNING/)
+  end
+
+  after(:all) do
+    #Make ecj appear as a compiler that applies:
+    module Buildr
+      module Compiler
+
+        class Ecj
+
+          class << self
+
+            def applies_to?(project, task)
+              false
+            end
+          end
+        end
+      end
+    end
+  end
+end
+
+