Skip to content

Commit 1a2377e

Browse files
committed
Better platform detection in Rake compile and build tasks.
1 parent 3369e46 commit 1a2377e

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

Rakefile

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
#!/usr/bin/env rake
2+
3+
require_relative './lib/extension_helper'
4+
5+
## load the two gemspec files
16
CORE_GEMSPEC = Gem::Specification.load('concurrent-ruby.gemspec')
27
EXT_GEMSPEC = Gem::Specification.load('concurrent-ruby-ext.gemspec')
8+
9+
## constants used for compile/build tasks
10+
311
GEM_NAME = 'concurrent-ruby'
412
EXTENSION_NAME = 'extension'
513

6-
$:.push File.join(File.dirname(__FILE__), 'lib')
7-
require 'extension_helper'
14+
if Concurrent.jruby?
15+
CORE_GEM = "#{GEM_NAME}-#{Concurrent::VERSION}-java.gem"
16+
else
17+
CORE_GEM = "#{GEM_NAME}-#{Concurrent::VERSION}.gem"
18+
EXTENSION_GEM = "#{GEM_NAME}-ext-#{Concurrent::VERSION}.gem"
19+
NATIVE_GEM = "#{GEM_NAME}-ext-#{Concurrent::VERSION}-#{Gem::Platform.new(RUBY_PLATFORM)}.gem"
20+
end
21+
22+
## safely load all the rake tasks in the `tasks` directory
823

924
def safe_load(file)
1025
begin
1126
load file
1227
rescue LoadError => ex
13-
puts 'Error loading rake tasks, but will continue...'
28+
puts "Error loading rake tasks from '#{file}' but will continue..."
1429
puts ex.message
1530
end
1631
end
@@ -19,17 +34,21 @@ Dir.glob('tasks/**/*.rake').each do |rakefile|
1934
safe_load rakefile
2035
end
2136

22-
if defined?(JRUBY_VERSION)
37+
if Concurrent.jruby?
38+
39+
## create the compile task for the JRuby-specific gem
2340
require 'rake/javaextensiontask'
2441

25-
Rake::JavaExtensionTask.new('concurrent_ruby_ext', CORE_GEMSPEC) do |ext|
42+
Rake::JavaExtensionTask.new('java', CORE_GEMSPEC) do |ext|
2643
ext.ext_dir = 'ext'
2744
end
2845

2946
elsif Concurrent.allow_c_extensions?
47+
48+
## create the compile tasks for the extension gem
3049
require 'rake/extensiontask'
3150

32-
Rake::ExtensionTask.new(EXTENSION_NAME, EXT_GEMSPEC) do |ext|
51+
Rake::ExtensionTask.new('ext', EXT_GEMSPEC) do |ext|
3352
ext.ext_dir = 'ext/concurrent'
3453
ext.lib_dir = 'lib/concurrent'
3554
ext.source_pattern = '*.{c,h}'
@@ -52,6 +71,7 @@ elsif Concurrent.allow_c_extensions?
5271
end
5372
end
5473
else
74+
## create an empty compile task
5575
task :compile
5676
end
5777

@@ -66,52 +86,55 @@ task :clean do
6686
mkdir_p 'pkg'
6787
end
6888

69-
begin
70-
require 'rspec'
71-
require 'rspec/core/rake_task'
72-
73-
RSpec::Core::RakeTask.new(:spec) do |t|
74-
t.rspec_opts = '--color --backtrace --format documentation'
75-
end
76-
77-
task :default => [:clean, :compile, :spec]
78-
rescue LoadError
79-
puts 'Error loading Rspec rake tasks, probably building the gem...'
80-
end
81-
82-
build_deps = [:clean]
83-
build_deps << :compile if defined?(JRUBY_VERSION)
84-
85-
build_tasks = ['build:core']
86-
build_tasks += ['build:ext', 'build:native'] if Concurrent.allow_c_extensions?
87-
88-
CoreGem = "#{GEM_NAME}-#{Concurrent::VERSION}.gem"
89-
ExtensionGem = "#{GEM_NAME}-ext-#{Concurrent::VERSION}.gem"
90-
NativeGem = "#{GEM_NAME}-ext-#{Concurrent::VERSION}-#{Gem::Platform.new(RUBY_PLATFORM)}.gem"
89+
## create build tasks tailored to current platform
9190

9291
namespace :build do
9392

94-
desc "Build #{CoreGem} into the pkg directory"
93+
build_deps = [:clean]
94+
build_deps << :compile if Concurrent.jruby?
95+
96+
desc "Build #{CORE_GEM} into the pkg directory"
9597
task :core => build_deps do
9698
sh "gem build #{CORE_GEMSPEC.name}.gemspec"
9799
sh 'mv *.gem pkg/'
98100
end
99101

100-
if Concurrent.allow_c_extensions?
101-
102-
desc "Build #{ExtensionGem}.gem into the pkg directory"
102+
unless Concurrent.jruby?
103+
desc "Build #{EXTENSION_GEM} into the pkg directory"
103104
task :ext => [:clean] do
104105
sh "gem build #{EXT_GEMSPEC.name}.gemspec"
105106
sh 'mv *.gem pkg/'
106107
end
108+
end
107109

108-
desc "Build #{NativeGem} into the pkg directory"
110+
if Concurrent.allow_c_extensions?
111+
desc "Build #{NATIVE_GEM} into the pkg directory"
109112
task :native do
110-
sh "gem compile pkg/#{ExtensionGem}"
113+
sh "gem compile pkg/#{EXTENSION_GEM}"
111114
sh 'mv *.gem pkg/'
112115
end
113116
end
114117
end
115118

116-
desc 'Build all gems for this platform'
117-
task :build => build_tasks
119+
if Concurrent.jruby?
120+
desc 'Build JRuby-specific core gem (alias for `build:core`)'
121+
task :build => ['build:core']
122+
else
123+
desc 'Build core and extension gems'
124+
task :build => ['build:core', 'build:ext']
125+
end
126+
127+
## the RSpec task that compiles extensions when available
128+
129+
begin
130+
require 'rspec'
131+
require 'rspec/core/rake_task'
132+
133+
RSpec::Core::RakeTask.new(:spec) do |t|
134+
t.rspec_opts = '--color --backtrace --format documentation'
135+
end
136+
137+
task :default => [:clean, :compile, :spec]
138+
rescue LoadError
139+
puts 'Error loading Rspec rake tasks, probably building the gem...'
140+
end

lib/extension_helper.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Concurrent
2-
2+
33
@@c_ext_loaded ||= false
44
@@java_ext_loaded ||= false
55

@@ -8,6 +8,11 @@ def self.allow_c_extensions?
88
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby'
99
end
1010

11+
# @!visibility private
12+
def self.jruby?
13+
RUBY_PLATFORM == 'java'
14+
end
15+
1116
if allow_c_extensions? && !@@c_ext_loaded
1217
begin
1318
require 'concurrent/extension'
@@ -21,7 +26,7 @@ def self.allow_c_extensions?
2126
warn 'Performance on MRI may be improved with the concurrent-ruby-ext gem. Please see http://concurrent-ruby.com'
2227
end
2328
end
24-
elsif RUBY_PLATFORM == 'java' && !@@java_ext_loaded
29+
elsif jruby? && !@@java_ext_loaded
2530
begin
2631
require 'concurrent/extension'
2732
@@java_ext_loaded = true

yardoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit db7483b80f6efb7df9930f0031508d7e2eb6bad0

0 commit comments

Comments
 (0)