0

I am creating a new gem that uses FFI to create a ruby binding to webview C library

The gem structure is as follows:

.
├── ...
├── Rakefile
├── ext
│   └── webview
│       ├── extconf.rb
│       ├── webview.cc
│       └── webview.h
├── lib
│   ├── webview
│   │   └── version.rb
│   ├── webview.rb
│   └── webview.so
├── ...
└── webview.gemspec

The Rakefile is as follows:

require "bundler/gem_tasks"
require "rubocop/rake_task"
require "rake/extensiontask"

RuboCop::RakeTask.new
task default: :rubocop

Rake::ExtensionTask.new "webview"

The Gemspec is as follows:

# frozen_string_literal: true

require_relative "lib/webview/version"

Gem::Specification.new do |spec|
  ...
  # Specify which files should be added to the gem when it is released.
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
  end
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.extensions = ["ext/webview/extconf.rb"]

  # Uncomment to register a new dependency of your gem
  spec.add_dependency "ffi"
  spec.add_dependency "rake-compiler"
end

When I run rake compile it compiles the library and adds webview.so to the lib/ dir.

But when I run rake install:local the webview.so is not in place!

What's wrong?

Mohamed Anwer
  • 172
  • 2
  • 15

1 Answers1

0

I needed to add the ext folder to git using git add ext/ this way git ls-files was able to capture it and includes the file in the installation process.

Also, I followed the convention of rubygems by moving webview.rb into lib/webview/ and created a webview.rb in lib/ that contains only:

require_relative "webview/webview"

Also, I added this block to Rake::ExtensionTask in Rakefile:

Rake::ExtensionTask.new "webview" do |ext|
  ext.lib_dir = "lib/webview"
end

in order to move the shared object inside lib/webview/

Last thing, I did is to call ffi_lib with absolute path not relative path like this:

ffi_lib File.dirname(__FILE__) + '/webview.so'

And Voila! it works

Mohamed Anwer
  • 172
  • 2
  • 15