Skip to content

Commit 16a0c39

Browse files
authored
Unrolled build for rust-lang#122634
Rollup merge of rust-lang#122634 - Enselic:aux-bin, r=oli-obk compiletest: Add support for `//@ aux-bin: foo.rs` Which enables ui tests to use auxiliary binaries. See the added self-test for an example. This is an enabler for the test in rust-lang#121573.
2 parents a385e56 + 3a5eb35 commit 16a0c39

File tree

4 files changed

+98
-29
lines changed

4 files changed

+98
-29
lines changed

src/tools/compiletest/src/header.rs

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl HeadersCache {
3636
#[derive(Default)]
3737
pub struct EarlyProps {
3838
pub aux: Vec<String>,
39+
pub aux_bin: Vec<String>,
3940
pub aux_crate: Vec<(String, String)>,
4041
pub revisions: Vec<String>,
4142
}
@@ -59,6 +60,12 @@ impl EarlyProps {
5960
config.push_name_value_directive(ln, directives::AUX_BUILD, &mut props.aux, |r| {
6061
r.trim().to_string()
6162
});
63+
config.push_name_value_directive(
64+
ln,
65+
directives::AUX_BIN,
66+
&mut props.aux_bin,
67+
|r| r.trim().to_string(),
68+
);
6269
config.push_name_value_directive(
6370
ln,
6471
directives::AUX_CRATE,
@@ -95,6 +102,8 @@ pub struct TestProps {
95102
// directory as the test, but for backwards compatibility reasons
96103
// we also check the auxiliary directory)
97104
pub aux_builds: Vec<String>,
105+
// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
106+
pub aux_bins: Vec<String>,
98107
// Similar to `aux_builds`, but a list of NAME=somelib.rs of dependencies
99108
// to build and pass with the `--extern` flag.
100109
pub aux_crates: Vec<(String, String)>,
@@ -217,6 +226,7 @@ mod directives {
217226
pub const PRETTY_EXPANDED: &'static str = "pretty-expanded";
218227
pub const PRETTY_MODE: &'static str = "pretty-mode";
219228
pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
229+
pub const AUX_BIN: &'static str = "aux-bin";
220230
pub const AUX_BUILD: &'static str = "aux-build";
221231
pub const AUX_CRATE: &'static str = "aux-crate";
222232
pub const EXEC_ENV: &'static str = "exec-env";
@@ -252,6 +262,7 @@ impl TestProps {
252262
run_flags: None,
253263
pp_exact: None,
254264
aux_builds: vec![],
265+
aux_bins: vec![],
255266
aux_crates: vec![],
256267
revisions: vec![],
257268
rustc_env: vec![("RUSTC_ICE".to_string(), "0".to_string())],
@@ -417,6 +428,9 @@ impl TestProps {
417428
config.push_name_value_directive(ln, AUX_BUILD, &mut self.aux_builds, |r| {
418429
r.trim().to_string()
419430
});
431+
config.push_name_value_directive(ln, AUX_BIN, &mut self.aux_bins, |r| {
432+
r.trim().to_string()
433+
});
420434
config.push_name_value_directive(
421435
ln,
422436
AUX_CRATE,
@@ -683,6 +697,7 @@ pub fn line_directive<'line>(
683697
const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
684698
// tidy-alphabetical-start
685699
"assembly-output",
700+
"aux-bin",
686701
"aux-build",
687702
"aux-crate",
688703
"build-aux-docs",

src/tools/compiletest/src/runtest.rs

+71-29
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
8282
}
8383

8484
/// The platform-specific library name
85-
pub fn get_lib_name(lib: &str, dylib: bool) -> String {
86-
// In some casess (e.g. MUSL), we build a static
87-
// library, rather than a dynamic library.
88-
// In this case, the only path we can pass
89-
// with '--extern-meta' is the '.lib' file
90-
if !dylib {
91-
return format!("lib{}.rlib", lib);
92-
}
93-
94-
if cfg!(windows) {
95-
format!("{}.dll", lib)
96-
} else if cfg!(target_os = "macos") {
97-
format!("lib{}.dylib", lib)
98-
} else {
99-
format!("lib{}.so", lib)
85+
fn get_lib_name(lib: &str, aux_type: AuxType) -> Option<String> {
86+
match aux_type {
87+
AuxType::Bin => None,
88+
// In some cases (e.g. MUSL), we build a static
89+
// library, rather than a dynamic library.
90+
// In this case, the only path we can pass
91+
// with '--extern-meta' is the '.rlib' file
92+
AuxType::Lib => Some(format!("lib{}.rlib", lib)),
93+
AuxType::Dylib => Some(if cfg!(windows) {
94+
format!("{}.dll", lib)
95+
} else if cfg!(target_os = "macos") {
96+
format!("lib{}.dylib", lib)
97+
} else {
98+
format!("lib{}.so", lib)
99+
}),
100100
}
101101
}
102102

@@ -2098,19 +2098,36 @@ impl<'test> TestCx<'test> {
20982098
create_dir_all(&aux_dir).unwrap();
20992099
}
21002100

2101+
if !self.props.aux_bins.is_empty() {
2102+
let aux_bin_dir = self.aux_bin_output_dir_name();
2103+
let _ = fs::remove_dir_all(&aux_bin_dir);
2104+
create_dir_all(&aux_bin_dir).unwrap();
2105+
}
2106+
21012107
aux_dir
21022108
}
21032109

21042110
fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Command) {
21052111
for rel_ab in &self.props.aux_builds {
2106-
self.build_auxiliary(of, rel_ab, &aux_dir);
2112+
self.build_auxiliary(of, rel_ab, &aux_dir, false /* is_bin */);
2113+
}
2114+
2115+
for rel_ab in &self.props.aux_bins {
2116+
self.build_auxiliary(of, rel_ab, &aux_dir, true /* is_bin */);
21072117
}
21082118

21092119
for (aux_name, aux_path) in &self.props.aux_crates {
2110-
let is_dylib = self.build_auxiliary(of, &aux_path, &aux_dir);
2120+
let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, false /* is_bin */);
21112121
let lib_name =
2112-
get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), is_dylib);
2113-
rustc.arg("--extern").arg(format!("{}={}/{}", aux_name, aux_dir.display(), lib_name));
2122+
get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type);
2123+
if let Some(lib_name) = lib_name {
2124+
rustc.arg("--extern").arg(format!(
2125+
"{}={}/{}",
2126+
aux_name,
2127+
aux_dir.display(),
2128+
lib_name
2129+
));
2130+
}
21142131
}
21152132
}
21162133

@@ -2129,12 +2146,23 @@ impl<'test> TestCx<'test> {
21292146
}
21302147

21312148
/// Builds an aux dependency.
2132-
///
2133-
/// Returns whether or not it is a dylib.
2134-
fn build_auxiliary(&self, of: &TestPaths, source_path: &str, aux_dir: &Path) -> bool {
2149+
fn build_auxiliary(
2150+
&self,
2151+
of: &TestPaths,
2152+
source_path: &str,
2153+
aux_dir: &Path,
2154+
is_bin: bool,
2155+
) -> AuxType {
21352156
let aux_testpaths = self.compute_aux_test_paths(of, source_path);
21362157
let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
2137-
let aux_output = TargetLocation::ThisDirectory(aux_dir.to_path_buf());
2158+
let mut aux_dir = aux_dir.to_path_buf();
2159+
if is_bin {
2160+
// On unix, the binary of `auxiliary/foo.rs` will be named
2161+
// `auxiliary/foo` which clashes with the _dir_ `auxiliary/foo`, so
2162+
// put bins in a `bin` subfolder.
2163+
aux_dir.push("bin");
2164+
}
2165+
let aux_output = TargetLocation::ThisDirectory(aux_dir.clone());
21382166
let aux_cx = TestCx {
21392167
config: self.config,
21402168
props: &aux_props,
@@ -2152,15 +2180,17 @@ impl<'test> TestCx<'test> {
21522180
LinkToAux::No,
21532181
Vec::new(),
21542182
);
2155-
aux_cx.build_all_auxiliary(of, aux_dir, &mut aux_rustc);
2183+
aux_cx.build_all_auxiliary(of, &aux_dir, &mut aux_rustc);
21562184

21572185
for key in &aux_props.unset_rustc_env {
21582186
aux_rustc.env_remove(key);
21592187
}
21602188
aux_rustc.envs(aux_props.rustc_env.clone());
21612189

2162-
let (dylib, crate_type) = if aux_props.no_prefer_dynamic {
2163-
(true, None)
2190+
let (aux_type, crate_type) = if is_bin {
2191+
(AuxType::Bin, Some("bin"))
2192+
} else if aux_props.no_prefer_dynamic {
2193+
(AuxType::Dylib, None)
21642194
} else if self.config.target.contains("emscripten")
21652195
|| (self.config.target.contains("musl")
21662196
&& !aux_props.force_host
@@ -2185,9 +2215,9 @@ impl<'test> TestCx<'test> {
21852215
// Coverage tests want static linking by default so that coverage
21862216
// mappings in auxiliary libraries can be merged into the final
21872217
// executable.
2188-
(false, Some("lib"))
2218+
(AuxType::Lib, Some("lib"))
21892219
} else {
2190-
(true, Some("dylib"))
2220+
(AuxType::Dylib, Some("dylib"))
21912221
};
21922222

21932223
if let Some(crate_type) = crate_type {
@@ -2211,7 +2241,7 @@ impl<'test> TestCx<'test> {
22112241
&auxres,
22122242
);
22132243
}
2214-
dylib
2244+
aux_type
22152245
}
22162246

22172247
fn read2_abbreviated(&self, child: Child) -> (Output, Truncated) {
@@ -2677,6 +2707,12 @@ impl<'test> TestCx<'test> {
26772707
.with_extra_extension(self.config.mode.aux_dir_disambiguator())
26782708
}
26792709

2710+
/// Gets the directory where auxiliary binaries are written.
2711+
/// E.g., `/.../testname.revision.mode/auxiliary/bin`.
2712+
fn aux_bin_output_dir_name(&self) -> PathBuf {
2713+
self.aux_output_dir_name().join("bin")
2714+
}
2715+
26802716
/// Generates a unique name for the test, such as `testname.revision.mode`.
26812717
fn output_testname_unique(&self) -> PathBuf {
26822718
output_testname_unique(self.config, self.testpaths, self.safe_revision())
@@ -4826,3 +4862,9 @@ enum LinkToAux {
48264862
Yes,
48274863
No,
48284864
}
4865+
4866+
enum AuxType {
4867+
Bin,
4868+
Lib,
4869+
Dylib,
4870+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("it works");
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ ignore-cross-compile because we run the compiled code
2+
//@ aux-bin: print-it-works.rs
3+
//@ run-pass
4+
5+
fn main() {
6+
let stdout =
7+
std::process::Command::new("auxiliary/bin/print-it-works").output().unwrap().stdout;
8+
assert_eq!(stdout, b"it works\n");
9+
}

0 commit comments

Comments
 (0)