|
1 | 1 | --- |
2 | 2 | id: rb-sys-gem-config |
3 | | -title: rb_sys Gem Configuration |
| 3 | +title: Gem Configuration |
4 | 4 | --- |
5 | 5 |
|
6 | | -# rb_sys Gem Configuration |
| 6 | +# Gem Configuration |
7 | 7 |
|
8 | | -The `rb_sys` gem makes it easy to build native Ruby extensions in Rust. It interoperates with existing Ruby native |
9 | | -extension toolchains (i.e., `rake-compiler`) to make testing, building, and cross-compilation of gems easy. |
| 8 | +The `rb_sys` Ruby gem provides helpers for building Rust-based extensions, integrating with standard tools like `rake-compiler`. |
10 | 9 |
|
11 | | -## RbSys::ExtensionTask |
| 10 | +## `RbSys::ExtensionTask` |
12 | 11 |
|
13 | | -This gem provides a `RbSys::ExtensionTask` class that can be used to build a Ruby extension in Rust. It's a thin wrapper |
14 | | -around `Rake::ExtensionTask` that provides sane defaults for building Rust extensions. |
| 12 | +Define how your Rust extension is built within your `Rakefile` using `RbSys::ExtensionTask`. |
15 | 13 |
|
16 | 14 | ```ruby |
17 | 15 | # Rakefile |
18 | | - |
19 | 16 | require "rb_sys/extensiontask" |
20 | 17 |
|
21 | 18 | GEMSPEC = Gem::Specification.load("my_gem.gemspec") |
22 | 19 |
|
23 | 20 | RbSys::ExtensionTask.new("my-crate-name", GEMSPEC) do |ext| |
24 | 21 | ext.lib_dir = "lib/my_gem" |
25 | | - |
26 | | - # If you want to use `rb-sys-dock` for cross-compilation: |
27 | | - ext.cross_compile = true |
| 22 | + ext.cross_compile = true # Enable cross-compilation with rb-sys-dock. |
28 | 23 | end |
29 | 24 | ``` |
30 | 25 |
|
31 | | -## create_rust_makefile |
| 26 | +## `create_rust_makefile` |
32 | 27 |
|
33 | | -The gem provides a simple helper to build a Ruby-compatible Makefile for your Rust extension: |
| 28 | +Generate a Ruby-compatible `Makefile` for your Rust extension in your `extconf.rb`. |
34 | 29 |
|
35 | 30 | ```ruby |
36 | | -# ext/rust_reverse/extconf.rb |
37 | | - |
38 | | -# We need to require mkmf *first* since `rake-compiler` injects code here for cross compilation |
| 31 | +# ext/my_gem/extconf.rb |
39 | 32 | require "mkmf" |
40 | 33 | require "rb_sys/mkmf" |
41 | 34 |
|
42 | | -create_rust_makefile("rust_reverse") do |r| |
43 | | - # Create debug builds in dev. Make sure that release gems are compiled with |
44 | | - # `RB_SYS_CARGO_PROFILE=release` (optional) |
45 | | - r.profile = ENV.fetch("RB_SYS_CARGO_PROFILE", :dev).to_sym |
46 | | - |
47 | | - # Can be overridden with `RB_SYS_CARGO_FEATURES` env var (optional) |
48 | | - r.features = ["test-feature"] |
49 | | - |
50 | | - # You can add whatever env vars you want to the env hash (optional) |
51 | | - r.env = {"FOO" => "BAR"} |
52 | | - |
53 | | - # If your Cargo.toml is in a different directory, you can specify it here (optional) |
54 | | - r.ext_dir = "." |
55 | | - |
56 | | - # Extra flags to pass to the $RUSTFLAGS environment variable (optional) |
57 | | - r.extra_rustflags = ["--cfg=some_nested_config_var_for_crate"] |
58 | | - |
59 | | - # Force a rust toolchain to be installed via rustup (optional) |
60 | | - # You can also set the env var `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN=true` |
61 | | - r.force_install_rust_toolchain = "stable" |
62 | | - |
63 | | - # Clean up the target/ dir after `gem install` to reduce bloat (optional) |
64 | | - r.clean_after_install = false # default: true if invoked by rubygems |
65 | | - |
66 | | - # Auto-install Rust toolchain if not present on "gem install" (optional) |
67 | | - r.auto_install_rust_toolchain = false # default: true if invoked by rubygems |
| 35 | +create_rust_makefile("my_gem") do |r| |
| 36 | + r.profile = ENV.fetch("RB_SYS_CARGO_PROFILE", :dev).to_sym # Cargo build profile (:dev or :release). |
| 37 | + r.features = ["a-feature"] # Comma-separated list of Cargo features. |
| 38 | + r.extra_rustflags = ["--cfg=some_config_flag"] # Extra flags for $RUSTFLAGS. |
| 39 | + r.ext_dir = "." # Directory containing Cargo.toml, relative to extconf.rb. |
68 | 40 | end |
69 | 41 | ``` |
70 | 42 |
|
71 | 43 | ## Environment Variables |
72 | 44 |
|
73 | | -The `rb_sys` gem respects several environment variables that can modify its behavior: |
74 | | - |
75 | | -| Environment Variable | Description | |
76 | | -| ------------------------------------- | --------------------------------------------------- | |
77 | | -| `RB_SYS_CARGO_PROFILE` | Set the Cargo profile (i.e., `release` or `dev`) | |
78 | | -| `RB_SYS_CARGO_FEATURES` | Comma-separated list of Cargo features to enable | |
79 | | -| `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN` | Force installation of a Rust toolchain | |
80 | | -| `RUBY_STATIC` | Force static linking of libruby if set to `true` | |
81 | | -| `LIBCLANG_PATH` | Path to libclang if it can't be found automatically | |
82 | | - |
83 | | -## Tips and Tricks |
84 | | - |
85 | | -- When using `rake-compiler` to build your gem, you can use the `RB_SYS_CARGO_PROFILE` environment variable to set the |
86 | | - Cargo profile (i.e., `release` or `dev`). |
87 | | - |
88 | | -- You can pass Cargo arguments to `rake-compiler` like so: `rake compile -- --verbose` |
| 45 | +These environment variables control the `rb_sys` build process. |
89 | 46 |
|
90 | | -- It's possible to force an installation of a Rust toolchain by setting the `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN` |
91 | | - environment variable. This will install [rustup](https://rustup.rs/) and [cargo](https://crates.io/) in the build |
92 | | - directory, so the end user does not have to have Rust pre-installed. Ideally, this should be a last resort, as it's |
93 | | - better to already have the toolchain installed on your system. |
| 47 | +| Environment Variable | Description | |
| 48 | +| :------------------------------------ | :------------------------------------------------------- | |
| 49 | +| `RB_SYS_CARGO_PROFILE` | Set the Cargo profile (e.g., `release` or `dev`). | |
| 50 | +| `RB_SYS_CARGO_FEATURES` | Comma-separated list of Cargo features to enable. | |
| 51 | +| `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN` | Set to `true` to force installation of a Rust toolchain. | |
| 52 | +| `RUBY_STATIC` | Set to `true` to force static linking of `libruby`. | |
| 53 | +| `LIBCLANG_PATH` | Path to `libclang` if it can't be found automatically. | |
| 54 | +| `RB_SYS_VERBOSE_BUILD` | Set to `true` to enable verbose output during the build. | |
94 | 55 |
|
95 | 56 | ## Troubleshooting |
96 | 57 |
|
97 | | -### Libclang Issues |
98 | | - |
99 | | -If you see an error like this: |
100 | | - |
101 | | -``` |
102 | | -thread 'main' panicked at 'Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"' |
103 | | -``` |
| 58 | +### `libclang` Not Found |
104 | 59 |
|
105 | | -This means that bindgen is having issues finding a usable version of libclang. An easy way to fix this is to install the |
106 | | -[`libclang` gem](https://github.com/oxidize-rb/libclang-rb), which will install a pre-built version of libclang for you. |
107 | | -`rb_sys` will automatically detect this gem and use it. |
| 60 | +If `libclang` is not found during `bindgen` execution, add the `libclang` gem to your `Gemfile`. |
108 | 61 |
|
109 | 62 | ```ruby |
110 | 63 | # Gemfile |
|
0 commit comments