rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.52k stars 94 forks source link

Invoking via 'cargo clif' instead of 'cargo-clif' spawns processes endlessly #1383

Closed LPGhatguy closed 11 months ago

LPGhatguy commented 1 year ago

Hello! Today I set up rustc_codegen_cranelift to try it out on some of my projects on Windows.

I added the binaries to my PATH and ran

cargo clif build

which resulted in thousands of copies of cargo.exe and cargo-clif.exe being spawned! Taskmgr_5ktZkSBJxD

This made my PC fairly angry.

Running as cargo-clif build works fine. Is running this backend as a cargo subcommand an intended feature, or did I stumble upon something goofy on accident?

bjorn3 commented 1 year ago

I haven't implemented running as cargo clif build yet, but that shouldn't be too hard to do. The reason it is called cargo-clif rather than cargo is to avoid infinite recursion on Windows. cargo-clif calls cargo, expecting it to be the real cargo, but on windows this would previously call itself as windows looks in the current directory before looking at PATH like other OSes.

bjorn3 commented 1 year ago

Does this work?

diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs
index 99b97be2..5977652a 100644
--- a/scripts/cargo-clif.rs
+++ b/scripts/cargo-clif.rs
@@ -40,14 +40,20 @@ fn main() {
         "cargo"
     };

-    let args: Vec<_> = match env::args().nth(1).as_deref() {
+    let mut args = env::args().skip(1).collect::<Vec<_>>();
+    if args.get(0).map(|arg| &**arg) == Some("clif") {
+        args.remove(0);
+    }
+
+    let args: Vec<_> = match args.get(0).map(|arg| &**arg) {
         Some("jit") => {
             env::set_var(
                 "RUSTFLAGS",
                 env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
             );
+            args.remove(0);
             IntoIterator::into_iter(["rustc".to_string()])
-                .chain(env::args().skip(2))
+                .chain(args)
                 .chain([
                     "--".to_string(),
                     "-Zunstable-options".to_string(),
@@ -60,8 +66,9 @@ fn main() {
                 "RUSTFLAGS",
                 env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
             );
+            args.remove(0);
             IntoIterator::into_iter(["rustc".to_string()])
-                .chain(env::args().skip(2))
+                .chain(args)
                 .chain([
                     "--".to_string(),
                     "-Zunstable-options".to_string(),
@@ -69,7 +76,7 @@ fn main() {
                 ])
                 .collect()
         }
-        _ => env::args().skip(1).collect(),
+        _ => args,
     };

     #[cfg(unix)]