Skip to content

Commit

Permalink
fix ratan pr
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas committed Aug 20, 2024
1 parent 77aefa4 commit 63fa8f7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
37 changes: 15 additions & 22 deletions crates/build/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ use crate::{
BuildArgs,
};

/// Build a program with the specified [`BuildArgs`]. The `program_dir` is specified as an argument when
/// the program is built via `build_program`.
/// Build a program with the specified [`BuildArgs`]. The `program_dir` is specified as an argument
/// when the program is built via `build_program`.
///
/// # Arguments
///
/// * `args` - A reference to a `BuildArgs` struct that holds various arguments used for building the program.
/// * `args` - A reference to a `BuildArgs` struct that holds various arguments used for building
/// the program.
/// * `program_dir` - An optional `PathBuf` specifying the directory of the program to be built.
///
/// # Returns
///
/// * `Result<Utf8PathBuf>` - The path to the built program as a `Utf8PathBuf` on success, or an error on failure.
/// * `Result<Utf8PathBuf>` - The path to the built program as a `Utf8PathBuf` on success, or an
/// error on failure.
pub fn execute_build_program(
args: &BuildArgs,
program_dir: Option<PathBuf>,
) -> Result<Utf8PathBuf> {
// If the program directory is not specified, use the current directory.
let program_dir = program_dir
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current directory."));
let program_dir: Utf8PathBuf = program_dir
.try_into()
.expect("Failed to convert PathBuf to Utf8PathBuf");
let program_dir: Utf8PathBuf =
program_dir.try_into().expect("Failed to convert PathBuf to Utf8PathBuf");

// Get the program metadata.
let program_metadata_file = program_dir.join("Cargo.toml");
let mut program_metadata_cmd = cargo_metadata::MetadataCommand::new();
let program_metadata = program_metadata_cmd
.manifest_path(program_metadata_file)
.exec()
.unwrap();
let program_metadata =
program_metadata_cmd.manifest_path(program_metadata_file).exec().unwrap();

// Get the command corresponding to Docker or local build.
let cmd = if args.docker {
Expand All @@ -59,10 +58,7 @@ pub(crate) fn build_program_internal(path: &str, args: Option<BuildArgs>) {
let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
let metadata = metadata_cmd.manifest_path(metadata_file).exec().unwrap();
let root_package = metadata.root_package();
let root_package_name = root_package
.as_ref()
.map(|p| p.name.as_str())
.unwrap_or("Program");
let root_package_name = root_package.as_ref().map(|p| p.name.as_str()).unwrap_or("Program");

// Skip the program build if the SP1_SKIP_PROGRAM_BUILD environment variable is set to true.
let skip_program_build = std::env::var("SP1_SKIP_PROGRAM_BUILD")
Expand All @@ -80,8 +76,9 @@ pub(crate) fn build_program_internal(path: &str, args: Option<BuildArgs>) {
// Activate the build command if the dependencies change.
cargo_rerun_if_changed(&metadata, program_dir);

// Check if RUSTC_WORKSPACE_WRAPPER is set to clippy-driver (i.e. if `cargo clippy` is the current
// compiler). If so, don't execute `cargo prove build` because it breaks rust-analyzer's `cargo clippy` feature.
// Check if RUSTC_WORKSPACE_WRAPPER is set to clippy-driver (i.e. if `cargo clippy` is the
// current compiler). If so, don't execute `cargo prove build` because it breaks
// rust-analyzer's `cargo clippy` feature.
let is_clippy_driver = std::env::var("RUSTC_WORKSPACE_WRAPPER")
.map(|val| val.contains("clippy-driver"))
.unwrap_or(false);
Expand All @@ -100,9 +97,5 @@ pub(crate) fn build_program_internal(path: &str, args: Option<BuildArgs>) {
panic!("Failed to build SP1 program: {}.", err);
}

println!(
"cargo:warning={} built at {}",
root_package_name,
current_datetime()
);
println!("cargo:warning={} built at {}", root_package_name, current_datetime());
}
21 changes: 8 additions & 13 deletions crates/build/src/command/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,35 @@ pub(crate) fn create_local_command(
program_metadata: &cargo_metadata::Metadata,
) -> Command {
let mut command = Command::new("cargo");
let canonicalized_program_dir = program_dir
.canonicalize()
.expect("Failed to canonicalize program directory");
let canonicalized_program_dir =
program_dir.canonicalize().expect("Failed to canonicalize program directory");

// If CC_riscv32im_succinct_zkvm_elf is not set, set it to the default C++ toolchain
// downloaded by 'sp1up --c-toolchain'.
if env::var("CC_riscv32im_succinct_zkvm_elf").is_err() {
if let Some(home_dir) = home_dir() {
let cc_path = home_dir
.join(".sp1")
.join("bin")
.join("riscv32-unknown-elf-gcc");
let cc_path = home_dir.join(".sp1").join("bin").join("riscv32-unknown-elf-gcc");
if cc_path.exists() {
command.env("CC_riscv32im_succinct_zkvm_elf", cc_path);
}
}
}

// When executing the local command:
// 1. Set the target directory to a subdirectory of the program's target directory to avoid build
// 1. Set the target directory to a subdirectory of the program's target directory to avoid
// build
// conflicts with the parent process. Source: https://github.com/rust-lang/cargo/issues/6412
// 2. Set the rustup toolchain to succinct.
// 3. Set the encoded rust flags.
// 4. Remove the rustc configuration, otherwise in a build script it will attempt to compile the
// program with the toolchain of the normal build process, rather than the Succinct toolchain.
// program with the toolchain of the normal build process, rather than the Succinct
// toolchain.
command
.current_dir(canonicalized_program_dir)
.env("RUSTUP_TOOLCHAIN", "succinct")
.env("CARGO_ENCODED_RUSTFLAGS", get_rust_compiler_flags())
.env_remove("RUSTC")
.env(
"CARGO_TARGET_DIR",
program_metadata.target_directory.join(HELPER_TARGET_SUBDIR),
)
.env("CARGO_TARGET_DIR", program_metadata.target_directory.join(HELPER_TARGET_SUBDIR))
.args(&get_program_build_args(args));
command
}
4 changes: 2 additions & 2 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ impl Default for BuildArgs {

/// Builds the program if the program at the specified path, or one of its dependencies, changes.
///
/// This function monitors the program and its dependencies for changes. If any changes are detected,
/// it triggers a rebuild of the program.
/// This function monitors the program and its dependencies for changes. If any changes are
/// detected, it triggers a rebuild of the program.
///
/// # Arguments
///
Expand Down
20 changes: 5 additions & 15 deletions crates/build/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub(crate) fn copy_elf_to_output_dir(
let root_package = program_metadata.root_package();
let root_package_name = root_package.as_ref().map(|p| &p.name);

// The ELF is written to a target folder specified by the program's package. If built with Docker,
// includes /docker after HELPER_TARGET_SUBDIR.
// The ELF is written to a target folder specified by the program's package. If built with
// Docker, includes /docker after HELPER_TARGET_SUBDIR.
let target_dir_suffix = if args.docker {
format!("{}/{}", HELPER_TARGET_SUBDIR, "docker")
} else {
Expand All @@ -42,11 +42,7 @@ pub(crate) fn copy_elf_to_output_dir(
BUILD_TARGET.to_string()
};

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

Expand All @@ -73,19 +69,13 @@ pub(crate) fn cargo_rerun_if_changed(metadata: &Metadata, program_dir: &Path) {
];
for dir in dirs {
if dir.exists() {
println!(
"cargo::rerun-if-changed={}",
dir.canonicalize().unwrap().display()
);
println!("cargo::rerun-if-changed={}", dir.canonicalize().unwrap().display());
}
}

// Re-run the build script if the workspace root's Cargo.lock changes. If the program is its own
// workspace, this will be the program's Cargo.lock.
println!(
"cargo:rerun-if-changed={}",
metadata.workspace_root.join("Cargo.lock").as_str()
);
println!("cargo:rerun-if-changed={}", metadata.workspace_root.join("Cargo.lock").as_str());

// Re-run if any local dependency changes.
for package in &metadata.packages {
Expand Down
3 changes: 0 additions & 3 deletions crates/cli/src/commands/prove.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use anstyle::*;
use anyhow::Result;
use clap::Parser;
use sp1_build::{build_program, BuildArgs};
use sp1_build::{execute_build_program, BuildArgs};
use sp1_core::utils::{setup_logger, setup_tracer};
use sp1_core_machine::{
io::SP1Stdin,
utils::{setup_logger, setup_tracer},
};
use sp1_prover::SP1Stdin;
use sp1_sdk::ProverClient;
use std::{env, fs::File, io::Read, path::PathBuf, str::FromStr, time::Instant};

Expand Down

0 comments on commit 63fa8f7

Please sign in to comment.