Examples of C extensions in Ruby gems

Overview

Ruby C Extensions, Explained

Background

Hello there! Welcome to Ruby C Extensions, Explained.

This repository, and the example ruby gems in it, were originally written as companion materials for some conference talks being given in 2021 by Mike Dalessio (@flavorjones).

RubyKaigi 2021 talk 2021-09-11

This talk focuses mostly on the mechanics of the gems in this repository.

RubyConf 2021 talk 2021-11-08

This talk may cover slightly different material, including an approach to rigorous testing, how to think about Trust in the context of precompilation, and what industrializing precompilation might look like.

How To Use This Repository

Each of the gems in this repository demonstrate a different strategy for integrating with (or packaging) third-party libraries. A brief explanation of the strategies is below, and the code for each gem is in a distinct subdirectory, along with a README that contains a more complete explanation.

Each gem builds on the previous one if you tackle them in this order:

These are actual, working gems! And they've all been pushed to rubygems.org:

  • gem install rcee_isolated
  • gem install rcee_system
  • gem install rcee_packaged_source
  • gem install rcee_packaged_tarball
  • gem install rcee_precompiled

Or they can all be installed as dependencies of the meta-gem rcee:

  • gem install rcee

Also note that these gems are tested. In particular, the precompiled native gems have a rather complex setup to build, install, and test across platforms using rake-compiler-dock and Github Actions. See the Actions running at https://github.com/flavorjones/ruby-c-extensions-explained/actions

Strategies

A few commonly-followed approaches have emerged in the Ruby ecosystem. Broadly categorized, the strategies are:

Strategy 0, "isolated"

A self-contained C extension (no third-party libraries).

See subdirectory /isolated.

Real-world examples:

Strategy 1, "system"

Find and use an third-party library already installed on the target system.

See subdirectory /system.

Real-world examples:

Strategy 2a, "packaged_source"

Package the source code for the third-party library, and compile it into the C extension during installation.

See subdirectory /packaged_source.

Real-world examples:

Strategy 2b, "packaged_tarball"

Package the tarball distribution for the third-party library, use its configuration toolchain and build a library that the C extension can link against.

See subdirectory /packaged_tarball.

Real-world examples:

Strategy 3, "precompiled"

Precompile the library and C extension ahead-of-time for a subset of platforms; fall back to packaged_tarball strategy for other platforms.

See subdirectory /precompiled

Real-world examples:

Strategy Combinations

These strategies can also be combined!

"system", fallback to "packaged"

Real-world examples:

"precompiled", fall back to "packaged", leave option for "system"

Real-world examples:

FAQ

I'll try to answer questions asked during conference Q&A here.

How do you test cross-compiling gems on Github Actions?

Nokogiri uses the rake-compiler-dock OCI images directly to build the gem; and then uses the upload-artifact/download-artifact actions to install that gem on the target system.

See https://github.com/sparklemotion/nokogiri/blob/main/.github/workflows/gem-install.yml for the concrete implementation!

What's the significance of the flowers you're using as a background image for your RubyKaigi slides?

I tweeted a brief teaser video in which I explained the joke!

When I edited my RubyKaigi talk about C extensions, some bits had to be cut for time. Here's a fun teaser that I put together from some of those bits. [video link] pic.twitter.com/wZjSepHNpq

— mike dalessio (@flavorjones) September 9, 2021

In short, my friend Mark once sent me flowers for my birthday along with a card that read:

Fetching nokogiri-1.8.0.gem (100%)
Building native extensions. This could take a while...

Mark is funny. This was the inspiration for the talk's title.

Issues
  • Document trilogy's variation on the packaged_source strategy

    Document trilogy's variation on the packaged_source strategy

    Trilogy concatenates all the packaged source into a single C file, see https://github.com/github/trilogy/blob/main/contrib/ruby/ext/trilogy-ruby/extconf.rb#L5

    which is a notable variation from what we do with libyaml in https://github.com/flavorjones/ruby-c-extensions-explained/tree/main/packaged_source

    opened by flavorjones 0
Releases(v0.4.0)
  • v0.4.0(May 19, 2022)

    0.4.0 / 2022-05-19

    Added

    • precompiled gem now supports Ruby 3.1 and drops support for Ruby 2.5
    • precompiled gem now supports the aarch64-linux platform.
    • precompiled gem now supports the arm-linux platform.
    • precompiled gem now supports the x86-linux platform.
    • precompiled gem now supports the x64-mingw-ucrt platform for Ruby 3.1
    Source code(tar.gz)
    Source code(zip)
  • v0.3.0(Nov 8, 2021)

    0.3.0 / 2021-11-08

    Fixed

    • precompiled gem now builds correctly when compiling form source on Windows

    Added

    • Full test coverage for all gems can be inspected at https://github.com/flavorjones/ruby-c-extensions-explained/actions and in .github/workflows/.
    Source code(tar.gz)
    Source code(zip)
