Skip to content

Commit

Permalink
fix: toolchain normalization in gc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Oct 8, 2024
1 parent acf9f4f commit 4dd88de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
14 changes: 5 additions & 9 deletions src/elan/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use toolchain::Toolchain;

use toml;

use crate::{gc, lookup_toolchain_desc};
use crate::{gc, lookup_toolchain_desc, read_toolchain_desc_from_file};

#[derive(Debug, Serialize, Clone)]
pub enum OverrideReason {
Expand Down Expand Up @@ -242,14 +242,10 @@ impl Cfg {

// Then look for 'lean-toolchain'
let toolchain_file = d.join("lean-toolchain");
if let Ok(s) = utils::read_file("toolchain file", &toolchain_file) {
if let Some(s) = s.lines().next() {
let toolchain_name = s.trim();
let desc = lookup_toolchain_desc(self, toolchain_name)?;
let reason = OverrideReason::ToolchainFile(toolchain_file);
gc::add_root(self, d)?;
return Ok(Some((desc, reason)));
}
if let Ok(desc) = read_toolchain_desc_from_file(self, &toolchain_file) {
let reason = OverrideReason::ToolchainFile(toolchain_file);
gc::add_root(self, d)?;
return Ok(Some((desc, reason)));
}

// Then look for 'leanpkg.toml'
Expand Down
17 changes: 10 additions & 7 deletions src/elan/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::{
path::{Path, PathBuf},
};

use elan_dist::dist::ToolchainDesc;
use itertools::Itertools;

use crate::{lookup_toolchain_desc, Cfg, Toolchain};
use crate::{lookup_toolchain_desc, read_toolchain_desc_from_file, Cfg, Toolchain};

fn get_root_file(cfg: &Cfg) -> PathBuf {
cfg.elan_dir.join("known-projects")
Expand Down Expand Up @@ -33,28 +34,30 @@ pub fn add_root(cfg: &Cfg, root: &Path) -> elan_utils::Result<()> {
Ok(())
}

pub fn analyze_toolchains(cfg: &Cfg) -> crate::Result<(Vec<Toolchain>, Vec<(String, String)>)> {
pub fn analyze_toolchains(cfg: &Cfg) -> crate::Result<(Vec<Toolchain>, Vec<(String, ToolchainDesc)>)> {
let roots = get_roots(cfg)?;
let mut used_toolchains = roots
.into_iter()
.filter_map(|r| {
let path = PathBuf::from(r.clone()).join("lean-toolchain");
if path.exists() {
Some((r, std::fs::read_to_string(path).unwrap().trim().to_string()))
if let Ok(desc) = read_toolchain_desc_from_file(cfg, &path) {
Some((r, desc))
} else {
None
}
})
.collect::<Vec<_>>();
if let Some(default) = cfg.get_default()? {
let default = lookup_toolchain_desc(cfg, &default)?;
used_toolchains.push(("default toolchain".to_string(), default.to_string()));
used_toolchains.push(("default toolchain".to_string(), default));
}
if let Some(ref env_override) = cfg.env_override {
used_toolchains.push(("ELAN_TOOLCHAIN".to_string(), env_override.clone()));
if let Ok(desc) = lookup_toolchain_desc(cfg, env_override) {
used_toolchains.push(("ELAN_TOOLCHAIN".to_string(), desc));
}
}
for (path, tc) in cfg.get_overrides()? {
used_toolchains.push((format!("{} (override)", path), tc.to_string()));
used_toolchains.push((format!("{} (override)", path), tc));
}
let used_toolchains_set = used_toolchains.iter().map(|p| p.1.to_string()).collect::<HashSet<_>>();
let unused_toolchains = cfg
Expand Down
10 changes: 10 additions & 0 deletions src/elan/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
lookup_toolchain_desc_ext(cfg, name, false)
}

pub fn read_toolchain_desc_from_file(cfg: &Cfg, toolchain_file: &Path) -> Result<ToolchainDesc> {
let s = utils::read_file("toolchain file", &toolchain_file)?;
if let Some(s) = s.lines().next() {
let toolchain_name = s.trim();
lookup_toolchain_desc(cfg, toolchain_name)
} else {
Err(Error::from(format!("empty toolchain file '{}'", toolchain_file.display())))
}
}

impl<'a> Toolchain<'a> {
pub fn from(cfg: &'a Cfg, desc: &ToolchainDesc) -> Self {
//We need to replace ":" and "/" with "-" in the toolchain name in order to make a name which is a valid
Expand Down

0 comments on commit 4dd88de

Please sign in to comment.