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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use std::process::Command;
#[cfg(not(windows))]
pub fn find(_target: &str, _tool: &str) -> Option<Command> {
None
}
#[cfg(windows)]
pub fn find(target: &str, tool: &str) -> Option<Command> {
use std::env;
use std::ffi::OsString;
use std::io;
use std::fs;
use std::path::{Path, PathBuf};
use registry::{RegistryKey, LOCAL_MACHINE};
if !target.contains("msvc") { return None }
let extra = if target.starts_with("i686") {
""
} else if target.starts_with("x86_64") {
"amd64"
} else if target.starts_with("arm") {
"arm"
} else {
return None
};
let vs_install_dir = get_vs_install_dir();
let mut path_to_add = None;
let mut cmd = env::var_os("VCINSTALLDIR").and_then(|dir| {
let mut p = PathBuf::from(dir);
p.push("bin");
p.push(extra);
let tool = p.join(tool);
if fs::metadata(&tool).is_ok() {
path_to_add = Some(p);
Some(tool)
} else {
None
}
}).or_else(|| {
env::var_os("PATH").and_then(|path| {
env::split_paths(&path).map(|p| p.join(tool)).find(|path| {
fs::metadata(path).is_ok()
})
})
}).or_else(|| {
vs_install_dir.as_ref().and_then(|p| {
let mut p = p.join("VC/bin");
p.push(extra);
let tool = p.join(tool);
if fs::metadata(&tool).is_ok() {
path_to_add = Some(p);
Some(tool)
} else {
None
}
})
}).map(|tool| {
Command::new(tool)
}).unwrap_or_else(|| {
Command::new(tool)
});
let mut paths = Vec::new();
if let Some(path) = path_to_add {
paths.push(path);
if let Some(root) = get_windows_sdk_bin_path(target) {
paths.push(root);
}
}
if let Some(path) = env::var_os("PATH") {
paths.extend(env::split_paths(&path));
}
cmd.env("PATH", env::join_paths(&paths).unwrap());
if env::var_os("INCLUDE").is_none() {
let mut includes = Vec::new();
if let Some(ref vs_install_dir) = vs_install_dir {
includes.push(vs_install_dir.join("VC/include"));
if let Some((ucrt_root, vers)) = ucrt_install_dir(vs_install_dir) {
includes.push(ucrt_root.join("Include").join(vers).join("ucrt"));
}
}
if let Some((path, major)) = get_windows_sdk_path() {
if major >= 8 {
includes.push(path.join("include/shared"));
includes.push(path.join("include/um"));
includes.push(path.join("include/winrt"));
} else {
includes.push(path.join("include"));
}
} else if let Some(ref vs_install_dir) = vs_install_dir {
includes.push(vs_install_dir.clone());
}
cmd.env("INCLUDE", env::join_paths(&includes).unwrap());
}
if env::var_os("LIB").is_none() {
let mut libs = Vec::new();
if let Some(ref vs_install_dir) = vs_install_dir {
libs.push(vs_install_dir.join("VC/lib").join(extra));
if let Some((ucrt_root, vers)) = ucrt_install_dir(vs_install_dir) {
if let Some(arch) = windows_sdk_v8_subdir(target) {
libs.push(ucrt_root.join("Lib").join(vers)
.join("ucrt").join(arch));
}
}
}
if let Some(path) = get_windows_sdk_lib_path(target) {
libs.push(path);
}
cmd.env("LIB", env::join_paths(&libs).unwrap());
}
return Some(cmd);
fn get_vs_install_dir() -> Option<PathBuf> {
LOCAL_MACHINE.open(r"SOFTWARE\Microsoft\VisualStudio".as_ref()).or_else(|_| {
LOCAL_MACHINE.open(r"SOFTWARE\Microsoft\VCExpress".as_ref())
}).ok().and_then(|key| {
max_version(&key).and_then(|(_vers, key)| {
key.query_str("InstallDir").ok()
})
}).or_else(|| {
env::var_os("VS120COMNTOOLS")
}).or_else(|| {
env::var_os("VS100COMNTOOLS")
}).or_else(|| {
env::var_os("VS90COMNTOOLS")
}).or_else(|| {
env::var_os("VS80COMNTOOLS")
}).map(PathBuf::from).and_then(|mut dir| {
if dir.ends_with("Common7/IDE") || dir.ends_with("Common7/Tools") {
dir.pop();
dir.pop();
Some(dir)
} else {
None
}
})
}
fn max_version(key: &RegistryKey) -> Option<(OsString, RegistryKey)> {
let mut max_vers = 0;
let mut max_key = None;
for subkey in key.iter().filter_map(|k| k.ok()) {
let val = subkey.to_str().and_then(|s| {
s.trim_left_matches("v").replace(".", "").parse().ok()
});
let val = match val {
Some(s) => s,
None => continue,
};
if val > max_vers {
if let Ok(k) = key.open(&subkey) {
max_vers = val;
max_key = Some((subkey, k));
}
}
}
return max_key
}
fn get_windows_sdk_path() -> Option<(PathBuf, usize)> {
let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows";
let key = LOCAL_MACHINE.open(key.as_ref());
let (n, k) = match key.ok().as_ref().and_then(max_version) {
Some(p) => p,
None => return None,
};
let mut parts = n.to_str().unwrap().trim_left_matches("v").splitn(2, ".");
let major = parts.next().unwrap().parse::<usize>().unwrap();
let _minor = parts.next().unwrap().parse::<usize>().unwrap();
k.query_str("InstallationFolder").ok().map(|p| {
(PathBuf::from(p), major)
})
}
fn get_windows_sdk_lib_path(target: &str) -> Option<PathBuf> {
let (mut root, major) = match get_windows_sdk_path() {
Some(pair) => pair,
None => return None,
};
root.push("Lib");
if major <= 7 {
if target.starts_with("i686") {
Some(root)
} else if target.starts_with("x86_64") {
Some(root.join("x64"))
} else {
None
}
} else {
let extra = match windows_sdk_v8_subdir(target) {
Some(extra) => extra,
None => return None,
};
["winv6.3", "win8", "win7"].iter().map(|p| root.join(p)).find(|part| {
fs::metadata(part).is_ok()
}).map(|path| {
path.join("um").join(extra)
})
}
}
fn get_windows_sdk_bin_path(target: &str) -> Option<PathBuf> {
let (mut root, major) = match get_windows_sdk_path() {
Some(pair) => pair,
None => return None,
};
root.push("bin");
if major <= 7 {
None
} else {
root.push(match windows_sdk_v8_subdir(target) {
Some(extra) => extra,
None => return None,
});
if fs::metadata(&root).is_ok() {Some(root)} else {None}
}
}
fn windows_sdk_v8_subdir(target: &str) -> Option<&'static str> {
if target.starts_with("i686") {
Some("x86")
} else if target.starts_with("x86_64") {
Some("x64")
} else if target.starts_with("arm") {
Some("arm")
} else {
None
}
}
fn ucrt_install_dir(vs_install_dir: &Path) -> Option<(PathBuf, String)> {
let is_vs_14 = vs_install_dir.iter().filter_map(|p| p.to_str()).any(|s| {
s == "Microsoft Visual Studio 14.0"
});
if !is_vs_14 {
return None
}
let sdk_dir = match get_windows_sdk_path() {
Some((mut root, _)) => { root.pop(); root.push("10"); root }
None => return None,
};
(move || -> io::Result<_> {
let mut max = None;
let mut max_s = None;
for entry in try!(fs::read_dir(&sdk_dir.join("Lib"))) {
let entry = try!(entry);
if let Ok(s) = entry.file_name().into_string() {
if let Ok(u) = s.replace(".", "").parse::<usize>() {
if Some(u) > max {
max = Some(u);
max_s = Some(s);
}
}
}
}
Ok(max_s.map(|m| (sdk_dir, m)))
})().ok().and_then(|x| x)
}
}