[Feature #20345] Add `--target-rbconfig` option to mkmf

Introduce a new mkmf option `--target-rbconfig` to specify the RbConfig
file for the deployment target platform. This option is useful for
cross-compiling Ruby extensions without faking the global top-level
`RbConfig` constant.
This commit is contained in:
Yuta Saito 2024-02-20 07:03:11 +00:00
Родитель 457a30df85
Коммит 8b55aaa85c
2 изменённых файлов: 44 добавлений и 0 удалений

Просмотреть файл

@ -44,6 +44,23 @@ end
# correctly compile and link the C extension to Ruby and a third-party # correctly compile and link the C extension to Ruby and a third-party
# library. # library.
module MakeMakefile module MakeMakefile
target_rbconfig = nil
ARGV.delete_if do |arg|
opt = arg.delete_prefix("--target-rbconfig=")
unless opt == arg
target_rbconfig = opt
end
end
if target_rbconfig
# Load the RbConfig for the target platform into this module.
# Cross-compiling needs the same version of Ruby.
Kernel.load target_rbconfig, self
else
# The RbConfig for the target platform where the built extension runs.
RbConfig = ::RbConfig
end
#### defer until this module become global-state free. #### defer until this module become global-state free.
# def self.extended(obj) # def self.extended(obj)
# obj.init_mkmf # obj.init_mkmf

Просмотреть файл

@ -1,5 +1,6 @@
# frozen_string_literal: false # frozen_string_literal: false
require_relative 'base' require_relative 'base'
require 'tempfile'
class TestMkmfConfig < TestMkmf class TestMkmfConfig < TestMkmf
def test_dir_config def test_dir_config
@ -27,4 +28,30 @@ class TestMkmfConfig < TestMkmf
assert_equal(false, with_config("foo")) assert_equal(false, with_config("foo"))
end; end;
end end
def test_with_target_rbconfig
Tempfile.create(%w"rbconfig .rb", ".") do |tmp|
tmp.puts <<~'end;'
module RbConfig
CONFIG = {}
MAKEFILE_CONFIG = {}
def self.fire_update!(key, value); end
def self.expand(val, config = CONFIG); val; end
end;
::RbConfig::CONFIG.each do |k, v|
tmp.puts " CONFIG[#{k.dump}] = #{v.dump}"
end
::RbConfig::MAKEFILE_CONFIG.each do |k, v|
tmp.puts " MAKEFILE_CONFIG[#{k.dump}] = #{v.dump}"
end
tmp.puts " CONFIG['testing-only'] = 'ok'"
tmp.puts "end"
tmp.close
assert_separately([], ["--target-rbconfig=#{tmp.path}"], <<-'end;')
assert_equal("ok", MakeMakefile::RbConfig::CONFIG["testing-only"])
assert_not_equal(::RbConfig, MakeMakefile::RbConfig)
end;
end
end
end end