Class: Gem::Ext::CargoBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/rubygems/ext/cargo_builder.rb,
lib/rubygems/ext/cargo_builder/link_flag_converter.rb

Overview

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.

Defined Under Namespace

Classes: DylibNotFoundError, LinkFlagConverter

Instance Attribute Summary collapse

Attributes inherited from Builder

#build_args

Instance Method Summary collapse

Methods inherited from Builder

#build_error, #build_extension, #build_extensions, #builder_for, class_name, make, run, #write_gem_make_out

Methods included from UserInteraction

#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction, #verbose

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Constructor Details

#initialize(spec) ⇒ CargoBuilder

Returns a new instance of CargoBuilder.



9
10
11
12
13
14
15
16
# File 'lib/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

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/rubygems/ext/cargo_builder.rb', line 7

def profile
  @profile
end

#runnerObject

Returns the value of attribute runner.



7
8
9
# File 'lib/rubygems/ext/cargo_builder.rb', line 7

def runner
  @runner
end

#specObject

Returns the value of attribute spec.



7
8
9
# File 'lib/rubygems/ext/cargo_builder.rb', line 7

def spec
  @spec
end

Instance Method Details

#build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/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) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/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_envObject



37
38
39
40
41
# File 'lib/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
end

#cargo_command(cargo_dir, dest_path, args = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubygems/ext/cargo_builder.rb', line 43

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