Skip to content

Commit

Permalink
feat(blockifier): compile cairo native runtime (#1242)
Browse files Browse the repository at this point in the history
* feat: add cairo native runtime installation to dependencies.sh

* chore: update .gitignore

* chore: preemptevely install Cargo

* fix: add sudo to git clone

* fix: add WORKDIR to ci dockerfile

* fix: runtime is .a and not .so

* feat: rework native runtime dependency CI

Instead of custom script, swapped for the addition of Cairo Native as a git submodule.
The runtime will be compiled once the compilation is succesful.

A known caveat of this approach (using build.rs) is that if there is nothing new to
compile and the native_runtime.a library won't be recompiled, even if it was deleted or updated.

Another caveat is that both the git submodule and Cairo Native
dependency need be updated at the same time. We cannot directly set the
submodule as a dependency because cairo native is also a workspace (such
as this repo) and there cannot be two workspace roots.

Co-Authored-By: Pearson White <pearson.white@nethermind.io>
  • Loading branch information
rodrigo-pino and PearsonWhite authored Nov 5, 2024
1 parent b20329c commit fcc6b10
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[env]
CAIRO_NATIVE_RUNTIME_LIBRARY = "./libcairo_native_runtime.a"
LLVM_SYS_191_PREFIX = "/usr/lib/llvm-19/"
MLIR_SYS_190_PREFIX = "/usr/lib/llvm-19/"
TABLEGEN_190_PREFIX = "/usr/lib/llvm-19/"
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/blockifier_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ jobs:
runs-on: starkware-ubuntu-20-04-medium
steps:
- uses: actions/checkout@v4
with:
# required to clone native as a gitsubmodule
submodules: recursive
fetch-depth: 0
- uses: ./.github/actions/bootstrap
# No features - build blockifier without features activated by dependencies in the workspace.
- run: cargo build -p blockifier
- run: cargo test -p blockifier
# transaction_serde is not activated by any workspace crate; test the build.
- run: cargo build -p blockifier --features transaction_serde
- run: cargo test -p blockifier --features transaction_serde
# cairo_native is not activated by any workspace crate; test the build.
# cairo_native is not activated by any workspace crate; test the build.
- run: cargo build -p blockifier --features cairo_native
- run: cargo test -p blockifier --features cairo_native
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
# Environment setup.
- uses: actions/checkout@v4
with:
# required to clone native as a git submodule
submodules: recursive
# Fetch the entire history. Required to checkout the merge target commit, so the diff can
# be computed.
fetch-depth: 0
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ scripts/papyrus/p2p_sync_e2e_test/data_server/
# Papyrus helm chart
deployments/papyrus/helm/config/*
!deployments/papyrus/helm/config/example.json

# Generated file used for running contracts compiled with Cairo Native
crates/blockifier/libcairo_native_runtime.a
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "crates/blockifier/cairo_native"]
path = crates/blockifier/cairo_native
url = https://github.com/lambdaclass/cairo_native
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ cairo-lang-sierra = "=2.8.4"
cairo-lang-sierra-to-casm = "2.8.4"
cairo-lang-starknet-classes = "2.8.4"
cairo-lang-utils = "2.8.4"
# Important: when updated, make sure to update the cairo-native submodule as well.
cairo-native = "0.2.0-alpha.4"
cairo-vm = "=1.0.1"
camelpaste = "0.1.0"
Expand Down
68 changes: 68 additions & 0 deletions crates/blockifier/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::path::PathBuf;
use std::process::Command;

fn compile_cairo_native_aot_runtime() {
let cairo_native_dir = std::env::current_dir()
.expect("Failed to get current directory")
.join(PathBuf::from("cairo_native"));

if !cairo_native_dir.exists() || !cairo_native_dir.join(".git").exists() {
panic!(
"It seems git submodule at {} doesn't exist or it is not initialized, please \
run:\n\ngit submodule update --init --recursive\n",
cairo_native_dir.to_str().unwrap()
);
}

let runtime_target_dir = cairo_native_dir.join(PathBuf::from("target"));
let status = Command::new("cargo")
.args([
"build",
"--release",
"-p",
"cairo-native-runtime",
"--message-format=json",
"--target-dir",
runtime_target_dir.to_str().unwrap(),
])
.current_dir(cairo_native_dir)
.status()
.expect("Failed to execute cargo");
if !status.success() {
panic!("Building cairo native runtime failed: {status}")
}

let runtime_target_path =
runtime_target_dir.join(PathBuf::from("release/libcairo_native_runtime.a"));

const RUNTIME_LIBRARY: &str = "CAIRO_NATIVE_RUNTIME_LIBRARY";
let runtime_expected_path = {
let expected_path_env =
std::env::var(RUNTIME_LIBRARY).expect("Cairo Native rutime path variable is not set");
let expected_path = PathBuf::from(&expected_path_env);

if expected_path.is_absolute() {
expected_path
} else {
std::env::current_dir().expect("Failed to get current directory").join(expected_path)
}
};

std::fs::copy(&runtime_target_path, &runtime_expected_path)
.expect("Failed to copy native runtime");

println!("cargo::rerun-if-changed=./cairo_native/runtime/");
// todo(rodrigo): this directive seems to cause the build script to trigger everytime on
// Linux based machines. Investigate the issue further.
println!("cargo::rerun-if-changed={}", runtime_expected_path.to_str().unwrap());
println!("cargo::rerun-if-env-changed={RUNTIME_LIBRARY}");
}

fn main() {
// `CARGO_FEATURE_CAIRO_NATIVE` env var is set by Cargo when compiling with the `cairo_native`
// feature flag. Build instructions are defined behind this condition since they are only
// relevant when using Cairo Native.
if std::env::var("CARGO_FEATURE_CAIRO_NATIVE").is_ok() {
compile_cairo_native_aot_runtime();
}
}
1 change: 1 addition & 0 deletions crates/blockifier/cairo_native
Submodule cairo_native added at b5769e
5 changes: 4 additions & 1 deletion taplo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
exclude = ["crates/native_blockifier/.cargo/config.toml"]
exclude = [
"crates/blockifier/cairo_native/**/*.toml",
"crates/native_blockifier/.cargo/config.toml",
]

[formatting]
column_width = 100
Expand Down

0 comments on commit fcc6b10

Please sign in to comment.