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

[beta] Encode linker arguments as UTF-16 on MSVC platforms #48455

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,19 @@ fn exec_linker(sess: &Session, cmd: &mut Command, tmpdir: &Path)
args.push_str("\n");
}
let file = tmpdir.join("linker-arguments");
fs::write(&file, args.as_bytes())?;
let bytes = if sess.target.target.options.is_like_msvc {
let mut out = vec![];
// start the stream with a UTF-16 BOM
for c in vec![0xFEFF].into_iter().chain(args.encode_utf16()) {
// encode in little endian
out.push(c as u8);
out.push((c >> 8) as u8);
}
out
} else {
args.into_bytes()
};
fs::write(&file, &bytes)?;
cmd2.arg(format!("@{}", file.display()));
return cmd2.output();

Expand Down
25 changes: 20 additions & 5 deletions src/test/run-make/long-linker-command-lines-cmd-exe/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ fn main() {
let ok = tmpdir.join("ok");
let not_ok = tmpdir.join("not_ok");
if env::var("YOU_ARE_A_LINKER").is_ok() {
match env::args().find(|a| a.contains("@")) {
Some(file) => { fs::copy(&file[1..], &ok).unwrap(); }
match env::args_os().find(|a| a.to_string_lossy().contains("@")) {
Some(file) => {
let file = file.to_str().unwrap();
fs::copy(&file[1..], &ok).unwrap();
}
None => { File::create(&not_ok).unwrap(); }
}
return
Expand Down Expand Up @@ -84,11 +87,23 @@ fn main() {
continue
}

let mut contents = String::new();
File::open(&ok).unwrap().read_to_string(&mut contents).unwrap();
let mut contents = Vec::new();
File::open(&ok).unwrap().read_to_end(&mut contents).unwrap();

for j in 0..i {
assert!(contents.contains(&format!("{}{}", lib_name, j)));
let exp = format!("{}{}", lib_name, j);
let exp = if cfg!(target_env = "msvc") {
let mut out = Vec::with_capacity(exp.len() * 2);
for c in exp.encode_utf16() {
// encode in little endian
out.push(c as u8);
out.push((c >> 8) as u8);
}
out
} else {
exp.into_bytes()
};
assert!(contents.windows(exp.len()).any(|w| w == &exp[..]));
}

break
Expand Down
21 changes: 17 additions & 4 deletions src/test/run-make/long-linker-command-lines/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fn main() {
let tmpdir = PathBuf::from(env::var_os("TMPDIR").unwrap());
let ok = tmpdir.join("ok");
if env::var("YOU_ARE_A_LINKER").is_ok() {
if let Some(file) = env::args().find(|a| a.contains("@")) {
if let Some(file) = env::args_os().find(|a| a.to_string_lossy().contains("@")) {
let file = file.to_str().expect("non-utf8 file argument");
fs::copy(&file[1..], &ok).unwrap();
}
return
Expand Down Expand Up @@ -76,11 +77,23 @@ fn main() {
continue
}

let mut contents = String::new();
File::open(&ok).unwrap().read_to_string(&mut contents).unwrap();
let mut contents = Vec::new();
File::open(&ok).unwrap().read_to_end(&mut contents).unwrap();

for j in 0..i {
assert!(contents.contains(&format!("{}{}", lib_name, j)));
let exp = format!("{}{}", lib_name, j);
let exp = if cfg!(target_env = "msvc") {
let mut out = Vec::with_capacity(exp.len() * 2);
for c in exp.encode_utf16() {
// encode in little endian
out.push(c as u8);
out.push((c >> 8) as u8);
}
out
} else {
exp.into_bytes()
};
assert!(contents.windows(exp.len()).any(|w| w == &exp[..]));
}

break
Expand Down