Owner
Mike Dalessio
(looks around anxiously)
Mike Dalessio
Breakout detection for Ruby

Breakout ?? BreakoutDetection for Ruby Learn how it works Installation Add this line to your application’s Gemfile: gem 'breakout-detection' Getting S

Andrew Kane 31 Feb 4, 2022
Seasonal-trend decomposition for Ruby

STL Ruby Seasonal-trend decomposition for Ruby Installation Add this line to your application’s Gemfile: gem 'stl-rb' Getting Started Decompose a time

Andrew Kane 15 Mar 14, 2022
List of Persian Colors and hex colors for CSS, SCSS, PHP, JS, Python, and Ruby.

Persian Colors (Iranian colors) List of Persian Colors and hex colors for CSS, SCSS, PHP, C++, QML, JS, Python, Ruby and CSharp. Persian colors Name H

Max Base 12 Feb 22, 2022
Access USB devices from Ruby via libusb-1.x

Access USB devices from Ruby LIBUSB is a Ruby binding that gives Ruby programmers access to arbitrary USB devices. libusb is a library that gives full

Lars Kanis 148 May 28, 2022
First-up chord-send and tap-and-hold chord repeat extensions to QMK.

Quantum Mechanical Keyboard Firmware This is a keyboard firmware based on the tmk_keyboard firmware with some useful features for Atmel AVR and ARM co

Joshua Grams 6 May 28, 2022
C Extensions i made for DragonRuby!

drext C Extensions i made for DragonRuby NOTE: DragonRuby Pro required for C Extensions. List Name Description Platforms drbat Battery information lib

Rabia Alhaffar 7 May 19, 2022
Some extensions for windows explorer, tested on windows 10+

WindowsExplorerExtension Extensions for windows explorer, tested on windows 10 & windows 11. New Folder Extension What's This A Gnome nautilus inspire

anpho 4 Jan 13, 2022
C++17 & C++ 20 error-handling and utility extensions.

C++ 17 & C++ 20 error-handling and utility extensions. Overview STX is a collection of libraries and utilities designed to make working with C++ easie

Basit Ayantunde 426 Jun 25, 2022
An extension manager for browsing and installing GNOME Shell Extensions.

Extension Manager A native tool for browsing, installing, and managing GNOME Shell Extensions. Written with GTK 4 and libadwaita. Features The tool su

Matt Jakeman 363 Jun 27, 2022
Useful UE4 Visual Studio extensions.

UE4 Smarter Macro Indenting This extension was designed to fix the unnecessary and annoying "smart" indenting that Visual Studio likes to do around va

Chris Pawlukowsky 241 Jun 14, 2022
ESP32 S2 USB host with examples.

This new USB host library based is based on changed and hopefully somehow final API from espressif. The idea is to make this library usable with esp-i

null 17 Jun 17, 2022
Educational material and examples for those interested in learning the C programming language

Learn C Educational material and examples for those interested in learning the C programming language Files: examples.c Various examples of programs w

Michael Kolesidis 14 May 15, 2022
Set of examples how to use CLion with various unsupported compilers using Custom Defined Compiler feature

About This repository contains: Set of examples how to use CLion with various unsupported compilers using Custom Defined Compiler feature Public set o

JetBrains 30 Jun 15, 2022
Examples for the "Introduction to programming" course given by me and @bzareva @ Faculty of Mathematics and Informatics, Sofia University (2021/22)

Теми от практикумите по "Увод в програмирането", зимен семестър 2021/2022, спец. "Информатика", група 5 Тема 1 (04.10.2021) : Променливи. Типове проме

null 6 May 4, 2022
Official examples and tools from the JACK project

JACK example tools This repository holds the official JACK example clients and tools, which have been tracked in the example-clients and tools reposit

JACK Audio Connection Kit 19 Jun 13, 2022
Raspberry Pi Pico SDK Examples

Raspberry Pi Pico SDK Examples Getting started See Getting Started with the Raspberry Pi Pico and the README in the pico-sdk for information on gettin

Raspberry Pi 1.2k Jun 23, 2022
A repo containing examples of embedded systems based on the STM32 micro-controller.

STM32 Embedded Development A repo containing examples of embedded systems based on the STM32 micro-controller. I use a Nucleo-F446RE and the STM32Cube

Vincent Ho 1 Jan 10, 2022
The repository contains some examples of pre-trained SNN (Spiking Neural Network) models.

About the Project The repository contains some examples of pre-trained SNN (Spiking Neural Network) models. The models were trained using the MM-BP tr

ETRI 12 Dec 30, 2021
NCURSES-Programming-HOWTO examples

NCURSES-Programming-HOWTO examples

Tony Narlock 102 Jun 19, 2022