Class: RbSys::CargoBuilder

Inherits:
Gem::Ext::Builder
  • Object
show all
Defined in:
lib/rb_sys/cargo_builder.rb,
lib/rb_sys/cargo_builder/link_flag_converter.rb

Defined Under Namespace

Classes: DylibNotFoundError, LinkFlagConverter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ CargoBuilder

Returns a new instance of CargoBuilder.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rb_sys/cargo_builder.rb', line 5

def initialize(spec)
  require "rubygems/command"
  require_relative "cargo_builder/link_flag_converter"

  @spec = spec
  @runner = self.class.method(:run)
  @profile = ENV.fetch("CARGO_BUILD_PROFILE", :release).to_sym
  @env = {}
  @features = []
  @target = ENV["CARGO_BUILD_TARGET"]
  @extra_rustc_args = []
  @dry_run = true
end

Instance Attribute Details

#dry_run ⇒ Object

Returns the value of attribute dry_run.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def dry_run
  @dry_run
end

#env ⇒ Object

Returns the value of attribute env.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def env
  @env
end

#extra_rustc_args ⇒ Object

Returns the value of attribute extra_rustc_args.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def extra_rustc_args
  @extra_rustc_args
end

#features ⇒ Object

Returns the value of attribute features.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def features
  @features
end

#profile ⇒ Object

Returns the value of attribute profile.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def profile
  @profile
end

#runner ⇒ Object

Returns the value of attribute runner.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def runner
  @runner
end

#spec ⇒ Object

Returns the value of attribute spec.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def spec
  @spec
end

#target ⇒ Object

Returns the value of attribute target.



3
4
5
# File 'lib/rb_sys/cargo_builder.rb', line 3

def target
  @target
end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/rb_sys/cargo_builder.rb', line 19

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



30
31
32
33
34
35
36
# File 'lib/rb_sys/cargo_builder.rb', line 30

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 ⇒ Object



38
39
40
41
42
# File 'lib/rb_sys/cargo_builder.rb', line 38

def build_env
  build_env = rb_config_env
  build_env["RUBY_STATIC"] = "true" if ruby_static? && ENV.key?("RUBY_STATIC")
  build_env.merge(env)
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rb_sys/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", target] if target
  cmd += ["--target-dir", dest_path]
  cmd += ["--features", features.join(",")] unless features.empty?
  cmd += ["--manifest-path", manifest]
  cmd += ["--lib"]
  cmd += ["--profile", profile.to_s]
  cmd += Gem::Command.build_args
  cmd += args
  cmd += ["--"]
  cmd += [*cargo_rustc_args(dest_path)]
  cmd += extra_rustc_args
  cmd
end

#cargo_dylib_path(dest_path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/rb_sys/cargo_builder.rb', line 64

def cargo_dylib_path(dest_path)
  prefix = so_ext == "dll" ? "" : "lib"
  path_parts = [dest_path]
  path_parts << target if target
  path_parts += [profile_target_directory, "#{prefix}#{cargo_crate_name}.#{so_ext}"]
  File.join(*path_parts)
end