Skip to content

Commit 5f11473

Browse files
kkawuladdoktorskicptarturTHenry14
authored
Support for prebuilt plugin (#2864)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2827 ## Introduced changes <!-- A brief description of the changes --> - Updated `publish_plugin` workflow to compile plugin for different platforms. ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: ddoktorski <[email protected]> Co-authored-by: Artur Michałek <[email protected]> Co-authored-by: Wojciech Szymczyk <[email protected]> Co-authored-by: Artur Michalek <[email protected]>
1 parent 2cdbf6d commit 5f11473

File tree

8 files changed

+278
-93
lines changed

8 files changed

+278
-93
lines changed

.github/workflows/publish_plugin.yml

Lines changed: 131 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,152 @@ on:
55
workflow_dispatch:
66

77
jobs:
8-
upload-to-registry:
9-
name: Upload plugin to the registry
8+
check-version:
9+
name: Check snforge_scarb_plugin Version
1010
runs-on: ubuntu-latest
11-
env:
12-
SCARB_REGISTRY_AUTH_TOKEN: ${{ secrets.SCARB_REGISTRY_AUTH_TOKEN }}
11+
outputs:
12+
plugin_uploaded: ${{ steps.check-version.outputs.plugin_uploaded }}
1313
steps:
1414
- uses: actions/checkout@v4
15-
1615
- name: Check version
1716
id: check-version
18-
if: ${{ github.event_name != 'workflow_dispatch' }}
1917
run: |
2018
set -exo pipefail
2119
2220
snforge_scarb_plugin_version=$(grep version crates/snforge-scarb-plugin/Scarb.toml | cut -d '"' -f 2)
23-
snforge_scarb_plugin_uploaded=$(curl -s https://scarbs.xyz/api/v1/index/sn/fo/snforge_scarb_plugin.json | jq --arg version "$snforge_scarb_plugin_version" '[.[] | select(.v == $version)] | length > 0')
24-
echo "snforge_scarb_plugin_uploaded=$snforge_scarb_plugin_uploaded" >> $GITHUB_OUTPUT
21+
plugin_uploaded=$(curl -s https://scarbs.xyz/api/v1/index/sn/fo/snforge_scarb_plugin.json | jq --arg version "$snforge_scarb_plugin_version" '[.[] | select(.v == $version)] | length > 0')
22+
echo "plugin_uploaded=$plugin_uploaded" >> $GITHUB_OUTPUT
23+
24+
build-binaries:
25+
name: Build ${{ matrix.target }}
26+
needs: check-version
27+
if: needs.check-version.outputs.plugin_uploaded == 'false' || github.event_name == 'workflow_dispatch'
28+
runs-on: ${{ matrix.os }}
29+
30+
env:
31+
# Cross-compiled targets will override this to `cross`.
32+
CARGO: cargo
33+
34+
strategy:
35+
matrix:
36+
include:
37+
- target: x86_64-unknown-linux-gnu
38+
os: ubuntu-latest
39+
# Use cross to link oldest GLIBC possible.
40+
cross: true
41+
lib-name: "libsnforge_scarb_plugin"
42+
ext: "so"
43+
44+
- target: aarch64-unknown-linux-gnu
45+
os: ubuntu-latest
46+
cross: true
47+
lib-name: "libsnforge_scarb_plugin"
48+
ext: "so"
49+
50+
- target: x86_64-apple-darwin
51+
os: macos-latest
52+
lib-name: "libsnforge_scarb_plugin"
53+
ext: "dylib"
54+
55+
- target: aarch64-apple-darwin
56+
os: macos-latest
57+
lib-name: "libsnforge_scarb_plugin"
58+
ext: "dylib"
2559

26-
- uses: dtolnay/rust-toolchain@7b1c307e0dcbda6122208f10795a713336a9b35a
60+
- target: x86_64-pc-windows-msvc
61+
os: windows-latest
62+
lib-name: "snforge_scarb_plugin"
63+
ext: "dll"
64+
65+
# The scarb builds for following platforms are experimental and not officially supported by starknet-foundry.
66+
# https://docs.swmansion.com/scarb/download.html#platform-support
67+
# Reference issue: TODO(#2886)
68+
69+
# - target: aarch64-unknown-linux-musl
70+
# os: ubuntu-latest
71+
# cross: true
72+
# ext: "so"
73+
74+
# - target: x86_64-unknown-linux-musl
75+
# os: ubuntu-latest
76+
# cross: true
77+
# ext: "so"
78+
79+
steps:
80+
- uses: actions/checkout@v4
81+
82+
- uses: dtolnay/rust-toolchain@stable
2783
with:
28-
toolchain: stable
84+
target: ${{ matrix.target }}
85+
86+
- uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
87+
88+
- name: Install cross
89+
if: matrix.cross
90+
uses: taiki-e/install-action@cross
91+
92+
- name: Enable cross-compilation
93+
if: matrix.cross
94+
shell: bash
95+
run: |
96+
echo "CARGO=cross" >> $GITHUB_ENV
2997
98+
- name: Build
99+
working-directory: crates/snforge-scarb-plugin
100+
run: ${{ env.CARGO }} build --release --locked --target ${{ matrix.target }}
101+
102+
- name: Rename Binary
103+
shell: bash
104+
run: |
105+
set -euxo pipefail
106+
107+
PACKAGE_NAME="snforge_scarb_plugin"
108+
PACKAGE_VERSION=$(grep version crates/snforge-scarb-plugin/Scarb.toml | cut -d '"' -f 2)
109+
110+
TARGET="${{ matrix.target }}"
111+
EXT="${{ matrix.ext }}"
112+
LIB_NAME="${{ matrix.lib-name }}"
113+
114+
OUTPUT_BINARY="${PACKAGE_NAME}_v${PACKAGE_VERSION}_${TARGET}.${EXT}"
115+
116+
mv ./target/${TARGET}/release/${LIB_NAME}.${EXT} ./target/${TARGET}/release/${OUTPUT_BINARY}
117+
118+
echo "OUTPUT_BINARY_PATH=./target/${TARGET}/release/${OUTPUT_BINARY}" >> $GITHUB_ENV
119+
120+
- name: Upload Artifact
121+
uses: actions/upload-artifact@v4
122+
with:
123+
name: build-${{ matrix.target }}
124+
path: ${{ env.OUTPUT_BINARY_PATH }}
125+
compression-level: 0
126+
127+
upload-to-registry:
128+
name: Upload snforge_scarb_plugin to the registry
129+
runs-on: ubuntu-latest
130+
needs: [check-version, build-binaries]
131+
env:
132+
SCARB_REGISTRY_AUTH_TOKEN: ${{ secrets.SCARB_REGISTRY_AUTH_TOKEN }}
133+
steps:
134+
- uses: actions/checkout@v4
135+
- uses: dtolnay/rust-toolchain@stable
30136
- uses: software-mansion/setup-scarb@v1
31137
with:
32-
scarb-version: "2.8.5"
138+
scarb-version: "2.10.0-rc.1"
139+
140+
- name: Download artifacts
141+
uses: actions/download-artifact@v4
142+
with:
143+
path: artifacts-dl
144+
145+
- name: Unpack artifacts to target directory
146+
shell: bash
147+
run: |
148+
set -euxo pipefail
149+
mkdir -p crates/snforge-scarb-plugin/target/scarb/cairo-plugin
150+
151+
mv artifacts-dl/build-*/snforge_scarb_plugin_* crates/snforge-scarb-plugin/target/scarb/cairo-plugin/
33152
34153
- name: Publish snforge_scarb_plugin
35-
if: ${{ steps.check-version.outputs.snforge_scarb_plugin_uploaded == 'false' || github.event_name == 'workflow_dispatch' }}
154+
if: needs.check-version.outputs.plugin_uploaded == 'false' || github.event_name == 'workflow_dispatch'
36155
working-directory: crates/snforge-scarb-plugin
37156
run: scarb publish

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
#### Added
1313

14+
- Rust is no longer required to use `snforge` if using Scarb >= 2.10.0 on supported platforms - precompiled `snforge_scarb_plugin` plugin binaries are now published to [package registry](https://scarbs.xyz) for new versions.
1415
- Added a suggestion for using the `--max-n-steps` flag when the Cairo VM returns the error: `Could not reach the end of the program. RunResources has no remaining steps`.
1516

16-
### Forge
17-
1817
#### Fixed
1918

2019
- coverage validation now supports comments in `Scarb.toml`

crates/forge/src/new.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::env;
1111
use std::fs::{self, OpenOptions};
1212
use std::io::Write;
1313
use std::path::{Path, PathBuf};
14-
use toml_edit::{value, ArrayOfTables, DocumentMut, Item, Table};
14+
use toml_edit::{value, Array, ArrayOfTables, DocumentMut, Item, Table, Value};
1515

1616
static TEMPLATE: Dir = include_dir!("starknet_forge_template");
1717

@@ -96,6 +96,7 @@ fn update_config(config_path: &Path, scarb: &Version) -> Result<()> {
9696
set_cairo_edition(&mut document, CAIRO_EDITION);
9797
add_test_script(&mut document);
9898
add_assert_macros(&mut document, scarb)?;
99+
add_allow_prebuilt_macros(&mut document)?;
99100

100101
fs::write(config_path, document.to_string())?;
101102

@@ -142,6 +143,28 @@ fn add_assert_macros(document: &mut DocumentMut, scarb: &Version) -> Result<()>
142143
Ok(())
143144
}
144145

146+
fn add_allow_prebuilt_macros(document: &mut DocumentMut) -> Result<()> {
147+
let tool_section = document.entry("tool").or_insert(Item::Table(Table::new()));
148+
let tool_table = tool_section
149+
.as_table_mut()
150+
.context("Failed to get tool table from Scarb.toml")?;
151+
tool_table.set_implicit(true);
152+
153+
let mut scarb_table = Table::new();
154+
155+
let mut allow_prebuilt_macros = Array::new();
156+
allow_prebuilt_macros.push("snforge_std");
157+
158+
scarb_table.insert(
159+
"allow-prebuilt-plugins",
160+
Item::Value(Value::Array(allow_prebuilt_macros)),
161+
);
162+
163+
tool_table.insert("scarb", Item::Table(scarb_table));
164+
165+
Ok(())
166+
}
167+
145168
fn extend_gitignore(path: &Path) -> Result<()> {
146169
if path.join(".gitignore").exists() {
147170
let mut file = OpenOptions::new()

crates/forge/tests/e2e/running.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool) {
818818
819819
[scripts]
820820
test = "snforge test"
821+
822+
[tool.scarb]
823+
allow-prebuilt-plugins = ["snforge_std"]
821824
{SCARB_MANIFEST_TEMPLATE_CONTENT}
822825
"#,
823826
snforge_std_assert

crates/snforge-scarb-plugin/Scarb.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
name = "snforge_scarb_plugin"
33
version = "0.36.0"
44
edition = "2024_07"
5+
include = ["target/scarb/cairo-plugin/"]
56

67
[cairo-plugin]

docs/src/appendix/scarb-toml.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ url = "http://your.second.rpc.url"
7878
block_id.number = "123"
7979
```
8080

81+
### `[tool.scarb]`
82+
83+
```toml
84+
[tool.scarb]
85+
allow-prebuilt-plugins = ["snforge_std"]
86+
```
87+
Note: This configuration requires Scarb version >= 2.10.0 .
88+
89+
It allows `scarb` to download precompiled dependencies used by `snforge_std` from [the registry](https://scarbs.xyz).
90+
The `snforge_std` library depends on a Cairo plugin that is written in Rust, and otherwise is compiled locally on the user's side.
91+
8192
### `[profile.<dev|release>.cairo]`
8293
By default, these arguments do not need to be defined. Only set them to use [profiler](https://foundry-rs.github.io/starknet-foundry/snforge-advanced-features/profiling.html#profiling) or [coverage](https://foundry-rs.github.io/starknet-foundry/testing/coverage.html#coverage).
8394

docs/src/getting-started/first-steps.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ Additionally, ensure that starknet-contract target is enabled in the `Scarb.toml
100100
```toml
101101
# ...
102102
[[target.starknet-contract]]
103-
```
103+
```
104+
105+
> 📝 **Note**
106+
>
107+
> You can additionally specify `scarb` settings to avoid compiling Cairo plugin which `snforge_std` depends on. The plugin is written in Rust and, by default, is compiled locally on the user's side.
108+
> ```
109+
> [tool.scarb]
110+
> allow-prebuilt-plugins = ["snforge_std"]
111+
> ```
112+
> This configuration requires Scarb version >= 2.10.0 .
113+
>

0 commit comments

Comments
 (0)