class Gem::Ext::CargoBuilder

This class is used by rubygems to build Rust extensions. It is a thin-wrapper over the ‘cargo rustc` command which takes care of building Rust code in a way that Ruby can use.

Attributes

profile[RW]
runner[RW]
spec[RW]

Public Class Methods

new(spec) click to toggle source
# File rubygems/ext/cargo_builder.rb, line 9
def initialize(spec)
  require_relative "../command"
  require_relative "cargo_builder/link_flag_converter"

  @spec = spec
  @runner = self.class.method(:run)
  @profile = :release
end

Public Instance Methods

build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd) click to toggle source
# File rubygems/ext/cargo_builder.rb, line 18
def build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd)
  require "fileutils"
  require "shellwords"

  build_crate(dest_path, results, args, cargo_dir)
  validate_cargo_build!(dest_path)
  rename_cdylib_for_ruby_compatibility(dest_path)
  finalize_directory(dest_path, lib_dir, cargo_dir)
  results
end
build_crate(dest_path, results, args, cargo_dir) click to toggle source
# File rubygems/ext/cargo_builder.rb, line 29
def build_crate(dest_path, results, args, cargo_dir)
  env = build_env
  cmd = cargo_command(cargo_dir, dest_path, args)
  runner.call cmd, results, "cargo", cargo_dir, env

  results
end
build_env() click to toggle source
# File rubygems/ext/cargo_builder.rb, line 37
def build_env
  build_env = rb_config_env
  build_env["RUBY_STATIC"] = "true" if ruby_static? && ENV.key?("RUBY_STATIC")
  build_env["RUSTFLAGS"] = "#{ENV["RUSTFLAGS"]} --cfg=rb_sys_gem".strip
  build_env
end
cargo_command(cargo_dir, dest_path, args = []) click to toggle source
# File rubygems/ext/cargo_builder.rb, line 44
def cargo_command(cargo_dir, dest_path, args = [])
  manifest = File.join(cargo_dir, "Cargo.toml")
  cargo = ENV.fetch("CARGO", "cargo")

  cmd = []
  cmd += [cargo, "rustc"]
  cmd += ["--target", ENV["CARGO_BUILD_TARGET"]] if ENV["CARGO_BUILD_TARGET"]
  cmd += ["--target-dir", dest_path]
  cmd += ["--manifest-path", manifest]
  cmd += ["--lib"]
  cmd += ["--profile", profile.to_s]
  cmd += ["--locked"] if profile == :release
  cmd += Gem::Command.build_args
  cmd += args
  cmd += ["--"]
  cmd += [*cargo_rustc_args(dest_path)]
  cmd
end