Skip to content

Commit ff6e555

Browse files
committed
Make MSVC detection ludicrously robust
Should fix a few more edge cases Signed-off-by: Peter Atashian <[email protected]>
1 parent 0486e12 commit ff6e555

File tree

4 files changed

+206
-140
lines changed

4 files changed

+206
-140
lines changed

src/librustc_trans/back/link.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,17 @@ pub fn mangle_internal_name_by_path_and_seq(path: DefPath, flav: &str) -> String
371371
mangle(names, None)
372372
}
373373

374-
pub fn get_linker(sess: &Session) -> (String, Command) {
374+
// The third parameter is for an extra path to add to PATH for MSVC
375+
// cross linkers for host toolchain DLL dependencies
376+
pub fn get_linker(sess: &Session) -> (String, Command, Option<PathBuf>) {
375377
if let Some(ref linker) = sess.opts.cg.linker {
376-
(linker.clone(), Command::new(linker))
378+
(linker.clone(), Command::new(linker), None)
377379
} else if sess.target.target.options.is_like_msvc {
378-
("link.exe".to_string(), msvc::link_exe_cmd(sess))
380+
let (cmd, host) = msvc::link_exe_cmd(sess);
381+
("link.exe".to_string(), cmd, host)
379382
} else {
380383
(sess.target.target.options.linker.clone(),
381-
Command::new(&sess.target.target.options.linker))
384+
Command::new(&sess.target.target.options.linker), None)
382385
}
383386
}
384387

@@ -388,17 +391,15 @@ pub fn get_ar_prog(sess: &Session) -> String {
388391
})
389392
}
390393

391-
fn command_path(sess: &Session) -> OsString {
394+
fn command_path(sess: &Session, extra: Option<PathBuf>) -> OsString {
392395
// The compiler's sysroot often has some bundled tools, so add it to the
393396
// PATH for the child.
394397
let mut new_path = sess.host_filesearch(PathKind::All)
395398
.get_tools_search_paths();
396399
if let Some(path) = env::var_os("PATH") {
397400
new_path.extend(env::split_paths(&path));
398401
}
399-
if sess.target.target.options.is_like_msvc {
400-
new_path.extend(msvc::host_dll_path());
401-
}
402+
new_path.extend(extra);
402403
env::join_paths(new_path).unwrap()
403404
}
404405

@@ -608,7 +609,7 @@ fn archive_config<'a>(sess: &'a Session,
608609
src: input.map(|p| p.to_path_buf()),
609610
lib_search_paths: archive_search_paths(sess),
610611
ar_prog: get_ar_prog(sess),
611-
command_path: command_path(sess),
612+
command_path: command_path(sess, None),
612613
}
613614
}
614615

@@ -854,8 +855,8 @@ fn link_natively(sess: &Session, dylib: bool,
854855
out_filename);
855856

856857
// The invocations of cc share some flags across platforms
857-
let (pname, mut cmd) = get_linker(sess);
858-
cmd.env("PATH", command_path(sess));
858+
let (pname, mut cmd, extra) = get_linker(sess);
859+
cmd.env("PATH", command_path(sess, extra));
859860

860861
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
861862
cmd.args(&sess.target.target.options.pre_link_args);

src/librustc_trans/back/msvc/arch.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(non_camel_case_types, non_snake_case)]
12+
13+
use libc::c_void;
14+
use std::mem;
15+
16+
type DWORD = u32;
17+
type WORD = u16;
18+
type LPVOID = *mut c_void;
19+
type DWORD_PTR = usize;
20+
21+
const PROCESSOR_ARCHITECTURE_INTEL: WORD = 0;
22+
const PROCESSOR_ARCHITECTURE_AMD64: WORD = 9;
23+
24+
#[repr(C)]
25+
struct SYSTEM_INFO {
26+
wProcessorArchitecture: WORD,
27+
_wReserved: WORD,
28+
_dwPageSize: DWORD,
29+
_lpMinimumApplicationAddress: LPVOID,
30+
_lpMaximumApplicationAddress: LPVOID,
31+
_dwActiveProcessorMask: DWORD_PTR,
32+
_dwNumberOfProcessors: DWORD,
33+
_dwProcessorType: DWORD,
34+
_dwAllocationGranularity: DWORD,
35+
_wProcessorLevel: WORD,
36+
_wProcessorRevision: WORD,
37+
}
38+
39+
extern "system" {
40+
fn GetNativeSystemInfo(lpSystemInfo: *mut SYSTEM_INFO);
41+
}
42+
43+
pub enum Arch {
44+
X86,
45+
Amd64,
46+
}
47+
48+
pub fn host_arch() -> Option<Arch> {
49+
let mut info = unsafe { mem::zeroed() };
50+
unsafe { GetNativeSystemInfo(&mut info) };
51+
match info.wProcessorArchitecture {
52+
PROCESSOR_ARCHITECTURE_INTEL => Some(Arch::X86),
53+
PROCESSOR_ARCHITECTURE_AMD64 => Some(Arch::Amd64),
54+
_ => None,
55+
}
56+
}

0 commit comments

Comments
 (0)