Install rust src if not found (#185)

This commit is contained in:
Han
2025-10-31 13:09:14 +08:00
committed by GitHub
parent ffc2fccaea
commit ceba282275
13 changed files with 54 additions and 45 deletions

View File

@@ -30,13 +30,13 @@ pub enum CommonError {
},
#[error("Command `{cmd}` exit with {status}{stdout}{stderr}",
stdout = if stdout.is_empty() { String::new() } else { format!("\nstdout: {}", String::from_utf8_lossy(stdout)) },
stderr = if stderr.is_empty() { String::new() } else { format!("\nstdout: {}", String::from_utf8_lossy(stderr)) })]
stdout = if stdout.is_empty() { String::new() } else { format!("\nstdout: {stdout}") },
stderr = if stderr.is_empty() { String::new() } else { format!("\nstderr: {stderr}") })]
CommandExitNonZero {
cmd: String,
status: ExitStatus,
stdout: Vec<u8>,
stderr: Vec<u8>,
stdout: String,
stderr: String,
},
#[error("`cargo metadata` in {manifest_dir} failed: {err}")]
@@ -102,12 +102,10 @@ impl CommonError {
cmd: format!("{cmd:?}"),
status,
stdout: output
.map(|output| &output.stdout)
.cloned()
.map(|output| String::from_utf8_lossy(&output.stdout).to_string())
.unwrap_or_default(),
stderr: output
.map(|output| &output.stderr)
.cloned()
.map(|output| String::from_utf8_lossy(&output.stderr).to_string())
.unwrap_or_default(),
}
}

View File

@@ -3,5 +3,5 @@ mod rust;
pub use {
error::CommonError,
rust::{CargoBuildCmd, cargo_metadata, rustc_path},
rust::{CargoBuildCmd, cargo_metadata, install_rust_src, rustc_path},
};

View File

@@ -4,6 +4,7 @@ use std::{
fs, iter,
path::{Path, PathBuf},
process::Command,
sync::Mutex,
};
use tempfile::tempdir;
@@ -82,6 +83,14 @@ impl CargoBuildCmd {
let metadata = cargo_metadata(manifest_dir.as_ref())?;
let package = metadata.root_package().unwrap();
if self
.build_options
.iter()
.any(|opt| opt.contains("build-std"))
{
install_rust_src(&self.toolchain)?;
}
let tempdir = tempdir().map_err(CommonError::tempdir)?;
let linker_script_path = tempdir
.path()
@@ -107,7 +116,7 @@ impl CargoBuildCmd {
.join(CARGO_ENCODED_RUSTFLAGS_SEPARATOR);
let args = iter::empty()
.chain([format!("+{}", &self.toolchain)])
.chain([plus_toolchain(&self.toolchain)])
.chain(["build".into()])
.chain(self.build_options.iter().cloned())
.chain(["--profile".into(), self.profile.clone()])
@@ -176,3 +185,30 @@ pub fn rustc_path(toolchain: &str) -> Result<PathBuf, CommonError> {
.join("rustc"),
)
}
/// Install component `rust-src` for the given `toolchain` if not found.
pub fn install_rust_src(toolchain: &str) -> Result<(), CommonError> {
static LOCK: Mutex<()> = Mutex::new(());
let _guard = LOCK.lock().unwrap_or_else(|err| err.into_inner());
let mut cmd = Command::new("rustup");
let output = cmd
.args([&plus_toolchain(toolchain), "component", "add", "rust-src"])
.output()
.map_err(|err| CommonError::command(&cmd, err))?;
if !output.status.success() {
return Err(CommonError::command_exit_non_zero(
&cmd,
output.status,
Some(&output),
));
}
Ok(())
}
fn plus_toolchain(toolchain: &str) -> String {
format!("+{toolchain}")
}

View File

@@ -39,13 +39,13 @@ pub enum CommonError {
},
#[error("Command `{cmd}` exit with {status}{stdout}{stderr}",
stdout = if stdout.is_empty() { String::new() } else { format!("\nstdout: {}", String::from_utf8_lossy(stdout)) },
stderr = if stderr.is_empty() { String::new() } else { format!("\nstdout: {}", String::from_utf8_lossy(stderr)) })]
stdout = if stdout.is_empty() { String::new() } else { format!("\nstdout: {stdout}") },
stderr = if stderr.is_empty() { String::new() } else { format!("\nstderr: {stderr}") })]
CommandExitNonZero {
cmd: String,
status: ExitStatus,
stdout: Vec<u8>,
stderr: Vec<u8>,
stdout: String,
stderr: String,
},
#[error("Unsupported proof kind {unsupported:?}, expect one of {supported:?}")]
@@ -126,12 +126,10 @@ impl CommonError {
cmd: format!("{cmd:?}"),
status,
stdout: output
.map(|output| &output.stdout)
.cloned()
.map(|output| String::from_utf8_lossy(&output.stdout).to_string())
.unwrap_or_default(),
stderr: output
.map(|output| &output.stderr)
.cloned()
.map(|output| String::from_utf8_lossy(&output.stderr).to_string())
.unwrap_or_default(),
}
}

