Skip to content

Commit

Permalink
try build rapidsnark
Browse files Browse the repository at this point in the history
  • Loading branch information
vivianjeng committed Jan 31, 2025
1 parent 8c669fa commit d9ec711
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ jobs:
with:
toolchain: "1.81.0"
targets: ${{ matrix.target }}
- name: Install Dependencies
run: |
if [[ "${{ matrix.target }}" == "aarch64-apple-ios-sim" || "${{ matrix.target }}" == "x86_64-apple-ios" ]]; then
rustup target add ${{ matrix.target }}
sudo apt update && sudo apt install -y libclang-dev
fi
- name: Build
run: cargo build --target ${{ matrix.target }}
test-linux:
Expand Down
84 changes: 80 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;

const CLONE_RAPIDSNARK_SCRIPT: &str = include_str!("./clone_rapidsnark.sh");

fn main() {
if std::env::var("RUST_RAPIDSNARK_LINK_TEST_WITNESS").is_ok() {
Expand All @@ -11,6 +14,74 @@ fn main() {
let target = std::env::var("TARGET").unwrap();
let arch = target.split('-').next().unwrap();

let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
let lib_dir = Path::new(&out_dir)
.join("rapidsnark")
.join("package")
.join("lib");

let rapidsnark_path = Path::new(&out_dir).join(Path::new("rapidsnark"));
// If the rapidsnark repo is not cloned, clone it
if !rapidsnark_path.exists() {
let clone_script_path = Path::new(&out_dir).join(Path::new("clone_rapidsnark.sh"));
fs::write(&clone_script_path, CLONE_RAPIDSNARK_SCRIPT)
.expect("Failed to write build script");
Command::new("sh")
.arg(clone_script_path.to_str().unwrap())
.spawn()
.expect("Failed to spawn rapidsnark build")
.wait()
.expect("rapidsnark build errored");
}

println!("Detected target: {}", target);
//For possible options see witnesscalc/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",
};

//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 rapidsnark library is not built, build it
let gmp_dir = rapidsnark_path.join("depends").join("gmp");
if !gmp_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");

// Try to list contents of the target directory
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let rapidsnark_dir = manifest_dir.join("rapidsnark");
Expand All @@ -27,10 +98,10 @@ fn main() {
"stdc++"
};

println!(
"cargo:rustc-link-search=native={}",
absolute_lib_path.clone().display()
);
// 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);
Expand All @@ -48,6 +119,11 @@ fn main() {
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" {
android();
}

println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_string_lossy()
);
}

fn android() {
Expand Down
22 changes: 22 additions & 0 deletions clone_rapidsnark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

# Exit on error
set -e

# OUT_DIR is specified by the rust build environment
if [ -z $OUT_DIR ]; then
echo "OUT_DIR not specified"
exit 1
fi
BUILD_DIR=$OUT_DIR/rapidsnark
BINARY_PATH=$BUILD_DIR/build/rapidsnark/package/bin

# If binary exists, exit
if [ -e $BINARY_PATH ]; then
exit 0
fi

rm -rf $BUILD_DIR
git clone https://github.com/iden3/rapidsnark.git $BUILD_DIR
cd $BUILD_DIR
git submodule update --init --recursive

0 comments on commit d9ec711

Please sign in to comment.