Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Msvc link output improve backup (backup version). #1

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 96 additions & 14 deletions src/librustc_codegen_ssa/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tempfile::{Builder as TempFileBuilder, TempDir};

use std::ascii;
use std::char;
use std::borrow::Cow;
use std::fmt;
use std::fs;
use std::io;
Expand Down Expand Up @@ -417,6 +418,93 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
}
}

#[cfg(not(windows))]
fn escape_win_oemcp_str(s: &[u8]) -> Result<String, &[u8]> {
Err(s)
}

#[cfg(windows)]
fn escape_win_oemcp_str(s: &[u8]) -> Result<String, &[u8]> {
use std::ffi::OsString;
use std::os::raw::{c_char, c_int, c_uint, c_ulong};
use std::os::windows::ffi::OsStringExt;
use std::ptr::null_mut;
type UINT = c_uint;
type DWORD = c_ulong;
type LPCSTR = *const c_char;
type LPWSTR = *mut u16;
const CP_OEMCP: DWORD = 1;
const MB_ERR_INVALID_CHARS: DWORD = 0x00000008;
extern "system" {
fn MultiByteToWideChar(
CodePage: UINT,
dwFlags: DWORD,
lpMultiByteStr: LPCSTR,
cbMultiByte: c_int,
lpWideCharStr: LPWSTR,
cchWideChar: c_int,
) -> c_int;
}
if s.is_empty() {
return Ok(String::new());
}
if s.len() > libc::INT_MAX as _ {
return Err(s);
}
let len = unsafe {
let l = MultiByteToWideChar(
CP_OEMCP,
MB_ERR_INVALID_CHARS,
s.as_ptr() as _,
s.len() as c_int,
null_mut(),
0,
);
match l {
0 => return Err(s),
x => x,
}
};
assert!(len > 0);
let mut widestr = vec![0u16; len as usize];
unsafe {
let l = MultiByteToWideChar(
CP_OEMCP,
MB_ERR_INVALID_CHARS,
s.as_ptr() as _,
s.len() as c_int,
widestr.as_mut_ptr(),
len,
);
assert_eq!(l, len);
}
let os_string = OsString::from_wide(&widestr[..]);
let x = match os_string.into_string() {
Err(_) => return Err(s),
Ok(x) => x,
};
Ok(x)
}

fn escape_string(s: &[u8]) -> Cow<'_, str> {
// UTF-8
if let Ok(x) = str::from_utf8(s) {
return Cow::Borrowed(x);
}

// Windows OEMCP
if let Ok(os) = escape_win_oemcp_str(s) {
let mut x = "OEM codepage output: ".to_string();
x += &os;
return Cow::Owned(x);
}

// Fallback: Non-UTF-8
let mut x = "Non-UTF-8 output: ".to_string();
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
Cow::Owned(x)
}

// Create a dynamic library or executable
//
// This will invoke the system linker/cc to create the resulting file. This
Expand Down Expand Up @@ -586,16 +674,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,

match prog {
Ok(prog) => {
fn escape_string(s: &[u8]) -> String {
str::from_utf8(s).map(|s| s.to_owned())
.unwrap_or_else(|_| {
let mut x = "Non-UTF-8 output: ".to_string();
x.extend(s.iter()
.flat_map(|&b| ascii::escape_default(b))
.map(char::from));
x
})
}
if !prog.status.success() {
let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout);
Expand Down Expand Up @@ -630,10 +708,14 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
linker_error.emit();

if sess.target.target.options.is_like_msvc && linker_not_found {
sess.note_without_error("the msvc targets depend on the msvc linker \
but `link.exe` was not found");
sess.note_without_error("please ensure that VS 2013, VS 2015 or VS 2017 \
was installed with the Visual C++ option");
sess.note_without_error(
"the msvc targets depend on the msvc linker \
but `link.exe` was not found",
);
sess.note_without_error(
"please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 \
was installed with the Visual C++ option",
);
}
sess.abort_if_errors();
}
Expand Down