Skip to content

Commit

Permalink
feat: allow to specify multiple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa committed Nov 5, 2024
1 parent 4370aad commit 67effd2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
13 changes: 8 additions & 5 deletions crates/build/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ pub fn execute_build_program(

let target_elf_paths = generate_elf_paths(&program_metadata, Some(args))?;

// Temporary backward compatibility with the deprecated behavior of copying the ELF file.
// TODO: add option to turn off this behavior
if target_elf_paths.len() == 1 {
copy_elf_to_output_dir(args, &program_metadata, &target_elf_paths[0].1)?;
if args.binaries.is_empty() && !target_elf_paths.is_empty() {
// Backward compatibility: if the --bin arg is not set, default to riscv32im-succinct-zkvm-elf
copy_elf_to_output_dir(args, &program_metadata, BUILD_TARGET, &target_elf_paths[0].1)?;
} else {
for (bin_target_name, elf_path) in &target_elf_paths {
copy_elf_to_output_dir(args, &program_metadata, bin_target_name, elf_path)?;
}
}

Ok(target_elf_paths)
Expand Down Expand Up @@ -133,7 +136,7 @@ fn generate_elf_paths(
}) {
// Filter out irrelevant targets if `--bin` is used.
if let Some(args) = args {
if !args.binary.is_empty() && bin_target.name != args.binary {
if !args.binaries.is_empty() && !args.binaries.contains(&bin_target.name) {
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/build/src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub(crate) fn get_program_build_args(args: &BuildArgs) -> Vec<String> {

build_args.push("-Ztrim-paths".to_string());

if !args.binary.is_empty() {
if !args.binaries.is_empty() {
build_args.push("--bin".to_string());
build_args.push(args.binary.clone());
build_args.extend_from_slice(&args.binaries);
}

if !args.features.is_empty() {
Expand Down
9 changes: 5 additions & 4 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ pub struct BuildArgs {
alias = "bin",
long,
action,
help = "Build only the specified binary",
default_value = ""
help = "Build only the specified binaries",
default_value = "",
num_args = 0..
)]
pub binary: String,
pub binaries: Vec<String>,
#[clap(long, action, help = "ELF binary name", default_value = "")]
pub elf_name: String,
#[clap(
Expand All @@ -79,7 +80,7 @@ impl Default for BuildArgs {
features: vec![],
rustflags: vec![],
ignore_rust_version: false,
binary: "".to_string(),
binaries: vec![],
elf_name: "".to_string(),
output_directory: DEFAULT_OUTPUT_DIR.to_string(),
locked: false,
Expand Down
20 changes: 8 additions & 12 deletions crates/build/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ use anyhow::Result;
use cargo_metadata::{camino::Utf8PathBuf, Metadata};
use chrono::Local;

use crate::{BuildArgs, BUILD_TARGET};
use crate::BuildArgs;

/// Copy the ELF to the specified output directory.
/// Copy the ELF to the specified output directory, using the specified binary target name
pub(crate) fn copy_elf_to_output_dir(
args: &BuildArgs,
program_metadata: &cargo_metadata::Metadata,
bin_target_name: &str,
elf_path: &Utf8PathBuf,
) -> Result<Utf8PathBuf> {
// The order of precedence for the ELF name is:
// 1. --elf_name flag
// 2. --binary flag + -elf suffix (defaults to riscv32im-succinct-zkvm-elf)
let elf_name = if !args.elf_name.is_empty() {
args.elf_name.clone()
} else if !args.binary.is_empty() {
// TODO: In the future, change this to default to the package name. Will require updating
// docs and examples.
args.binary.clone()
} else {
BUILD_TARGET.to_string()
};
// 2. target name
// TODO: In the future, change this to default to the package name. Will require updating
// docs and examples.
let elf_name =
if !args.elf_name.is_empty() { args.elf_name.clone() } else { bin_target_name.to_string() };

let elf_dir = program_metadata.target_directory.parent().unwrap().join(&args.output_directory);
fs::create_dir_all(&elf_dir)?;
Expand Down

0 comments on commit 67effd2

Please sign in to comment.