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
This commit is contained in:
KITAITI Makoto 2025-06-23 13:34:05 +09:00 committed by GitHub
parent e6c10cf3d5
commit cead8f5357
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 49 additions and 13 deletions

View File

@ -6,3 +6,4 @@ ext/ggml/
ext/include/ ext/include/
ext/scripts/ ext/scripts/
ext/src/ ext/src/
test/fixtures/

View File

@ -69,6 +69,21 @@ CLEAN.include LIB_FILE
Rake::TestTask.new Rake::TestTask.new
TEST_FIXTURE_AUDIO = "test/fixtures/jfk.wav"
TEST_FIXTURE_AUDIO_SRC = File.expand_path(File.join(__dir__, "..", "..", "samples", "jfk.wav"))
TEST_FIXTURE_AUDIO_DIR = TEST_FIXTURE_AUDIO.pathmap("%d")
directory TEST_FIXTURE_AUDIO_DIR
if File.exist? TEST_FIXTURE_AUDIO_SRC
file TEST_FIXTURE_AUDIO => [TEST_FIXTURE_AUDIO_SRC, TEST_FIXTURE_AUDIO_DIR] do |t|
symlink t.source, t.name
end
else
require "open-uri"
file TEST_FIXTURE_AUDIO => TEST_FIXTURE_AUDIO_DIR do |t|
File.write t.name, URI("https://github.com/ggml-org/whisper.cpp/raw/refs/heads/master/samples/jfk.wav").read
end
end
TEST_MEMORY_VIEW = "test/jfk_reader/jfk_reader.#{RbConfig::CONFIG['DLEXT']}" TEST_MEMORY_VIEW = "test/jfk_reader/jfk_reader.#{RbConfig::CONFIG['DLEXT']}"
file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t| file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
chdir "test/jfk_reader" do chdir "test/jfk_reader" do
@ -76,6 +91,6 @@ file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
sh "make" sh "make"
end end
end end
CLEAN.include "test/jfk_reader/jfk_reader.{o,#{RbConfig::CONFIG['DLEXT']}}" CLEAN.include TEST_MEMORY_VIEW
task test: [LIB_FILE, TEST_MEMORY_VIEW] task test: [LIB_FILE, TEST_MEMORY_VIEW, TEST_FIXTURE_AUDIO]

View File

@ -43,19 +43,40 @@ class Options
@options[name] = [type, value] @options[name] = [type, value]
end end
configure_accelerate
configure_metal
configure_coreml configure_coreml
end end
def configure_coreml # See ggml/src/ggml-cpu/CMakeLists.txt
use_coreml = if @options["WHISPER_COREML"][1].nil? def configure_accelerate
cmake_options["WHISPER_COREML"][1] if RUBY_PLATFORM.match?(/darwin/) && enabled?("GGML_ACCELERATE")
else $LDFLAGS << " -framework Accelerate"
@options["WHISPER_COREML"][1] end
end
# See ggml/src/ggml-metal/CMakeLists.txt
def configure_metal
$LDFLAGS << " -framework Foundation -framework Metal -framework MetalKit" if enabled?("GGML_METAL")
end
# See src/CmakeLists.txt
def configure_coreml
if enabled?("WHISPER_COREML")
$LDFLAGS << " -framework Foundation -framework CoreML"
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML"
end end
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML" if use_coreml
end end
def option_name(name) def option_name(name)
name.downcase.gsub("_", "-") name.downcase.gsub("_", "-")
end end
def enabled?(option)
if @options[option][1].nil?
cmake_options[option][1]
else
@options[option][1]
end
end
end end

View File

@ -132,13 +132,13 @@ module Whisper
class ZipURI < URI class ZipURI < URI
def cache def cache
zip_path = Pathname(super) zip_path = super
dest = unzipped_path dest = unzipped_path
return if dest.exist? && dest.mtime >= zip_path.mtime return if dest.exist? && dest.mtime >= zip_path.mtime
escaping dest do escaping dest do
system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true
end end
zip_path.to_path zip_path
end end
def clear_cache def clear_cache

View File

@ -412,7 +412,7 @@ module Whisper
end end
class ZipURI < URI class ZipURI < URI
def cache: () -> String def cache: () -> Pathname
def clear_cache: () -> void def clear_cache: () -> void
end end
end end

View File

@ -3,7 +3,7 @@ require "whisper"
require_relative "jfk_reader/jfk_reader" require_relative "jfk_reader/jfk_reader"
class TestBase < Test::Unit::TestCase class TestBase < Test::Unit::TestCase
AUDIO = File.join(__dir__, "..", "..", "..", "samples", "jfk.wav") AUDIO = File.join(__dir__, "fixtures", "jfk.wav")
class << self class << self
def whisper def whisper

View File

@ -4,7 +4,6 @@ Gem::Specification.new do |s|
s.name = "whispercpp" s.name = "whispercpp"
s.authors = ["Georgi Gerganov", "Todd A. Fisher"] s.authors = ["Georgi Gerganov", "Todd A. Fisher"]
s.version = '1.3.3' s.version = '1.3.3'
s.date = '2025-06-10'
s.description = %q{High-performance inference of OpenAI's Whisper automatic speech recognition (ASR) model via Ruby} s.description = %q{High-performance inference of OpenAI's Whisper automatic speech recognition (ASR) model via Ruby}
s.email = 'todd.fisher@gmail.com' s.email = 'todd.fisher@gmail.com'
s.extra_rdoc_files = ['LICENSE', 'README.md'] s.extra_rdoc_files = ['LICENSE', 'README.md']