Skip to content

Commit 806ce30

Browse files
committed
Cache rmeta files whenever metadata is present in emit. Fixes #436, Fixes #429
1 parent fd3cc39 commit 806ce30

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/compiler/rust.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::collections::hash_map::RandomState;
3737
#[cfg(feature = "dist-client")]
3838
use std::env::consts::{DLL_PREFIX, EXE_EXTENSION};
3939
use std::env::consts::DLL_EXTENSION;
40-
use std::ffi::{OsStr, OsString};
40+
use std::ffi::OsString;
4141
use std::fmt;
4242
use std::fs;
4343
use std::hash::Hash;
@@ -133,7 +133,7 @@ pub struct ParsedArguments {
133133
dep_info: Option<PathBuf>,
134134
/// rustc says that emits .rlib for --emit=metadata
135135
/// https://github.com/rust-lang/rust/issues/54852
136-
rename_rlib_to_rmeta: bool,
136+
emit: HashSet<String>,
137137
/// The value of any `--color` option passed on the commandline.
138138
color_mode: ColorMode,
139139
}
@@ -317,6 +317,9 @@ fn get_compiler_outputs<T>(creator: &T,
317317
let outputs = run_input_output(cmd, None);
318318
Box::new(outputs.and_then(move |output| -> Result<_> {
319319
let outstr = String::from_utf8(output.stdout).chain_err(|| "Error parsing rustc output")?;
320+
if log_enabled!(Trace) {
321+
trace!("get_compiler_outputs: {:?}", outstr);
322+
}
320323
Ok(outstr.lines().map(|l| l.to_owned()).collect())
321324
}))
322325
}
@@ -873,8 +876,6 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
873876
cannot_cache!("unsupported --emit");
874877
}
875878

876-
let rename_rlib_to_rmeta = emit.contains("metadata") && !emit.contains("link");
877-
878879
// Figure out the dep-info filename, if emitting dep-info.
879880
let dep_info = if emit.contains("dep-info") {
880881
let mut dep_info = crate_name.clone();
@@ -915,7 +916,7 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
915916
staticlibs: staticlibs,
916917
crate_name: crate_name.to_string(),
917918
dep_info: dep_info.map(|s| s.into()),
918-
rename_rlib_to_rmeta,
919+
emit,
919920
color_mode,
920921
})
921922
}
@@ -948,7 +949,7 @@ impl<T> CompilerHasher<T> for RustHasher
948949
crate_name,
949950
crate_types,
950951
dep_info,
951-
rename_rlib_to_rmeta,
952+
emit,
952953
color_mode: _,
953954
},
954955
} = me;
@@ -1064,7 +1065,26 @@ impl<T> CompilerHasher<T> for RustHasher
10641065
let flat_os_string_arguments: Vec<OsString> = os_string_arguments.into_iter()
10651066
.flat_map(|(arg, val)| iter::once(arg).into_iter().chain(val))
10661067
.collect();
1067-
Box::new(get_compiler_outputs(&creator, &executable, &flat_os_string_arguments, &cwd, &env_vars).map(move |outputs| {
1068+
Box::new(get_compiler_outputs(&creator, &executable, &flat_os_string_arguments, &cwd, &env_vars).map(move |mut outputs| {
1069+
if emit.contains("metadata") {
1070+
// rustc currently does not report rmeta outputs with --print file-names
1071+
// --emit metadata the rlib is printed, and with --emit metadata,link
1072+
// only the rlib is printed.
1073+
let rlibs: HashSet<_> = outputs.iter().cloned().filter(|p| {
1074+
p.ends_with(".rlib")
1075+
}).collect();
1076+
for lib in rlibs {
1077+
let rmeta = lib.replacen(".rlib", ".rmeta", 1);
1078+
// Do this defensively for future versions of rustc that may
1079+
// be fixed.
1080+
if !outputs.contains(&rmeta) {
1081+
outputs.push(rmeta);
1082+
}
1083+
if !emit.contains("link") {
1084+
outputs.retain(|p| *p != lib);
1085+
}
1086+
}
1087+
}
10681088
let output_dir = PathBuf::from(output_dir);
10691089
// Convert output files into a map of basename -> full path.
10701090
let mut outputs = outputs.into_iter()
@@ -1085,14 +1105,6 @@ impl<T> CompilerHasher<T> for RustHasher
10851105
arguments.push(Argument::WithValue("--color", ArgData::Color("always".into()), ArgDisposition::Separated));
10861106
let inputs = source_files.into_iter().chain(abs_externs).chain(abs_staticlibs).collect();
10871107

1088-
if rename_rlib_to_rmeta {
1089-
for output in outputs.values_mut() {
1090-
if output.extension() == Some(OsStr::new("rlib")) {
1091-
output.set_extension("rmeta");
1092-
}
1093-
}
1094-
}
1095-
10961108
HashResult {
10971109
key: m.finish(),
10981110
compilation: Box::new(RustCompilation {
@@ -2038,6 +2050,9 @@ c:/foo/bar.rs:
20382050
for s in ["foo.rs", "bar.rs", "bar.rlib", "libbaz.a"].iter() {
20392051
f.touch(s).unwrap();
20402052
}
2053+
let mut emit = HashSet::new();
2054+
emit.insert("link".to_string());
2055+
emit.insert("metadata".to_string());
20412056
let hasher = Box::new(RustHasher {
20422057
executable: "rustc".into(),
20432058
host: "x86-64-unknown-unknown-unknown".to_owned(),
@@ -2063,7 +2078,7 @@ c:/foo/bar.rs:
20632078
crate_name: "foo".into(),
20642079
crate_types: CrateTypes { rlib: true, staticlib: false },
20652080
dep_info: None,
2066-
rename_rlib_to_rmeta: false,
2081+
emit: emit,
20672082
color_mode: ColorMode::Auto,
20682083
}
20692084
});
@@ -2108,7 +2123,7 @@ c:/foo/bar.rs:
21082123
assert_eq!(res.key, digest);
21092124
let mut out = res.compilation.outputs().map(|(k, _)| k.to_owned()).collect::<Vec<_>>();
21102125
out.sort();
2111-
assert_eq!(out, vec!["foo.a", "foo.rlib"]);
2126+
assert_eq!(out, vec!["foo.a", "foo.rlib", "foo.rmeta"]);
21122127
}
21132128

21142129
fn hash_key<'a, F>(f: &TestFixture, args: &[OsString], env_vars: &[(OsString, OsString)],

0 commit comments

Comments
 (0)