From 4210f1b322a8f29ac0d400a2e9415988c5b4d8c6 Mon Sep 17 00:00:00 2001 From: Burke Libbey Date: Tue, 27 Feb 2024 14:49:01 -0500 Subject: [PATCH] Add Config#use_cargo_build: (#337) * 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 --- .github/workflows/integration.yml | 4 ++-- CONTRIBUTING.md | 7 +++---- gem/lib/rb_sys/cargo_builder.rb | 16 +++++++++++----- gem/lib/rb_sys/mkmf.rb | 13 ++++++++----- gem/lib/rb_sys/mkmf/config.rb | 5 +++++ readme.md | 19 ++++++++++--------- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index c4b372c4..a9d882a1 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8c2cc6f1..b824d471 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/gem/lib/rb_sys/cargo_builder.rb b/gem/lib/rb_sys/cargo_builder.rb index ed1508f3..d150abaa 100644 --- a/gem/lib/rb_sys/cargo_builder.rb +++ b/gem/lib/rb_sys/cargo_builder.rb @@ -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) @@ -66,7 +66,11 @@ 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? @@ -74,9 +78,11 @@ def cargo_command(dest_path, args = []) 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 diff --git a/gem/lib/rb_sys/mkmf.rb b/gem/lib/rb_sys/mkmf.rb index 79b989e4..30b4a3c1 100644 --- a/gem/lib/rb_sys/mkmf.rb +++ b/gem/lib/rb_sys/mkmf.rb @@ -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. @@ -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 @@ -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 diff --git a/gem/lib/rb_sys/mkmf/config.rb b/gem/lib/rb_sys/mkmf/config.rb index 7519fd5f..ce3c48dc 100644 --- a/gem/lib/rb_sys/mkmf/config.rb +++ b/gem/lib/rb_sys/mkmf/config.rb @@ -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 diff --git a/readme.md b/readme.md index 4a4939f4..110578c5 100644 --- a/readme.md +++ b/readme.md @@ -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** @@ -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