From 47e96438e8ec5cf9e7084eb85b9acd53ff200c35 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Wed, 31 Jul 2024 16:07:37 -0400 Subject: [PATCH 1/2] rewrite emit-to-stdout to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/emit-to-stdout/Makefile | 51 ------------ tests/run-make/emit-to-stdout/rmake.rs | 79 +++++++++++++++++++ 3 files changed, 79 insertions(+), 52 deletions(-) delete mode 100644 tests/run-make/emit-to-stdout/Makefile create mode 100644 tests/run-make/emit-to-stdout/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 22d1e93bf399a..65c12ff929fa1 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -1,6 +1,5 @@ run-make/branch-protection-check-IBT/Makefile run-make/cat-and-grep-sanity-check/Makefile -run-make/emit-to-stdout/Makefile run-make/extern-fn-reachable/Makefile run-make/incr-add-rust-src-component/Makefile run-make/issue-84395-lto-embed-bitcode/Makefile diff --git a/tests/run-make/emit-to-stdout/Makefile b/tests/run-make/emit-to-stdout/Makefile deleted file mode 100644 index 80e9b6a4ded67..0000000000000 --- a/tests/run-make/emit-to-stdout/Makefile +++ /dev/null @@ -1,51 +0,0 @@ -include ../tools.mk - -SRC=test.rs -OUT=$(TMPDIR)/out - -all: asm llvm-ir dep-info mir llvm-bc obj metadata link multiple-types multiple-types-option-o - -asm: $(OUT) - $(RUSTC) --emit asm=$(OUT)/$@ $(SRC) - $(RUSTC) --emit asm=- $(SRC) | diff - $(OUT)/$@ -llvm-ir: $(OUT) - $(RUSTC) --emit llvm-ir=$(OUT)/$@ $(SRC) - $(RUSTC) --emit llvm-ir=- $(SRC) | diff - $(OUT)/$@ -dep-info: $(OUT) - $(RUSTC) -Z dep-info-omit-d-target=yes --emit dep-info=$(OUT)/$@ $(SRC) - $(RUSTC) --emit dep-info=- $(SRC) | diff - $(OUT)/$@ -mir: $(OUT) - $(RUSTC) --emit mir=$(OUT)/$@ $(SRC) - $(RUSTC) --emit mir=- $(SRC) | diff - $(OUT)/$@ - -llvm-bc: $(OUT) - $(RUSTC) --emit llvm-bc=- $(SRC) 1>/dev/ptmx 2>$(OUT)/$@ || true - diff $(OUT)/$@ emit-llvm-bc.stderr -obj: $(OUT) - $(RUSTC) --emit obj=- $(SRC) 1>/dev/ptmx 2>$(OUT)/$@ || true - diff $(OUT)/$@ emit-obj.stderr - -# For metadata output, a temporary directory will be created to hold the temporary -# metadata file. But when output is stdout, the temporary directory will be located -# in the same place as $(SRC), which is mounted as read-only in the tests. Thus as -# a workaround, $(SRC) is copied to the test output directory $(OUT) and we compile -# it there. -metadata: $(OUT) - cp $(SRC) $(OUT) - (cd $(OUT); $(RUSTC) --emit metadata=- $(SRC) 1>/dev/ptmx 2>$(OUT)/$@ || true) - diff $(OUT)/$@ emit-metadata.stderr - -link: $(OUT) - $(RUSTC) --emit link=- $(SRC) 1>/dev/ptmx 2>$(OUT)/$@ || true - diff $(OUT)/$@ emit-link.stderr - -multiple-types: $(OUT) - $(RUSTC) --emit asm=- --emit llvm-ir=- --emit dep-info=- --emit mir=- $(SRC) 2>$(OUT)/$@ || true - diff $(OUT)/$@ emit-multiple-types.stderr - -multiple-types-option-o: $(OUT) - $(RUSTC) -o - --emit asm,llvm-ir,dep-info,mir $(SRC) 2>$(OUT)/$@ || true - diff $(OUT)/$@ emit-multiple-types.stderr - -$(OUT): - mkdir -p $(OUT) diff --git a/tests/run-make/emit-to-stdout/rmake.rs b/tests/run-make/emit-to-stdout/rmake.rs new file mode 100644 index 0000000000000..c84d02f49d7da --- /dev/null +++ b/tests/run-make/emit-to-stdout/rmake.rs @@ -0,0 +1,79 @@ +// If `-o -` or `--emit KIND=-` is provided, output should be written +// to stdout instead. Binary output (`obj`, `llvm-bc`, `link` and +// `metadata`) being written this way will result in an error unless +// stdout is not a tty. Multiple output types going to stdout will +// trigger an error too, as they will all be mixed together. +// See https://github.com/rust-lang/rust/pull/111626 + +use run_make_support::{diff, rfs, rustc}; + +fn main() { + rfs::create_dir("out"); + let tests = ["asm", "llvm-ir", "dep-info", "mir", "llvm-bc", "obj", "metadata", "link"]; + for test in tests { + test_emit(test); + } + // These two last tests, which combine multiple emit types, should be done separately. + diff() + .expected_file("emit-multiple-types.stderr") + .actual_text( + "actual", + rustc() + .output("-") + .emit("asm=-") + .emit("llvm-ir=-") + .emit("dep-info=-") + .emit("mir=-") + .input("test.rs") + .run_fail() + .stderr_utf8(), + ) + .run(); + diff() + .expected_file("emit-multiple-types.stderr") + .actual_text( + "actual", + rustc() + .output("-") + .emit("asm,llvm-ir,dep-info,mir") + .input("test.rs") + .run_fail() + .stderr_utf8(), + ) + .run(); +} + +fn test_emit(emit_type: &str) { + // Emitting these types will cause a compilation failure, which should be compared to a + // blessed stderr file for differences. + let stderr_types = ["llvm-bc", "obj", "metadata", "link"]; + // Successful types (not in stderr_types) should start by outputting one emit file. + if !stderr_types.contains(&emit_type) { + let mut initial_compile = rustc(); + initial_compile.emit(&format!("{emit_type}=out/{emit_type}")).input("test.rs"); + // dep-info requires an extra unstable argument. + if emit_type == "dep-info" { + initial_compile.arg("-Zdep-info-omit-d-target=yes"); + } + initial_compile.run(); + } + let mut compile = rustc(); + compile.emit(&format!("{emit_type}=-")).input("test.rs"); + // Check if compilation should succeed or fail depending on the emit type. + let compile = + if stderr_types.contains(&emit_type) { compile.run_fail() } else { compile.run() }; + let emit = if stderr_types.contains(&emit_type) { + compile.stderr_utf8() + } else { + compile.stdout_utf8() + }; + let mut diff = diff(); + // Compare the output with either an emit file or stderr file, depending on success + // or failure. + if stderr_types.contains(&emit_type) { + diff.expected_file(&format!("emit-{emit_type}.stderr")); + } else { + diff.expected_file(&format!("out/{emit_type}")); + } + diff.actual_text("actual", &emit).run(); +} From 4506f6753bd82dd1032e21abe533ba95a4f65f11 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Mon, 26 Aug 2024 11:25:33 -0400 Subject: [PATCH 2/2] split test_emit into individual emit type tests --- tests/run-make/emit-to-stdout/rmake.rs | 108 ++++++++++++++++--------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/tests/run-make/emit-to-stdout/rmake.rs b/tests/run-make/emit-to-stdout/rmake.rs index c84d02f49d7da..cc29b7f605199 100644 --- a/tests/run-make/emit-to-stdout/rmake.rs +++ b/tests/run-make/emit-to-stdout/rmake.rs @@ -9,11 +9,71 @@ use run_make_support::{diff, rfs, rustc}; fn main() { rfs::create_dir("out"); - let tests = ["asm", "llvm-ir", "dep-info", "mir", "llvm-bc", "obj", "metadata", "link"]; - for test in tests { - test_emit(test); - } - // These two last tests, which combine multiple emit types, should be done separately. + test_asm(); + test_llvm_ir(); + test_dep_info(); + test_mir(); + test_llvm_bc(); + test_obj(); + test_metadata(); + test_link(); + test_multiple_types(); + test_multiple_types_option_o(); +} + +fn test_asm() { + rustc().emit("asm=out/asm").input("test.rs").run(); + let emit = rustc().emit("asm=-").input("test.rs").run().stdout_utf8(); + diff().expected_file("out/asm").actual_text("actual", &emit).run(); +} + +fn test_llvm_ir() { + rustc().emit("llvm-ir=out/llvm-ir").input("test.rs").run(); + let emit = rustc().emit("llvm-ir=-").input("test.rs").run().stdout_utf8(); + diff().expected_file("out/llvm-ir").actual_text("actual", &emit).run(); +} + +fn test_dep_info() { + rustc() + .emit("dep-info=out/dep-info") + .input("test.rs") + .arg("-Zdep-info-omit-d-target=yes") + .run(); + let emit = rustc().emit("dep-info=-").input("test.rs").run().stdout_utf8(); + diff().expected_file("out/dep-info").actual_text("actual", &emit).run(); +} + +fn test_mir() { + rustc().emit("mir=out/mir").input("test.rs").run(); + let emit = rustc().emit("mir=-").input("test.rs").run().stdout_utf8(); + diff().expected_file("out/mir").actual_text("actual", &emit).run(); +} + +// FIXME: ptmx +fn test_llvm_bc() { + let emit = rustc().emit("llvm-bc=-").input("test.rs").run().stderr_utf8(); + diff().expected_file("emit-llvm-bc.stderr").actual_text("actual", &emit).run(); +} + +// FIXME: ptmx +fn test_obj() { + let emit = rustc().emit("obj=-").input("test.rs").run().stderr_utf8(); + diff().expected_file("emit-obj.stderr").actual_text("actual", &emit).run(); +} + +// FIXME: ptmx +fn test_metadata() { + let emit = rustc().emit("metadata=-").input("test.rs").run().stderr_utf8(); + diff().expected_file("emit-metadata.stderr").actual_text("actual", &emit).run(); +} + +// FIXME: ptmx +fn test_link() { + let emit = rustc().emit("link=-").input("test.rs").run().stderr_utf8(); + diff().expected_file("emit-link.stderr").actual_text("actual", &emit).run(); +} + +fn test_multiple_types() { diff() .expected_file("emit-multiple-types.stderr") .actual_text( @@ -29,6 +89,9 @@ fn main() { .stderr_utf8(), ) .run(); +} + +fn test_multiple_types_option_o() { diff() .expected_file("emit-multiple-types.stderr") .actual_text( @@ -42,38 +105,3 @@ fn main() { ) .run(); } - -fn test_emit(emit_type: &str) { - // Emitting these types will cause a compilation failure, which should be compared to a - // blessed stderr file for differences. - let stderr_types = ["llvm-bc", "obj", "metadata", "link"]; - // Successful types (not in stderr_types) should start by outputting one emit file. - if !stderr_types.contains(&emit_type) { - let mut initial_compile = rustc(); - initial_compile.emit(&format!("{emit_type}=out/{emit_type}")).input("test.rs"); - // dep-info requires an extra unstable argument. - if emit_type == "dep-info" { - initial_compile.arg("-Zdep-info-omit-d-target=yes"); - } - initial_compile.run(); - } - let mut compile = rustc(); - compile.emit(&format!("{emit_type}=-")).input("test.rs"); - // Check if compilation should succeed or fail depending on the emit type. - let compile = - if stderr_types.contains(&emit_type) { compile.run_fail() } else { compile.run() }; - let emit = if stderr_types.contains(&emit_type) { - compile.stderr_utf8() - } else { - compile.stdout_utf8() - }; - let mut diff = diff(); - // Compare the output with either an emit file or stderr file, depending on success - // or failure. - if stderr_types.contains(&emit_type) { - diff.expected_file(&format!("emit-{emit_type}.stderr")); - } else { - diff.expected_file(&format!("out/{emit_type}")); - } - diff.actual_text("actual", &emit).run(); -}