sp1_cli/
lib.rs

1pub mod commands;
2
3use anyhow::{Context, Result};
4use reqwest::Client;
5use std::process::{Command, Stdio};
6
7pub const RUSTUP_TOOLCHAIN_NAME: &str = "succinct";
8
9/// The latest version (github tag) of the toolchain that is supported by our build system.
10///
11/// This tag has support for older x86 libc versions (like the one found in Ubuntu 20.04).
12/// This tag has support for the recent Macos and ARM targets.
13pub const LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG: &str = "succinct-1.85.0";
14
15pub const SP1_VERSION_MESSAGE: &str =
16    concat!("sp1", " (", env!("VERGEN_GIT_SHA"), " ", env!("VERGEN_BUILD_TIMESTAMP"), ")");
17
18trait CommandExecutor {
19    fn run(&mut self) -> Result<()>;
20}
21
22impl CommandExecutor for Command {
23    fn run(&mut self) -> Result<()> {
24        self.stderr(Stdio::inherit())
25            .stdout(Stdio::inherit())
26            .stdin(Stdio::inherit())
27            .output()
28            .with_context(|| format!("while executing `{:?}`", &self))
29            .map(|_| ())
30    }
31}
32
33pub async fn url_exists(client: &Client, url: &str) -> bool {
34    let res = client.head(url).send().await;
35    res.is_ok()
36}
37
38#[allow(unreachable_code)]
39pub fn is_supported_target() -> bool {
40    #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
41    return true;
42
43    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
44    return true;
45
46    #[cfg(all(target_arch = "x86_64", target_os = "macos"))]
47    return true;
48
49    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
50    return true;
51
52    false
53}
54
55pub fn get_target() -> String {
56    let mut target: target_lexicon::Triple = target_lexicon::HOST;
57
58    // We don't want to operate on the musl toolchain, even if the CLI was compiled with musl
59    if target.environment == target_lexicon::Environment::Musl {
60        target.environment = target_lexicon::Environment::Gnu;
61    }
62
63    target.to_string()
64}
65
66pub async fn get_toolchain_download_url(client: &Client, target: String) -> String {
67    // Get latest tag from https://api.github.com/repos/succinctlabs/rust/releases/latest
68    // and use it to construct the download URL.
69    let all_releases = client
70        .get("https://api.github.com/repos/succinctlabs/rust/releases")
71        .send()
72        .await
73        .unwrap()
74        .json::<serde_json::Value>()
75        .await
76        .unwrap();
77
78    // Check if the release exists.
79    let _ = all_releases
80        .as_array()
81        .expect("Failed to fetch releases list")
82        .iter()
83        .find(|release| {
84            release["tag_name"].as_str().unwrap() == LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG
85        })
86        .unwrap_or_else(|| {
87            panic!(
88                "No release found for the expected tag: {}",
89                LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG
90            );
91        });
92
93    let url = format!(
94        "https://github.com/succinctlabs/rust/releases/download/{}/rust-toolchain-{}.tar.gz",
95        LATEST_SUPPORTED_TOOLCHAIN_VERSION_TAG, target
96    );
97
98    url
99}