Skip to content

Commit

Permalink
Show files produced by --emit foo in json artifact notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pacak committed Mar 31, 2024
1 parent 439f67d commit 3986132
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
20 changes: 20 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,26 @@ fn produce_final_output_artifacts(
}
}

if sess.opts.json_artifact_notifications {
if compiled_modules.modules.len() == 1 {
compiled_modules.modules[0].traverse_artifacts_with(|_path, descr, ty| {
if sess.opts.output_types.contains_key(&ty) {
let path = crate_output.path(ty);

sess.dcx().emit_artifact_notification(path.as_path(), descr);
}
});
} else {
for module in &compiled_modules.modules {
module.traverse_artifacts_with(|path, descr, ty| {
if sess.opts.output_types.contains_key(&ty) {
sess.dcx().emit_artifact_notification(&path, descr);
}
});
}
}
}

// We leave the following files around by default:
// - #crate#.o
// - #crate#.crate.metadata.o
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ pub struct CompiledModule {
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
}

impl CompiledModule {
pub fn traverse_artifacts_with<F>(&self, mut emit: F)
where
F: FnMut(&Path, &str, OutputType),
{
if let Some(path) = self.object.as_deref() {
emit(path, "obj", OutputType::Object);
}
if let Some(path) = self.bytecode.as_deref() {
emit(path, "llvm-bc", OutputType::Bitcode);
}
if let Some(path) = self.llvm_ir.as_deref() {
emit(path, "llvm-ir", OutputType::LlvmAssembly);
}
if let Some(path) = self.assembly.as_deref() {
emit(path, "asm", OutputType::Assembly);
}
}
}

pub struct CachedModuleCodegen {
pub name: String,
pub source: WorkProduct,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_mir_transform/src/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
OutFileName::Real(path) => {
let mut f = io::BufWriter::new(File::create(&path)?);
write_mir_pretty(tcx, None, &mut f)?;
if tcx.sess.opts.json_artifact_notifications {
tcx.dcx().emit_artifact_notification(&path, "mir");
}
}
}
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/doc/rustc/src/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ Diagnostics have the following format:
Artifact notifications are emitted when the [`--json=artifacts`
flag][option-json] is used. They indicate that a file artifact has been saved
to disk. More information about emit kinds may be found in the [`--emit`
flag][option-emit] documentation.
flag][option-emit] documentation. Notifications can contain more than one file
for each type, for example when using multiple codegen units.

```javascript
{
Expand All @@ -229,6 +230,11 @@ flag][option-emit] documentation.
- "link": The generated crate as specified by the crate-type.
- "dep-info": The `.d` file with dependency information in a Makefile-like syntax.
- "metadata": The Rust `.rmeta` file containing metadata about the crate.
- "asm": The `.s` file with generated assembly
- "llvm-ir": The `.ll` file with generated textual LLVM IR
- "llvm-bc": The `.bc` file with generated LLVM bitcode
- "mir": The `.mir` file with rustc's mid-level intermediate representation.
- "obj": The `.o` file with generated native object code
*/
"emit": "link"
}
Expand Down

0 comments on commit 3986132

Please sign in to comment.