diff --git a/DEVELOPERS_NOTE.md b/DEVELOPERS_NOTE.md index 031109d..650ce34 100644 --- a/DEVELOPERS_NOTE.md +++ b/DEVELOPERS_NOTE.md @@ -14,12 +14,11 @@ * [hoe](https://github.com/seattlerb/hoe) * [hoe-gemspec](https://github.com/flavorjones/hoe-gemspec) * [rake-compiler](https://github.com/luislavena/rake-compiler) - * [gem-compile](https://github.com/frsyuki/gem-compile) ## Create ruby-opencv gem Run the following commands. -When you use mingw32, use **MSYS console**, or when you use mswin32, +When you use mingw32, use **MSYS console**, or when you use mswin32, use [**Visual Studio Command Prompt**](http://msdn.microsoft.com/en-us/library/ms229859.aspx) instead of cmd.exe. @@ -32,16 +31,34 @@ $ git ls-files > Manifest.txt $ rake gem:spec $ rake gem ``` -**ruby-opencv-x.y.z.gem** will be created in pkg/ directory. +**ruby-opencv-x.y.z.gem** will be created in **pkg** directory. -To create pre-build binaries, run the following commands in Windows. +To create pre-build binaries, create a config file firstly: -``` -$ cd pkg -$ gem compile ruby-opencv-*.gem +```yml +# config.yml +platform: mingw32 +rubies: + - C:/ruby-1.9.3-p392-mingw32/bin/ruby.exe + - C:/ruby-2.0.0-p0-mingw32/bin/ruby.exe +extopts: + - --with-opencv-include=C:/opencv/build/include + - --with-opencv-lib=C:/opencv/build/x86/mingw/lib ``` -**ruby-opencv-x.y.z-x86-mingw32.gem** will be created when you use mingw32, or +Entries are below: + +- **platform**: Target platform (e.g. mingw32, mswin32) +- **rubies**: Array of target versions of ruby's paths (You can create fat gems if you specify multiple versions of ruby) +- **extopts**: Array of options to be passed to **extconf.rb** + +Then, run the following command: + +``` +$ rake gem:precompile CONFIG=config.yml +``` + +**ruby-opencv-x.y.z-mingw32.gem** will be created when you use mingw32, or **ruby-opencv-x.y.z-x86-mswin32.gem** when you use mswin32. diff --git a/Gemfile b/Gemfile index b0e2a1a..1c6b08d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,5 @@ group :development do gem "hoe" gem "hoe-gemspec" gem "rake-compiler" - gem "gem-compile" end diff --git a/Manifest.txt b/Manifest.txt index f8b850f..19a8c61 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -6,6 +6,7 @@ License.txt Manifest.txt README.md Rakefile +config.yml examples/alpha_blend.rb examples/box.png examples/box_in_scene.png diff --git a/Rakefile b/Rakefile index 33bb37b..7f059d7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,13 @@ # -*- mode: ruby; coding: utf-8 -*- require 'rubygems' -require './lib/opencv/psyched_yaml' +require "rubygems/ext" +require "rubygems/installer" require 'hoe' require 'rake/extensiontask' +require 'fileutils' +require './lib/opencv/psyched_yaml' + +SO_FILE = 'opencv.so' Hoe.plugin :gemspec @@ -16,14 +21,16 @@ hoespec = Hoe.spec 'ruby-opencv' do |s| s.readme_file = 'README.md' s.history_file = 'History.txt' + s.spec_extras = { :extensions => ['ext/opencv/extconf.rb'] } + s.test_globs = ['test/test_*.rb'] s.urls = ['https://github.com/ruby-opencv/ruby-opencv/'] s.extra_dev_deps << ['rake-compiler', '>= 0'] << ['hoe-gemspec'] Rake::ExtensionTask.new('opencv', spec) do |ext| - ext.lib_dir = File.join('lib', 'opencv') + ext.lib_dir = 'lib' end end @@ -31,4 +38,53 @@ hoespec.spec.files.delete('.gemtest') Rake::Task[:test].prerequisites << :compile +desc 'Create a pre-compiled gem' +task 'gem:precompile' => ['gem'] do + tmp_dir = Dir.mktmpdir('tmp', '.') + gemfile = Dir.glob("pkg/*.gem")[0] + target_dir = File.join(tmp_dir, File.basename(gemfile, '.gem')) + + installer = Gem::Installer.new(gemfile) + installer.unpack(target_dir) + + gemspec = installer.spec + extension = gemspec.extensions[0] + gemspec.extensions.clear + + config = ENV['CONFIG'] ? YAML.load_file(ENV['CONFIG']) : {} + rubies = config['rubies'] || [Gem.ruby] + args = config['extopts'] || [] + gemspec.platform = config['platform'] || Gem::Platform::CURRENT + + multi = rubies.size > 1 + rubies.each { |ruby| + lib_dir = 'lib' + if multi + major, minor, _ = `#{ruby} -e "print RUBY_VERSION"`.chomp.split('.') + lib_dir = File.join(lib_dir, [major, minor].join('.')) + end + + make_cmd = (`#{ruby} -e "print RUBY_PLATFORM"` =~ /mswin/) ? 'nmake' : 'make' + Dir.chdir(target_dir) { + cmd = [ruby, extension, *args].join(' ') + results = [] + Gem::Ext::ExtConfBuilder.run(cmd, results) + Gem::Ext::ExtConfBuilder.make('', results) + + FileUtils.mkdir_p lib_dir + FileUtils.mv SO_FILE, lib_dir + sh "#{make_cmd} clean" + } + + gemspec.files << File.join(lib_dir, SO_FILE) + } + + Dir.chdir(target_dir) { + gemfile = Gem::Package.build(gemspec) + FileUtils.mv gemfile, File.dirname(__FILE__) + } + + FileUtils.rm_rf tmp_dir +end + # vim: syntax=ruby diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..4034d68 --- /dev/null +++ b/config.yml @@ -0,0 +1,7 @@ +platform: mingw32 +rubies: + - C:/ruby-1.9.3-p392-mingw32/bin/ruby.exe + - C:/ruby-2.0.0-p0-mingw32/bin/ruby.exe +extopts: + - --with-opencv-include=C:/opencv/build/include + - --with-opencv-lib=C:/opencv/build/x86/mingw/lib diff --git a/lib/opencv.rb b/lib/opencv.rb index 068e5e7..d6ba2e1 100755 --- a/lib/opencv.rb +++ b/lib/opencv.rb @@ -1,3 +1,12 @@ require (File.dirname(__FILE__) + '/opencv/version') -require 'opencv.so' +if RUBY_PLATFORM =~ /mingw|mswin/ + major, minor, subminor = RUBY_VERSION.split('.') + begin + require "#{major}.#{minor}/opencv.so" + rescue LoadError + require 'opencv.so' + end +else + require 'opencv.so' +end