Class: Kar::CargoTask::CargoBuilder

Inherits:
Gem::Ext::CargoBuilder
  • Object
show all
Defined in:
lib/kar/cargotask.rb

Instance Method Summary collapse

Instance Method Details

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

Original Gem::Ext::CargoBuilder removes compiled files after installation. It is ideal for installation, but not for development. We override the behavior to keep the compiled files.

Raises:

  • (DylibNotFoundError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kar/cargotask.rb', line 119

def build(extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd,
          target_rbconfig=Gem.target_rbconfig)
  require "tempfile"
  require "fileutils"

  if target_rbconfig.path
    warn "--target-rbconfig is not yet supported for Rust extensions. Ignoring"
  end

  # Where's the Cargo.toml of the crate we're building
  cargo_toml = File.join(cargo_dir, "Cargo.toml")
  # What's the crate's name
  crate_name = cargo_crate_name(cargo_dir, cargo_toml, results)

  # Run the build
  cmd = cargo_command(cargo_toml, cargo_dir, args, crate_name)
  runner.call(cmd, results, "cargo", cargo_dir, build_env)

  # Where do we expect Cargo to write the compiled library
  dylib_path = cargo_dylib_path(cargo_dir, crate_name)

  # Helpful error if we didn't find the compiled library
  raise DylibNotFoundError, cargo_dir unless File.exist?(dylib_path)

  # Cargo and Ruby differ on how the library should be named, rename from
  # what Cargo outputs to what Ruby expects
  dlext_name = "#{crate_name}.#{makefile_config("DLEXT")}"
  dlext_path = File.join(File.dirname(dylib_path), dlext_name)
  FileUtils.cp(dylib_path, dlext_path)

  nesting = extension_nesting(extension)

  if Gem.install_extension_in_lib && lib_dir
    nested_lib_dir = File.join(lib_dir, nesting)
    FileUtils.mkdir_p nested_lib_dir
    FileUtils.cp_r dlext_path, nested_lib_dir, remove_destination: true
  end

  # move to final destination
  nested_dest_path = File.join(dest_path, nesting)
  FileUtils.mkdir_p nested_dest_path
  FileUtils.cp_r dlext_path, nested_dest_path, remove_destination: true

  results
end