Skip to content

Commit

Permalink
Sync Java compilation scenarios
Browse files Browse the repository at this point in the history
Merged Java compilation scenarios with minor adjustments
to exclude non-POSIX OS.
  • Loading branch information
myabc authored and luislavena committed Dec 7, 2009
1 parent f729a4b commit 3f27432
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 13 deletions.
3 changes: 3 additions & 0 deletions cucumber.yml
@@ -1 +1,4 @@
default: --tags ~@java --format progress features
java: --tags @java --format progress features
all: --format progress features
autotest: --format progress features
22 changes: 22 additions & 0 deletions features/java-compile.feature
@@ -0,0 +1,22 @@
Feature: JCompile Java extensions

In order to avoid bitching from Enterprise users
As a Ruby developer
I want some rake tasks that take away the pain of compilation

@java
Scenario: Compile single Java extension (with default Rake)
Given that all my Java source files are in place
And I've installed the Java Development Kit
When rake task 'java compile' is invoked
Then rake task 'java compile' succeeded
And binaries for platform 'java' get generated

@java
Scenario: Compile single Java extension (with Rake on JRuby)
Given that all my Java source files are in place
And I've installed the Java Development Kit
When I've installed JRuby
When rake task 'java compile' is invoked on JRuby
Then rake task 'java compile' succeeded
And binaries for platform 'java' get generated
33 changes: 33 additions & 0 deletions features/java-no-native-compile.feature
@@ -0,0 +1,33 @@
Feature: No native or cross compilation on JRuby

In order to present a good user experience to users of rake-compiler
As a user of JRuby
I want to be warned that my platform does not provide any support for C Extensions
I want to be be informed of this without rake-compiler blowing up in my face

@java
Scenario: Attempting to do a cross compilation while on JRuby (without prerequisites)
Given that all my source files are in place
And I'm running a POSIX operating system
When rake task 'cross compile' is invoked on JRuby
Then rake task 'cross compile' should fail
And output of rake task 'cross compile' warns
"""
WARNING: You're attempting to (cross-)compile C extensions from a platform
(jruby) that does not support native extensions or mkmf.rb.
"""
And output of rake task 'cross compile' contains /Don't know how to build task 'cross'/

@java
Scenario: Attempting to do a cross compilation while on JRuby (even with prerequisites)
Given that all my source files are in place
And I'm running a POSIX operating system
And I've installed cross compile toolchain
When rake task 'cross compile' is invoked on JRuby
Then rake task 'cross compile' should fail
And output of rake task 'cross compile' warns
"""
WARNING: You're attempting to (cross-)compile C extensions from a platform
(jruby) that does not support native extensions or mkmf.rb.
"""
And output of rake task 'cross compile' contains /Don't know how to build task 'cross'/
24 changes: 24 additions & 0 deletions features/java-package.feature
@@ -0,0 +1,24 @@
Feature: Generate JRuby gems from JRuby or MRI

In order to keep sanity in the Ruby world
As a Gem developer who used to do J2EE development
I want more rake magic that turns monotony into joy.

@java
Scenario: package a gem for Java (with default Rake)
Given that my JRuby gem source is all in place
And I've installed the Java Development Kit
And I've already successfully executed rake task 'java compile'
When rake task 'java gem' is invoked
Then rake task 'java gem' succeeded
And gem for platform 'java' get generated

@java
Scenario: package a gem for Java (with Rake on JRuby)
Given that my JRuby gem source is all in place
And I've installed the Java Development Kit
And I've installed JRuby
And I've already successfully executed rake task 'java compile' on JRuby
When rake task 'java gem' is invoked
Then rake task 'java gem' succeeded
And gem for platform 'java' get generated
16 changes: 16 additions & 0 deletions features/step_definitions/compilation.rb
Expand Up @@ -8,6 +8,11 @@
generate_source_code_for extension_name
end

Given /^a extension Java-compilable '(.*)'$/ do |extension_name|
generate_java_compile_extension_task_for extension_name
generate_java_source_code_for extension_name
end

Given /^a extension '(.*)' multi cross\-compilable$/ do |extension_name|
generate_multi_cross_compile_extension_task_for extension_name
generate_source_code_for extension_name
Expand All @@ -23,12 +28,23 @@
Given "a extension cross-compilable 'extension_one'"
end

Given /^that all my Java source files are in place$/ do
Given "a safe project directory"
Given "a extension Java-compilable 'extension_one'"
end

