Skip to content

Commit

Permalink
Support for stable host toolchain (risc0#817)
Browse files Browse the repository at this point in the history
* Switch to `stable` channel in `rust-toolchain.toml`
* Adjust `risc0-build` to support any host-side rustc version.
* Download LLVM source for build-toolchain
* `risc0-build` uses env vars to make debugging toolchains easier
  • Loading branch information
flaub authored Aug 27, 2023
1 parent 0379f7f commit b498f9a
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 173 deletions.
2 changes: 1 addition & 1 deletion bonsai/ethereum-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bincode = "1.3"
bonsai-ethereum-contracts = { workspace = true }
bonsai-rest-api-mock = { workspace = true }
bonsai-sdk = { workspace = true, features = ["async"] }
clap = { version = "4.0, <4.4.0", features = ["derive", "env"] }
clap = { version = "4.4", features = ["derive", "env"] }
displaydoc = "0.2"
ethers = { version = "2.0", features = ["rustls", "ws", "ethers-solc"] }
ethers-signers = { version = "2.0", features = ["aws"] }
Expand Down
22 changes: 11 additions & 11 deletions bonsai/examples/governance/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 bonsai/examples/governance/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "nightly-2023-03-06"
components = [ "rustfmt", "rust-src" ]
channel = "stable"
components = ["rustfmt", "rust-src"]
profile = "minimal"
33 changes: 16 additions & 17 deletions examples/Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
default-run = "ecdsa-example"

[dependencies]
clap = "4.0, <4.4.0"
clap = "4.4"
ecdsa-methods = { path = "methods" }
k256 = { version = "0.13", features = ["serde"] }
rand_core = "0.6.4"
Expand Down
54 changes: 45 additions & 9 deletions risc0/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,16 @@ fn guest_packages(pkg: &Package) -> Vec<Package> {
.collect()
}

fn is_debug() -> bool {
get_env_var("RISC0_BUILD_DEBUG") == "1"
}

/// Returns all methods associated with the given riscv guest package.
fn guest_methods<P>(pkg: &Package, target_dir: P) -> Vec<Risc0Method>
where
P: AsRef<Path>,
{
let profile = if is_debug() { "debug" } else { "release" };
pkg.targets
.iter()
.filter(|target| target.kind.iter().any(|kind| kind == "bin"))
Expand All @@ -185,21 +190,32 @@ where
elf_path: target_dir
.as_ref()
.join("riscv32im-risc0-zkvm-elf")
.join("release")
.join(profile)
.join(&target.name),
})
.collect()
}

fn get_env_var(name: &str) -> String {
println!("cargo:rerun-if-env-changed={name}");
env::var(name).unwrap_or_default()
}

fn sanitized_cmd(tool: &str) -> Command {
let mut cmd = Command::new(tool);
for (key, _) in env::vars().filter(|x| x.0.starts_with("CARGO") || x.0.starts_with("RUSTUP")) {
cmd.env_remove(key);
}
cmd
}

// Builds a package that targets the riscv guest into the specified target
// directory.
fn build_guest_package<P>(pkg: &Package, target_dir: P, features: Vec<String>)
where
P: AsRef<Path>,
{
let skip_var_name = "RISC0_SKIP_BUILD";
println!("cargo:rerun-if-env-changed={}", skip_var_name);
if env::var(skip_var_name).is_ok() {
if !get_env_var("RISC0_SKIP_BUILD").is_empty() {
return;
}

Expand All @@ -208,27 +224,47 @@ where
let mut args = vec![
"+risc0",
"build",
"--release",
"--target",
"riscv32im-risc0-zkvm-elf",
"--manifest-path",
pkg.manifest_path.as_str(),
"--target-dir",
target_dir.as_ref().to_str().unwrap(),
];

if !is_debug() {
args.push("--release");
}

let features_str = features.join(",");
if !features.is_empty() {
args.push("--features");
args.push(&features_str);
}
println!("Building guest package: cargo {}", args.join(" "));

let mut cmd = Command::new("cargo");
for (key, _) in env::vars().filter(|x| x.0.starts_with("CARGO") || x.0.starts_with("RUSTUP")) {
cmd.env_remove(key);
let rustc = sanitized_cmd("rustup")
.args(["+risc0", "which", "rustc"])
.output()
.unwrap()
.stdout;
let rustc = String::from_utf8(rustc).unwrap();
let rustc = rustc.trim();

let mut cmd = sanitized_cmd("cargo");

let rust_src = get_env_var("RISC0_RUST_SRC");
if !rust_src.is_empty() {
args.push("-Z");
args.push("build-std=alloc,core,proc_macro,panic_abort,std");
args.push("-Z");
args.push("build-std-features=compiler-builtins-mem");
cmd.env("__CARGO_TESTS_ONLY_SRC_ROOT", rust_src);
}

println!("Building guest package: cargo {}", args.join(" "));

let mut child = cmd
.env("RUSTC", rustc)
.env(
"CARGO_ENCODED_RUSTFLAGS",
[
Expand Down
2 changes: 1 addition & 1 deletion risc0/cargo-risczero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "cargo-risczero"
anyhow = { version = "1.0", features = ["backtrace"] }
cargo-generate = "0.18"
cargo_metadata = "0.17"
clap = { version = "4.0, <4.4.0", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
const_format = "0.2"
dirs = "5.0"
docker-generate = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions risc0/cargo-risczero/src/commands/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ configure-args = []
[rust]
lld = true
llvm-tools = true

[llvm]
download-ci-llvm = false
Loading

0 comments on commit b498f9a

Please sign in to comment.