6a711d
From 8e0007f829661e57d008d2e908c95f6e84b04b25 Mon Sep 17 00:00:00 2001
6a711d
From: bors <bors@rust-lang.org>
6a711d
Date: Thu, 24 Oct 2019 07:27:00 +0000
6a711d
Subject: [PATCH] Auto merge of #65474 - Mark-Simulacrum:rustc-dev-split,
6a711d
 r=pietroalbini
6a711d
6a711d
Split the rustc target libraries into separate rustc-dev component
6a711d
6a711d
This is re-applies a squashed version of #64823 as well as including #65337 to fix bugs noted after merging the first PR.
6a711d
6a711d
The second PR is confirmed as fixing windows-gnu, and presumably also fixes other platforms, such as musl (i.e. #65335 should be fixed); `RUSTUP_DIST_SERVER=https://dev-static.rust-lang.org rustup toolchain install nightly-2019-10-16` can be installed to confirm that this is indeed the case.
6a711d
6a711d
diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs
6a711d
index b8071b98f707..2748903f2d47 100644
6a711d
--- a/src/bootstrap/builder.rs
6a711d
+++ b/src/bootstrap/builder.rs
6a711d
@@ -443,6 +443,7 @@ impl<'a> Builder<'a> {
6a711d
                 dist::Rustc,
6a711d
                 dist::DebuggerScripts,
6a711d
                 dist::Std,
6a711d
+                dist::RustcDev,
6a711d
                 dist::Analysis,
6a711d
                 dist::Src,
6a711d
                 dist::PlainSourceTarball,
6a711d
diff --git a/src/bootstrap/check.rs b/src/bootstrap/check.rs
6a711d
index cadb9a7e441f..df1c72575846 100644
6a711d
--- a/src/bootstrap/check.rs
6a711d
+++ b/src/bootstrap/check.rs
6a711d
@@ -55,6 +55,7 @@ impl Step for Std {
6a711d
                   cargo,
6a711d
                   args(builder.kind),
6a711d
                   &libstd_stamp(builder, compiler, target),
6a711d
+                  vec![],
6a711d
                   true);
6a711d
 
6a711d
         let libdir = builder.sysroot_libdir(compiler, target);
6a711d
@@ -103,6 +104,7 @@ impl Step for Rustc {
6a711d
                   cargo,
6a711d
                   args(builder.kind),
6a711d
                   &librustc_stamp(builder, compiler, target),
6a711d
+                  vec![],
6a711d
                   true);
6a711d
 
6a711d
         let libdir = builder.sysroot_libdir(compiler, target);
6a711d
@@ -155,6 +157,7 @@ impl Step for CodegenBackend {
6a711d
                   cargo,
6a711d
                   args(builder.kind),
6a711d
                   &codegen_backend_stamp(builder, compiler, target, backend),
6a711d
+                  vec![],
6a711d
                   true);
6a711d
     }
6a711d
 }
6a711d
@@ -199,6 +202,7 @@ impl Step for Rustdoc {
6a711d
                   cargo,
6a711d
                   args(builder.kind),
6a711d
                   &rustdoc_stamp(builder, compiler, target),
6a711d
+                  vec![],
6a711d
                   true);
6a711d
 
6a711d
         let libdir = builder.sysroot_libdir(compiler, target);
6a711d
diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
6a711d
index 5074b035789a..da8d43ed49b7 100644
6a711d
--- a/src/bootstrap/compile.rs
6a711d
+++ b/src/bootstrap/compile.rs
6a711d
@@ -69,7 +69,7 @@ impl Step for Std {
6a711d
             return;
6a711d
         }
6a711d
 
6a711d
-        builder.ensure(StartupObjects { compiler, target });
6a711d
+        let mut target_deps = builder.ensure(StartupObjects { compiler, target });
6a711d
 
6a711d
         let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
