Skip to content

Commit ce4a881

Browse files
committed
windows: Hint windows user to install latest vc-redist if host vc-redist is lower than 14.44
Signed-off-by: Eval EXEC <[email protected]>
1 parent 815f8d2 commit ce4a881

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

Cargo.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ ckb-bin.workspace = true
2828
ckb-test.workspace = true
2929
console-subscriber = { workspace = true, optional = true }
3030

31+
[target.'cfg(target_env = "msvc")'.dependencies]
32+
winreg = "0.55"
33+
3134
[target.'cfg(all(not(target_env = "msvc"), not(target_os="macos")))'.dependencies]
3235
tikv-jemallocator = { version = "0.5.0", features = [
3336
"unprefixed_malloc_on_supported_platforms",

src/main.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,78 @@ fn main() {
1010
#[cfg(feature = "tokio-trace")]
1111
console_subscriber::init();
1212

13+
#[cfg(target_os = "windows")]
14+
check_msvc_version();
15+
1316
let version = get_version();
1417
if let Some(exit_code) = run_app(version).err() {
1518
::std::process::exit(exit_code.into());
1619
}
1720
}
1821

22+
#[cfg(target_os = "windows")]
23+
fn check_msvc_version() {
24+
use winreg::RegKey;
25+
use winreg::enums::*;
26+
// if users msvc version less than 14.44, print a warning
27+
28+
fn get_vc_redist_version(arch: &str) -> io::Result<Option<String>> {
29+
// arch: "x64" or "x86"
30+
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
31+
let key_path = format!(
32+
r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\{}",
33+
arch
34+
);
35+
match hklm.open_subkey(&key_path) {
36+
Ok(key) => {
37+
let version: String = key.get_value("Version")?;
38+
Ok(Some(version))
39+
}
40+
Err(_) => Ok(None),
41+
}
42+
}
43+
44+
fn version_string_to_tuple(ver: &str) -> Option<(u32, u32, u32, u32)> {
45+
let parts: Vec<&str> = ver.split('.').collect();
46+
if parts.len() >= 4 {
47+
let major = parts[0].parse().ok()?;
48+
let minor = parts[1].parse().ok()?;
49+
let bld = parts[2].parse().ok()?;
50+
let rbld = parts[3].parse().ok()?;
51+
Some((major, minor, bld, rbld))
52+
} else {
53+
None
54+
}
55+
}
56+
fn is_version_at_least(current: &str, threshold: &str) -> bool {
57+
if let (Some(cur), Some(thr)) = (
58+
version_string_to_tuple(current),
59+
version_string_to_tuple(threshold),
60+
) {
61+
cur >= thr
62+
} else {
63+
false
64+
}
65+
}
66+
67+
if let Some(version) = get_vc_redist_version("x64")? {
68+
eprintln!("Detected VC++ Redistributable version (x64): {}", version);
69+
let threshold = "14.44.0.0";
70+
if !is_version_at_least(&version, threshold) {
71+
eprintln!(
72+
"Version is below {}. Please download/upgrade the Visual C++ Redistributable. Help: https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#latest-supported-redistributable-version ",
73+
threshold
74+
);
75+
} else {
76+
eprintln!("MSVC Version: {} meets the requirement.", version);
77+
}
78+
} else {
79+
eprintln!(
80+
"Visual C++ Redistributable version not found. Please install it. Help: https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#latest-supported-redistributable-version"
81+
);
82+
}
83+
}
84+
1985
#[allow(unexpected_cfgs)]
2086
fn get_version() -> Version {
2187
let major = env!("CARGO_PKG_VERSION_MAJOR")

0 commit comments

Comments
 (0)