Create a native extension of Ruby in Rust

Yesterday, I posted Article on making native extensions of Ruby in Mac environment, and I was able to confirm the settings that work on Windows and Linux.


The basics are the same as last time.

The first thing that changed was, and I was able to get common values ​​for Win, Mac, and Linux from the Ruby config for the library name.

use std::process::Command;

fn rb_config(key: &str) -> String {
    let output = Command::new("ruby")
        .args(&["-e", &format!("print RbConfig::CONFIG['{}']", key)])
        .expect("failed run ruby");

    return String::from_utf8(output.stdout).unwrap();

fn main() {
    println!("cargo:rustc-link-search={}", rb_config("libdir"));
    println!("cargo:rustc-link-lib={}", rb_config("RUBY_SO_NAME"));

Next is Rakefile, which I changed from Makefile to make it easier to run on Windows. The Windows environment supported by Rust is mingw and msvc, but it enforces mingw.


require 'fileutils'
require 'json'

NAME = JSON.parse(`cargo metadata --format-version=1`).dig("packages", 0, "name")
TARGET_SO = "#{NAME}.#{RbConfig::CONFIG["DLEXT"]}"

desc "Delete artifacts"
task :clean do
  sh "cargo clean"

desc "Create native extension"
task :build do
  env = {}
  when /mingw/
    env = {"RUSTUP_TOOLCHAIN" => "stable-#{RbConfig::CONFIG["host_cpu"]}-pc-windows-gnu"}
    cargo_out = "target/release/#{NAME}.dll"
  when /darwin/
    cargo_out = "target/release/lib#{NAME}.dylib"
  when /linux/
    cargo_out = "target/release/lib#{NAME}.so"
    raise "Platform #{RUBY_PLATFORM} is not supported"
  sh env, "cargo build --release"
  cp cargo_out, TARGET_SO, preserve: true, verbose: true

desc "Run Ruby script"
task :run => [:clean, :build] do
  ruby "run.rb"

Others unchanged

from now on

To publish it as a gem, gemspec etc. are required, so I will investigate it in the future.

Recommended Posts

Create a native extension of Ruby in Rust
Created a native extension of Ruby with Rust and published it as a gem
I made a Ruby extension library in C
Count the number of occurrences of a string in Ruby
Ruby / Rust integration (7) Create Rust extension gem built at installation
[Programming complete] §5 Create a review management app in Ruby
Multiplication in a Ruby array
Create a fortune using Ruby
Judgment of fractions in Ruby
Create a MySQL test environment (+ millions of test data) in 5 minutes
Determine that the value is a multiple of 〇 in Ruby
I want to create a Parquet file even in Ruby
Sorting hashes in a Ruby array
Implementation of ls command in Ruby
Create a database in a production environment
Create a new app in Rails
Implement a gRPC client in Ruby
Create a Servlet program in Eclipse
Ruby / Rust linkage (6) Extraction of morphemes
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
I tried to make a parent class of a value object in Ruby
Make a note of Ruby keyword arguments
I tried a calendar problem in Ruby
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
How to create a query using variables in GraphQL [Using Ruby on Rails]
Minimal configuration for creating Ruby native extensions in Rust (for now Mac only)
Summary of hashes and symbols in Ruby
Create a TODO app in Java 7 Create Header
Extract a part of a string with Ruby
I thought about the best way to create a ValueObject in Ruby
[Ruby] Classification and usage of loops in Ruby
Beginners create portfolio in Ruby on Rails
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
Create a Windows desktop application in Ruby and distribute an executable file (.exe)!
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] Behavior of evaluation of conditional expression in while
Create a CSR with extended information in Java
Stuck in an enum in front of a blacksmith
Create a simple batch processing framework in Eclipse.
Measure the size of a folder in Java
Recommendation of Service class in Ruby on Rails
Try to create a bulletin board in Java
Let's create a super-simple web framework in Java
How to create a theme in Liferay 7 / DXP
[Ruby on Rails] A memorandum of layout templates
Create a tool for name identification in Salesforce
A quick review of Java learned in class
Enumerate subsets of arrays given in Ruby (+ α)
(Ruby on Rails6) Creating data in a table
React Native vs. Ionic – A head-to-head Comparison in 2020
Class in Ruby
Basics of Ruby
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Write a test by implementing the story of Mr. Nabeats in the world with ruby
Heavy in Ruby! ??
Docker command to create Rails project with a single blow in environment without Ruby
Do something like a JS immediate function in Ruby