]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
ruby : specify Apple frameworks explicitly on build (#3270)
authorKITAITI Makoto <redacted>
Mon, 23 Jun 2025 04:34:05 +0000 (13:34 +0900)
committerGitHub <redacted>
Mon, 23 Jun 2025 04:34:05 +0000 (06:34 +0200)
* 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

bindings/ruby/.gitignore
bindings/ruby/Rakefile
bindings/ruby/ext/options.rb
bindings/ruby/lib/whisper/model/uri.rb
bindings/ruby/sig/whisper.rbs
bindings/ruby/test/helper.rb
bindings/ruby/whispercpp.gemspec

index 4bafb2220d3e3ed2396efe87d614a36ec6dcdc07..54e3a2ac18408c55f28a491e02f38887c9d1fae8 100644 (file)
@@ -6,3 +6,4 @@ ext/ggml/
 ext/include/
 ext/scripts/
 ext/src/
+test/fixtures/
index 08a2312a551cc48e5ab2869a32693845facfd347..d9a66030de4377d4f6aec1da000bbaeea524906e 100644 (file)
@@ -69,6 +69,21 @@ CLEAN.include LIB_FILE
 
 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']}"
 file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
   chdir "test/jfk_reader" do
@@ -76,6 +91,6 @@ file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
     sh "make"
   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]
index 03648fbf846c0e1232b5085107d42348e08b81fc..30cda0f80188fa80faaf5001173e93673fb5d37c 100644 (file)
@@ -43,19 +43,40 @@ class Options
       @options[name] = [type, value]
     end
 
+    configure_accelerate
+    configure_metal
     configure_coreml
   end
 
+  # See ggml/src/ggml-cpu/CMakeLists.txt
+  def configure_accelerate
+    if RUBY_PLATFORM.match?(/darwin/) && enabled?("GGML_ACCELERATE")
+      $LDFLAGS << " -framework Accelerate"
+    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
-    use_coreml = if @options["WHISPER_COREML"][1].nil?
-                   cmake_options["WHISPER_COREML"][1]
-                 else
-                   @options["WHISPER_COREML"][1]
-                 end
-    $CPPFLAGS << " -DRUBY_WHISPER_USE_COREML" if use_coreml
+    if enabled?("WHISPER_COREML")
+      $LDFLAGS << " -framework Foundation -framework CoreML"
+      $CPPFLAGS << " -DRUBY_WHISPER_USE_COREML"
+    end
   end
 
   def option_name(name)
     name.downcase.gsub("_", "-")
   end
+
+  def enabled?(option)
+    if @options[option][1].nil?
+      cmake_options[option][1]
+    else
+      @options[option][1]
+    end
+  end
 end
index 31b608ac5fb1698f2062f542399607329f310a3d..5bf5a098624f15a0ecb1dbe091b0c61a79315471 100644 (file)
@@ -132,13 +132,13 @@ module Whisper
 
     class ZipURI < URI
       def cache
-        zip_path = Pathname(super)
+        zip_path = super
         dest = unzipped_path
         return if dest.exist? && dest.mtime >= zip_path.mtime
         escaping dest do
           system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true
         end
-        zip_path.to_path
+        zip_path
       end
 
       def clear_cache
index c73e6ad6c74dc20cd916594b103a5bbfbf9e095d..e6c0e139492f9177989877cb6d258d99e493d5c1 100644 (file)
@@ -412,7 +412,7 @@ module Whisper
     end
 
     class ZipURI < URI
-      def cache: () -> String
+      def cache: () -> Pathname
       def clear_cache: () -> void
     end
   end
index 389e15c9c155b92dd65062378a5a6d8e3751c6c6..56cd3849fddcfd6bb14cabf31a2107afde10c41e 100644 (file)
@@ -3,7 +3,7 @@ require "whisper"
 require_relative "jfk_reader/jfk_reader"
 
 class TestBase < Test::Unit::TestCase
-  AUDIO = File.join(__dir__, "..", "..", "..", "samples", "jfk.wav")
+  AUDIO = File.join(__dir__, "fixtures", "jfk.wav")
 
   class << self
     def whisper
index 0a2a0c5fdabf7a6fae52a4dce28d955289b4080d..c6e88dff7bd276d8ea25580fbd3d2ab4b9e56246 100644 (file)
@@ -4,7 +4,6 @@ Gem::Specification.new do |s|
   s.name    = "whispercpp"
   s.authors = ["Georgi Gerganov", "Todd A. Fisher"]
   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.email   = 'todd.fisher@gmail.com'
   s.extra_rdoc_files = ['LICENSE', 'README.md']