Thermite is a Rake-based helper for building and distributing Rust-based Ruby extensions.
- Provides wrappers for
cargocommands. - Handles non-standard
cargoinstallations via theCARGOenvironment variable. - Opt-in to allow users to install pre-compiled Rust extensions hosted on GitHub releases.
- Opt-in to allow users to install pre-compiled Rust extensions hosted on a third party server.
- Provides a wrapper for initializing a Rust extension via Fiddle.
- Add the following to your gemspec file:
spec.extensions << 'ext/Rakefile'
spec.add_runtime_dependency 'thermite', '~> 0'- Create
ext/Rakefilewith the following code, assuming that the Cargo project root is the same as the Ruby project root:
require 'thermite/tasks'
project_dir = File.dirname(File.dirname(__FILE__))
Thermite::Tasks.new(cargo_project_path: project_dir, ruby_project_path: project_dir)
task default: %w(thermite:build)- In
Rakefile, integrate Thermite into your build-test workflow:
require 'thermite/tasks'
Thermite::Tasks.new
desc 'Run Rust & Ruby testsuites'
task test: ['thermite:build', 'thermite:test'] do
# …
endRun rake -T thermite to view all of the available tasks in the thermite namespace.
Task configuration for your project can be set in two ways:
- passing arguments to
Thermite::Tasks.new - adding a
package.metadata.thermitesection toCargo.toml. These settings override the arguments passed to theTasksclass. Due to the conflict, it is infeasible forcargo_project_pathorcargo_workspace_memberto be set in this way. Example section:
[package.metadata.thermite]
github_releases = truePossible options:
binary_uri_format- if set, the interpolation-formatted string used to construct the download URI for the pre-built native extension. If the environment variableTHERMITE_BINARY_URI_FORMATis set, it takes precedence over this option. Either method of setting this option overrides thegithub_releasesoption. Example:https://example.com/download/%{version}/%{filename}. Replacement variables:filename- The value ofConfig.tarball_filenameversion- the crate version fromCargo.toml
cargo_project_path- the path to the top-level Cargo project. Defaults to the current working directory.cargo_workspace_member- if set, the relative path to the Cargo workspace member. Usually used when it is part of a repository containing multiple crates.github_releases- whether to look for Rust binaries via GitHub releases when installing the gem, andcargois not found. Defaults tofalse.github_release_type- whengithub_releasesistrue, the mode to use to download the Rust binary from GitHub releases.'cargo'(the default) uses the version inCargo.toml, along with thegit_tag_formatoption (described below) to determine the download URI.'latest'takes the latest release matching thegit_tag_regexoption (described below) to determine the download URI.git_tag_format- whengithub_release_typeis'cargo'(the default), the format string used to determine the tag used in the GitHub download URI. Defaults tov%s, where%sis the version inCargo.toml.git_tag_regex- whengithub_releasesis enabled andgithub_release_typeis'latest', a regular expression (expressed as aString) that determines which tagged releases to look for precompiled Rust tarballs. One group must be specified that indicates the version number to be used in the tarball filename. Defaults to the semantic versioning 2.0.0 format. In this case, the group is around the entire expression.optional_rust_extension- prints a warning to STDERR instead of raising an exception, if Cargo is unavailable andgithub_releasesis either disabled or unavailable. Useful for projects where either fallback code exists, or a native extension is desirable but not required. Defaults tofalse.ruby_project_path- the top-level directory of the Ruby gem's project. Defaults to the current working directory.ruby_extension_dir- the directory relative toruby_project_pathwhere the extension is located. Defaults tolib.
Using the cliché Rust+Ruby example, the rusty_blank
repository contains an example of using Thermite with ruru
to provide a String.blank? speedup extension. While the example uses ruru, this gem should be
usable with any method of integrating Rust and Ruby that you choose.
By default Thermite will do a release build of your Rust code. To do a debug build instead,
set the CARGO_PROFILE environment variable to debug.
For example, you can run CARGO_PROFILE=debug rake thermite:build.
Debug statements can be written to a file specified by the THERMITE_DEBUG_FILENAME environment
variable.
According to Wikipedia:
- The chemical formula for ruby includes Al2O3, or aluminum oxide.
- Rust is iron oxide, or Fe2O3.
- A common thermite reaction uses iron oxide and aluminum to produce iron and aluminum oxide: Fe2O3 + 2Al → 2Fe + Al2O3
This gem is licensed under the MIT license.