Skip to content

Commit

Permalink
feat(cargo-build): Add ability to compile specified binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cardosaum committed Jan 22, 2024
1 parent 11c9cbf commit 6baaca7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
25 changes: 23 additions & 2 deletions risc0/build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const DOCKER_IGNORE: &str = r#"
const TARGET_DIR: &str = "target/riscv-guest/riscv32im-risc0-zkvm-elf/docker";

/// Build the package in the manifest path using a docker environment.
pub fn docker_build(manifest_path: &Path, src_dir: &Path, features: &[String]) -> Result<()> {
pub fn docker_build(
manifest_path: &Path,
src_dir: &Path,
binaries: &[String],
features: &[String],
) -> Result<()> {
let manifest_path = manifest_path
.canonicalize()
.context(format!("manifest_path: {manifest_path:?}"))?;
Expand Down Expand Up @@ -69,14 +74,24 @@ pub fn docker_build(manifest_path: &Path, src_dir: &Path, features: &[String]) -
let temp_dir = tempdir()?;
let temp_path = temp_dir.path();
let rel_manifest_path = manifest_path.strip_prefix(&src_dir)?;
create_dockerfile(&rel_manifest_path, temp_path, pkg_name.as_str(), features)?;
create_dockerfile(
&rel_manifest_path,
temp_path,
pkg_name.as_str(),
binaries,
features,
)?;
build(&src_dir, temp_path)?;
}
println!("ELFs ready at:");

let target_dir = src_dir.join(TARGET_DIR);
for target in root_pkg.targets.iter() {
if target.is_bin() {
if !binaries.is_empty() && !binaries.contains(&target.name) {
println!("Skipping target: {}", target.name);
continue;
}
let elf_path = target_dir.join(&pkg_name).join(&target.name);
let image_id = compute_image_id(&elf_path)?;
let rel_elf_path = Path::new(TARGET_DIR).join(&pkg_name).join(&target.name);
Expand All @@ -94,6 +109,7 @@ fn create_dockerfile(
manifest_path: &Path,
temp_dir: &Path,
pkg_name: &str,
binaries: &[String],
features: &[String],
) -> Result<()> {
let manifest_env = &[("CARGO_MANIFEST_PATH", manifest_path.to_str().unwrap())];
Expand All @@ -111,6 +127,11 @@ fn create_dockerfile(
];

let mut build_args = common_args.clone();
let binaries_str = binaries.join(",");
if !binaries.is_empty() {
build_args.push("--bin");
build_args.push(&binaries_str);
}
let features_str = features.join(",");
if !features.is_empty() {
build_args.push("--features");
Expand Down
9 changes: 9 additions & 0 deletions risc0/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ fn build_guest_package<P>(
cargo_command("build", &[])
};

let binaries_str = guest_opts.bin.join(",");
if !guest_opts.bin.is_empty() {
cmd.args(&["--bin", &binaries_str]);
}

let features_str = guest_opts.features.join(",");
if !features_str.is_empty() {
cmd.args(&["--features", &features_str]);
Expand Down Expand Up @@ -500,6 +505,9 @@ pub struct GuestOptions {
/// Features for cargo to build the guest with.
pub features: Vec<String>,

/// Binary names to build.
pub bin: Vec<String>,

/// Use a docker environment for building.
pub use_docker: Option<DockerOptions>,
}
Expand Down Expand Up @@ -557,6 +565,7 @@ pub fn embed_methods_with_options(mut guest_pkg_to_options: HashMap<&str, GuestO
docker_build(
guest_pkg.manifest_path.as_std_path(),
&src_dir,
&guest_opts.bin,
&guest_opts.features,
)
.unwrap();
Expand Down
6 changes: 5 additions & 1 deletion risc0/cargo-risczero/src/commands/build_guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct BuildGuest {
#[arg(long)]
pub manifest_path: PathBuf,

/// Specify binaries to build.
#[clap(long, value_delimiter = ',')]
pub bin: Vec<String>,

/// Feature flags passed to cargo.
#[arg(long, value_delimiter = ',')]
pub features: Vec<String>,
Expand All @@ -34,6 +38,6 @@ pub struct BuildGuest {
impl BuildGuest {
pub fn run(&self) -> Result<()> {
let src_dir = std::env::current_dir().unwrap();
risc0_build::docker_build(&self.manifest_path, &src_dir, &self.features)
risc0_build::docker_build(&self.manifest_path, &src_dir, &self.bin, &self.features)
}
}
2 changes: 2 additions & 0 deletions risc0/zkvm/methods/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ fn main() {
(
"risc0-zkvm-methods-guest",
GuestOptions {
bin: vec![],
features: vec![],
use_docker,
},
),
(
"risc0-zkvm-methods-std",
GuestOptions {
bin: vec![],
features: vec!["test_feature1".to_string(), "test_feature2".to_string()],
use_docker: None,
},
Expand Down

0 comments on commit 6baaca7

Please sign in to comment.