Class: RbSys::CargoBuilder Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Defined Under Namespace

Classes: DylibNotFoundError, LinkFlagConverter

Constant Summary collapse

WELL_KNOWN_WRAPPERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[sccache cachepot].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ CargoBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CargoBuilder.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rb_sys/cargo_builder.rb', line 15

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 = []
  @extra_cargo_args = []
  @dry_run = true
  @ext_dir = ""
  @extra_rustflags = []
end

Instance Attribute Details

#configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def config
  @config
end

#dry_runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def dry_run
  @dry_run
end

#envObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def env
  @env
end

#ext_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def ext_dir
  @ext_dir
end

#extra_cargo_argsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def extra_cargo_args
  @extra_cargo_args
end

#extra_rustc_argsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def extra_rustc_args
  @extra_rustc_args
end

#extra_rustflagsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def extra_rustflags
  @extra_rustflags
end

#featuresObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def features
  @features
end

#profileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def profile
  return :release if rubygems_invoked?

  @profile
end

#runnerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def runner
  @runner
end

#specObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def spec
  @spec
end

#targetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/rb_sys/cargo_builder.rb', line 11

def target
  @target
end

Instance Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
# File 'lib/rb_sys/cargo_builder.rb', line 49

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
60
61
# File 'lib/rb_sys/cargo_builder.rb', line 57

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rb_sys/cargo_builder.rb', line 67

def cargo_command(dest_path, args = [])
  cmd = []
  cmd += if config.use_cargo_build
    ["cargo", "build"]
  else
    ["cargo", "rustc"]
  end
  cmd += ["--target", target] if target
  cmd += ["--target-dir", dest_path]
  cmd += ["--features", features.join(",")] unless features.empty?
  cmd += ["--lib"]
  cmd += ["--profile", profile.to_s]
  cmd += Gem::Command.build_args
  cmd += args
  if !config.use_cargo_build
    cmd += ["--"]
    cmd += [*rustc_args(dest_path)]
    cmd += extra_rustc_args
  end
  cmd
end

#cargo_dylib_path(dest_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#manifest_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
# File 'lib/rb_sys/cargo_builder.rb', line 63

def manifest_dir
  ext_dir
end

#so_extObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rb_sys/cargo_builder.rb', line 101

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

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