|
| 1 | +// Copyright 2026 The Fuchsia Authors |
| 2 | +// |
| 3 | +// Licensed under the 2-Clause BSD License <LICENSE-BSD or |
| 4 | +// https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0 |
| 5 | +// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 6 | +// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 7 | +// This file may not be copied, modified, or distributed except according to |
| 8 | +// those terms. |
| 9 | + |
| 10 | +use anyhow::Context as _; |
| 11 | + |
| 12 | +pub struct SetupArgs { |
| 13 | + pub local_archive: Option<std::path::PathBuf>, |
| 14 | +} |
| 15 | + |
| 16 | +exocrate::config! { |
| 17 | + pub const CONFIG: Config = Config { |
| 18 | + rel_dir_path: [".anneal", "toolchain"], |
| 19 | + versioned_files: &["../Cargo.toml", "../Cargo.lock"], |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +exocrate::parse_remote_archive! { |
| 24 | + pub const REMOTE: RemoteArchive = "Cargo.toml" [ |
| 25 | + (linux, x86_64), |
| 26 | + (macos, x86_64), |
| 27 | + (linux, aarch64), |
| 28 | + (macos, aarch64), |
| 29 | + ]; |
| 30 | +} |
| 31 | + |
| 32 | +pub enum Tool { |
| 33 | + Charon, |
| 34 | +} |
| 35 | + |
| 36 | +impl Tool { |
| 37 | + pub fn name(&self) -> &'static str { |
| 38 | + match self { |
| 39 | + Self::Charon => "charon", |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn path(&self, toolchain: &Toolchain) -> std::path::PathBuf { |
| 44 | + match self { |
| 45 | + Self::Charon => toolchain.aeneas_bin_dir().join(self.name()), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const AENEAS_DIR: &str = "aeneas"; |
| 51 | +const RUST_SYSROOT: &str = "rust"; |
| 52 | +const AENEAS_BIN_DIR: &str = "bin"; |
| 53 | +const RUST_BIN_DIR: &str = "bin"; |
| 54 | +const RUST_LIB_DIR: &str = "lib"; |
| 55 | + |
| 56 | +pub struct Toolchain { |
| 57 | + root: std::path::PathBuf, |
| 58 | +} |
| 59 | + |
| 60 | +impl Toolchain { |
| 61 | + pub fn resolve() -> anyhow::Result<Self> { |
| 62 | + let location = if std::env::var("__ANNEAL_LOCAL_DEV").is_ok() { |
| 63 | + exocrate::Location::LocalDev |
| 64 | + } else { |
| 65 | + exocrate::Location::UserGlobal |
| 66 | + }; |
| 67 | + let root = CONFIG |
| 68 | + .resolve_installation_dir(location) |
| 69 | + .context("Toolchain not installed. Please run 'cargo anneal setup' first.")?; |
| 70 | + Ok(Self { root }) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn root(&self) -> &std::path::Path { |
| 74 | + &self.root |
| 75 | + } |
| 76 | + |
| 77 | + pub fn aeneas_bin_dir(&self) -> std::path::PathBuf { |
| 78 | + self.root.join(AENEAS_DIR).join(AENEAS_BIN_DIR) |
| 79 | + } |
| 80 | + |
| 81 | + pub fn rust_sysroot(&self) -> std::path::PathBuf { |
| 82 | + self.root.join(RUST_SYSROOT) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn rust_bin(&self) -> std::path::PathBuf { |
| 86 | + self.rust_sysroot().join(RUST_BIN_DIR) |
| 87 | + } |
| 88 | + |
| 89 | + pub fn rust_lib(&self) -> std::path::PathBuf { |
| 90 | + self.rust_sysroot().join(RUST_LIB_DIR) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn command(&self, tool: Tool) -> std::process::Command { |
| 94 | + std::process::Command::new(tool.path(self)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +pub fn run_setup(args: SetupArgs) -> anyhow::Result<()> { |
| 99 | + let location = if std::env::var("__ANNEAL_LOCAL_DEV").is_ok() { |
| 100 | + exocrate::Location::LocalDev |
| 101 | + } else { |
| 102 | + exocrate::Location::UserGlobal |
| 103 | + }; |
| 104 | + let source = match args.local_archive { |
| 105 | + Some(local_archive) => exocrate::Source::Local(local_archive), |
| 106 | + None => exocrate::Source::Remote(REMOTE), |
| 107 | + }; |
| 108 | + |
| 109 | + let installation_dir = CONFIG |
| 110 | + .resolve_installation_dir_or_install(location, source) |
| 111 | + .context("failed to resolve-or-install dependencies")?; |
| 112 | + log::info!("anneal toolchain is installed at {:?}", installation_dir); |
| 113 | + Ok(()) |
| 114 | +} |
0 commit comments