From d05d4cc0838d42e903fec5ffed17a8cb380b74e2 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 17 May 2021 12:20:03 +0200 Subject: [PATCH] feat: Remove Ruby dependency to `rutie`. By removing the dependency to `rutie` for the Ruby code (not for the Rust code!), we can simplify our Rust code, including the Rust doctests: * No need to emulate `RbConfig`, * So no need to have a build script for `wasmer` (`build.rs`), * No need to get `rbconfig` in `Rakefile`, * No need to manually add `rutie` to `$LOAD_PATh` when doctesting. --- Rakefile | 1 - crates/rutie-test/src/lib.rs | 7 ------- crates/wasmer/build.rs | 7 ------- lib/wasmer.rb | 31 +++++++++++++++++++++++++++++-- wasmer.gemspec | 3 +-- 5 files changed, 30 insertions(+), 19 deletions(-) delete mode 100644 crates/wasmer/build.rs diff --git a/Rakefile b/Rakefile index 89900da..00e33bc 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,3 @@ -require "rbconfig" require "bundler/gem_tasks" require "rake/testtask" diff --git a/crates/rutie-test/src/lib.rs b/crates/rutie-test/src/lib.rs index 6e064a7..85d6b74 100644 --- a/crates/rutie-test/src/lib.rs +++ b/crates/rutie-test/src/lib.rs @@ -9,14 +9,7 @@ root = File.expand_path("../..", ENV["CARGO_MANIFEST_DIR"]) Dir.chdir(root) -class RbConfig - CONFIG = {{ - "host_os" => ENV["TARGET"] - }} -end - $LOAD_PATH.unshift(File.expand_path("lib", root)) -$LOAD_PATH.unshift(File.expand_path("vendor/bundle/rutie/lib", root)) require "wasmer" diff --git a/crates/wasmer/build.rs b/crates/wasmer/build.rs deleted file mode 100644 index 745c02d..0000000 --- a/crates/wasmer/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - // To satisfy `rutie-test`'s macros. - println!( - "cargo:rustc-env=TARGET={}", - std::env::var("TARGET").unwrap() - ); -} diff --git a/lib/wasmer.rb b/lib/wasmer.rb index 4b25a8c..611bee5 100644 --- a/lib/wasmer.rb +++ b/lib/wasmer.rb @@ -1,5 +1,32 @@ -require "rutie" +require "fiddle" module Wasmer - Rutie.new(:wasmer_ruby).init "init", __dir__ + shared_library_name = :wasmer_ruby + init_function = :init + + shared_library_prefix = + case RUBY_PLATFORM + when /windows|mswin|mingw/ then "" + when /cygwin/ then "cyg" + else "lib" + end + + shared_library_suffix = + case RUBY_PLATFORM + when /darwin/ then "dylib" + when /windows|mswin|mingw|cygwin/ then "dll" + else "so" + end + + shared_library_directory = File.expand_path "../target/release/", __dir__ + shared_library_path = File.join( + shared_library_directory, + [shared_library_prefix, shared_library_name, ".", shared_library_suffix].join() + ) + + Fiddle::Function.new( + Fiddle::dlopen(shared_library_path)[init_function.to_s], + [], + Fiddle::TYPE_VOIDP + ).call end diff --git a/wasmer.gemspec b/wasmer.gemspec index 2fc1d94..42f8ef0 100644 --- a/wasmer.gemspec +++ b/wasmer.gemspec @@ -15,12 +15,11 @@ Gem::Specification.new do |spec| spec.extensions = %w(Rakefile) spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do - `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(examples|tests)/}) } + `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(examples|tests|doc)/}) } end spec.require_paths = %w(lib) - spec.add_dependency "rutie", "~> 0.0.4" spec.add_development_dependency "bundler", "~> 2.1" spec.add_development_dependency "rake", "~> 13.0" end