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

Optimize long-linker-command-line test #65945

Merged
merged 1 commit into from
Nov 7, 2019
Merged
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
73 changes: 44 additions & 29 deletions src/test/run-make-fulldeps/long-linker-command-lines/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,43 @@
// Eventually we should see an argument that looks like `@` as we switch from
// passing literal arguments to passing everything in the file.

use std::collections::HashSet;
use std::env;
use std::fs::{self, File};
use std::io::{BufWriter, Write, Read};
use std::path::PathBuf;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::Command;

fn write_test_case(file: &Path, n: usize) -> HashSet<String> {
let mut libs = HashSet::new();
let mut f = BufWriter::new(File::create(&file).unwrap());
let mut prefix = String::new();
for _ in 0..n {
prefix.push_str("foo");
}
for i in 0..n {
writeln!(f, "#[link(name = \"S{}{}S\")]", prefix, i).unwrap();
libs.insert(format!("{}{}", prefix, i));
}
writeln!(f, "extern {{}}\nfn main() {{}}").unwrap();
f.into_inner().unwrap();

libs
}

fn read_linker_args(path: &Path) -> String {
let contents = fs::read(path).unwrap();
if cfg!(target_env = "msvc") {
let mut i = contents.chunks(2).map(|c| {
c[0] as u16 | ((c[1] as u16) << 8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be u16::from_le_bytes

});
assert_eq!(i.next(), Some(0xfeff), "Expected UTF-16 BOM");
String::from_utf16(&i.collect::<Vec<u16>>()).unwrap()
} else {
String::from_utf8(contents).unwrap()
}
}

fn main() {
let tmpdir = PathBuf::from(env::var_os("TMPDIR").unwrap());
let ok = tmpdir.join("ok");
Expand All @@ -29,16 +60,7 @@ fn main() {
for i in (1..).map(|i| i * 100) {
println!("attempt: {}", i);
let file = tmpdir.join("bar.rs");
let mut f = BufWriter::new(File::create(&file).unwrap());
let mut lib_name = String::new();
for _ in 0..i {
lib_name.push_str("foo");
}
for j in 0..i {
writeln!(f, "#[link(name = \"{}{}\")]", lib_name, j).unwrap();
}
writeln!(f, "extern {{}}\nfn main() {{}}").unwrap();
f.into_inner().unwrap();
let mut expected_libs = write_test_case(&file, i);

drop(fs::remove_file(&ok));
let output = Command::new(&rustc)
Expand Down Expand Up @@ -67,25 +89,18 @@ fn main() {
continue
}

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

for j in 0..i {
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[..]));
let linker_args = read_linker_args(&ok);
for mut arg in linker_args.split('S') {
expected_libs.remove(arg);
}

assert!(
expected_libs.is_empty(),
"expected but missing libraries: {:#?}\nlinker arguments: \n{}",
expected_libs,
linker_args,
);

break
}
}