Skip to content

Commit

Permalink
fix: build macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 25, 2024
1 parent 40846e6 commit fa3129e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
12 changes: 1 addition & 11 deletions crates/build/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,9 @@ pub fn execute_build_program(
}

/// Internal helper function to build the program with or without arguments.
///
/// If the path is not absolute, it is assumed to be relative to the `CARGO_MANIFEST_DIR`.
pub(crate) fn build_program_internal(path: impl AsRef<Path>, args: Option<BuildArgs>) {
// Get the root package name and metadata.
//
// Note:
// You _MUST_ read the `CARGO_MANIFEST_DIR` at runtime (`std::env::var` as opposed to `env!`).
// Otherwise it will be the manifest dir of this crate, not the caller.
let mut program_dir = path.as_ref().to_path_buf();
if program_dir.is_relative() {
program_dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).join(program_dir);
}

let program_dir = path.as_ref().to_path_buf();
let metadata_file = program_dir.join("Cargo.toml");
let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
let metadata = metadata_cmd.manifest_path(metadata_file).exec().unwrap();
Expand Down
37 changes: 37 additions & 0 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ pub fn build_program_with_args(path: impl AsRef<Path>, args: BuildArgs) {
build_program_internal(path, Some(args))
}

/// Builds the program with the given arguments if the program at path, or one of its dependencies,
///
/// ### Note: This function is only exposed to support the `build_program_from_path!` macro.
/// It is not recommended to use this function directly.
pub fn build_program_with_maybe_args(path: impl AsRef<Path>, args: Option<BuildArgs>) {
build_program_internal(path, args)
}

/// Build a program with the given _RELATIVE_ path.
///
/// # Arguments
/// * `path` - A path to the guest program directory, if not absolute, assumed to be relative to
/// the caller manifest directory.
///
/// `args` - A [`BuildArgs`] struct that contains various build configuration options.
/// If not provided, the default options are used.
#[macro_export]
macro_rules! build_program_from_path {
($path:expr, $args:expr) => {
const MANIFEST: &str = std::env!("CARGO_MANIFEST_DIR");

fn adjust_path(p: impl AsRef<::std::path::Path>) -> ::std::path::PathBuf {
let p = p.as_ref();
if p.is_absolute() {
p.to_path_buf()
} else {
::std::path::Path::new(MANIFEST).join(p)
}
}

::sp1_build::build_program_with_maybe_args(adjust_path($path), $args)
};
($path:expr) => {
::sp1_build::build_program_from_path!($path, None)
}
}

/// Returns the raw ELF bytes by the zkVM program target name.
///
/// Note that this only works when using `sp1_build::build_program` or
Expand Down

0 comments on commit fa3129e

Please sign in to comment.