Skip to content

Commit 2b114da

Browse files
committed
Rollup merge of #48548 - alexcrichton:msvc-linker-utf16, r=alexcrichton
Encode linker arguments as UTF-16 on MSVC platforms This is a forward-port of #48455 to the master branch
2 parents 8bdad67 + 5db73fc commit 2b114da

File tree

3 files changed

+50
-10
lines changed
  • src
    • librustc_trans/back
    • test/run-make
      • long-linker-command-lines
      • long-linker-command-lines-cmd-exe

3 files changed

+50
-10
lines changed

src/librustc_trans/back/link.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,19 @@ fn exec_linker(sess: &Session, cmd: &mut Command, tmpdir: &Path)
849849
args.push_str("\n");
850850
}
851851
let file = tmpdir.join("linker-arguments");
852-
fs::write(&file, args.as_bytes())?;
852+
let bytes = if sess.target.target.options.is_like_msvc {
853+
let mut out = vec![];
854+
// start the stream with a UTF-16 BOM
855+
for c in vec![0xFEFF].into_iter().chain(args.encode_utf16()) {
856+
// encode in little endian
857+
out.push(c as u8);
858+
out.push((c >> 8) as u8);
859+
}
860+
out
861+
} else {
862+
args.into_bytes()
863+
};
864+
fs::write(&file, &bytes)?;
853865
cmd2.arg(format!("@{}", file.display()));
854866
return cmd2.output();
855867

src/test/run-make/long-linker-command-lines-cmd-exe/foo.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ fn main() {
3636
let ok = tmpdir.join("ok");
3737
let not_ok = tmpdir.join("not_ok");
3838
if env::var("YOU_ARE_A_LINKER").is_ok() {
39-
match env::args().find(|a| a.contains("@")) {
40-
Some(file) => { fs::copy(&file[1..], &ok).unwrap(); }
39+
match env::args_os().find(|a| a.to_string_lossy().contains("@")) {
40+
Some(file) => {
41+
let file = file.to_str().unwrap();
42+
fs::copy(&file[1..], &ok).unwrap();
43+
}
4144
None => { File::create(&not_ok).unwrap(); }
4245
}
4346
return
@@ -84,11 +87,23 @@ fn main() {
8487
continue
8588
}
8689

87-
let mut contents = String::new();
88-
File::open(&ok).unwrap().read_to_string(&mut contents).unwrap();
90+
let mut contents = Vec::new();
91+
File::open(&ok).unwrap().read_to_end(&mut contents).unwrap();
8992

9093
for j in 0..i {
91-
assert!(contents.contains(&format!("{}{}", lib_name, j)));
94+
let exp = format!("{}{}", lib_name, j);
95+
let exp = if cfg!(target_env = "msvc") {
96+
let mut out = Vec::with_capacity(exp.len() * 2);
97+
for c in exp.encode_utf16() {
98+
// encode in little endian
99+
out.push(c as u8);
100+
out.push((c >> 8) as u8);
101+
}
102+
out
103+
} else {
104+
exp.into_bytes()
105+
};
106+
assert!(contents.windows(exp.len()).any(|w| w == &exp[..]));
92107
}
93108

94109
break

src/test/run-make/long-linker-command-lines/foo.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ fn main() {
2727
let tmpdir = PathBuf::from(env::var_os("TMPDIR").unwrap());
2828
let ok = tmpdir.join("ok");
2929
if env::var("YOU_ARE_A_LINKER").is_ok() {
30-
if let Some(file) = env::args().find(|a| a.contains("@")) {
30+
if let Some(file) = env::args_os().find(|a| a.to_string_lossy().contains("@")) {
31+
let file = file.to_str().expect("non-utf8 file argument");
3132
fs::copy(&file[1..], &ok).unwrap();
3233
}
3334
return
@@ -76,11 +77,23 @@ fn main() {
7677
continue
7778
}
7879

79-
let mut contents = String::new();
80-
File::open(&ok).unwrap().read_to_string(&mut contents).unwrap();
80+
let mut contents = Vec::new();
81+
File::open(&ok).unwrap().read_to_end(&mut contents).unwrap();
8182

8283
for j in 0..i {
83-
assert!(contents.contains(&format!("{}{}", lib_name, j)));
84+
let exp = format!("{}{}", lib_name, j);
85+
let exp = if cfg!(target_env = "msvc") {
86+
let mut out = Vec::with_capacity(exp.len() * 2);
87+
for c in exp.encode_utf16() {
88+
// encode in little endian
89+
out.push(c as u8);
90+
out.push((c >> 8) as u8);
91+
}
92+
out
93+
} else {
94+
exp.into_bytes()
95+
};
96+
assert!(contents.windows(exp.len()).any(|w| w == &exp[..]));
8497
}
8598

8699
break

0 commit comments

Comments
 (0)