Skip to content

Commit

Permalink
Add Config#use_cargo_build: (#337)
Browse files Browse the repository at this point in the history
* Add Config#use_cargo_build:

This switches behaviour to use `cargo build` instead of `cargo rustc`,
which has the effect of sharing cache better with `cargo build`, but
requires setting rb_sys environment in the default build.

* fmt
  • Loading branch information
burke authored Feb 27, 2024
1 parent 8608122 commit 4210f1b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ jobs:
ruby: ["2.7", "3.2", "3.3"]
exclude:
- os: windows-latest
ruby: '3.3' # remove this once 3.3 binaries are available for windows
ruby: "3.3" # remove this once 3.3 binaries are available for windows
repo:
name: "matsadler/magnus"
slug: magnus-head
ref: "05359fcd2369f014570f1e417c7c2d462625af5e"
run: cargo test
- os: windows-latest
ruby: '3.3' # remove this once 3.3 binaries are available for windows
ruby: "3.3" # remove this once 3.3 binaries are available for windows
repo:
name: "matsadler/magnus"
slug: magnus-0.5
Expand Down
7 changes: 3 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ make contribution easier. Here are the steps to use it:

## Running benchmarks

To run the benchmarks, make sure your dev environment, then run `cargo bench`.
This will run the `criterion` benchmarks and print the results to the console.
To run the benchmarks, make sure your dev environment, then run `cargo bench`. This will run the `criterion` benchmarks
and print the results to the console.

To see see plots of the results, you can open
`target/criterion/report/index.html` in your browser.
To see see plots of the results, you can open `target/criterion/report/index.html` in your browser.
16 changes: 11 additions & 5 deletions gem/lib/rb_sys/cargo_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CargoBuilder < Gem::Ext::Builder
WELL_KNOWN_WRAPPERS = %w[sccache cachepot].freeze

attr_accessor :spec, :runner, :env, :features, :target, :extra_rustc_args, :dry_run, :ext_dir, :extra_rustflags,
:extra_cargo_args
:extra_cargo_args, :config
attr_writer :profile

def initialize(spec)
Expand Down Expand Up @@ -66,17 +66,23 @@ def manifest_dir

def cargo_command(dest_path, args = [])
cmd = []
cmd += ["cargo", "rustc"]
cmd += if config.use_cargo_build
["cargo", "build"]
else
["cargo", "rustc"]
end
cmd += ["--target", target] if target
cmd += ["--target-dir", dest_path]
cmd += ["--features", features.join(",")] unless features.empty?
cmd += ["--lib"]
cmd += ["--profile", profile.to_s]
cmd += Gem::Command.build_args
cmd += args
cmd += ["--"]
cmd += [*rustc_args(dest_path)]
cmd += extra_rustc_args
if !config.use_cargo_build
cmd += ["--"]
cmd += [*rustc_args(dest_path)]
cmd += extra_rustc_args
end
cmd
end

Expand Down
13 changes: 8 additions & 5 deletions gem/lib/rb_sys/mkmf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
module RbSys
# Helper class for creating Rust Makefiles
module Mkmf
# @api private
GLOBAL_RUSTFLAGS = ["--cfg=rb_sys_gem"]

# Helper for building Rust extensions by creating a Ruby compatible makefile
# for Rust. By using this class, your rust extension will be 100% compatible
# with the rake-compiler gem, which allows for easy cross compilation.
Expand Down Expand Up @@ -53,7 +50,9 @@ def create_rust_makefile(target, &blk)
RbConfig.expand(srcdir = srcprefix.dup)

full_cargo_command = cargo_command(srcdir, builder)
global_rustflags = GLOBAL_RUSTFLAGS.dup

global_rustflags = []
global_rustflags << "--cfg=rb_sys_gem" unless builder.use_cargo_build
global_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback" if builder.use_stable_api_compiled_fallback?

