Permalink
Browse files

Switch to rake-compiler for building binary win32/java gems.

To build java gems:
  $ rvm use jruby
  $ rake java gem
  cp tmp/java/bcrypt_ext/bcrypt_ext.jar lib/bcrypt_ext.jar
  Successfully built RubyGem
  File: bcrypt-ruby-2.1.2-java.gem

To build win32 fat 1.8/1.9 binary gems:
  $ rake cross native gem RUBY_CC_VERSION=1.8.6:1.9.2
  cp tmp/x86-mswin32-60/bcrypt_ext/1.8.6/bcrypt_ext.so lib/1.8/bcrypt_ext.so
  cp tmp/x86-mswin32-60/bcrypt_ext/1.9.2/bcrypt_ext.so lib/1.9/bcrypt_ext.so
  Successfully built RubyGem
  File: bcrypt-ruby-2.1.2-x86-mingw32.gem

Note, to build win32 gems you will need to setup a cross-compilation environment:

  1. Download and run "MinGW 4.3.0" installer from
     http://crossgcc.rts-software.org/doku.php
  2. Add "/usr/local/i386-mingw32-4.3.0/" to your PATH
  3. gem install rake-compiler
  4. rake-compiler cross-ruby # to cross-compile 1.8.6
  5. rake-compiler cross-ruby RUBY_VERSION=1.9.2-p0
  • Loading branch information...
1 parent 974b0f6 commit 1965feb04521deb88687096017f69da1bd59d952 @tmm1 tmm1 committed Dec 18, 2010
Showing with 28 additions and 36 deletions.
  1. +1 −0 Gemfile
  2. +25 −27 Rakefile
  3. +2 −9 lib/bcrypt.rb
View
@@ -0,0 +1 @@
+gem 'rake-compiler', '>= 0.7.5'
View
@@ -1,6 +1,7 @@
-gem "rspec"
require "spec/rake/spectask"
require 'rake/gempackagetask'
+require 'rake/extensiontask'
+require 'rake/javaextensiontask'
require 'rake/contrib/rubyforgepublisher'
require 'rake/clean'
require 'rake/rdoctask'
@@ -16,8 +17,7 @@ PKG_FILES = FileList[
'ext/mri/*.c',
'ext/mri/*.h',
'ext/mri/*.rb',
- 'ext/jruby/bcrypt_jruby/BCrypt.java',
- 'ext/jruby/bcrypt_jruby/BCrypt.class'
+ 'ext/jruby/bcrypt_jruby/BCrypt.java'
]
CLEAN.include(
"ext/mri/*.o",
@@ -68,6 +68,7 @@ spec = Gem::Specification.new do |s|
s.files = PKG_FILES.to_a
s.require_path = 'lib'
+ s.add_development_dependency 'rake-compiler'
s.has_rdoc = true
s.rdoc_options = rd.options
@@ -81,40 +82,37 @@ spec = Gem::Specification.new do |s|
s.rubyforge_project = "bcrypt-ruby"
end
-file 'ext/jruby/bcrypt_jruby/BCrypt.class' => ["ext/jruby/bcrypt_jruby/BCrypt.java"] do
- Rake::Task['compile:jruby'].invoke
-end
-
Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_zip = true
pkg.need_tar = true
end
-desc "Clean, then compile the extension that's native to the current Ruby compiler."
-if RUBY_PLATFORM == "java"
- task :compile => 'compile:jruby'
+if RUBY_PLATFORM =~ /java/
+ Rake::JavaExtensionTask.new('bcrypt_ext', spec) do |ext|
+ ext.ext_dir = 'ext/jruby'
+ end
else
- task :compile => 'compile:mri'
-end
+ Rake::ExtensionTask.new("bcrypt_ext", spec) do |ext|
+ ext.ext_dir = 'ext/mri'
+ ext.cross_compile = true
+ ext.cross_platform = ['x86-mingw32', 'x86-mswin32-60']
-namespace :compile do
- desc "CLean, then compile all extensions"
- task :all => [:mri, :jruby]
-
- desc "Clean, then compile the MRI extension"
- task :mri => :clean do
- Dir.chdir('ext/mri') do
- ruby "extconf.rb"
- sh "make"
+ # inject 1.8/1.9 pure-ruby entry point
+ ext.cross_compiling do |spec|
+ spec.files += ["lib/#{ext.name}.rb"]
end
end
-
- desc "Clean, then compile the JRuby extension"
- task :jruby => :clean do
- Dir.chdir('ext/jruby/bcrypt_jruby') do
- sh "javac -source 1.4 -target 1.4 BCrypt.java"
- end
+end
+
+# Entry point for fat-binary gems on win32
+file("lib/bcrypt_ext.rb") do |t|
+ File.open(t.name, 'wb') do |f|
+ f.write <<-eoruby
+RUBY_VERSION =~ /(\\d+.\\d+)/
+require "\#{$1}/#{File.basename(t.name, '.rb')}"
+ eoruby
end
+ at_exit{ FileUtils.rm t.name if File.exists?(t.name) }
end
desc "Run a set of benchmarks on the compiled extension."
View
@@ -2,19 +2,12 @@
if RUBY_PLATFORM == "java"
require 'java'
- $CLASSPATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "ext", "jruby"))
else
- begin
- require "bcrypt_ext"
- rescue LoadError
- extdir = File.expand_path(File.join(File.dirname(__FILE__), "..", "ext", "mri"))
- $LOAD_PATH.unshift(extdir) if File.directory?(extdir) && !$LOAD_PATH.include?(extdir)
- require "bcrypt_ext"
- end
-
require "openssl"
end
+require 'bcrypt_ext'
+
# A Ruby library implementing OpenBSD's bcrypt()/crypt_blowfish algorithm for
# hashing passwords.
module BCrypt

0 comments on commit 1965feb

Please sign in to comment.