Skip to content

Commit cead8f5

Browse files
ruby : specify Apple frameworks explicitly on build (#3270)
* Add Apple frameworks to $LDFLAGS when needed * Add utility method to Options * Remove unnecessary propaty date from gemspec * Add Apple frameworks for CoreML build * Add Accelerate framework only for Apple platform * Fix ZipURI#cache signature * Download test fixtures if needed
1 parent e6c10cf commit cead8f5

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

bindings/ruby/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ ext/ggml/
66
ext/include/
77
ext/scripts/
88
ext/src/
9+
test/fixtures/

bindings/ruby/Rakefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,28 @@ CLEAN.include LIB_FILE
6969

7070
Rake::TestTask.new
7171

72+
TEST_FIXTURE_AUDIO = "test/fixtures/jfk.wav"
73+
TEST_FIXTURE_AUDIO_SRC = File.expand_path(File.join(__dir__, "..", "..", "samples", "jfk.wav"))
74+
TEST_FIXTURE_AUDIO_DIR = TEST_FIXTURE_AUDIO.pathmap("%d")
75+
directory TEST_FIXTURE_AUDIO_DIR
76+
if File.exist? TEST_FIXTURE_AUDIO_SRC
77+
file TEST_FIXTURE_AUDIO => [TEST_FIXTURE_AUDIO_SRC, TEST_FIXTURE_AUDIO_DIR] do |t|
78+
symlink t.source, t.name
79+
end
80+
else
81+
require "open-uri"
82+
file TEST_FIXTURE_AUDIO => TEST_FIXTURE_AUDIO_DIR do |t|
83+
File.write t.name, URI("https://github.com/ggml-org/whisper.cpp/raw/refs/heads/master/samples/jfk.wav").read
84+
end
85+
end
86+
7287
TEST_MEMORY_VIEW = "test/jfk_reader/jfk_reader.#{RbConfig::CONFIG['DLEXT']}"
7388
file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
7489
chdir "test/jfk_reader" do
7590
ruby "extconf.rb"
7691
sh "make"
7792
end
7893
end
79-
CLEAN.include "test/jfk_reader/jfk_reader.{o,#{RbConfig::CONFIG['DLEXT']}}"
94+
CLEAN.include TEST_MEMORY_VIEW
8095

81-
task test: [LIB_FILE, TEST_MEMORY_VIEW]
96+
task test: [LIB_FILE, TEST_MEMORY_VIEW, TEST_FIXTURE_AUDIO]

bindings/ruby/ext/options.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,40 @@ def configure
4343
@options[name] = [type, value]
4444
end
4545

46+
configure_accelerate
47+
configure_metal
4648
configure_coreml
4749
end
4850

51+
# See ggml/src/ggml-cpu/CMakeLists.txt
52+
def configure_accelerate
53+
if RUBY_PLATFORM.match?(/darwin/) && enabled?("GGML_ACCELERATE")
54+
$LDFLAGS << " -framework Accelerate"
55+
end
56+
end
57+
58+
# See ggml/src/ggml-metal/CMakeLists.txt
59+
def configure_metal
60+
$LDFLAGS << " -framework Foundation -framework Metal -framework MetalKit" if enabled?("GGML_METAL")
61+
end
62+
63+
# See src/CmakeLists.txt
4964
def configure_coreml
50-
use_coreml = if @options["WHISPER_COREML"][1].nil?
51-
cmake_options["WHISPER_COREML"][1]
52-
else
53-
@options["WHISPER_COREML"][1]
54-
end
55-
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML" if use_coreml
65+
if enabled?("WHISPER_COREML")
66+
$LDFLAGS << " -framework Foundation -framework CoreML"
67+
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML"
68+
end
5669
end
5770

5871
def option_name(name)
5972
name.downcase.gsub("_", "-")
6073
end
74+
75+
def enabled?(option)
76+
if @options[option][1].nil?
77+
cmake_options[option][1]
78+
else
79+
@options[option][1]
80+
end
81+
end
6182
end

bindings/ruby/lib/whisper/model/uri.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ def format_bytesize(bytesize)
132132

133133
class ZipURI < URI
134134
def cache
135-
zip_path = Pathname(super)
135+
zip_path = super
136136
dest = unzipped_path
137137
return if dest.exist? && dest.mtime >= zip_path.mtime
138138
escaping dest do
139139
system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true
140140
end
141-
zip_path.to_path
141+
zip_path
142142
end
143143

144144
def clear_cache

bindings/ruby/sig/whisper.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ module Whisper
412412
end
413413

414414
class ZipURI < URI
415-
def cache: () -> String
415+
def cache: () -> Pathname
416416
def clear_cache: () -> void
417417
end
418418
end

bindings/ruby/test/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_relative "jfk_reader/jfk_reader"
44

55
class TestBase < Test::Unit::TestCase
6-
AUDIO = File.join(__dir__, "..", "..", "..", "samples", "jfk.wav")
6+
AUDIO = File.join(__dir__, "fixtures", "jfk.wav")
77

88
class << self
99
def whisper

bindings/ruby/whispercpp.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Gem::Specification.new do |s|
44
s.name = "whispercpp"
55
s.authors = ["Georgi Gerganov", "Todd A. Fisher"]
66
s.version = '1.3.3'
7-
s.date = '2025-06-10'
87
s.description = %q{High-performance inference of OpenAI's Whisper automatic speech recognition (ASR) model via Ruby}
98
s.email = '[email protected]'
109
s.extra_rdoc_files = ['LICENSE', 'README.md']

0 commit comments

Comments
 (0)