sp1_cli/commands/
build_toolchain.rs

1use anyhow::{Context, Result};
2use clap::Parser;
3use std::{path::PathBuf, process::Command};
4
5use crate::{
6    get_target, CommandExecutor, LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG, RUSTUP_TOOLCHAIN_NAME,
7};
8
9#[derive(Parser)]
10#[command(name = "build-toolchain", about = "Build the cargo-prove toolchain.")]
11pub struct BuildToolchainCmd {}
12
13impl BuildToolchainCmd {
14    pub fn run(&self) -> Result<()> {
15        // Get environment variables.
16        let github_access_token = std::env::var("GITHUB_ACCESS_TOKEN");
17        let build_dir = std::env::var("SP1_BUILD_DIR");
18
19        // Clone our rust fork, if necessary.
20        let rust_dir = match build_dir {
21            Ok(build_dir) => {
22                println!("Detected SP1_BUILD_DIR, skipping cloning rust.");
23                PathBuf::from(build_dir).join("rust")
24            }
25            Err(_) => {
26                let temp_dir = std::env::temp_dir();
27                let dir = temp_dir.join("sp1-rust");
28                if dir.exists() {
29                    std::fs::remove_dir_all(&dir)?;
30                }
31
32                println!("No SP1_BUILD_DIR detected, cloning rust.");
33                let repo_url = match github_access_token {
34                    Ok(github_access_token) => {
35                        println!("Detected GITHUB_ACCESS_TOKEN, using it to clone rust.");
36                        format!("https://{github_access_token}@github.com/succinctlabs/rust")
37                    }
38                    Err(_) => {
39                        println!("No GITHUB_ACCESS_TOKEN detected. If you get throttled by Github, set it to bypass the rate limit.");
40                        "ssh://git@github.com/succinctlabs/rust".to_string()
41                    }
42                };
43                Command::new("git")
44                    .args([
45                        "clone",
46                        &repo_url,
47                        "--depth=1",
48                        "--single-branch",
49                        &format!("--branch={LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG}"),
50                        "sp1-rust",
51                    ])
52                    .current_dir(&temp_dir)
53                    .run()?;
54                Command::new("git").args(["reset", "--hard"]).current_dir(&dir).run()?;
55                Command::new("git")
56                    .args(["submodule", "update", "--init", "--recursive", "--progress"])
57                    .current_dir(&dir)
58                    .run()?;
59                dir
60            }
61        };
62
63        // Install our bootstrap.toml.
64        let bootstrap_toml = include_str!("bootstrap.toml");
65        let bootstrap_file = rust_dir.join("bootstrap.toml");
66        std::fs::write(&bootstrap_file, bootstrap_toml)
67            .with_context(|| format!("while writing configuration to {bootstrap_file:?}"))?;
68
69        // Work around target sanity check added in
70        // rust-lang/rust@09c076810cb7649e5817f316215010d49e78e8d7.
71        let temp_dir = std::env::temp_dir().join("rustc-targets");
72        if !temp_dir.exists() {
73            std::fs::create_dir_all(&temp_dir)?;
74        }
75        std::fs::File::create(temp_dir.join("riscv32im-succinct-zkvm-elf.json"))?;
76
77        // Build the toolchain.
78        Command::new("python3")
79            .env("RUST_TARGET_PATH", &temp_dir)
80            .env("CARGO_TARGET_RISCV32IM_SUCCINCT_ZKVM_ELF_RUSTFLAGS", "-Cpasses=lower-atomic")
81            .args([
82                "x.py",
83                "build",
84                "--stage",
85                "2",
86                "compiler/rustc",
87                "library",
88                "--target",
89                &format!("riscv32im-succinct-zkvm-elf,{}", get_target()),
90            ])
91            .current_dir(&rust_dir)
92            .run()?;
93
94        // Remove the existing toolchain from rustup, if it exists.
95        match Command::new("rustup").args(["toolchain", "remove", RUSTUP_TOOLCHAIN_NAME]).run() {
96            Ok(_) => println!("Successfully removed existing toolchain."),
97            Err(_) => println!("No existing toolchain to remove."),
98        }
99
100        // Find the toolchain directory.
101        let mut toolchain_dir = None;
102        for wentry in std::fs::read_dir(rust_dir.join("build"))? {
103            let entry = wentry?;
104            let toolchain_dir_candidate = entry.path().join("stage2");
105            if toolchain_dir_candidate.is_dir() {
106                toolchain_dir = Some(toolchain_dir_candidate);
107                break;
108            }
109        }
110        let toolchain_dir = toolchain_dir.unwrap();
111        println!(
112            "Found built toolchain directory at {}.",
113            toolchain_dir.as_path().to_str().unwrap()
114        );
115
116        // Link the toolchain to rustup.
117        Command::new("rustup")
118            .args(["toolchain", "link", RUSTUP_TOOLCHAIN_NAME])
119            .arg(&toolchain_dir)
120            .run()?;
121        println!("Successfully linked the toolchain to rustup.");
122
123        // Compressing toolchain directory to tar.gz.
124        let target = get_target();
125        let tar_gz_path = format!("rust-toolchain-{target}.tar.gz");
126        Command::new("tar")
127            .args([
128                "--exclude",
129                "lib/rustlib/src",
130                "--exclude",
131                "lib/rustlib/rustc-src",
132                "-hczvf",
133                &tar_gz_path,
134                "-C",
135                toolchain_dir.to_str().unwrap(),
136                ".",
137            ])
138            .run()?;
139        println!("Successfully compressed the toolchain to {tar_gz_path}.");
140
141        Ok(())
142    }
143}