-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2d2930f
Showing
9 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Metrics/LineLength: | ||
Max: 100 | ||
AllowURI: true | ||
URISchemes: | ||
- http | ||
- https | ||
|
||
Metrics/MethodLength: | ||
Max: 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Mark Lee and contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Thermite | ||
|
||
Thermite is a Rake-based helper for building and distributing Rust-based Ruby extensions. | ||
|
||
## Usage | ||
|
||
1. Add `thermite` as a runtime dependency in your gem. | ||
2. In your gemspec, add `Rakefile` to specification's `extensions` list. | ||
3. In `Rakefile`, add `require 'thermite/tasks'` and then add the tasks to the file by running: | ||
|
||
```ruby | ||
Thermite::Tasks.new | ||
``` | ||
|
||
### Example | ||
|
||
Using the cliché Rust+Ruby example, the [`rusty_blank`](https://github.com/malept/rusty_blank) | ||
repository contains an example of using Thermite with [ruru](https://github.com/d-unseductable/ruru) | ||
to provide a `String.blank?` speedup extension. | ||
|
||
## FAQ | ||
|
||
### Why is it named Thermite? | ||
|
||
According to Wikipedia: | ||
|
||
* The chemical formula for ruby includes Al<sub>2</sub>O<sub>3</sub>, or aluminum oxide. | ||
* Rust is iron oxide, or Fe<sub>2</sub>O<sub>3</sub>. | ||
* A common thermite reaction uses iron oxide and aluminum to produce iron and aluminum oxide: | ||
Fe<sub>2</sub>O<sub>3</sub> + 2Al → 2Fe + Al<sub>2</sub>O<sub>3</sub> | ||
|
||
## Legal | ||
|
||
This gem is licensed under the MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- encoding: utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
require 'mkmf' | ||
|
||
module Thermite | ||
# | ||
# Cargo helpers | ||
# | ||
module Cargo | ||
def cargo | ||
@cargo ||= find_executable(ENV.fetch('CARGO', 'cargo')) | ||
end | ||
|
||
def run_cargo(*args) | ||
sh "#{cargo} #{args.join(' ')}" | ||
end | ||
|
||
def run_cargo_if_exists(*args) | ||
run_cargo(*args) if cargo | ||
end | ||
|
||
def cargo_required_msg | ||
<<EOM | ||
**** | ||
Rust's Cargo is required to build this extension. Please install Rust and put | ||
it in the PATH, or set the CARGO environment variable appropriately. | ||
**** | ||
EOM | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'rbconfig' | ||
require 'tomlrb' | ||
|
||
module Thermite | ||
# | ||
# Configuration helpers | ||
# | ||
module Config | ||
def shared_ext | ||
@shared_ext ||= RbConfig::CONFIG['DLEXT'] == 'bundle' ? 'dylib' : RbConfig::CONFIG['DLEXT'] | ||
end | ||
|
||
def target_os | ||
@target_os ||= RbConfig::CONFIG['target_os'] | ||
end | ||
|
||
def target_arch | ||
@target_arch ||= RbConfig::CONFIG['target_cpu'] | ||
end | ||
|
||
def library_name | ||
@library_name ||= begin | ||
if toml[:lib] && toml[:lib][:name] | ||
toml[:lib][:name] | ||
else | ||
toml[:package][:name] | ||
end | ||
end | ||
end | ||
|
||
def shared_library | ||
"lib#{library_name}.#{shared_ext}" | ||
end | ||
|
||
def tarball_filename(version) | ||
"#{library_name}-#{version}-#{target_os}-#{target_arch}.tar.gz" | ||
end | ||
|
||
def toml | ||
@toml ||= begin | ||
project_path = options.fetch(:cargo_project_path, FileUtils.pwd) | ||
toml_path = File.join(project_path, 'Cargo.toml') | ||
Tomlrb.load_file(toml_path, symbolize_keys: true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- encoding: utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
require 'rexml/document' | ||
require 'rubygems/package' | ||
require 'uri' | ||
require 'zlib' | ||
|
||
module Thermite | ||
# | ||
# GitHub releases binary helpers. | ||
# | ||
module GithubReleaseBinary | ||
def http_get(uri) | ||
Net::HTTP.get(URI(uri)) | ||
end | ||
|
||
def download_latest_binary_from_github_release | ||
return false unless options[:github_username] | ||
username = options[:github_username] | ||
project = options.fetch(:github_repo, library_name) | ||
installed_binary = false | ||
github_uri = "https://github.com/#{username}/#{project}" | ||
each_github_release(github_uri) do |version, download_uri| | ||
tgz = download_binary_from_github_release(download_uri, version) | ||
next unless tgz | ||
unpack_tarball(tgz) | ||
installed_binary = true | ||
break | ||
end | ||
|
||
installed_binary | ||
end | ||
|
||
def each_github_release(github_uri) | ||
releases_uri = "#{github_uri}/releases.atom" | ||
feed = REXML::Document.new(http_get(releases_uri)) | ||
REXML::XPath.each(feed, '//entry/title[contains(.,"-rust")]/text()') do |tag| | ||
version = tag.to_s.slice(1..-6) | ||
download_uri = "#{github_uri}/releases/download/#{tag}/#{tarball_filename(version)}" | ||
|
||
yield(version, download_uri) | ||
end | ||
end | ||
|
||
def download_binary_from_github_release(uri, version) | ||
case (response = Net::HTTP.get_response(URI(uri))) | ||
when Net::HTTPClientError | ||
nil | ||
when Net::HTTPServerError | ||
fail response | ||
else | ||
puts "Downloading latest compiled version (#{version}) from GitHub" | ||
StringIO.new(http_get(response['location'])) | ||
end | ||
end | ||
|
||
def unpack_tarball(tgz) | ||
gz = Zlib::GzipReader.new(tgz) | ||
tar = Gem::Package::TarReader.new(gz) | ||
tar.each do |entry| | ||
path = entry.header.name | ||
next if path.end_with?('/') | ||
File.open(path, 'wb') do |f| | ||
f.write(entry.read) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Thermite | ||
# | ||
# Helpers to package the Rust library | ||
# | ||
module Package | ||
def build_package | ||
dir = FPM::Package::Dir.new | ||
dir.input("lib/#{shared_library}") | ||
tarball = dir.convert(FPM::Package::Tar) | ||
tarball.name = library_name | ||
tarball.output(tarball_filename(toml[:package][:version])) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- encoding: utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
require 'fileutils' | ||
# Defined here instead of in thermite/package because otherwise a weird FFI exception occurs | ||
require 'fpm' | ||
require 'rake/tasklib' | ||
require 'thermite/cargo' | ||
require 'thermite/config' | ||
require 'thermite/github_release_binary' | ||
require 'thermite/package' | ||
|
||
# | ||
# Helpers for Rust-based Ruby extensions. | ||
# | ||
module Thermite | ||
# | ||
# Create the following rake tasks: | ||
# | ||
# * `thermite:build` | ||
# * `thermite:clean` | ||
# * `thermite:test` | ||
# * `thermite:tarball` | ||
# | ||
class Tasks < Rake::TaskLib | ||
include Thermite::Cargo | ||
include Thermite::Config | ||
include Thermite::GithubReleaseBinary | ||
include Thermite::Package | ||
|
||
attr_reader :options | ||
|
||
def initialize(options = {}) | ||
@options = options | ||
define_build_task | ||
define_clean_task | ||
define_test_task | ||
define_package_task | ||
end | ||
|
||
def define_build_task | ||
desc 'Build or download the Rust shared library: TARGET controls Cargo target' | ||
task 'thermite:build' do | ||
# if cargo found, build. Otherwise, grab binary. | ||
if cargo | ||
target = ENV.fetch('TARGET', 'release') | ||
cargo_args = %w(build) | ||
cargo_args << '--release' if target == 'release' | ||
run_cargo cargo_args | ||
FileUtils.cp "target/#{target}/#{shared_library}", 'lib/' | ||
elsif !download_latest_binary_from_github_release | ||
fail cargo_required_msg | ||
end | ||
end | ||
end | ||
|
||
def define_clean_task | ||
desc 'Clean up after thermite:build task' | ||
task 'thermite:clean' do | ||
FileUtils.rm "lib/#{shared_library}", force: true | ||
run_cargo_if_exists 'clean' | ||
end | ||
end | ||
|
||
def define_test_task | ||
desc 'Run Rust testsuite' | ||
task 'thermite:test' do | ||
run_cargo_if_exists 'test' | ||
end | ||
end | ||
|
||
def define_package_task | ||
namespace :thermite do | ||
desc 'Package rust library in a tarball' | ||
task tarball: %w(thermite:build) do | ||
build_package | ||
end | ||
end | ||
end | ||
end | ||
end |