Skip to content

Commit 46bfa14

Browse files
committed
Check dependency of whisper.cpp accurately
1 parent 9294167 commit 46bfa14

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

bindings/ruby/ext/extconf.rb

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "mkmf"
2+
require "tsort"
23

34
# TODO: options such as CoreML
45

@@ -8,20 +9,53 @@
89

910
prefix = File.join("build", "whisper.cpp.dot")
1011
system cmake, "-S", "sources", "-B", "build", "--graphviz", prefix, "-D", "BUILD_SHARED_LIBS=OFF", exception: true
11-
libs = Dir["#{prefix}.*"].collect {|file|
12-
"lib" + file.sub("#{prefix}.", "") + ".a"
13-
}
1412

13+
static_lib_shape = nil
14+
nodes = {}
15+
depends = {}
16+
class << depends
17+
include TSort
18+
alias tsort_each_node each_key
19+
def tsort_each_child(node, &block)
20+
fetch(node, []).each(&block)
21+
end
22+
end
23+
File.open(File.join("build", "whisper.cpp.dot")).each_line do |line|
24+
case line
25+
when /\[\s*label\s*=\s*"Static Library"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]/
26+
static_lib_shape = $~[:shape]
27+
when /\A\s*"(?<node>\w+)"\s*\[\s*label\s*=\s*"(?<label>\S+)"\s*,\s*shape\s*=\s*(?<shape>\w+)\s*\]\s*;\s*\z/
28+
node = $~[:node]
29+
label = $~[:label]
30+
shape = $~[:shape]
31+
nodes[node] = [label, shape]
32+
when /\A\s*"(?<depender>\w+)"\s*->\s*"(?<dependee>\w+)"/
33+
depender = $~[:depender]
34+
dependee = $~[:dependee]
35+
depends[depender] ||= []
36+
depends[depender] << dependee
37+
end
38+
end
39+
libs = depends.tsort.filter_map {|node|
40+
label, shape = nodes[node]
41+
shape == static_lib_shape ? label : nil
42+
}.collect {|lib| "lib#{lib}.a"}
43+
.reverse
44+
.join(" ")
45+
46+
$CFLAGS << " -std=c11 -fPIC"
47+
$CXXFLAGS << " -std=c++17 -O3 -DNDEBUG"
1548
$INCFLAGS << " -Isources/include -Isources/ggml/include -Isources/examples"
16-
$LOCAL_LIBS << " " << libs.join(" ")
17-
$cleanfiles << " build" << libs.join(" ")
49+
$LOCAL_LIBS << " #{libs}"
50+
$cleanfiles << " build #{libs}"
1851

1952
create_makefile "whisper" do |conf|
2053
conf << <<~EOF
21-
$(TARGET_SO): #{libs.join(" ")}
22-
#{libs.join(" ")}: cmake-targets
54+
$(TARGET_SO): #{libs}
55+
#{libs}: cmake-targets
2356
cmake-targets:
2457
#{"\t"}#{cmake} -S sources -B build -D BUILD_SHARED_LIBS=OFF -D CMAKE_ARCHIVE_OUTPUT_DIRECTORY=#{__dir__} -D CMAKE_POSITION_INDEPENDENT_CODE=ON
2558
#{"\t"}#{cmake} --build build --config Release --target common whisper
59+
#{"\t"}
2660
EOF
2761
end

0 commit comments

Comments
 (0)