diff --git a/crates/build/src/build.rs b/crates/build/src/build.rs index 6e25ecc0f..03858133e 100644 --- a/crates/build/src/build.rs +++ b/crates/build/src/build.rs @@ -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, args: Option) { // 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(); diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 74ebfdaf9..2415b72ad 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -132,6 +132,43 @@ pub fn build_program_with_args(path: impl AsRef, 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, args: Option) { + 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