Skip to content

Commit

Permalink
fix: build rapidsnark
Browse files Browse the repository at this point in the history
  • Loading branch information
vivianjeng committed Feb 10, 2025
1 parent f910802 commit 33c2fe8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
toolchain: "1.81.0"
override: true
- name: Run tests
run: cargo test
run: cargo test -vv
test-macOS:
runs-on: macos-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
Expand Down
79 changes: 69 additions & 10 deletions crates/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const RAPIDSNARK_DOWNLOAD_SCRIPT: &str = include_str!("./download_rapidsnark.sh"
fn main() {
let target = env::var("TARGET").unwrap();
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
let arch = target.split('-').next().unwrap();
let lib_dir = Path::new(&out_dir)
.join("rapidsnark")
.join("package")
.join("lib");

// Try to list contents of the target directory
let rapidsnark_path = Path::new(&out_dir).join(Path::new("rapidsnark"));
Expand All @@ -31,23 +34,73 @@ fn main() {
panic!("Failed to wait for rapidsnark download");
}
}
let absolute_lib_path = if rapidsnark_path.join(&target).exists() {
rapidsnark_path.join(&target)
} else {
rapidsnark_path.join(arch)

println!("Detected target: {}", target);
//For possible options see rapidsnark/build_gmp.sh
let gmp_build_target = match target.as_str() {
"aarch64-apple-ios" => "ios",
"aarch64-apple-ios-sim" => "ios_simulator",
"x86_64-apple-ios" => "ios_simulator",
"x86_64-linux-android" => "android_x86_64",
"i686-linux-android" => "android_x86_64",
"armv7-linux-androideabi" => "android",
"aarch64-linux-android" => "android",
"aarch64-apple-darwin" => "host", //Use "host" for M Macs, macos_arm64 would fail the subsequent build
_ => "host",
};

let gmp_lib_folder = match target.as_str() {
"aarch64-apple-ios" => "package_ios_arm64",
"aarch64-apple-ios-sim" => "package_iphone_simulator_arm64",
"x86_64-apple-ios" => "package_iphone_simulator_x86_64",
"x86_64-linux-android" => "package_android_x86_64",
"i686-linux-android" => "package_android_x86_64",
"armv7-linux-androideabi" => "package_android_arm64",
"aarch64-linux-android" => "package_android_arm64",
_ => "package",
};
//For possible options see rapidsnark/Makefile
let rapidsnark_build_target = match target.as_str() {
"aarch64-apple-ios" => "ios",
"aarch64-apple-ios-sim" => "ios_simulator_arm64",
"x86_64-apple-ios" => "ios_simulator_x86_64",
"x86_64-linux-android" => "android_x86_64",
"i686-linux-android" => "android_x86_64",
"armv7-linux-androideabi" => "android",
"aarch64-linux-android" => "android",
"aarch64-apple-darwin" => "arm64_host",
_ => "host",
};

// If the gmp library is not built, build it
let gmp_dir = rapidsnark_path.join("depends").join("gmp");
let target_dir = gmp_dir.join(gmp_lib_folder);
if !target_dir.exists() {
Command::new("bash")
.current_dir(&rapidsnark_path)
.arg("./build_gmp.sh")
.arg(gmp_build_target)
.spawn()
.expect("Failed to spawn build_gmp.sh")
.wait()
.expect("build_gmp.sh errored");
}

Command::new("make")
.arg(rapidsnark_build_target)
.current_dir(&rapidsnark_path)
.spawn()
.expect("Failed to spawn make arm64_host")
.wait()
.expect("make arm64_host errored");

let compiler = cc::Build::new().get_compiler();
let cpp_stdlib = if compiler.is_like_clang() {
"c++"
} else {
"stdc++"
};

println!(
"cargo:rustc-link-search=native={}",
absolute_lib_path.clone().display()
);

println!("cargo:rustc-link-lib=static=rapidsnark");
println!("cargo:rustc-link-lib={}", cpp_stdlib);
if target.contains("android") {
Expand All @@ -60,6 +113,12 @@ fn main() {
println!("cargo:rustc-link-lib=static=fq");
println!("cargo:rustc-link-lib=static=gmp");

// Specify the path to the rapidsnark library for the linker
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_string_lossy()
);

// refer to https://github.com/bbqsrc/cargo-ndk to see how to link the libc++_shared.so file in Android
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" {
android();
Expand Down
42 changes: 9 additions & 33 deletions crates/download_rapidsnark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,15 @@ if [ -z $TARGET ]; then
exit 1
fi
BUILD_DIR=$OUT_DIR/rapidsnark
BINARY_PATH=$BUILD_DIR/package/lib
mkdir -p $BUILD_DIR

download_and_unzip() {
local target="$1"
local zip_file="$BUILD_DIR/$target.zip"

echo "Downloading $target..."

# Download file with error handling
if ! curl -L -o "$zip_file" "https://rapidsnark.zkmopro.org/$target.zip"; then
echo "Failed to download $target.zip"
return 1 # Return failure status
fi

echo "Unzipping $zip_file..."

# Unzip with error handling
if ! unzip "$zip_file" -d "$BUILD_DIR"; then
echo "Failed to unzip $zip_file"
return 1
fi

echo "✅ Successfully downloaded and extracted $target.zip"
}

# Try downloading the full target
if ! download_and_unzip "$TARGET"; then
echo "Retrying with local architecture..."

local_arch=$(echo "$TARGET" | cut -d'-' -f1)

if ! download_and_unzip "$local_arch"; then
echo "Download failed for both $TARGET and $local_arch"
exit 1 # Exit the script with failure
fi
# If binary exists, exit
if [ -e $BINARY_PATH ]; then
exit 0
fi

rm -rf $BUILD_DIR
git clone https://github.com/zkmopro/rapidsnark.git $BUILD_DIR
cd $BUILD_DIR
git submodule update --init --recursive
1 change: 0 additions & 1 deletion crates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct ProofResult {
pub public_signals: String,
}

#[link(name = "rapidsnark", kind = "static")]
extern "C" {
pub fn groth16_prover_zkey_file(
zkey_file_path: *const std::os::raw::c_char,
Expand Down

0 comments on commit 33c2fe8

Please sign in to comment.