|
| 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 | + Cargo, |
| 34 | + Charon, |
| 35 | + Rustc, |
| 36 | +} |
| 37 | + |
| 38 | +impl Tool { |
| 39 | + pub fn name(&self) -> &'static str { |
| 40 | + match self { |
| 41 | + Self::Cargo => "cargo", |
| 42 | + Self::Charon => "charon", |
| 43 | + Self::Rustc => "rustc", |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn path(&self, toolchain: &Toolchain) -> std::path::PathBuf { |
| 48 | + match self { |
| 49 | + Self::Cargo | Self::Rustc => toolchain.rust_bin().join(self.name()), |
| 50 | + Self::Charon => toolchain.aeneas_bin_dir().join(self.name()), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +const AENEAS_DIR: &str = "aeneas"; |
| 56 | +const RUST_SYSROOT: &str = "rust"; |
| 57 | +const AENEAS_BIN_DIR: &str = "bin"; |
| 58 | +const RUST_BIN_DIR: &str = "bin"; |
| 59 | +const RUST_LIB_DIR: &str = "lib"; |
| 60 | + |
| 61 | +pub struct Toolchain { |
| 62 | + root: std::path::PathBuf, |
| 63 | +} |
| 64 | + |
| 65 | +impl Toolchain { |
| 66 | + pub fn resolve() -> anyhow::Result<Self> { |
| 67 | + let location = resolve_location(); |
| 68 | + let root = CONFIG |
| 69 | + .resolve_installation_dir(location) |
| 70 | + .context("Toolchain not installed. Please run 'cargo anneal setup' first.")?; |
| 71 | + Ok(Self { root }) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn root(&self) -> &std::path::Path { |
| 75 | + &self.root |
| 76 | + } |
| 77 | + |
| 78 | + pub fn aeneas_bin_dir(&self) -> std::path::PathBuf { |
| 79 | + self.root.join(AENEAS_DIR).join(AENEAS_BIN_DIR) |
| 80 | + } |
| 81 | + |
| 82 | + pub fn rust_sysroot(&self) -> std::path::PathBuf { |
| 83 | + self.root.join(RUST_SYSROOT) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn rust_bin(&self) -> std::path::PathBuf { |
| 87 | + self.rust_sysroot().join(RUST_BIN_DIR) |
| 88 | + } |
| 89 | + |
| 90 | + pub fn rust_lib(&self) -> std::path::PathBuf { |
| 91 | + self.rust_sysroot().join(RUST_LIB_DIR) |
| 92 | + } |
| 93 | + |
| 94 | + pub fn command(&self, tool: Tool) -> anyhow::Result<std::process::Command> { |
| 95 | + let mut cmd = std::process::Command::new(tool.path(self)); |
| 96 | + cmd.env_clear(); |
| 97 | + match tool { |
| 98 | + Tool::Cargo | Tool::Rustc => {} |
| 99 | + Tool::Charon => { |
| 100 | + // The archive supplies Rust tools, but not host build tools |
| 101 | + // such as the linker. Keep the caller's `PATH` after our Rust |
| 102 | + // bin directory so Cargo builds use the managed Rust toolchain |
| 103 | + // while still finding those host tools. |
| 104 | + cmd.env("CHARON_TOOLCHAIN_IS_IN_PATH", "1") |
| 105 | + .env("PATH", prepend_current_path(self.rust_bin())?) |
| 106 | + .env(rust_library_path_env_var(), self.rust_lib()); |
| 107 | + } |
| 108 | + } |
| 109 | + Ok(cmd) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn prepend_current_path(path: std::path::PathBuf) -> anyhow::Result<std::ffi::OsString> { |
| 114 | + let mut paths = vec![path]; |
| 115 | + if let Some(current_path) = std::env::var_os("PATH") { |
| 116 | + paths.extend(std::env::split_paths(¤t_path)); |
| 117 | + } |
| 118 | + std::env::join_paths(paths).context("failed to construct PATH for tool command") |
| 119 | +} |
| 120 | + |
| 121 | +/// Returns the platform library search path variable used by Rust tools. |
| 122 | +pub(crate) fn rust_library_path_env_var() -> &'static str { |
| 123 | + if cfg!(target_os = "macos") { "DYLD_LIBRARY_PATH" } else { "LD_LIBRARY_PATH" } |
| 124 | +} |
| 125 | + |
| 126 | +pub fn run_setup(args: SetupArgs) -> anyhow::Result<()> { |
| 127 | + let location = resolve_location(); |
| 128 | + let source = match args.local_archive { |
| 129 | + Some(local_archive) => exocrate::Source::Local(local_archive), |
| 130 | + None => exocrate::Source::Remote(REMOTE), |
| 131 | + }; |
| 132 | + |
| 133 | + let (installation_dir, status) = CONFIG |
| 134 | + .resolve_installation_dir_or_install(location, source) |
| 135 | + .context("failed to resolve-or-install dependencies")?; |
| 136 | + if status == exocrate::ResolvedOrInstalled::ResolvedExisting { |
| 137 | + log::warn!("anneal toolchain was already installed at {:?}", installation_dir); |
| 138 | + } else { |
| 139 | + log::info!("anneal toolchain freshly installed at {:?}", installation_dir); |
| 140 | + } |
| 141 | + Ok(()) |
| 142 | +} |
| 143 | + |
| 144 | +fn resolve_location() -> exocrate::Location { |
| 145 | + if std::env::var("__ANNEAL_LOCAL_DEV").is_ok() { |
| 146 | + exocrate::Location::LocalDev |
| 147 | + } else { |
| 148 | + exocrate::Location::UserGlobal |
| 149 | + } |
| 150 | +} |
0 commit comments