Module: Rubycc::MkmfShim

Defined in:
lib/rubycc/mkmf_shim.rb

Constant Summary collapse

LIB_DIR =

The repository's lib/ directory (this file lives at lib/rubycc/mkmf_shim.rb) and the gcc-compatible executables that stand in for the external toolchain.

File.expand_path("..", __dir__)
RUBYCC_EXE =
File.expand_path("../../exe/rubycc", __dir__)
PKGCONF_EXE =
File.expand_path("../../exe/rubycc-pkgconf", __dir__)

Class Method Summary collapse

Class Method Details

.install!Object

Rewrites the toolchain keys in both RbConfig hashes and prepends the repository lib/ to RUBYLIB. Idempotent: the RUBYLIB entry is added only once, and re-assigning the same command strings is harmless.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubycc/mkmf_shim.rb', line 51

def self.install!
  configs = [RbConfig::CONFIG]
  configs << RbConfig::MAKEFILE_CONFIG if defined?(RbConfig::MAKEFILE_CONFIG)

  replacements.each do |key, value|
    configs.each do |config|
      # PKG_CONFIG is only meaningful if the host build already knew a
      # pkg-config; leave an absent key absent so mkmf's own "no pkg-config"
      # path is preserved, but override a present one (even the empty string
      # RbConfig ships when none was found) to point at rubycc-pkgconf.
      next if key == "PKG_CONFIG" && !config.key?(key)

      config[key] = value
    end
  end

  prepend_rubylib!
end

.prepend_rubylib!Object

Puts the repository lib/ at the front of RUBYLIB so a rubycc executable spawned by mkmf can require "rubycc" from a source checkout. Skips the work when the directory is already the leading entry.



73
74
75
76
77
78
79
# File 'lib/rubycc/mkmf_shim.rb', line 73

def self.prepend_rubylib!
  current = ENV["RUBYLIB"]
  entries = current.to_s.split(File::PATH_SEPARATOR)
  return if entries.first == LIB_DIR

  ENV["RUBYLIB"] = [LIB_DIR, *current&.split(File::PATH_SEPARATOR)].join(File::PATH_SEPARATOR)
end

.replacementsObject

The RbConfig keys rewritten to point at rubycc. Each maps to the command string mkmf embeds verbatim when it expands $(CC) and friends: a single shell-metacharacter-free executable path (plus, for the compound tools, the driver's own mode flag) so mkmf's system(env, *command) execs it directly.



39
40
41
42
43
44
45
46
# File 'lib/rubycc/mkmf_shim.rb', line 39

def self.replacements
  {
    "CC"        => RUBYCC_EXE,
    "LDSHARED"  => "#{RUBYCC_EXE} -shared",
    "CPP"       => "#{RUBYCC_EXE} -E",
    "PKG_CONFIG" => PKGCONF_EXE
  }
end