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