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 f37648d
Show file tree
Hide file tree
Showing 6 changed files with 110 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
23 changes: 23 additions & 0 deletions tests/run-make/emit-notify/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ compile-flags: -C codegen-units=1 --emit asm,llvm-ir,llvm-bc,obj --json=artifacts --error-format=json

fn one() -> usize {
1
}

pub mod a {
pub fn two() -> usize {
::one() + ::one()
}
}

pub mod b {
pub fn three() -> usize {
::one() + ::a::two()
}
}

#[inline(never)]
pub fn main() {
a::two();
b::three();
}
37 changes: 37 additions & 0 deletions tests/run-make/emit-notify/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extern crate run_make_support;

use run_make_support::{rustc, tmp_dir};

fn main() {
let inc_dir = tmp_dir();

for _ in 0..=1 {
let output = rustc()
.input("lib.rs")
.arg("--emit=obj,asm,llvm-ir,llvm-bc,mir")
.arg("-Ccodegen-units=1")
.arg("--json=artifacts")
.arg("--error-format=json")
.arg(format!("-Cincremental={}", inc_dir.to_str().expect("temp dir?")).as_str())
.run();
let stderr = String::from_utf8_lossy(&output.stderr);
for file in &["lib.o", "lib.ll", "lib.bc", "lib.s"] {
assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr);
}
}

for _ in 0..=1 {
let output = rustc()
.input("lib.rs")
.arg("--emit=obj,asm,llvm-ir,llvm-bc,mir")
.arg("-Ccodegen-units=2")
.arg("--json=artifacts")
.arg("--error-format=json")
.arg(format!("-Cincremental={}", inc_dir.to_str().expect("temp dir?")).as_str())
.run();
let stderr = String::from_utf8_lossy(&output.stderr);
for file in &["rcgu.o", "rcgu.ll", "rcgu.bc", "rcgu.s"] {
assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr);
}
}
}

0 comments on commit f37648d

Please sign in to comment.