Skip to content

Commit 65e08a7

Browse files
committed
fix get_min_version() for completeness
1 parent 60e51ce commit 65e08a7

2 files changed

Lines changed: 34 additions & 28 deletions

File tree

clang-installer/src/downloader/static_dist.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub enum StaticDistDownloadError {
4242
#[error("Failed to parse the SHA512 sum file")]
4343
Sha512Corruption,
4444
}
45-
const MIN_CLANG_TOOLS_VERSION: &str = "9";
46-
const MAX_CLANG_TOOLS_VERSION: &str = "21";
45+
const MIN_CLANG_TOOLS_VERSION: u8 = 9;
46+
pub(crate) const MAX_CLANG_TOOLS_VERSION: u8 = 21;
4747
const CLANG_TOOLS_REPO: &str = "https://github.com/cpp-linter/clang-tools-static-binaries";
4848
const CLANG_TOOLS_TAG: &str = "master-6e612956";
4949

@@ -54,21 +54,34 @@ pub struct StaticDistDownloader;
5454
impl Cacher for StaticDistDownloader {}
5555

5656
impl StaticDistDownloader {
57+
pub fn get_major_version_range() -> RangeInclusive<u8> {
58+
let min_clang_tools_version: u8 = option_env!("MIN_CLANG_TOOLS_VERSION")
59+
.and_then(|v| match v.parse::<u8>() {
60+
Ok(parsed) => Some(parsed),
61+
Err(e) => {
62+
log::error!("Invalid MIN_CLANG_TOOLS_VERSION env var value: {v}. Error: {e}");
63+
None
64+
}
65+
})
66+
.unwrap_or(MIN_CLANG_TOOLS_VERSION);
67+
let max_clang_tools_version: u8 = option_env!("MAX_CLANG_TOOLS_VERSION")
68+
.and_then(|v| match v.parse::<u8>() {
69+
Ok(parsed) => Some(parsed),
70+
Err(e) => {
71+
log::error!("Invalid MAX_CLANG_TOOLS_VERSION env var value: {v}. Error: {e}");
72+
None
73+
}
74+
})
75+
.unwrap_or(MAX_CLANG_TOOLS_VERSION);
76+
min_clang_tools_version..=max_clang_tools_version
77+
}
78+
5779
/// Finds a suitable version from `req_ver` within the range of available clang tools versions.
5880
///
5981
/// The available versions are determined by the `MIN_CLANG_TOOLS_VERSION` and
6082
/// `MAX_CLANG_TOOLS_VERSION` environment variables (inclusive) at compile time.
6183
fn find_suitable_version(req_ver: &VersionReq) -> Option<Version> {
62-
let min_clang_tools_version: u8 = option_env!("MIN_CLANG_TOOLS_VERSION")
63-
.unwrap_or(MIN_CLANG_TOOLS_VERSION)
64-
.parse()
65-
.expect("Invalid MIN_CLANG_TOOLS_VERSION env var value");
66-
let max_clang_tools_version: u8 = option_env!("MAX_CLANG_TOOLS_VERSION")
67-
.unwrap_or(MAX_CLANG_TOOLS_VERSION)
68-
.parse()
69-
.expect("Invalid MAX_CLANG_TOOLS_VERSION env var value");
70-
let clang_tools_versions: RangeInclusive<u8> =
71-
min_clang_tools_version..=max_clang_tools_version;
84+
let clang_tools_versions: RangeInclusive<u8> = Self::get_major_version_range();
7285
let outlier = Version::new(12, 0, 1);
7386
for ver in clang_tools_versions
7487
.map(|v| Version::new(v as u64, 0, 0))

clang-installer/src/version.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub enum GetToolError {
4747
#[error("Failed to parse version: {0}")]
4848
VersionParseError(String),
4949

50-
/// The version requirement does not specify a major version.
51-
#[error("The version requirement does not specify a major version")]
52-
VersionMajorRequired,
50+
/// The version requirement does satisfy any known/supported clang version
51+
#[error("The version requirement does satisfy any known/supported clang version")]
52+
UnsupportedVersion,
5353

5454
/// Binary executable in cache has no parent directory.
5555
#[error("Binary executable in cache has no parent directory")]
@@ -116,7 +116,7 @@ impl RequestedVersion {
116116

117117
// check if cache has a suitable version
118118
let bin_ext = if cfg!(windows) { ".exe" } else { "" };
119-
let min_ver = get_min_ver(version_req).ok_or(GetToolError::VersionMajorRequired)?;
119+
let min_ver = get_min_ver(version_req).ok_or(GetToolError::UnsupportedVersion)?;
120120
let cached_bin = StaticDistDownloader::get_cache_dir()
121121
.join("bin")
122122
.join(format!("{tool}-{min_ver}{bin_ext}"));
@@ -181,18 +181,11 @@ impl RequestedVersion {
181181

182182
pub fn get_min_ver(version_req: &VersionReq) -> Option<Version> {
183183
let mut result = None;
184-
for cmp in &version_req.comparators {
185-
if matches!(cmp.op, semver::Op::Exact | semver::Op::Caret) {
186-
let ver = Version {
187-
major: cmp.major,
188-
minor: cmp.minor.unwrap_or(0),
189-
patch: cmp.patch.unwrap_or(0),
190-
pre: cmp.pre.clone(),
191-
build: Default::default(),
192-
};
193-
if result.as_ref().is_none_or(|r| ver < *r) {
194-
result = Some(ver);
195-
}
184+
let supported_version_range = StaticDistDownloader::get_major_version_range();
185+
for major in supported_version_range.rev() {
186+
let ver = Version::new(major as u64, 0, 0);
187+
if version_req.matches(&ver) {
188+
result = Some(ver);
196189
}
197190
}
198191
result

0 commit comments

Comments
 (0)