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
18
19
# 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("RB_SYS_CARGO_BUILD_PROFILE", :release).to_sym
  @env = {}
  @features = []
  @target = ENV["CARGO_BUILD_TARGET"] || ENV["RUST_TARGET"]
  @extra_rustc_args = []
  @dry_run = true
  @ext_dir = nil
  @extra_rustflags = []
end

Instance Attribute Details

#dry_runObject

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

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#ext_dirObject

Returns the value of attribute ext_dir.



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

def ext_dir
  @ext_dir
end

#extra_rustc_argsObject

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

#extra_rustflagsObject

Returns the value of attribute extra_rustflags.



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

def extra_rustflags
  @extra_rustflags
end

#featuresObject

Returns the value of attribute features.



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

def features
  @features
end

#profileObject

Returns the value of attribute profile.



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

def profile
  @profile
end

#runnerObject

Returns the value of attribute runner.



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

def runner
  @runner
end

#specObject

Returns the value of attribute spec.



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

def spec
  @spec
end

#targetObject

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



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

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



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

def build_crate(dest_path, results, args, cargo_dir)
  env = build_env
  cmd = cargo_command(dest_path, args)
  runner.call cmd, results, "cargo", cargo_dir, env

  results
end

#build_envObject



40
41
42
43
44
# File 'lib/rb_sys/cargo_builder.rb', line 40

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(dest_path, args = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rb_sys/cargo_builder.rb', line 46

def cargo_command(dest_path, args = [])
  manifest = File.join(ext_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



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

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

#so_extObject

We have to basically reimplement RbConfig::CONFIG here to support Ruby < 2.5



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rb_sys/cargo_builder.rb', line 78

def so_ext
  return RbConfig::CONFIG["SOEXT"] if RbConfig::CONFIG.key?("SOEXT")

  if win_target?
    "dll"
  elsif darwin_target?
    "dylib"
  else
    "so"
  end
end