1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#![cfg(target_os = "windows")]
#![forbid(warnings)]
#![forbid(future_incompatible)]
#![deny(unused)]
#![forbid(box_pointers)]
#![forbid(missing_copy_implementations)]
#![forbid(missing_debug_implementations)]
#![forbid(missing_docs)]
#![forbid(trivial_casts)]
#![forbid(trivial_numeric_casts)]
#![forbid(unsafe_code)]
#![forbid(unused_import_braces)]
#![deny(unused_qualifications)]
#![forbid(unused_results)]
#![forbid(variant_size_differences)]
#![cfg_attr(feature = "cargo-clippy", forbid(clippy))]
#![cfg_attr(feature = "cargo-clippy", forbid(clippy_pedantic))]
#![cfg_attr(feature = "cargo-clippy", forbid(clippy_complexity))]
#![cfg_attr(feature = "cargo-clippy", deny(clippy_correctness))]
#![cfg_attr(feature = "cargo-clippy", forbid(clippy_perf))]
#![cfg_attr(feature = "cargo-clippy", forbid(clippy_style))]
#[macro_use]
extern crate serde_derive;
extern crate winreg;
use std::env;
use std::ffi::OsStr;
use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};
use winreg::enums::{KEY_WOW64_32KEY, HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE};
use winreg::RegKey;
const V10_0_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0";
const V8_1A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.1A";
const V8_1_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.1";
const V8_0A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0A";
const V8_0_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0";
const V7_1A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A";
const V7_1_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1";
const V7_0A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0a";
const V7_0_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0";
const V6_1A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.1a";
const V6_1_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.1";
const V6_0A_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0a";
const V6_0_REG_KEY: &str = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SdkVersion {
Any,
Env,
V10_0,
V8_1,
V8_0,
V7_1,
V7_0,
V6_1,
V6_0,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SdkInfo {
installation_folder: PathBuf,
product_name: Option<String>,
product_version: String,
}
impl SdkInfo {
pub fn find(version: SdkVersion) -> io::Result<Option<Self>> {
match version {
SdkVersion::Any => {
use SdkVersion::*;
let vers = [Env, V10_0, V8_1, V8_0, V7_1, V7_0, V6_1, V6_0];
for res in vers.iter().map(|v| Self::find(*v)) {
match res {
Ok(None) => (),
_ => return res,
}
}
Ok(None)
}
SdkVersion::Env => Ok(Self::query_env()),
SdkVersion::V10_0 => Self::query_reg(V10_0_REG_KEY),
SdkVersion::V8_1 => Self::find_double_release((V8_1A_REG_KEY, V8_1_REG_KEY)),
SdkVersion::V8_0 => Self::find_double_release((V8_0A_REG_KEY, V8_0_REG_KEY)),
SdkVersion::V7_1 => Self::find_double_release((V7_1A_REG_KEY, V7_1_REG_KEY)),
SdkVersion::V7_0 => Self::find_double_release((V7_0A_REG_KEY, V7_0_REG_KEY)),
SdkVersion::V6_1 => Self::find_double_release((V6_1A_REG_KEY, V6_1_REG_KEY)),
SdkVersion::V6_0 => Self::find_double_release((V6_0A_REG_KEY, V6_0_REG_KEY)),
}
}
fn find_double_release(keys: (&str, &str)) -> io::Result<Option<Self>> {
let res = Self::query_reg(keys.0);
match res {
Ok(None) => Self::query_reg(keys.1),
_ => res,
}
}
fn query_env() -> Option<Self> {
env::var_os("WindowsSdkDir")
.and_then(|install_dir| {
env::var_os("WindowsSdkVersion").map(|version| (install_dir, version))
})
.map(|(install_dir, version)| {
let ver = version
.into_string()
.map(|s| {
s.split(r".0\")
.next()
.expect("`str::split` failed")
.to_owned()
})
.expect("`WindowsSdkVersion` was not valid UTF-8");
Self {
installation_folder: Path::new(&install_dir).to_owned(),
product_name: None,
product_version: ver,
}
})
}
fn query_reg<P: AsRef<OsStr>>(subkey: P) -> io::Result<Option<Self>> {
RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags(subkey, KEY_QUERY_VALUE | KEY_WOW64_32KEY)
.map(|key| {
if let Ok(info) = key.decode() {
Some(info)
} else {
None
}
})
.or_else(|e| {
if e.kind() == ErrorKind::NotFound {
Ok(None)
} else {
Err(e)
}
})
}
pub fn installation_folder(&self) -> &Path {
&self.installation_folder
}
pub fn product_name(&self) -> Option<&str> {
self.product_name.as_ref().map(|s| s.as_ref())
}
pub fn product_version(&self) -> &str {
&self.product_version
}
}
#[cfg(test)]
mod tests {
use {SdkInfo, SdkVersion};
#[test]
fn any() {
let _ = SdkInfo::find(SdkVersion::Any)
.expect("could not retrieve Windows SDK info from registry")
.expect("Windows SDK is not installed");
}
#[test]
fn winsdk_env() {
let _ = SdkInfo::find(SdkVersion::Env)
.unwrap_or_else(|_| unreachable!())
.expect("environment does not specify a Windows SDK installation");
}
#[test]
fn winsdk_10_0() {
let _ = SdkInfo::find(SdkVersion::V10_0)
.expect("could not retrieve Windows 10 SDK info from registry")
.expect("Windows 10 SDK is not installed");
}
#[test]
fn winsdk_8_1() {
let _ = SdkInfo::find(SdkVersion::V8_1)
.expect("could not retrieve Windows 8.1 SDK info from registry")
.expect("Windows 8.1 SDK is not installed");
}
}