From 81c068a7a6cd6c7efb85e372769ca6deb4a892ea Mon Sep 17 00:00:00 2001
From: onur-ozkan <work@onurozkan.dev>
Date: Wed, 14 Feb 2024 13:31:50 +0300
Subject: [PATCH 1/2] install tools documentations

Previously, we were trying to install all doc files under "share/doc/rust"
which caused `rust-installer` tool to create backup files (*.old) due to filename
conflicts. With this change, doc files is now installed under "share/doc/{package}",
where {package} could be rustc, cargo, clippy, etc.

Signed-off-by: onur-ozkan <work@onurozkan.dev>
---
 src/bootstrap/src/core/build_steps/install.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bootstrap/src/core/build_steps/install.rs b/src/bootstrap/src/core/build_steps/install.rs
index 1c565e7f7cc06..0225f8f24a5ef 100644
--- a/src/bootstrap/src/core/build_steps/install.rs
+++ b/src/bootstrap/src/core/build_steps/install.rs
@@ -95,7 +95,7 @@ fn install_sh(
     }
 
     let datadir = prefix.join(default_path(&builder.config.datadir, "share"));
-    let docdir = prefix.join(default_path(&builder.config.docdir, "share/doc/rust"));
+    let docdir = prefix.join(default_path(&builder.config.docdir, &format!("share/doc/{package}")));
     let mandir = prefix.join(default_path(&builder.config.mandir, "share/man"));
     let libdir = prefix.join(default_path(&builder.config.libdir, "lib"));
     let bindir = prefix.join(&builder.config.bindir); // Default in config.rs

From 435e1c6dc5959d8987ab5647bf868bfdc4adca31 Mon Sep 17 00:00:00 2001
From: onur-ozkan <work@onurozkan.dev>
Date: Wed, 14 Feb 2024 13:52:45 +0300
Subject: [PATCH 2/2] install rustc before the tools

Signed-off-by: onur-ozkan <work@onurozkan.dev>
---
 src/bootstrap/src/core/build_steps/dist.rs | 3 ++-
 src/bootstrap/src/core/builder.rs          | 6 +++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs
index f50026368dabd..1c125926f9c0c 100644
--- a/src/bootstrap/src/core/build_steps/dist.rs
+++ b/src/bootstrap/src/core/build_steps/dist.rs
@@ -398,10 +398,11 @@ impl Step for Rustc {
             let host = compiler.host;
             let src = builder.sysroot(compiler);
 
-            // Copy rustc/rustdoc binaries
+            // Copy rustc binary
             t!(fs::create_dir_all(image.join("bin")));
             builder.cp_r(&src.join("bin"), &image.join("bin"));
 
+            // If enabled, copy rustdoc binary
             if builder
                 .config
                 .tools
diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs
index b21ffe868e145..662ba64a5650b 100644
--- a/src/bootstrap/src/core/builder.rs
+++ b/src/bootstrap/src/core/builder.rs
@@ -858,6 +858,11 @@ impl<'a> Builder<'a> {
             Kind::Install => describe!(
                 install::Docs,
                 install::Std,
+                // During the Rust compiler (rustc) installation process, we copy the entire sysroot binary
+                // path (build/host/stage2/bin). Since the building tools also make their copy in the sysroot
+                // binary path, we must install rustc before the tools. Otherwise, the rust-installer will
+                // install the same binaries twice for each tool, leaving backup files (*.old) as a result.
+                install::Rustc,
                 install::Cargo,
                 install::RustAnalyzer,
                 install::Rustfmt,
@@ -866,7 +871,6 @@ impl<'a> Builder<'a> {
                 install::Miri,
                 install::LlvmTools,
                 install::Src,
-                install::Rustc,
             ),
             Kind::Run => describe!(
                 run::ExpandYamlAnchors,