make_install = +<<~MAKE
Expand Down Expand Up @@ -169,7 +168,11 @@ def cargo_command(cargo_dir, builder)
cargo_cmd = builder.cargo_command(dest_path, args)
cmd = Shellwords.join(cargo_cmd)
cmd.gsub!("\\=", "=")
cmd.gsub!(/\Acargo rustc/, "$(CARGO) rustc $(RB_SYS_EXTRA_CARGO_ARGS) --manifest-path $(RB_SYS_CARGO_MANIFEST_DIR)/Cargo.toml")
if builder.use_cargo_build
cmd.gsub!(/\Acargo rustc/, "$(CARGO) build $(RB_SYS_EXTRA_CARGO_ARGS) --manifest-path $(RB_SYS_CARGO_MANIFEST_DIR)/Cargo.toml")
else
cmd.gsub!(/\Acargo rustc/, "$(CARGO) rustc $(RB_SYS_EXTRA_CARGO_ARGS) --manifest-path $(RB_SYS_CARGO_MANIFEST_DIR)/Cargo.toml")
end
cmd.gsub!(/-v=\d/, "")
cmd
end
Expand Down
5 changes: 5 additions & 0 deletions gem/lib/rb_sys/mkmf/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ class Config
# Use compiled C code fallback for stable API for ruby-head (default: false)
attr_accessor :use_stable_api_compiled_fallback

# Instead of the default `cargo rustc` behaviour, just call `cargo build`.
# Requires manually setting relevant rb-sys environment (default: false)
attr_accessor :use_cargo_build

def initialize(builder)
@builder = builder
@builder.config = self
@force_install_rust_toolchain = false
@auto_install_rust_toolchain = true
@use_stable_api_compiled_fallback = false
Expand Down
19 changes: 10 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ it's not easy, it's a bug.

## New to `rb-sys`?

- [Ruby on Rust Book πŸ“–](https://oxidize-rb.github.io/rb-sys/) to describe how to build, test, and deploy a Rusty Ruby Gem
- [Ruby on Rust Book πŸ“–](https://oxidize-rb.github.io/rb-sys/) to describe how to build, test, and deploy a Rusty Ruby
Gem
- [Contributing Docs πŸ’»](./CONTRIBUTING.md) to get started in making your first contributions to rb-sys
- [`rb-sys` gem πŸ’Ž](./gem/README.md) to learn more about the `rb-sys` gem for **compiling extensions**

Expand Down Expand Up @@ -58,14 +59,14 @@ directory for automation purposes):

| Platform | Supported | Docker Image |
| ----------------- | --------- | ---------------------------------------------- |
| x86_64-linux | βœ… | [`rbsys/x86_64-linux:0.9.89`][docker-hub] |
| x86_64-linux-musl | βœ… | [`rbsys/x86_64-linux-musl:0.9.89`][docker-hub] |
| aarch64-linux | βœ… | [`rbsys/aarch64-linux:0.9.89`][docker-hub] |
| arm-linux | βœ… | [`rbsys/arm-linux:0.9.89`][docker-hub] |
| arm64-darwin | βœ… | [`rbsys/arm64-darwin:0.9.89`][docker-hub] |
| x64-mingw32 | βœ… | [`rbsys/x64-mingw32:0.9.89`][docker-hub] |
| x64-mingw-ucrt | βœ… | [`rbsys/x64-mingw-ucrt:0.9.89`][docker-hub] |
| mswin | βœ… | not available on Docker |
| x86_64-linux | βœ… | [`rbsys/x86_64-linux:0.9.89`][docker-hub] |
| x86_64-linux-musl | βœ… | [`rbsys/x86_64-linux-musl:0.9.89`][docker-hub] |
| aarch64-linux | βœ… | [`rbsys/aarch64-linux:0.9.89`][docker-hub] |
| arm-linux | βœ… | [`rbsys/arm-linux:0.9.89`][docker-hub] |
| arm64-darwin | βœ… | [`rbsys/arm64-darwin:0.9.89`][docker-hub] |
| x64-mingw32 | βœ… | [`rbsys/x64-mingw32:0.9.89`][docker-hub] |
| x64-mingw-ucrt | βœ… | [`rbsys/x64-mingw-ucrt:0.9.89`][docker-hub] |
| mswin | βœ… | not available on Docker |

## Getting Help

Expand Down

0 comments on commit 4210f1b

Please sign in to comment.