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

Overview

A class to build a Ruby gem Cargo. Extracted from ‘rubygems` gem, with some modifications.

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rb_sys/cargo_builder.rb', line 7

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_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.



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

def dry_run
  @dry_run
end

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#ext_dirObject

Returns the value of attribute ext_dir.



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

def ext_dir
  @ext_dir
end

#extra_rustc_argsObject

Returns the value of attribute extra_rustc_args.



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

def extra_rustc_args
  @extra_rustc_args
end

#extra_rustflagsObject

Returns the value of attribute extra_rustflags.



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

def extra_rustflags
  @extra_rustflags
end

#featuresObject

Returns the value of attribute features.



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

def features
  @features
end

#profileObject



23
24
25
26
27
# File 'lib/rb_sys/cargo_builder.rb', line 23

def profile
  return :release if rubygems_invoked?

  @profile
end

#runnerObject

Returns the value of attribute runner.



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

def runner
  @runner
end

#specObject

Returns the value of attribute spec.



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

def spec
  @spec
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

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



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

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



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

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



48
49
50
51
52
# File 'lib/rb_sys/cargo_builder.rb', line 48

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rb_sys/cargo_builder.rb', line 54

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



74
75
76
77
78
79
80
# File 'lib/rb_sys/cargo_builder.rb', line 74

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



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rb_sys/cargo_builder.rb', line 86

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

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