Skip to content

Commit ff0f69d

Browse files
committed
Extract dependency check logic as Dependencies class
1 parent 2614b7c commit ff0f69d

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

bindings/ruby/ext/dependencies.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require "tsort"
2+
3+
class Dependencies
4+
def initialize(cmake, options)
5+
@cmake = cmake
6+
@options = options
7+
8+
generate_dot
9+
@libs = parse_dot
10+
end
11+
12+
def to_s
13+
@libs.join(" ")
14+
end
15+
16+
private
17+
18+
def dot_path
19+
File.join(__dir__, "build", "whisper.cpp.dot")
20+
end
21+
22+
def generate_dot
23+
system @cmake, "-S", "sources", "-B", "build", "--graphviz", dot_path, "-D", "BUILD_SHARED_LIBS=OFF", @options.to_s, exception: true
24+
end
25+
26+
def parse_dot
27+
static_lib_shape = nil
28+
nodes = {}
29+
depends = Hash.new {|h, k| h[k] = []}
30+
31+
class << depends
32+
include TSort
33+
alias tsort_each_node each_key
34+
def tsort_each_child(node, &block)
35+
fetch(node, []).each(&block)
36+
end
37+
end
38+
39+
File.open(dot_path).each_line do |line|
40+
case line
41+
when /\[\s*label\s*=\s*"Static Library"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]/
42+
static_lib_shape = $~[:shape]
43+
when /\A\s*"(?<node>\w+)"\s*\[\s*label\s*=\s*"(?<label>\S+)"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]\s*;\s*\z/
44+
node = $~[:node]
45+
label = $~[:label]
46+
shape = $~[:shape]
47+
nodes[node] = [label, shape]
48+
when /\A\s*"(?<depender>\w+)"\s*->\s*"(?<dependee>\w+)"/
49+
depender = $~[:depender]
50+
dependee = $~[:dependee]
51+
depends[depender] ||= []
52+
depends[depender] << dependee
53+
end
54+
end
55+
depends.tsort.filter_map {|node|
56+
label, shape = nodes[node]
57+
shape == static_lib_shape ? label : nil
58+
}.collect {|lib| "lib#{lib}.a"}
59+
.reverse
60+
end
61+
end

bindings/ruby/ext/extconf.rb

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
11
require "mkmf"
2-
require "tsort"
32
require_relative "options"
3+
require_relative "dependencies"
44

55
cmake = find_executable("cmake") || abort
66
options = Options.new
77
have_library("gomp") rescue nil
8-
9-
prefix = File.join("build", "whisper.cpp.dot")
10-
system cmake, "-S", "sources", "-B", "build", "--graphviz", prefix, "-D", "BUILD_SHARED_LIBS=OFF", options.to_s, exception: true
11-
12-
static_lib_shape = nil
13-
nodes = {}
14-
depends = {}
15-
class << depends
16-
include TSort
17-
alias tsort_each_node each_key
18-
def tsort_each_child(node, &block)
19-
fetch(node, []).each(&block)
20-
end
21-
end
22-
File.open(File.join("build", "whisper.cpp.dot")).each_line do |line|
23-
case line
24-
when /\[\s*label\s*=\s*"Static Library"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]/
25-
static_lib_shape = $~[:shape]
26-
when /\A\s*"(?<node>\w+)"\s*\[\s*label\s*=\s*"(?<label>\S+)"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]\s*;\s*\z/
27-
node = $~[:node]
28-
label = $~[:label]
29-
shape = $~[:shape]
30-
nodes[node] = [label, shape]
31-
when /\A\s*"(?<depender>\w+)"\s*->\s*"(?<dependee>\w+)"/
32-
depender = $~[:depender]
33-
dependee = $~[:dependee]
34-
depends[depender] ||= []
35-
depends[depender] << dependee
36-
end
37-
end
38-
libs = depends.tsort.filter_map {|node|
39-
label, shape = nodes[node]
40-
shape == static_lib_shape ? label : nil
41-
}.collect {|lib| "lib#{lib}.a"}
42-
.reverse
43-
.join(" ")
8+
libs = Dependencies.new(cmake, options)
449

4510
$INCFLAGS << " -Isources/include -Isources/ggml/include -Isources/examples"
4611
$LOCAL_LIBS << " #{libs}"

0 commit comments

Comments
 (0)