Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 3a5012e

Browse files
committed
Merge pull request #29 from ruby-concurrency/clean-up-rakefile
Cleaning up Rakefile
2 parents 9f16e93 + 612ae6f commit 3a5012e

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ gemspec
44

55
group :development do
66
gem 'rake', '~> 10.3.2'
7+
gem 'rake-compiler', '0.9.5'
78
end
89

910
group :testing do

Rakefile

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,65 @@ Rake::TestTask.new do |t|
1616
t.verbose = true
1717
end
1818

19-
spec = eval(File.read(File.expand_path('../ref.gemspec', __FILE__)))
2019

21-
Gem::PackageTask.new(spec) do |p|
22-
p.gem_spec = spec
23-
end
24-
Rake.application["package"].prerequisites.unshift("java:build")
25-
Rake.application["package"].prerequisites.unshift("rbx:delete_rbc_files")
20+
GEM_NAME = 'ref'
21+
EXTENSION_NAME = 'extension'
22+
JAVA_EXT_NAME = 'ref_ext'
23+
CORE_GEMSPEC = Gem::Specification.load('ref.gemspec')
2624

27-
desc "Release to rubygems.org"
28-
task :release => [:package, "gem:push"]
25+
if Ref.jruby?
26+
CORE_GEM = "#{GEM_NAME}-#{Ref::VERSION}-java.gem"
2927

30-
namespace :java do
31-
desc "Build the jar files for Jruby support. You must set your JRUBY_HOME environment variable to the root of your jruby install."
32-
task :build do
33-
base_dir = File.dirname(__FILE__)
34-
tmp_dir = File.join(base_dir, "tmp")
35-
classes_dir = File.join(tmp_dir, "classes")
36-
jar_dir = File.join(base_dir, "lib", "org", "jruby", "ext", "ref")
37-
FileUtils.rm_rf(classes_dir)
38-
ext_dir = File.join(base_dir, "ext", "java")
39-
source_files = FileList["#{base_dir}/ext/**/*.java"]
40-
jar_file = File.join(jar_dir, 'references.jar')
41-
# Only build if any of the source files have changed
42-
up_to_date = File.exist?(jar_file) && source_files.all?{|f| File.mtime(f) <= File.mtime(jar_file)}
43-
unless up_to_date
44-
FileUtils.mkdir_p(classes_dir)
45-
puts "#{ENV['JAVA_HOME']}/bin/javac -target 1.5 -classpath '#{"#{ENV['JRUBY_HOME']}/lib/jruby.jar"}' -d '#{classes_dir}' -sourcepath '#{ext_dir}' '#{source_files.join("' '")}'"
46-
`#{ENV['JAVA_HOME']}/bin/javac -target 1.5 -classpath '#{"#{ENV['JRUBY_HOME']}/lib/jruby.jar"}' -d '#{classes_dir}' -sourcepath '#{ext_dir}' '#{source_files.join("' '")}'`
47-
if $? == 0
48-
FileUtils.rm_rf(jar_dir) if File.exist?(jar_dir)
49-
FileUtils.mkdir_p(jar_dir)
50-
`#{ENV['JAVA_HOME']}/bin/jar cf '#{jar_file}' -C '#{classes_dir}' org`
51-
end
52-
FileUtils.rm_rf(classes_dir)
53-
end
28+
require 'rake/javaextensiontask'
29+
Rake::JavaExtensionTask.new(JAVA_EXT_NAME, CORE_GEMSPEC) do |ext|
30+
ext.ext_dir = 'ext'
5431
end
32+
else
33+
CORE_GEM = "#{GEM_NAME}-#{Ref::VERSION}.gem"
5534
end
5635

57-
namespace :rbx do
58-
desc "Cleanup *.rbc files in lib directory"
59-
task :delete_rbc_files do
60-
FileList["lib/**/*.rbc"].each do |rbc_file|
61-
File.delete(rbc_file)
62-
end
63-
nil
36+
task :clean do
37+
rm_f Dir.glob('./**/*.so')
38+
rm_f Dir.glob('./**/*.bundle')
39+
rm_f Dir.glob('./lib/*.jar')
40+
mkdir_p 'pkg'
41+
end
42+
43+
44+
namespace :build do
45+
build_deps = [:clean]
46+
build_deps << :compile if Ref.jruby?
47+
desc "Build #{CORE_GEM} into the pkg directory"
48+
task :core => build_deps do
49+
sh "gem build #{CORE_GEMSPEC.name}.gemspec"
50+
sh 'mv *.gem pkg/'
6451
end
6552
end
6653

54+
if Ref.jruby?
55+
desc 'Build JRuby-specific core gem (alias for `build:core`)'
56+
task :build => ['build:core']
57+
end
58+
6759
namespace :test do
6860
namespace :performance do
6961
desc "Run a speed test on how long it takes to create 100000 weak references"
7062
task :weak_reference do
7163
puts "Testing performance of weak references..."
72-
t = Time.now
73-
Process.fork do
74-
100000.times do
75-
Ref::WeakReference.new(Object.new)
64+
unless Ref.jruby?
65+
t = Time.now
66+
Process.fork do
67+
100000.times do
68+
Ref::WeakReference.new(Object.new)
69+
end
7670
end
71+
Process.wait
72+
puts "Creating 100,000 weak references took #{Time.now - t} seconds"
73+
else
74+
puts 'Cannot run weak_reference performance test on JRuby - Fork is not available on this platform.'
7775
end
78-
Process.wait
79-
puts "Creating 100,000 weak references took #{Time.now - t} seconds"
8076
end
81-
77+
8278
desc "Run a speed test on how long it takes to create 100000 soft references"
8379
task :soft_reference do
8480
puts "Testing performance of soft references..."

lib/org/jruby/ext/ref/references.jar

-4.44 KB
Binary file not shown.

lib/ref.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ module Ref
44
require 'ref/reference'
55
require 'ref/reference_queue'
66

7-
# Set the best implementation for weak references based on the runtime.
8-
if defined?(RUBY_PLATFORM) && RUBY_PLATFORM == 'java'
9-
# Use native Java references
7+
if defined?(Java)
108
begin
119
$LOAD_PATH.unshift(File.dirname(__FILE__))
10+
require 'ref_ext'
1211
require 'org/jruby/ext/ref/references'
13-
ensure
14-
$LOAD_PATH.shift if $LOAD_PATH.first == File.dirname(__FILE__)
12+
rescue LoadError
13+
require 'ref/soft_reference'
14+
require 'ref/weak_reference'
15+
warn 'Error loading Rspec rake tasks, probably building the gem...'
1516
end
1617
else
1718
require 'ref/soft_reference'
@@ -35,4 +36,8 @@ module Ref
3536
require 'ref/strong_reference'
3637
require 'ref/weak_key_map'
3738
require 'ref/weak_value_map'
39+
40+
def self.jruby?
41+
defined?(Java)
42+
end
3843
end

lib/ref_ext.jar

4.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)