|
1 | 1 | require "mkmf" |
| 2 | +require "tsort" |
2 | 3 |
|
3 | 4 | # TODO: options such as CoreML |
4 | 5 |
|
|
8 | 9 |
|
9 | 10 | prefix = File.join("build", "whisper.cpp.dot") |
10 | 11 | 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 | | -} |
14 | 12 |
|
| 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" |
15 | 48 | $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}" |
18 | 51 |
|
19 | 52 | create_makefile "whisper" do |conf| |
20 | 53 | conf << <<~EOF |
21 | | - $(TARGET_SO): #{libs.join(" ")} |
22 | | - #{libs.join(" ")}: cmake-targets |
| 54 | + $(TARGET_SO): #{libs} |
| 55 | + #{libs}: cmake-targets |
23 | 56 | cmake-targets: |
24 | 57 | #{"\t"}#{cmake} -S sources -B build -D BUILD_SHARED_LIBS=OFF -D CMAKE_ARCHIVE_OUTPUT_DIRECTORY=#{__dir__} -D CMAKE_POSITION_INDEPENDENT_CODE=ON |
25 | 58 | #{"\t"}#{cmake} --build build --config Release --target common whisper |
| 59 | + #{"\t"} |
26 | 60 | EOF |
27 | 61 | end |
0 commit comments