View File

@@ -2,9 +2,9 @@ use crate::{
compiler::{Error, read_app_config},
program::OpenVMProgram,
};
use ere_compile_utils::CommonError;
use ere_compile_utils::{CommonError, install_rust_src};
use ere_zkvm_interface::compiler::Compiler;
use openvm_build::GuestOptions;
use openvm_build::{GuestOptions, get_rustup_toolchain_name};
use std::{fs, path::Path};
/// Compiler for Rust guest program to RV32IMA architecture, using customized
@@ -17,6 +17,8 @@ impl Compiler for RustRv32imaCustomized {
type Program = OpenVMProgram;
fn compile(&self, guest_directory: &Path) -> Result<Self::Program, Self::Error> {
install_rust_src(&get_rustup_toolchain_name())?;
// Inlining `openvm_sdk::Sdk::build` in order to get raw elf bytes.
let pkg = openvm_build::get_package(guest_directory);
let guest_opts = GuestOptions::default().with_profile("release".to_string());

View File

@@ -29,7 +29,4 @@ RUN /tmp/install_airbender_sdk.sh && rm /tmp/install_airbender_sdk.sh
# Verify airbender-cli is accessible with the correct toolchain
RUN airbender-cli --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup component add rust-src
CMD ["/bin/bash"]

View File

@@ -19,7 +19,4 @@ RUN /tmp/install_jolt_sdk.sh && rm /tmp/install_jolt_sdk.sh # Clean up the scrip
# Verify jolt CLI is accessible.
RUN jolt --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup +nightly component add rust-src
CMD ["/bin/bash"]

View File

@@ -23,7 +23,4 @@ RUN /tmp/install_nexus_sdk.sh && rm /tmp/install_nexus_sdk.sh # Clean up the scr
# Verify Nexus installation
RUN echo "Verifying Nexus installation in Dockerfile (post-script)..." && cargo-nexus --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup component add rust-src --toolchain "$NEXUS_TOOLCHAIN_VERSION"
CMD ["/bin/bash"]

View File

@@ -30,7 +30,4 @@ RUN /tmp/install_openvm_sdk.sh && rm /tmp/install_openvm_sdk.sh
# Verify cargo-openvm is accessible with the correct toolchain
RUN cargo openvm --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup +nightly component add rust-src
CMD ["/bin/bash"]

View File

@@ -23,7 +23,4 @@ RUN /tmp/install_pico_sdk.sh && rm /tmp/install_pico_sdk.sh # Clean up the scrip
# Verify Pico installation
RUN echo "Verifying Pico installation in Dockerfile (post-script)..." && cargo pico --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup +nightly component add rust-src
CMD ["/bin/bash"]

View File

@@ -36,7 +36,4 @@ RUN /tmp/install_risc0_sdk.sh && rm /tmp/install_risc0_sdk.sh
# Verify Risc0 installation (script also does this, but good for Dockerfile sanity)
RUN echo "Verifying Risc0 installation in Dockerfile (post-script)..." && cargo risczero --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup +nightly component add rust-src
CMD ["/bin/bash"]

View File

@@ -23,9 +23,6 @@ ENV PATH="${SP1_DIR}/bin:$PATH"
# Verify SP1 installation (optional here, as script does it, but good for sanity)
RUN cargo prove --version
# Add `rust-src` component to enable std build for nightly rust.
RUN rustup +nightly component add rust-src
# Add Docker CLI
COPY --from=docker:cli /usr/local/bin/docker /usr/local/bin/docker

View File

@@ -47,7 +47,3 @@ fi
# Setup aggregation keys
cargo openvm setup
# Install the toolchain for guest compilation.
rustup install nightly-2025-02-14
rustup component add rust-src --toolchain nightly-2025-02-14