Skip to content
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

allow template crates to have a lib target #277

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
thread,
};

use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use cargo_metadata::camino::Utf8PathBuf;

const BUILD_TARGET: &str = "riscv32em-athena-zkvm-elf";
Expand Down Expand Up @@ -200,26 +200,29 @@ fn copy_elf_to_output_dir(
args: &BuildArgs,
program_metadata: &cargo_metadata::Metadata,
) -> Result<Utf8PathBuf> {
let root_package = program_metadata.root_package();
let root_package_name = root_package.as_ref().map(|p| &p.name);
println!("Root package name: {:?}", root_package_name.unwrap());
let root_package = program_metadata
.root_package()
.ok_or(anyhow!("root package not found"))?;
println!("Root package name: {}", root_package.name);

// Determine which target to use, and choose the artifact name based on the target type.
// For simplicity, we only consider the first target, and expect either bin or staticlib.
// For simplicity, we only consider the first bin or staticlib target.
// Our target does not support dylib.
// TODO: make this Mac and Windows-compatible.
let target = root_package.unwrap().targets.first().unwrap();
let artifact_name = if target.is_bin() {
root_package_name.unwrap()
} else if target.is_staticlib() {
&format!("lib{}.a", target.name)
} else {
panic!("Unsupported target kind");
};
println!(
"Target {:?} artifact path: {:?}",
target.name, artifact_name
);
let (target, artifact_name) = root_package
.targets
.iter()
.find_map(|t| {
if t.is_bin() {
Some((t, root_package.name.clone()))
} else if t.is_staticlib() {
Some((t, format!("lib{}.a", t.name)))
} else {
None
}
})
.ok_or(anyhow!("neither bin nor staticlib target found"))?;
println!("Target {} artifact path: {}", target.name, artifact_name);

// The ELF is written to a target folder specified by the program's package.
let original_elf_path = program_metadata
Expand All @@ -237,24 +240,20 @@ fn copy_elf_to_output_dir(
} else if !args.binary.is_empty() {
args.binary.clone()
} else {
root_package_name.unwrap().to_string()
root_package.name.clone()
};

let elf_dir = root_package
.unwrap()
.manifest_path
.parent()
.unwrap()
.ok_or(anyhow!("couldn't find manifest path"))?
.join(&args.output_directory);
println!("Creating output dir {:?}", elf_dir);
fs::create_dir_all(&elf_dir)?;
let result_elf_path = elf_dir.join(elf_name);

// Copy the ELF to the specified output directory.
println!(
"Copying original artifact {:?} to final path: {:?}",
original_elf_path, result_elf_path
);
println!("Copying original artifact {original_elf_path} to final path: {result_elf_path}");
fs::copy(original_elf_path, &result_elf_path)?;

Ok(result_elf_path)
Expand Down
16 changes: 8 additions & 8 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/fibonacci/program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/hello_world/program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/io/program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/wallet/program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/entrypoint/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/panic/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading