about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-04-28 14:44:30 +0200
committerUrgau <urgau@numericable.fr>2024-04-28 17:17:22 +0200
commit607497d57caf28c3d06adfdd5bca826e9c81205b (patch)
treeea8d64429ecbfb88a63b6b7afb5a53c94333692b
parent91d5e4af8674bde06096bac1e7ac04af0e94c691 (diff)
downloadrust-607497d57caf28c3d06adfdd5bca826e9c81205b.tar.gz
rust-607497d57caf28c3d06adfdd5bca826e9c81205b.zip
Port print-cfg run-make to Rust-based rmake.rs
-rw-r--r--src/tools/tidy/src/allowed_run_make_makefiles.txt1
-rw-r--r--tests/run-make/print-cfg/Makefile37
-rw-r--r--tests/run-make/print-cfg/rmake.rs100
-rw-r--r--tests/run-make/print-to-output/rmake.rs67
4 files changed, 167 insertions, 38 deletions
diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index 1b560ee352c..a2179b895b3 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -219,7 +219,6 @@ run-make/prefer-rlib/Makefile
 run-make/pretty-print-to-file/Makefile
 run-make/pretty-print-with-dep-file/Makefile
 run-make/print-calling-conventions/Makefile
-run-make/print-cfg/Makefile
 run-make/print-target-list/Makefile
 run-make/profile/Makefile
 run-make/prune-link-args/Makefile
