Skip to content

Commit c8390cd

Browse files
committed
Show files produced by --emit foo in json artifact notifications
1 parent 43a0686 commit c8390cd

File tree

7 files changed

+140
-1
lines changed

7 files changed

+140
-1
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+23
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,29 @@ fn produce_final_output_artifacts(
287287
}
288288
}
289289

290+
if sess.opts.json_artifact_notifications {
291+
if codegen_results.modules.len() == 1 {
292+
codegen_results.modules[0].for_each_output(|_path, ty| {
293+
if sess.opts.output_types.contains_key(&ty) {
294+
let descr = ty.shorthand();
295+
// for single cgu file is renamed to drop cgu specific suffix
296+
// so we regenerate it the same way
297+
let path = crate_output.path(ty);
298+
sess.dcx().emit_artifact_notification(path.as_path(), descr);
299+
}
300+
});
301+
} else {
302+
for module in &codegen_results.modules {
303+
module.for_each_output(|path, ty| {
304+
if sess.opts.output_types.contains_key(&ty) {
305+
let descr = ty.shorthand();
306+
sess.dcx().emit_artifact_notification(&path, descr);
307+
}
308+
});
309+
}
310+
}
311+
}
312+
290313
// We leave the following files around by default:
291314
// - #crate#.o
292315
// - #crate#.crate.metadata.o

compiler/rustc_codegen_ssa/src/back/write.rs

+23
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,29 @@ fn produce_final_output_artifacts(
706706
}
707707
}
708708

709+
if sess.opts.json_artifact_notifications {
710+
if compiled_modules.modules.len() == 1 {
711+
compiled_modules.modules[0].for_each_output(|_path, ty| {
712+
if sess.opts.output_types.contains_key(&ty) {
713+
let descr = ty.shorthand();
714+
// for single cgu file is renamed to drop cgu specific suffix
715+
// so we regenerate it the same way
716+
let path = crate_output.path(ty);
717+
sess.dcx().emit_artifact_notification(path.as_path(), descr);
718+
}
719+
});
720+
} else {
721+
for module in &compiled_modules.modules {
722+
module.for_each_output(|path, ty| {
723+
if sess.opts.output_types.contains_key(&ty) {
724+
let descr = ty.shorthand();
725+
sess.dcx().emit_artifact_notification(&path, descr);
726+
}
727+
});
728+
}
729+
}
730+
}
731+
709732
// We leave the following files around by default:
710733
// - #crate#.o
711734
// - #crate#.crate.metadata.o

compiler/rustc_codegen_ssa/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ pub struct CompiledModule {
113113
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
114114
}
115115

116+
impl CompiledModule {
117+
/// Call `emit` function with every artifact type currently compiled
118+
pub fn for_each_output(&self, mut emit: impl FnMut(&Path, OutputType)) {
119+
if let Some(path) = self.object.as_deref() {
120+
emit(path, OutputType::Object);
121+
}
122+
if let Some(path) = self.bytecode.as_deref() {
123+
emit(path, OutputType::Bitcode);
124+
}
125+
if let Some(path) = self.llvm_ir.as_deref() {
126+
emit(path, OutputType::LlvmAssembly);
127+
}
128+
if let Some(path) = self.assembly.as_deref() {
129+
emit(path, OutputType::Assembly);
130+
}
131+
}
132+
}
133+
116134
pub struct CachedModuleCodegen {
117135
pub name: String,
118136
pub source: WorkProduct,

compiler/rustc_mir_transform/src/dump_mir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
2828
OutFileName::Real(path) => {
2929
let mut f = io::BufWriter::new(File::create(&path)?);
3030
write_mir_pretty(tcx, None, &mut f)?;
31+
if tcx.sess.opts.json_artifact_notifications {
32+
tcx.dcx().emit_artifact_notification(&path, "mir");
33+
}
3134
}
3235
}
3336
Ok(())

src/doc/rustc/src/json.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ Diagnostics have the following format:
217217
Artifact notifications are emitted when the [`--json=artifacts`
218218
flag][option-json] is used. They indicate that a file artifact has been saved
219219
to disk. More information about emit kinds may be found in the [`--emit`
220-
flag][option-emit] documentation.
220+
flag][option-emit] documentation. Notifications can contain more than one file
221+
for each type, for example when using multiple codegen units.
221222

222223
```javascript
223224
{
@@ -229,6 +230,11 @@ flag][option-emit] documentation.
229230
- "link": The generated crate as specified by the crate-type.
230231
- "dep-info": The `.d` file with dependency information in a Makefile-like syntax.
231232
- "metadata": The Rust `.rmeta` file containing metadata about the crate.
233+
- "asm": The `.s` file with generated assembly
234+
- "llvm-ir": The `.ll` file with generated textual LLVM IR
235+
- "llvm-bc": The `.bc` file with generated LLVM bitcode
236+
- "mir": The `.mir` file with rustc's mid-level intermediate representation.
237+
- "obj": The `.o` file with generated native object code
232238
*/
233239
"emit": "link"
234240
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn one() -> usize {
2+
1
3+
}
4+
5+
pub mod a {
6+
pub fn two() -> usize {
7+
::one() + ::one()
8+
}
9+
}
10+
11+
pub mod b {
12+
pub fn three() -> usize {
13+
::one() + ::a::two()
14+
}
15+
}
16+
17+
#[inline(never)]
18+
pub fn main() {
19+
a::two();
20+
b::three();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rust should produce artifact notifications about files it was asked to --emit.
2+
//
3+
// It should work in incremental mode both on the first pass where files are generated as well
4+
// as on subsequent passes where they are taken from the incremental cache
5+
//
6+
// See <https://internals.rust-lang.org/t/easier-access-to-files-generated-by-emit-foo/20477>
7+
extern crate run_make_support;
8+
9+
use run_make_support::{rustc, tmp_dir};
10+
11+
fn main() {
12+
let inc_dir = tmp_dir();
13+
14+
// With single codegen unit files are renamed to match the source file name
15+
for _ in 0..=1 {
16+
let output = rustc()
17+
.input("lib.rs")
18+
.emit("obj,asm,llvm-ir,llvm-bc,mir")
19+
.codegen_units(1)
20+
.json("artifacts")
21+
.error_format("json")
22+
.incremental(&inc_dir)
23+
.run();
24+
let stderr = String::from_utf8_lossy(&output.stderr);
25+
for file in &["lib.o", "lib.ll", "lib.bc", "lib.s"] {
26+
assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr);
27+
}
28+
}
29+
30+
// with multiple codegen units files keep codegen unit id part.
31+
for _ in 0..=1 {
32+
let output = rustc()
33+
.input("lib.rs")
34+
.emit("obj,asm,llvm-ir,llvm-bc,mir")
35+
.codegen_units(2)
36+
.json("artifacts")
37+
.error_format("json")
38+
.incremental(&inc_dir)
39+
.run();
40+
let stderr = String::from_utf8_lossy(&output.stderr);
41+
for file in &["rcgu.o", "rcgu.ll", "rcgu.bc", "rcgu.s"] {
42+
assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)