Skip to content

Add rb-sys-cross crate for Docker-free cross-compilation via Zig#711

Open
ianks wants to merge 2 commits intomainfrom
rb-sys-cross
Open

Add rb-sys-cross crate for Docker-free cross-compilation via Zig#711
ianks wants to merge 2 commits intomainfrom
rb-sys-cross

Conversation

@ianks
Copy link
Collaborator

@ianks ianks commented Feb 21, 2026

Situation

Cross-compiling Ruby native extensions today means Docker via rake-compiler-dock — slow to set up, painful to debug, and a heavy dependency for something that's fundamentally a C compiler problem. Zig ships a hermetic cross-compiler for every major platform, making Docker unnecessary for this workflow.

Execution

New rb-sys-cross CLI crate that runs the full cross-compilation pipeline using cargo-zigbuild:

  1. Downloads and caches Ruby headers + rbconfig.json per platform/version (built from source with zig cc, or pulled from GitHub releases)
  2. Sets RBCONFIG_* env vars so rb-sys-build skips shelling out to Ruby entirely
  3. Runs cargo zigbuild --target <target> --release --lib
  4. Packs a native .gem with versioned .so files, a version-aware extension loader, and a gemspec derived from Cargo.toml

Supports 8 platforms (aarch64-linux, x86_64-linux, arm-linux, x86-linux, {aarch64,x86_64}-linux-musl, x64-mingw-ucrt, x64-mingw32). No Ruby install required — gem name, version, authors, and license are all pulled from cargo metadata.

```bash
rb-sys-cross build
--platform aarch64-linux --platform x86_64-linux
--ruby-version 3.3 --ruby-version 3.4

→ pkg/my_gem-1.0.0-aarch64-linux.gem, pkg/my_gem-1.0.0-x86_64-linux.gem

```

Introduces a new CLI tool that cross-compiles Ruby native extensions
using cargo-zigbuild and Zig as the C compiler, eliminating the need
for Docker containers during cross-compilation.

Key design decisions:
- Typed cargo metadata structs instead of raw serde_json::Value
- RbConfig newtype for type-safe rbconfig.json handling
- Profile enum replacing string-based profile matching
- CrossCompileEnv struct for typed cross-compilation environment
- GemPackOptions struct to avoid 7-parameter function signatures
- OnceLock for regex compilation in rbconfig.rb parsing
- Utility module for shared helpers (gz_compress, hex_sha256, etc.)
- Associated functions on Platform for find/all/list operations
- clap Args derive on BuildOpts to eliminate CLI field duplication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant