Use cmake's -C option instead of multiple -D options

This commit is contained in:
Kitaiti Makoto 2026-04-15 14:08:45 +09:00
parent ea34b17997
commit 9ca31b6981
3 changed files with 26 additions and 9 deletions

View File

@ -36,8 +36,7 @@ class Dependencies
end
def generate_dot
args = ["-S", "sources", "-B", "build", "--graphviz", dot_path, "-D", "BUILD_SHARED_LIBS=OFF"]
args << @options.to_s unless @options.to_s.empty?
args = ["-S", "sources", "-B", "build", "--graphviz", dot_path, "-D", "BUILD_SHARED_LIBS=OFF", "-C", @options.cache_path]
system @cmake, *args, exception: true
end

View File

@ -3,7 +3,7 @@ require_relative "options"
require_relative "dependencies"
cmake = find_executable("cmake") || abort
options = Options.new(cmake).to_s
options = Options.new(cmake)
have_library("gomp") rescue nil
libs = Dependencies.new(cmake, options).to_s
@ -17,7 +17,7 @@ create_makefile "whisper" do |conf|
$(TARGET_SO): #{libs}
#{libs}: cmake-targets
cmake-targets:
#{"\t"}#{cmake} -S sources -B build -D BUILD_SHARED_LIBS=OFF -D CMAKE_ARCHIVE_OUTPUT_DIRECTORY=#{__dir__} -D CMAKE_POSITION_INDEPENDENT_CODE=ON #{options}
#{"\t"}#{cmake} -S sources -B build -D BUILD_SHARED_LIBS=OFF -D CMAKE_ARCHIVE_OUTPUT_DIRECTORY=#{__dir__} -D CMAKE_POSITION_INDEPENDENT_CODE=ON -C #{options.cache_path}
#{"\t"}#{cmake} --build build --config Release --target common whisper
EOF
end

View File

@ -1,16 +1,16 @@
require "fileutils"
class Options
def initialize(cmake="cmake")
@cmake = cmake
@options = {}
configure
write_cache_file
end
def to_s
@options
.reject {|name, (type, value)| value.nil?}
.collect {|name, (type, value)| "-D #{name}=#{value == true ? "ON" : value == false ? "OFF" : value.shellescape}"}
.join(" ")
def cache_path
File.join(__dir__, "source", "Options.cmake")
end
def cmake_options
@ -82,4 +82,22 @@ class Options
op[1]
end
end
def write_cache_file
FileUtils.mkpath File.dirname(cache_path)
File.open cache_path, "w" do |file|
@options.reject {|name, (type, value)| value.nil?}.each do |name, (type, value)|
line = "set(CACHE{%<name>s} TYPE %<type>s FORCE VALUE %<value>s)" % {
name:,
type:,
value: value == true ? "ON" : value == false ? "OFF" : escape_cmake(value)
}
file.puts line
end
end
end
def escape_cmake(str)
str.gsub(/([\\"])/, '\\\\\1')
end
end