Given /^that my gem source is all in place$/ do
Given "a safe project directory"
Given "a gem named 'gem_abc'"
Given "a extension cross-compilable 'extension_one'"
end

Given /^that my JRuby gem source is all in place$/ do
Given "a safe project directory"
Given "a gem named 'gem_abc'"
Given "a extension Java-compilable 'extension_one'"
end

Given /^that my gem source is all in place to target two platforms$/ do
Given "a safe project directory"
Given "a gem named 'gem_abc'"
Expand Down
9 changes: 1 addition & 8 deletions features/step_definitions/cross_compilation.rb
Expand Up @@ -6,14 +6,7 @@
end

Given %r{^I've installed cross compile toolchain$} do
compilers = %w(i586-mingw32msvc-gcc i386-mingw32-gcc)
paths = ENV['PATH'].split(File::PATH_SEPARATOR)
compiler = compilers.find do |comp|
paths.find do |path|
File.exist? File.join(path, comp)
end
end
pending "Cannot locate suitable compiler in the PATH." unless compiler
pending 'Cannot locate suitable compiler in the PATH.' unless search_path(%w(i586-mingw32msvc-gcc i386-mingw32-gcc))
end

Then /^binaries for platform '(.*)' get generated$/ do |platform|
Expand Down
33 changes: 29 additions & 4 deletions features/step_definitions/execution.rb
@@ -1,15 +1,25 @@
Given %r{^I've already successfully executed rake task '(.*)'$} do |task_name|
emptyness = `rake #{task_name} 2>&1`
# FIXME: Make the Transform work
#
# Transform /^| on JRuby$/ do |step_arg|
# / on JRuby/.match(step_arg) != nil
# end

Given %r{^I've already successfully executed rake task '(.*)'(| on JRuby)$} do |task_name, on_jruby|
rake_cmd = "rake #{task_name}"
rake_cmd = 'jruby -S ' << rake_cmd if on_jruby == ' on JRuby'
emptyness = `#{rake_cmd} 2>&1`
unless $?.success?
warn emptyness
raise "rake failed with #{$?.exitstatus}"
end
end

When /^rake task '(.*)' is invoked$/ do |task_name|
When /^rake task '(.*)' is invoked(| on JRuby)$/ do |task_name, on_jruby|
@output ||= {}
@result ||= {}
@output[task_name] = `rake #{task_name} 2>&1`
rake_cmd = "rake #{task_name}"
rake_cmd = 'jruby -S ' << rake_cmd if on_jruby == ' on JRuby'
@output[task_name] = `#{rake_cmd} 2>&1`
@result[task_name] = $?.success?
end

Expand All @@ -21,10 +31,25 @@
end
end

Then /^rake task '(.*)' should fail$/ do |task_name|
if @result.nil? || !@result.include?(task_name) then
raise "The task #{task_name} should be invoked first."
else
@result[task_name].should be_false
end
end

Then /^output of rake task '(.*)' (contains|do not contain) \/(.*)\/$/ do |task_name, condition, regex|
if condition == 'contains' then
@output[task_name].should match(%r(#{regex}))
else
@output[task_name].should_not match(%r(#{regex}))
end
end

Then /^output of rake task '(.*)' warns$/ do |task_name, warning|
STDERR.puts task_name.inspect
STDERR.puts @output[task_name].inspect
STDERR.puts warning.inspect
@output[task_name].should include(warning)
end
12 changes: 12 additions & 0 deletions features/step_definitions/gem.rb
Expand Up @@ -12,6 +12,12 @@

Then /^a gem for '(.*)' version '(.*)' platform '(.*)' do exist in '(.*)'$/ do |name, version, platform, folder|
File.exist?(gem_file_platform(folder, name, version, platform)).should be_true

# unpack the Gem and check what's inside!
`gem unpack #{gem_file_platform(folder, name, version, platform)} --target tmp`
unpacked_gem_dir = unpacked_gem_dir_platform('tmp', name, version, platform)
File.exist?(unpacked_gem_dir).should be_true
Dir.glob(unpacked_gem_dir << "/lib/*.#{binary_extension(platform)}").should_not be_empty
end

Then /^gem for platform '(.*)' get generated$/ do |platform|
Expand All @@ -28,3 +34,9 @@ def gem_file_platform(folder, name, version, platform = nil)
file << ".gem"
file
end

def unpacked_gem_dir_platform(folder, name, version, platform = nil)
file = "#{folder}/#{name}-#{version}"
file << "-" << (platform || Gem::Platform.new(RUBY_PLATFORM).to_s)
file
end
7 changes: 7 additions & 0 deletions features/step_definitions/java_compilation.rb
@@ -0,0 +1,7 @@
Given %r{^I've installed the Java Development Kit$} do
pending('Cannot locate suitable Java compiler (the Java Development Kit) in the PATH.') unless search_path(%w(javac javac.exe))
end

Given %r{^I've installed JRuby$} do
pending('Cannot locate a JRuby installation in the PATH.') unless search_path(%w(jruby jruby.exe jruby.bat))
end
29 changes: 29 additions & 0 deletions features/support/file_template_helpers.rb
Expand Up @@ -75,6 +75,15 @@ def template_rake_extension_multi_cross_compile(extension_name)
EOF
end

def template_rake_extension_java_compile(extension_name, gem_spec = nil)
<<-EOF
require 'rake/javaextensiontask'
Rake::JavaExtensionTask.new("#{extension_name}"#{', SPEC' if gem_spec}) do |ext|
# nothing
end
EOF
end

def template_extconf(extension_name)
<<-EOF
require 'mkmf'
Expand All @@ -97,6 +106,26 @@ def template_source_h
#include "ruby.h"
EOF
end

def template_source_java(extension_name)
<<-EOF
import org.jruby.Ruby;
import org.jruby.runtime.load.BasicLibraryService;
public class #{camelize(extension_name)}Service implements BasicLibraryService {
public boolean basicLoad(final Ruby runtime) throws java.io.IOException {
System.out.println("#{camelize(extension_name)}Service.java of extension #{extension_name}\\n");
return true;
}
}
EOF
end

def camelize(str)
str.gsub(/(^|_)(.)/) { $2.upcase }
end

end

World(FileTemplateHelpers)
29 changes: 28 additions & 1 deletion features/support/generator_helpers.rb
Expand Up @@ -4,7 +4,7 @@ def generate_scaffold_structure
FileUtils.mkdir_p "lib"
FileUtils.mkdir_p "tasks"
FileUtils.mkdir_p "tmp"

# create Rakefile loader
File.open("Rakefile", 'w') do |rakefile|
rakefile.puts template_rakefile.strip
Expand Down Expand Up @@ -60,6 +60,25 @@ def generate_cross_compile_extension_task_for(extension_name)
end
end

def generate_java_compile_extension_task_for(extension_name)
# create folder structure
FileUtils.mkdir_p "ext/#{extension_name}"

return if File.exist?("tasks/#{extension_name}.rake")

# create specific extension rakefile
# Building a gem?
if File.exist?("tasks/gem.rake") then
File.open("tasks/gem.rake", 'a+') do |ext_in_gem|
ext_in_gem.puts template_rake_extension_java_compile(extension_name, true)
end
else
File.open("tasks/#{extension_name}.rake", 'w') do |ext_rake|
ext_rake.puts template_rake_extension_java_compile(extension_name)
end
end
end

def generate_multi_cross_compile_extension_task_for(extension_name)
# create folder structure
FileUtils.mkdir_p "ext/#{extension_name}"
Expand Down Expand Up @@ -91,6 +110,14 @@ def generate_source_code_for(extension_name)
ext.puts template_extconf(extension_name)
end
end

def generate_java_source_code_for(extension_name)
# source .java file
File.open("ext/#{extension_name}/#{camelize(extension_name)}Service.java", 'w') do |c|
c.puts template_source_java(extension_name)
end
end

end

World(GeneratorHelpers)
13 changes: 13 additions & 0 deletions features/support/platform_extension_helpers.rb
Expand Up @@ -5,10 +5,23 @@ def binary_extension(platform = RUBY_PLATFORM)
'bundle'
when /mingw|mswin|linux/
'so'
when /java/
'jar'
else
RbConfig::CONFIG['DLEXT']
end
end

def search_path(binaries)
paths = ENV['PATH'].split(File::PATH_SEPARATOR)
binary = binaries.find do |bin_file|
paths.find do |path|
bin = File.join(path, bin_file)
File.exist?(bin) && File.executable?(bin)
end
end
binary
end
end

World(PlatformExtensionHelpers)

0 comments on commit 3f27432

Please sign in to comment.