Skip to content

Commit

Permalink
Binary gem cross-compilation via rake-compiler-dock
Browse files Browse the repository at this point in the history
Simply have Docker installed and run this to compile binary gems:

    bundle exec rake gem:native
  • Loading branch information
glebm committed May 9, 2019
1 parent 3c3344f commit a17b927
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
19 changes: 13 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
begin
require 'bundler/gem_tasks'
rescue LoadError
puts 'Cannot load bundler/gem_tasks'
end
require 'bundler/gem_tasks'

task default: :test

require 'rake/extensiontask'
Rake::ExtensionTask.new do |ext|
gem_spec = Gem::Specification.load("sassc.gemspec")
Rake::ExtensionTask.new('libsass', gem_spec) do |ext|
ext.name = 'libsass'
ext.ext_dir = 'ext'
ext.lib_dir = 'lib/sassc'
ext.cross_compile = true
ext.cross_platform = %w[x86-mingw32 x64-mingw32 x86-linux x86_64-linux]
end

desc 'Compile all native gems via rake-compiler-dock (Docker)'
task 'gem:native' do
require 'rake_compiler_dock'
RakeCompilerDock.sh "bundle && gem i rake --no-document && "\
"rake cross native gem MAKE='nice make -j`nproc`' "\
"RUBY_CC_VERSION=2.6.0:2.5.0:2.4.0:2.3.0"
end

desc "Run all tests"
Expand Down
8 changes: 7 additions & 1 deletion lib/sassc/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ module Native

spec = Gem.loaded_specs["sassc"]
gem_root = spec.gem_dir
ffi_lib "#{gem_root}/lib/sassc/libsass.so"

ruby_version_so_path = "#{gem_root}/lib/sassc/#{RUBY_VERSION[/\d+.\d+/]}/libsass.so"
if File.exist?(ruby_version_so_path)
ffi_lib ruby_version_so_path
else
ffi_lib "#{gem_root}/lib/sassc/libsass.so"
end

require_relative "native/sass_value"

Expand Down
3 changes: 2 additions & 1 deletion sassc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "test_construct"
spec.add_development_dependency "pry"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rake-compiler"
spec.add_development_dependency "rake-compiler-dock"

spec.add_dependency "rake"
spec.add_dependency "ffi", "~> 1.9"

gem_dir = File.expand_path(File.dirname(__FILE__)) + "/"
Expand Down

0 comments on commit a17b927

Please sign in to comment.