Skip to content

Commit d3f7137

Browse files
ruby : fix bindings (#2484)
* Improve Rakefile * Remove intermediate files * Remove unnecessary manipulations from extconf.rb * Add README and LINCENSE to source files * Manage ext source files using YAML file * Use extsources.yaml to include files into gem package file * Add git-managed source files to build dependency * Add test task * Download model for test if not exists * Add test for build * Ignore gem package directory * Enable GitHub action for Ruby binding * Fix model name * Build lib file for test * Use extension for each platform * Use extension for each platform on testing * Move built lib file rather than copy * Add intermediate files to clean targets
1 parent f7c99e4 commit d3f7137

22 files changed

+201
-17732
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Bindings Tests (Ruby)
2+
on:
3+
push:
4+
paths:
5+
- bindings/ruby/**
6+
- src/whisper.cpp
7+
- include/whisper.h
8+
- ggml/src/ggml.c
9+
- ggml/src/ggml-impl.h
10+
- ggml/src/ggml-aarch64.h
11+
- ggml/src/ggml-aarch64.c
12+
- ggml/src/ggml-alloc.c
13+
- ggml/src/ggml-backend-impl.h
14+
- ggml/src/ggml-backend.cpp
15+
- ggml/src/ggml-common.h
16+
- ggml/src/ggml-quants.h
17+
- ggml/src/ggml-quants.c
18+
- ggml/src/ggml-cpu-impl.h
19+
- ggml/include/ggml.h
20+
- ggml/include/ggml-alloc.h
21+
- ggml/include/ggml-backend.h
22+
- ggml/include/ggml-cuda.h
23+
- ggml/include/ggml-kompute.h
24+
- ggml/include/ggml-metal.h
25+
- ggml/include/ggml-sycl.h
26+
- ggml/include/ggml-vulkan.h
27+
- examples/dr_wav.h
28+
pull_request:
29+
paths:
30+
- bindings/ruby/**
31+
- src/whisper.cpp
32+
- include/whisper.h
33+
- ggml/src/ggml.c
34+
- ggml/src/ggml-impl.h
35+
- ggml/src/ggml-aarch64.h
36+
- ggml/src/ggml-aarch64.c
37+
- ggml/src/ggml-alloc.c
38+
- ggml/src/ggml-backend-impl.h
39+
- ggml/src/ggml-backend.cpp
40+
- ggml/src/ggml-common.h
41+
- ggml/src/ggml-quants.h
42+
- ggml/src/ggml-quants.c
43+
- ggml/src/ggml-cpu-impl.h
44+
- ggml/include/ggml.h
45+
- ggml/include/ggml-alloc.h
46+
- ggml/include/ggml-backend.h
47+
- ggml/include/ggml-cuda.h
48+
- ggml/include/ggml-kompute.h
49+
- ggml/include/ggml-metal.h
50+
- ggml/include/ggml-sycl.h
51+
- ggml/include/ggml-vulkan.h
52+
- examples/dr_wav.h
53+
54+
jobs:
55+
ubuntu-latest:
56+
runs-on: ubuntu-latest
57+
defaults:
58+
run:
59+
working-directory: bindings/ruby
60+
steps:
61+
- uses: ruby/setup-ruby@v1
62+
with:
63+
ruby-version: '3.0'
64+
- uses: actions/checkout@v4
65+
- run: rake test

.github/workflows/bindings-ruby.yml.disabled

Lines changed: 0 additions & 23 deletions
This file was deleted.

bindings/ruby/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
README.md
2+
LICENSE
3+
pkg/
4+
lib/whisper.*

bindings/ruby/Rakefile

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
require 'rake/clean'
2-
require 'rubygems/package'
3-
4-
desc 'Build gem'
5-
task :package do
6-
spec_source = File.read File.join(File.dirname(__FILE__),'whispercpp.gemspec')
7-
spec = nil
8-
# see: http://gist.github.com/16215
9-
Thread.new { spec = eval("#{spec_source}") }.join
10-
spec.validate
11-
Gem::Package.build(spec)
2+
require "bundler/gem_tasks"
3+
require "pathname"
4+
require "yaml"
5+
require "rake/testtask"
6+
7+
extsources = YAML.load_file("extsources.yaml")
8+
extsources.each_pair do |src_dir, dests|
9+
dests.each do |dest|
10+
src = Pathname(src_dir)/File.basename(dest)
11+
12+
file src
13+
file dest => src do |t|
14+
cp t.source, t.name
15+
end
16+
end
17+
end
18+
SOURCES = extsources.values.flatten
19+
CLEAN.include SOURCES
20+
CLEAN.include FileList["ext/*.o", "ext/whisper.so", "ext/whisper.bundle", "ext/whisper.dll"]
21+
22+
task build: SOURCES + FileList[
23+
"ext/extconf.rb",
24+
"ext/ruby_whisper.h",
25+
"ext/ruby_whisper.cpp",
26+
"whispercpp.gemspec",
27+
]
28+
29+
directory "pkg"
30+
CLOBBER.include "pkg"
31+
32+
TEST_MODEL = "../../models/ggml-base.en.bin"
33+
LIB_NAME = "whisper".ext(RbConfig::CONFIG["DLEXT"])
34+
LIB_FILE = File.join("lib", LIB_NAME)
35+
36+
directory "lib"
37+
task LIB_FILE => SOURCES + ["lib"] do |t|
38+
Dir.chdir "ext" do
39+
sh "ruby extconf.rb"
40+
sh "make"
41+
end
42+
mv "ext/#{LIB_NAME}", t.name
43+
end
44+
CLEAN.include LIB_FILE
45+
46+
Rake::TestTask.new do |t|
47+
t.test_files = FileList["tests/test_*.rb"]
48+
end
49+
task test: [TEST_MODEL, LIB_FILE]
50+
51+
file TEST_MODEL do
52+
Dir.chdir "../.." do
53+
sh "./models/download-ggml-model.sh base.en"
54+
end
1255
end

bindings/ruby/ext/.gitignore

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@ ggml.c
33
ggml.h
44
ggml-alloc.c
55
ggml-alloc.h
6-
whisper.bundle
6+
ggml-aarch64.c
7+
ggml-aarch64.h
8+
ggml-backend.cpp
9+
ggml-backend-impl.h
10+
ggml-backend.c
11+
ggml-backend.h
12+
ggml-common.h
13+
ggml-cpu-impl.h
14+
ggml-cuda.h
15+
ggml-impl.h
16+
ggml-kompute.h
17+
ggml-metal.h
18+
ggml-opencl.h
19+
ggml-quants.c
20+
ggml-quants.h
21+
ggml-sycl.h
22+
ggml-vulkan.h
723
whisper.cpp
824
whisper.h
925
dr_wav.h
26+
whisper.bundle
27+
whisper.so
28+
whisper.dll

bindings/ruby/ext/extconf.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
11
require 'mkmf'
2-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','whisper.cpp')} .")
3-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','whisper.h')} .")
4-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml.h')} .")
5-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml.c')} .")
6-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-impl.h')} .")
7-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-aarch64.h')} .")
8-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-aarch64.c')} .")
9-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-alloc.h')} .")
10-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-alloc.c')} .")
11-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend-impl.h')} .")
12-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.h')} .")
13-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.cpp')} .")
14-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-common.h')} .")
15-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.h')} .")
16-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.c')} .")
17-
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','examples','dr_wav.h')} .")
18-
192

203
# need to use c++ compiler flags
214
$CXXFLAGS << ' -std=c++11'

bindings/ruby/ext/ggml-backend-impl.h

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)