diff --git a/tests/run-make/print-cfg/Makefile b/tests/run-make/print-cfg/Makefile
deleted file mode 100644
index 6b153e5b54e..00000000000
--- a/tests/run-make/print-cfg/Makefile
+++ /dev/null
@@ -1,37 +0,0 @@
-# needs-llvm-components: x86 arm
-
-include ../tools.mk
-
-all: default output_to_file
-	$(RUSTC) --target x86_64-pc-windows-gnu --print cfg | $(CGREP) windows
-	$(RUSTC) --target x86_64-pc-windows-gnu --print cfg | $(CGREP) x86_64
-	$(RUSTC) --target i686-pc-windows-msvc --print cfg | $(CGREP) msvc
-	$(RUSTC) --target i686-apple-darwin --print cfg | $(CGREP) macos
-	$(RUSTC) --target i686-unknown-linux-gnu --print cfg | $(CGREP) gnu
-	$(RUSTC) --target arm-unknown-linux-gnueabihf --print cfg | $(CGREP) target_abi=
-	$(RUSTC) --target arm-unknown-linux-gnueabihf --print cfg | $(CGREP) eabihf
-
-output_to_file:
-	# Backend-independent, printed by rustc_driver_impl/src/lib.rs
-	$(RUSTC) --target x86_64-pc-windows-gnu --print cfg=$(TMPDIR)/cfg.txt
-	$(CGREP) windows < $(TMPDIR)/cfg.txt
-
-	# Printed from CodegenBackend trait impl in rustc_codegen_llvm/src/lib.rs
-	$(RUSTC) --print relocation-models=$(TMPDIR)/relocation-models.txt
-	$(CGREP) dynamic-no-pic < $(TMPDIR)/relocation-models.txt
-
-	# Printed by compiler/rustc_codegen_llvm/src/llvm_util.rs
-	$(RUSTC) --target wasm32-unknown-unknown --print target-features=$(TMPDIR)/target-features.txt
-	$(CGREP) reference-types < $(TMPDIR)/target-features.txt
-
-	# Printed by C++ code in rustc_llvm/llvm-wrapper/PassWrapper.cpp
-	$(RUSTC) --target wasm32-unknown-unknown --print target-cpus=$(TMPDIR)/target-cpus.txt
-	$(CGREP) generic < $(TMPDIR)/target-cpus.txt
-
-ifdef IS_WINDOWS
-default:
-	$(RUSTC) --print cfg | $(CGREP) windows
-else
-default:
-	$(RUSTC) --print cfg | $(CGREP) unix
-endif
diff --git a/tests/run-make/print-cfg/rmake.rs b/tests/run-make/print-cfg/rmake.rs
new file mode 100644
index 00000000000..0f829c9a95f
--- /dev/null
+++ b/tests/run-make/print-cfg/rmake.rs
@@ -0,0 +1,100 @@
+//! This checks the output of `--print=cfg`
+//!
+//! Specifically it checks that output is correctly formatted
+//! (ie. no duplicated cfgs, values are between "", names are not).
+//!
+//! It also checks that some targets have the correct set cfgs.
+
+extern crate run_make_support;
+
+use std::collections::HashSet;
+use std::ffi::OsString;
+use std::io::BufRead;
+use std::iter::FromIterator;
+
+use run_make_support::{rustc, tmp_dir};
+
+fn main() {
+    check(
+        /*target*/ "x86_64-pc-windows-gnu",
+        /*includes*/ &["windows", "target_arch=\"x86_64\""],
+        /*disallow*/ &["unix"],
+    );
+    check(
+        /*target*/ "i686-pc-windows-msvc",
+        /*includes*/ &["windows", "target_env=\"msvc\""],
+        /*disallow*/ &["unix"],
+    );
+    check(
+        /*target*/ "i686-apple-darwin",
+        /*includes*/ &["unix", "target_os=\"macos\"", "target_vendor=\"apple\""],
+        /*disallow*/ &["windows"],
+    );
+    check(
+        /*target*/ "i686-unknown-linux-gnu",
+        /*includes*/ &["unix", "target_env=\"gnu\""],
+        /*disallow*/ &["windows"],
+    );
+    check(
+        /*target*/ "arm-unknown-linux-gnueabihf",
+        /*includes*/ &["unix", "target_abi=\"eabihf\""],
+        /*disallow*/ &["windows"],
+    );
+}
+
+fn check(target: &str, includes: &[&str], disallow: &[&str]) {
+    fn _inner(output: &str, includes: &[&str], disallow: &[&str]) {
+        let mut found = HashSet::<String>::new();
+        let mut recorded = HashSet::<String>::new();
+
+        for l in output.lines() {
+            assert!(l == l.trim());
+            if let Some((left, right)) = l.split_once('=') {
+                assert!(right.starts_with("\""));
+                assert!(right.ends_with("\""));
+                assert!(!left.contains("\""));
+            } else {
+                assert!(!l.contains("\""));
+            }
+
+            assert!(recorded.insert(l.to_string()), "duplicated: {}", &l);
+            assert!(!disallow.contains(&l), "found disallowed: {}", &l);
+            if includes.contains(&l) {
+                assert!(found.insert(l.to_string()), "duplicated (includes): {}", &l);
+            }
+        }
+
+        let should_found = HashSet::<String>::from_iter(includes.iter().map(|s| s.to_string()));
+        let diff: Vec<_> = should_found.difference(&found).collect();
+
+        assert!(
+            diff.is_empty(),
+            "expected: {:?}, found: {:?} (~ {:?})",
+            &should_found,
+            &found,
+            &diff
+        );
+    }
+
+    // --print=cfg
+    {
+        let output = rustc().target(target).print("cfg").run();
+
+        let stdout = String::from_utf8(output.stdout).unwrap();
+
+        _inner(&stdout, includes, disallow);
+    }
+
+    // --print=cfg=PATH
+    {
+        let tmp_path = tmp_dir().join(format!("{target}.cfg"));
+        let mut print_arg = OsString::from("--print=cfg=");
+        print_arg.push(tmp_path.as_os_str());
+
+        let output = rustc().target(target).arg(print_arg).run();
+
+        let output = std::fs::read_to_string(&tmp_path).unwrap();
+
+        _inner(&output, includes, disallow);
+    }
+}
diff --git a/tests/run-make/print-to-output/rmake.rs b/tests/run-make/print-to-output/rmake.rs
new file mode 100644
index 00000000000..91724b492f8
--- /dev/null
+++ b/tests/run-make/print-to-output/rmake.rs
@@ -0,0 +1,67 @@
+//! This checks the output of some `--print` options when
+//! output to a file (instead of stdout)
+
+extern crate run_make_support;
+
+use std::ffi::OsString;
+
+use run_make_support::{rustc, target, tmp_dir};
+
+fn main() {
+    // Printed from CodegenBackend trait impl in rustc_codegen_llvm/src/lib.rs
+    check(
+        /*target*/ &target(),
+        /*option*/ "relocation-models",
+        /*includes*/ &["dynamic-no-pic"],
+    );
+
+    // Printed by compiler/rustc_codegen_llvm/src/llvm_util.rs
+    check(
+        /*target*/ "wasm32-unknown-unknown",
+        /*option*/ "target-features",
+        /*includes*/ &["reference-types"],
+    );
+
+    // Printed by C++ code in rustc_llvm/llvm-wrapper/PassWrapper.cpp
+    check(
+        /*target*/ "wasm32-unknown-unknown",
+        /*option*/ "target-cpus",
+        /*includes*/ &["generic"],
+    );
+}
+
+fn check(target: &str, option: &str, includes: &[&str]) {
+    fn _inner(output: &str, includes: &[&str]) {
+        for i in includes {
+            assert!(output.contains(i), "output doesn't contains: {}", i);
+        }
+    }
+
+    // --print={option}
+    let stdout = {
+        let output = rustc().target(target).print(option).run();
+
+        let stdout = String::from_utf8(output.stdout).unwrap();
+
+        _inner(&stdout, includes);
+
+        stdout
+    };
+
+    // --print={option}=PATH
+    let output = {
+        let tmp_path = tmp_dir().join(format!("{option}.txt"));
+        let mut print_arg = OsString::from(format!("--print={option}="));
+        print_arg.push(tmp_path.as_os_str());
+
+        let _output = rustc().target(target).arg(print_arg).run();
+
+        let output = std::fs::read_to_string(&tmp_path).unwrap();
+
+        _inner(&output, includes);
+
+        output
+    };
+
+    assert_eq!(&stdout, &output);
+}