6a711d
         if compiler_to_use != compiler {
6a711d
@@ -91,7 +91,7 @@ impl Step for Std {
6a711d
             return;
6a711d
         }
6a711d
 
6a711d
-        copy_third_party_objects(builder, &compiler, target);
6a711d
+        target_deps.extend(copy_third_party_objects(builder, &compiler, target).into_iter());
6a711d
 
6a711d
         let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
6a711d
         std_cargo(builder, &compiler, target, &mut cargo);
6a711d
@@ -102,6 +102,7 @@ impl Step for Std {
6a711d
                   cargo,
6a711d
                   vec![],
6a711d
                   &libstd_stamp(builder, compiler, target),
6a711d
+                  target_deps,
6a711d
                   false);
6a711d
 
6a711d
         builder.ensure(StdLink {
6a711d
@@ -113,9 +114,22 @@ impl Step for Std {
6a711d
 }
6a711d
 
6a711d
 /// Copies third pary objects needed by various targets.
6a711d
-fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target: Interned<String>) {
6a711d
+fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target: Interned<String>)
6a711d
+    -> Vec<PathBuf>
6a711d
+{
6a711d
     let libdir = builder.sysroot_libdir(*compiler, target);
6a711d
 
6a711d
+    let mut target_deps = vec![];
6a711d
+
6a711d
+    let mut copy_and_stamp = |sourcedir: &Path, name: &str| {
6a711d
+        let target = libdir.join(name);
6a711d
+        builder.copy(
6a711d
+            &sourcedir.join(name),
6a711d
+            &target,
6a711d
+        );
6a711d
+        target_deps.push(target);
6a711d
+    };
6a711d
+
6a711d
     // Copies the crt(1,i,n).o startup objects
6a711d
     //
6a711d
     // Since musl supports fully static linking, we can cross link for it even
6a711d
@@ -123,19 +137,13 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
6a711d
     // files. As those shipped with glibc won't work, copy the ones provided by
6a711d
     // musl so we have them on linux-gnu hosts.
6a711d
     if target.contains("musl") {
6a711d
+        let srcdir = builder.musl_root(target).unwrap().join("lib");
6a711d
         for &obj in &["crt1.o", "crti.o", "crtn.o"] {
6a711d
-            builder.copy(
6a711d
-                &builder.musl_root(target).unwrap().join("lib").join(obj),
6a711d
-                &libdir.join(obj),
6a711d
-            );
6a711d
+            copy_and_stamp(&srcdir, obj);
6a711d
         }
6a711d
     } else if target.ends_with("-wasi") {
6a711d
-        for &obj in &["crt1.o"] {
6a711d
-            builder.copy(
6a711d
-                &builder.wasi_root(target).unwrap().join("lib/wasm32-wasi").join(obj),
6a711d
-                &libdir.join(obj),
6a711d
-            );
6a711d
-        }
6a711d
+        let srcdir = builder.wasi_root(target).unwrap().join("lib/wasm32-wasi");
6a711d
+        copy_and_stamp(&srcdir, "crt1.o");
6a711d
     }
6a711d
 
6a711d
     // Copies libunwind.a compiled to be linked wit x86_64-fortanix-unknown-sgx.
6a711d
@@ -145,11 +153,11 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
6a711d
     // which is provided by std for this target.
6a711d
     if target == "x86_64-fortanix-unknown-sgx" {
6a711d
         let src_path_env = "X86_FORTANIX_SGX_LIBS";
6a711d
-        let obj = "libunwind.a";
6a711d
         let src = env::var(src_path_env).expect(&format!("{} not found in env", src_path_env));
6a711d
-        let src = Path::new(&src).join(obj);
6a711d
-        builder.copy(&src, &libdir.join(obj));
6a711d
+        copy_and_stamp(Path::new(&src), "libunwind.a");
6a711d
     }
6a711d
+
6a711d
+    target_deps
6a711d
 }
6a711d
 
6a711d
 /// Configure cargo to compile the standard library, adding appropriate env vars
6a711d
@@ -306,7 +314,7 @@ pub struct StartupObjects {
6a711d
 }
6a711d
 
6a711d
 impl Step for StartupObjects {
6a711d
-    type Output = ();
6a711d
+    type Output = Vec<PathBuf>;
6a711d
 
6a711d
     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
6a711d
         run.path("src/rtstartup")
6a711d
@@ -325,13 +333,15 @@ impl Step for StartupObjects {
6a711d
     /// They don't require any library support as they're just plain old object
6a711d
     /// files, so we just use the nightly snapshot compiler to always build them (as
6a711d
     /// no other compilers are guaranteed to be available).
6a711d
-    fn run(self, builder: &Builder<'_>) {
6a711d
+    fn run(self, builder: &Builder<'_>) -> Vec<PathBuf> {
6a711d
         let for_compiler = self.compiler;
6a711d
         let target = self.target;
6a711d
         if !target.contains("windows-gnu") {
6a711d
-            return
6a711d
+            return vec![]
6a711d
         }
6a711d
 
6a711d
+        let mut target_deps = vec![];
6a711d
+
6a711d
         let src_dir = &builder.src.join("src/rtstartup");
6a711d
         let dst_dir = &builder.native_dir(target).join("rtstartup");
6a711d
         let sysroot_dir = &builder.sysroot_libdir(for_compiler, target);
6a711d
@@ -350,7 +360,9 @@ impl Step for StartupObjects {
6a711d
                             .arg(src_file));
6a711d
             }
6a711d
 
6a711d
-            builder.copy(dst_file, &sysroot_dir.join(file.to_string() + ".o"));
6a711d
+            let target = sysroot_dir.join(file.to_string() + ".o");
6a711d
+            builder.copy(dst_file, &target);
6a711d
+            target_deps.push(target);
6a711d
         }
6a711d
 
6a711d
         for obj in ["crt2.o", "dllcrt2.o"].iter() {
6a711d
@@ -358,8 +370,12 @@ impl Step for StartupObjects {
6a711d
                                     builder.cc(target),
6a711d
                                     target,
6a711d
                                     obj);
6a711d
-            builder.copy(&src, &sysroot_dir.join(obj));
6a711d
+            let target = sysroot_dir.join(obj);
6a711d
+            builder.copy(&src, &target);
6a711d
+            target_deps.push(target);
6a711d
         }
6a711d
+
6a711d
+        target_deps
6a711d
     }
6a711d
 }
6a711d
 
6a711d
@@ -437,6 +453,7 @@ impl Step for Rustc {
6a711d
                   cargo,
6a711d
                   vec![],
6a711d
                   &librustc_stamp(builder, compiler, target),
6a711d
+                  vec![],
6a711d
                   false);
6a711d
 
6a711d
         builder.ensure(RustcLink {
6a711d
@@ -585,7 +602,7 @@ impl Step for CodegenBackend {
6a711d
 
6a711d
         let tmp_stamp = out_dir.join(".tmp.stamp");
6a711d
 
6a711d
-        let files = run_cargo(builder, cargo, vec![], &tmp_stamp, false);
6a711d
+        let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false);
6a711d
         if builder.config.dry_run {
6a711d
             return;
6a711d
         }
6a711d
@@ -941,6 +958,7 @@ pub fn run_cargo(builder: &Builder<'_>,
6a711d
                  cargo: Cargo,
6a711d
                  tail_args: Vec<String>,
6a711d
                  stamp: &Path,
6a711d
+                 additional_target_deps: Vec<PathBuf>,
6a711d
                  is_check: bool)
6a711d
     -> Vec<PathBuf>
6a711d
 {
6a711d
@@ -1057,6 +1075,7 @@ pub fn run_cargo(builder: &Builder<'_>,
6a711d
         deps.push((path_to_add.into(), false));
6a711d
     }
6a711d
 
6a711d
+    deps.extend(additional_target_deps.into_iter().map(|d| (d, false)));
6a711d
     deps.sort();
6a711d
     let mut new_contents = Vec::new();
6a711d
     for (dep, proc_macro) in deps.iter() {
6a711d
diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs
6a711d
index 514ad1144491..93143570b0fe 100644
6a711d
--- a/src/bootstrap/dist.rs
6a711d
+++ b/src/bootstrap/dist.rs
6a711d
@@ -637,6 +637,28 @@ impl Step for DebuggerScripts {
6a711d
     }
6a711d
 }
6a711d
 
6a711d
+fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
6a711d
+    // The only true set of target libraries came from the build triple, so
6a711d
+    // let's reduce redundant work by only producing archives from that host.
6a711d
+    if compiler.host != builder.config.build {
6a711d
+        builder.info("\tskipping, not a build host");
6a711d
+        true
6a711d
+    } else {
6a711d
+        false
6a711d
+    }
6a711d
+}
6a711d
+
6a711d
+/// Copy stamped files into an image's `target/lib` directory.
6a711d
+fn copy_target_libs(builder: &Builder<'_>, target: &str, image: &Path, stamp: &Path) {
6a711d
+    let dst = image.join("lib/rustlib").join(target).join("lib");
6a711d
+    t!(fs::create_dir_all(&dst);;
6a711d
+    for (path, host) in builder.read_stamp_file(stamp) {
6a711d
+        if !host || builder.config.build == target {
6a711d
+            builder.copy(&path, &dst.join(path.file_name().unwrap()));
6a711d
+        }
6a711d
+    }
6a711d
+}
6a711d
+
6a711d
 #[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
6a711d
 pub struct Std {
6a711d
     pub compiler: Compiler,
6a711d
@@ -667,44 +689,19 @@ impl Step for Std {
6a711d
         let target = self.target;
6a711d
 
6a711d
         let name = pkgname(builder, "rust-std");
6a711d
-
6a711d
-        // The only true set of target libraries came from the build triple, so
6a711d
-        // let's reduce redundant work by only producing archives from that host.
6a711d
-        if compiler.host != builder.config.build {
6a711d
-            builder.info("\tskipping, not a build host");
6a711d
-            return distdir(builder).join(format!("{}-{}.tar.gz", name, target));
6a711d
+        let archive = distdir(builder).join(format!("{}-{}.tar.gz", name, target));
6a711d
+        if skip_host_target_lib(builder, compiler) {
6a711d
+            return archive;
6a711d
         }
6a711d
 
6a711d
-        // We want to package up as many target libraries as possible
6a711d
-        // for the `rust-std` package, so if this is a host target we
6a711d
-        // depend on librustc and otherwise we just depend on libtest.
6a711d
-        if builder.hosts.iter().any(|t| t == target) {
6a711d
-            builder.ensure(compile::Rustc { compiler, target });
6a711d
-        } else {
6a711d
-            builder.ensure(compile::Std { compiler, target });
6a711d
-        }
6a711d
+        builder.ensure(compile::Std { compiler, target });
6a711d
 
6a711d
         let image = tmpdir(builder).join(format!("{}-{}-image", name, target));
6a711d
         let _ = fs::remove_dir_all(&image);
6a711d
 
6a711d
-        let dst = image.join("lib/rustlib").join(target);
6a711d
-        t!(fs::create_dir_all(&dst);;
6a711d
-        let mut src = builder.sysroot_libdir(compiler, target).to_path_buf();
6a711d
-        src.pop(); // Remove the trailing /lib folder from the sysroot_libdir
6a711d
-        builder.cp_filtered(&src, &dst, &|path| {
6a711d
-            if let Some(name) = path.file_name().and_then(|s| s.to_str()) {
6a711d
-                if name == builder.config.rust_codegen_backends_dir.as_str() {
6a711d
-                    return false
6a711d
-                }
6a711d
-                if name == "bin" {
6a711d
-                    return false
6a711d
-                }
6a711d
-                if name.contains("LLVM") {
6a711d
-                    return false
6a711d
-                }
6a711d
-            }
6a711d
-            true
6a711d
-        });
6a711d
+        let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
6a711d
+        let stamp = compile::libstd_stamp(builder, compiler_to_use, target);
6a711d
+        copy_target_libs(builder, &target, &image, &stamp);
6a711d
 
6a711d
         let mut cmd = rust_installer(builder);
6a711d
         cmd.arg("generate")
6a711d
@@ -723,7 +720,73 @@ impl Step for Std {
6a711d
         let _time = timeit(builder);
6a711d
         builder.run(&mut cmd);
6a711d
         builder.remove_dir(&image);
6a711d
-        distdir(builder).join(format!("{}-{}.tar.gz", name, target))
6a711d
+        archive
6a711d
+    }
6a711d
+}
6a711d
+
6a711d
+#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
6a711d
+pub struct RustcDev {
6a711d
+    pub compiler: Compiler,
6a711d
+    pub target: Interned<String>,
6a711d
+}
6a711d
+
6a711d
+impl Step for RustcDev {
6a711d
+    type Output = PathBuf;
6a711d
+    const DEFAULT: bool = true;
6a711d
+    const ONLY_HOSTS: bool = true;
6a711d
+
6a711d
+    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
6a711d
+        run.path("rustc-dev")
6a711d
+    }
6a711d
+
6a711d
+    fn make_run(run: RunConfig<'_>) {
6a711d
+        run.builder.ensure(RustcDev {
6a711d
+            compiler: run.builder.compiler_for(
6a711d
+                run.builder.top_stage,
6a711d
+                run.builder.config.build,
6a711d
+                run.target,
6a711d
+            ),
6a711d
+            target: run.target,
6a711d
+        });
6a711d
+    }
6a711d
+
6a711d
+    fn run(self, builder: &Builder<'_>) -> PathBuf {
6a711d
+        let compiler = self.compiler;
6a711d
+        let target = self.target;
6a711d
+
6a711d
+        let name = pkgname(builder, "rustc-dev");
6a711d
+        let archive = distdir(builder).join(format!("{}-{}.tar.gz", name, target));
6a711d
+        if skip_host_target_lib(builder, compiler) {
6a711d
+            return archive;
6a711d
+        }
6a711d
+
6a711d
+        builder.ensure(compile::Rustc { compiler, target });
6a711d
+
6a711d
+        let image = tmpdir(builder).join(format!("{}-{}-image", name, target));
6a711d
+        let _ = fs::remove_dir_all(&image);
6a711d
+
6a711d
+        let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
6a711d
+        let stamp = compile::librustc_stamp(builder, compiler_to_use, target);
6a711d
+        copy_target_libs(builder, &target, &image, &stamp);
6a711d
+
6a711d
+        let mut cmd = rust_installer(builder);
6a711d
+        cmd.arg("generate")
6a711d
+           .arg("--product-name=Rust")
6a711d
+           .arg("--rel-manifest-dir=rustlib")
6a711d
+           .arg("--success-message=Rust-is-ready-to-develop.")
6a711d
+           .arg("--image-dir").arg(&image)
6a711d
+           .arg("--work-dir").arg(&tmpdir(builder))
6a711d
+           .arg("--output-dir").arg(&distdir(builder))
6a711d
+           .arg(format!("--package-name={}-{}", name, target))
6a711d
+           .arg(format!("--component-name=rustc-dev-{}", target))
6a711d
+           .arg("--legacy-manifest-dirs=rustlib,cargo");
6a711d
+
6a711d
+        builder.info(&format!("Dist rustc-dev stage{} ({} -> {})",
6a711d
+            compiler.stage, &compiler.host, target));
6a711d
+        let _time = timeit(builder);
6a711d
+        builder.run(&mut cmd);
6a711d
+        builder.remove_dir(&image);
6a711d
+        archive
6a711d
     }
6a711d
 }
6a711d
 
6a711d
diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs
6a711d
index a182405f3b2d..d1cf1cbca784 100644
6a711d
--- a/src/bootstrap/lib.rs
6a711d
+++ b/src/bootstrap/lib.rs
6a711d
@@ -1137,6 +1137,7 @@ impl Build {
6a711d
     pub fn copy(&self, src: &Path, dst: &Path) {
6a711d
         if self.config.dry_run { return; }
6a711d
         self.verbose_than(1, &format!("Copy {:?} to {:?}", src, dst));
6a711d
+        if src == dst { return; }
6a711d
         let _ = fs::remove_file(&dst);
6a711d
         let metadata = t!(src.symlink_metadata());
6a711d
         if metadata.file_type().is_symlink() {
6a711d
diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs
6a711d
index f41e7dd17ede..c0d2deab2f8b 100644
6a711d
--- a/src/tools/build-manifest/src/main.rs
6a711d
+++ b/src/tools/build-manifest/src/main.rs
6a711d
@@ -399,6 +399,7 @@ impl Builder {
6a711d
     fn add_packages_to(&mut self, manifest: &mut Manifest) {
6a711d
         let mut package = |name, targets| self.package(name, &mut manifest.pkg, targets);
6a711d
         package("rustc", HOSTS);
6a711d
+        package("rustc-dev", HOSTS);
6a711d
         package("cargo", HOSTS);
6a711d
         package("rust-mingw", MINGW);
6a711d
         package("rust-std", TARGETS);
6a711d
@@ -426,6 +427,13 @@ impl Builder {
6a711d
             "rls-preview", "rust-src", "llvm-tools-preview",
6a711d
             "lldb-preview", "rust-analysis", "miri-preview"
6a711d
         ]);
6a711d
+
6a711d
+        // The compiler libraries are not stable for end users, but `rustc-dev` was only recently
6a711d
+        // split out of `rust-std`. We'll include it by default as a transition for nightly users.
6a711d
+        if self.rust_release == "nightly" {
6a711d
+            self.extend_profile("default", &mut manifest.profiles, &["rustc-dev"]);
6a711d
+            self.extend_profile("complete", &mut manifest.profiles, &["rustc-dev"]);
6a711d
+        }
6a711d
     }
6a711d
 
6a711d
     fn add_renames_to(&self, manifest: &mut Manifest) {
6a711d
@@ -481,6 +489,15 @@ impl Builder {
6a711d
             components.push(host_component("rust-mingw"));
6a711d
         }
6a711d
 
6a711d
+        // The compiler libraries are not stable for end users, but `rustc-dev` was only recently
6a711d
+        // split out of `rust-std`. We'll include it by default as a transition for nightly users,
6a711d
+        // but ship it as an optional component on the beta and stable channels.
6a711d
+        if self.rust_release == "nightly" {
6a711d
+            components.push(host_component("rustc-dev"));
6a711d
+        } else {
6a711d
+            extensions.push(host_component("rustc-dev"));
6a711d
+        }
6a711d
+
6a711d
         // Tools are always present in the manifest,
6a711d
         // but might be marked as unavailable if they weren't built.
6a711d
         extensions.extend(vec![
6a711d
@@ -498,6 +515,11 @@ impl Builder {
6a711d
                 .filter(|&&target| target != host)
6a711d
                 .map(|target| Component::from_str("rust-std", target))
6a711d
         );
6a711d
+        extensions.extend(
6a711d
+            HOSTS.iter()
6a711d
+                .filter(|&&target| target != host)
6a711d
+                .map(|target| Component::from_str("rustc-dev", target))
6a711d
+        );
6a711d
         extensions.push(Component::from_str("rust-src", "*"));
6a711d
 
6a711d
         // If the components/extensions don't actually exist for this
6a711d
@@ -534,6 +556,14 @@ impl Builder {
6a711d
         dst.insert(profile_name.to_owned(), pkgs.iter().map(|s| (*s).to_owned()).collect());
6a711d
     }
6a711d
 
6a711d
+    fn extend_profile(&mut self,
6a711d
+               profile_name: &str,
6a711d
+               dst: &mut BTreeMap<String, Vec<String>>,
6a711d
+               pkgs: &[&str]) {
6a711d
+        dst.get_mut(profile_name).expect("existing profile")
6a711d
+            .extend(pkgs.iter().map(|s| (*s).to_owned()));
6a711d
+    }
6a711d
+
6a711d
     fn package(&mut self,
6a711d
                pkgname: &str,
6a711d
                dst: &mut BTreeMap<String, Package>,