Skip to content

Commit

Permalink
feat: rework native runtime dependency CI
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodrigo-pino committed Nov 4, 2024
1 parent 29453b0 commit ced58fb
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 36 deletions.
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ deployments/papyrus/helm/config/*
!deployments/papyrus/helm/config/example.json

# Generated file used for running contracts compiled with Cairo Native
libcairo_native_runtime.so
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
63 changes: 63 additions & 0 deletions crates/blockifier/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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/");
println!("cargo::rerun-if-changed={}", runtime_expected_path.to_str().unwrap());
println!("cargo::rerun-if-env-changed={RUNTIME_LIBRARY}");
}

fn main() {
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
31 changes: 0 additions & 31 deletions scripts/dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,10 @@ function setup_llvm_deps() {
esac
}

function compile_cairo_native_runtime() {
# First we need to make sure Cargo exists
if command -v cargo >/dev/null 2>&1; then
echo "Rust is already installed with cargo available in PATH."
else
echo "cargo not found. Installing Rust..."
if ! curl -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path; then
echo >&2 "Failed to install Rust. Aborting."
return 1
fi
# shellcheck disable=SC1090
source "$HOME/.cargo/env" || {
echo >&2 "Failed to source Rust environment. Aborting."
return 1
}
fi

# Then we clone and build the runtime from the repo
git clone https://github.com/lambdaclass/cairo_native.git
pushd ./cairo_native || exit 1
git switch v0.2.0-alpha.2 --detach
cargo build -p cairo-native-runtime --release --all-features --quiet
popd || exit 1

mv ./cairo_native/target/release/libcairo_native_runtime.a ./libcairo_native_runtime.a
rm -rf ./cairo_native
}

function main() {
[ "$(uname)" = "Linux" ] && install_essential_deps_linux
setup_llvm_deps
echo "LLVM dependencies installed successfully."

compile_cairo_native_runtime
echo "Cairo Native runtime compiled successfully."
}

main "$@"
Expand Down
2 changes: 0 additions & 2 deletions scripts/sequencer-ci.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ RUN echo "%${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/d

USER ${USERNAME}

WORKDIR /app

ENV RUSTUP_HOME=/var/tmp/rust
ENV CARGO_HOME=${RUSTUP_HOME}
ENV PATH=$PATH:${RUSTUP_HOME}/bin
Expand Down
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 ced58fb

Please sign in to comment.