-
Notifications
You must be signed in to change notification settings - Fork 170
[anneal][v2] Add exocrate toolchain setup and Toolchain resolver #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mdittmer
wants to merge
1
commit into
Gusk7sfx53btetxh4h4ew4mg24cjvtya6
Choose a base branch
from
Gbbpbt76nsgp2ohpclea46vot5joxx7b5
base: Gusk7sfx53btetxh4h4ew4mg24cjvtya6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright 2026 The Fuchsia Authors | ||
| // | ||
| // Licensed under the 2-Clause BSD License <LICENSE-BSD or | ||
| // https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0 | ||
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | ||
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | ||
| // This file may not be copied, modified, or distributed except according to | ||
| // those terms. | ||
|
|
||
| use anyhow::Context as _; | ||
|
|
||
| pub struct SetupArgs { | ||
| pub local_archive: Option<std::path::PathBuf>, | ||
| } | ||
|
|
||
| exocrate::config! { | ||
| pub const CONFIG: Config = Config { | ||
| rel_dir_path: [".anneal", "toolchain"], | ||
| versioned_files: &["../Cargo.toml", "../Cargo.lock"], | ||
| }; | ||
| } | ||
|
|
||
| exocrate::parse_remote_archive! { | ||
| pub const REMOTE: RemoteArchive = "Cargo.toml" [ | ||
| (linux, x86_64), | ||
| (macos, x86_64), | ||
| (linux, aarch64), | ||
| (macos, aarch64), | ||
| ]; | ||
| } | ||
|
|
||
| pub enum Tool { | ||
| Cargo, | ||
| Charon, | ||
| Rustc, | ||
| } | ||
|
|
||
| impl Tool { | ||
| pub fn name(&self) -> &'static str { | ||
| match self { | ||
| Self::Cargo => "cargo", | ||
| Self::Charon => "charon", | ||
| Self::Rustc => "rustc", | ||
| } | ||
| } | ||
|
|
||
| pub fn path(&self, toolchain: &Toolchain) -> std::path::PathBuf { | ||
| match self { | ||
| Self::Cargo | Self::Rustc => toolchain.rust_bin().join(self.name()), | ||
| Self::Charon => toolchain.aeneas_bin_dir().join(self.name()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const AENEAS_DIR: &str = "aeneas"; | ||
| const RUST_SYSROOT: &str = "rust"; | ||
| const AENEAS_BIN_DIR: &str = "bin"; | ||
| const RUST_BIN_DIR: &str = "bin"; | ||
| const RUST_LIB_DIR: &str = "lib"; | ||
|
|
||
| pub struct Toolchain { | ||
| root: std::path::PathBuf, | ||
| } | ||
|
|
||
| impl Toolchain { | ||
| pub fn resolve() -> anyhow::Result<Self> { | ||
| let location = resolve_location(); | ||
| let root = CONFIG | ||
| .resolve_installation_dir(location) | ||
| .context("Toolchain not installed. Please run 'cargo anneal setup' first.")?; | ||
| Ok(Self { root }) | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "exocrate_tests"))] | ||
| pub fn root(&self) -> &std::path::Path { | ||
| &self.root | ||
| } | ||
|
|
||
| pub fn aeneas_bin_dir(&self) -> std::path::PathBuf { | ||
| self.root.join(AENEAS_DIR).join(AENEAS_BIN_DIR) | ||
| } | ||
|
|
||
| pub fn rust_sysroot(&self) -> std::path::PathBuf { | ||
| self.root.join(RUST_SYSROOT) | ||
| } | ||
|
|
||
| pub fn rust_bin(&self) -> std::path::PathBuf { | ||
| self.rust_sysroot().join(RUST_BIN_DIR) | ||
| } | ||
|
|
||
| pub fn rust_lib(&self) -> std::path::PathBuf { | ||
| self.rust_sysroot().join(RUST_LIB_DIR) | ||
| } | ||
|
|
||
| pub fn command(&self, tool: Tool) -> anyhow::Result<std::process::Command> { | ||
| let mut cmd = std::process::Command::new(tool.path(self)); | ||
| cmd.env_clear(); | ||
| match tool { | ||
| Tool::Cargo | Tool::Rustc => {} | ||
| Tool::Charon => { | ||
| // The archive supplies Rust tools, but not host build tools | ||
| // such as the linker. Keep the caller's `PATH` after our Rust | ||
| // bin directory so Cargo builds use the managed Rust toolchain | ||
| // while still finding those host tools. | ||
| cmd.env("CHARON_TOOLCHAIN_IS_IN_PATH", "1") | ||
| .env("PATH", prepend_current_path(self.rust_bin())?) | ||
| .env(rust_library_path_env_var(), self.rust_lib()); | ||
| } | ||
| } | ||
| Ok(cmd) | ||
| } | ||
| } | ||
|
|
||
| fn prepend_current_path(path: std::path::PathBuf) -> anyhow::Result<std::ffi::OsString> { | ||
| let mut paths = vec![path]; | ||
| if let Some(current_path) = std::env::var_os("PATH") { | ||
| paths.extend(std::env::split_paths(¤t_path)); | ||
| } | ||
| std::env::join_paths(paths).context("failed to construct PATH for tool command") | ||
| } | ||
|
|
||
| /// Returns the platform library search path variable used by Rust tools. | ||
| pub(crate) fn rust_library_path_env_var() -> &'static str { | ||
| if cfg!(target_os = "macos") { "DYLD_LIBRARY_PATH" } else { "LD_LIBRARY_PATH" } | ||
| } | ||
|
|
||
| pub fn run_setup(args: SetupArgs) -> anyhow::Result<()> { | ||
| let location = resolve_location(); | ||
| let source = match args.local_archive { | ||
| Some(local_archive) => exocrate::Source::Local(local_archive), | ||
| None => exocrate::Source::Remote(REMOTE), | ||
| }; | ||
|
|
||
| let (installation_dir, status) = CONFIG | ||
| .resolve_installation_dir_or_install(location, source) | ||
| .context("failed to resolve-or-install dependencies")?; | ||
| match status { | ||
| exocrate::ResolvedOrInstalled::ResolvedExisting => { | ||
| log::warn!("anneal toolchain was already installed at {:?}", installation_dir); | ||
| } | ||
| exocrate::ResolvedOrInstalled::NewlyInstalled => { | ||
| log::info!("anneal toolchain freshly installed at {:?}", installation_dir); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn resolve_location() -> exocrate::Location { | ||
| if std::env::var("__ANNEAL_LOCAL_DEV").is_ok() { | ||
| exocrate::Location::LocalDev | ||
| } else { | ||
| exocrate::Location::UserGlobal | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.