-
Notifications
You must be signed in to change notification settings - Fork 587
feat: add download functionality to barretenberg-rs build.rs #20105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
johnathan79717
merged 4 commits into
merge-train/barretenberg
from
jh/barretenberg-rs-download
Feb 3, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5cce5bf
feat: add download functionality to build.rs for pre-built static lib…
johnathan79717 14fd301
fix: address PR review comments
johnathan79717 0059481
fix: flip BB_LOCAL_BUILD logic - download by default, local is opt-in
johnathan79717 78cacb1
fix: use BB_LIB_DIR env var to specify library path
johnathan79717 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,105 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| fn main() { | ||
| // Only for ffi feature - link libbb-external from cpp build | ||
| // Only for ffi feature - link libbb-external | ||
| #[cfg(feature = "ffi")] | ||
| { | ||
| // Find the cpp build lib directory relative to this crate | ||
| let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| let lib_dir = std::path::Path::new(&manifest_dir) | ||
| .join("../../cpp/build/lib") | ||
| .canonicalize() | ||
| .expect("Failed to find cpp/build/lib - run barretenberg/cpp/bootstrap.sh first"); | ||
|
|
||
| let lib_dir = get_lib_dir(); | ||
| println!("cargo:rustc-link-search=native={}", lib_dir.display()); | ||
|
|
||
| // libbb-external.a contains everything needed: barretenberg + env + vm2_stub | ||
| println!("cargo:rustc-link-lib=static=bb-external"); | ||
| println!("cargo:rustc-link-lib=dylib=stdc++"); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "ffi")] | ||
| fn get_lib_dir() -> PathBuf { | ||
| // Check if user provided a custom library path | ||
| if let Ok(lib_dir) = std::env::var("BB_LIB_DIR") { | ||
| let lib_dir = PathBuf::from(&lib_dir); | ||
| if lib_dir.join("libbb-external.a").exists() { | ||
| return lib_dir.canonicalize().unwrap(); | ||
| } | ||
| panic!( | ||
| "BB_LIB_DIR is set to {:?} but libbb-external.a not found there. \ | ||
| Build barretenberg locally: cd barretenberg/cpp && ./bootstrap.sh", | ||
| lib_dir | ||
| ); | ||
| } | ||
|
|
||
| // Download from GitHub releases | ||
| let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); | ||
| let lib_path = out_dir.join("libbb-external.a"); | ||
|
|
||
| if !lib_path.exists() { | ||
| download_lib(&out_dir); | ||
| } | ||
|
|
||
| out_dir | ||
| } | ||
|
|
||
| #[cfg(feature = "ffi")] | ||
| fn download_lib(out_dir: &PathBuf) { | ||
| let target = std::env::var("TARGET").unwrap(); | ||
| let arch = match target.as_str() { | ||
| t if t.contains("x86_64") && t.contains("linux") => "amd64-linux", | ||
| t if t.contains("aarch64") && t.contains("linux") => "arm64-linux", | ||
| _ => panic!( | ||
| "Unsupported target for FFI backend: {}. Supported: x86_64-linux, aarch64-linux", | ||
| target | ||
| ), | ||
| }; | ||
|
|
||
| // Use BARRETENBERG_VERSION env var, or fall back to crate version | ||
| let version = std::env::var("BARRETENBERG_VERSION") | ||
| .unwrap_or_else(|_| env!("CARGO_PKG_VERSION").to_string()); | ||
|
|
||
| // Skip download for test versions (0.0.1) | ||
| if version == "0.0.1" { | ||
| panic!( | ||
| "Cannot download pre-built library for test version 0.0.1. \ | ||
| Build barretenberg locally: cd barretenberg/cpp && ./bootstrap.sh" | ||
| ); | ||
| } | ||
|
|
||
| let url = format!( | ||
| "https://github.com/AztecProtocol/aztec-packages/releases/download/v{}/barretenberg-static-{}.tar.gz", | ||
| version, arch | ||
| ); | ||
|
|
||
| println!("cargo:warning=Downloading barretenberg static library from {}", url); | ||
|
|
||
| // Download and extract | ||
| let tar_gz_path = out_dir.join("barretenberg-static.tar.gz"); | ||
|
|
||
| let status = std::process::Command::new("curl") | ||
| .args(["-L", "-f", "-o"]) | ||
| .arg(&tar_gz_path) | ||
| .arg(&url) | ||
| .status() | ||
| .expect("Failed to run curl"); | ||
|
|
||
| if !status.success() { | ||
| panic!( | ||
| "Failed to download barretenberg static library from {}. \ | ||
| Make sure version v{} exists as a GitHub release.", | ||
| url, version | ||
| ); | ||
| } | ||
|
|
||
| let status = std::process::Command::new("tar") | ||
| .args(["-xzf"]) | ||
| .arg(&tar_gz_path) | ||
| .arg("-C") | ||
| .arg(out_dir) | ||
| .status() | ||
| .expect("Failed to run tar"); | ||
|
|
||
| if !status.success() { | ||
| panic!("Failed to extract barretenberg static library"); | ||
| } | ||
|
|
||
| // Clean up tar.gz | ||
| std::fs::remove_file(&tar_gz_path).ok(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.