Repository: rust
Issue: support for bootstrapping on linux with `target = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]`
Labels: C-enhancement,T-compiler,T-bootstrap

Spawned off of https://zulip-archive.rust-lang.org/131828tcompiler/10990bootstraponlinuxwsupportforcrosscompilingtowind.html

After being reminded that one can install the stdlib for windows-msvc on a Linux host, that led me to ask whether one could build that artifact locally.

I tried to do so, but failed. You can see the results of some questions I asked and suggestions made in response at the above chat thread.

I don't think we need to make this support 100% transparent; e.g., it seems okay to force people to *also* specify that they don't want any dylibs, just rlibs. (Though it might be nice if there were a `config.toml` line for this rather than being forced to edit the `Config.toml` for a bunch of stdlib crates.)

In any case, I couldn't figure out how to get it to work, so this issue is meant to track effort to make this possible, and, if necessary, document what other changes a developer would need to make to their local setup (such as the aforementioned `rlib` thing) to get it going.

(The current roadblock is that the attempt to build compiler-builtins is trying to add `/Zl` as an option, which does not work on this host.)

<details>
<summary>Click for more info on what changes I made locally to get as far as I could</summary>

```console
% git diff --no-color && diff -u ../config.toml.example config.toml ; time ../x.py build --stage 1
diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml
index 474765d8638..ed09312cb84 100644
--- a/library/std/Cargo.toml
+++ b/library/std/Cargo.toml
@@ -8,7 +8,7 @@ description = "The Rust Standard Library"
 edition = "2018"
 
 [lib]
-crate-type = ["dylib", "rlib"]
+crate-type = ["rlib"]
 
 [dependencies]
 alloc = { path = "../alloc" }
diff --git a/library/test/Cargo.toml b/library/test/Cargo.toml
index 7b76dc83aa2..42bfdb526c5 100644
--- a/library/test/Cargo.toml
+++ b/library/test/Cargo.toml
@@ -5,7 +5,7 @@ version = "0.0.0"
 edition = "2018"
 
 [lib]
-crate-type = ["dylib", "rlib"]
+crate-type = ["rlib"]
 
 [dependencies]
 cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] }
diff --git a/src/bootstrap/sanity.rs b/src/bootstrap/sanity.rs
index f89bef50de9..f319d6194d5 100644
--- a/src/bootstrap/sanity.rs
+++ b/src/bootstrap/sanity.rs
@@ -217,13 +217,14 @@ pub fn check(build: &mut Build) {
             }
         }
 
-        if target.contains("msvc") {
+        if building_llvm && build.hosts.iter().any(|host|host.contains("msvc"))
+        {
             // There are three builds of cmake on windows: MSVC, MinGW, and
             // Cygwin. The Cygwin build does not have generators for Visual
             // Studio, so detect that here and error.
             let out = output(Command::new("cmake").arg("--help"));
             if !out.contains("Visual Studio") {
-                panic!(
+                eprintln!(
                     "
 cmake does not support Visual Studio generators.
 
--- ../config.toml.example	2020-08-04 11:10:07.940144080 -0400
+++ config.toml	2020-08-04 11:23:11.839046744 -0400
@@ -136,6 +136,7 @@
 #
 # Defaults to just the build triple
 #target = ["x86_64-unknown-linux-gnu"]
+target = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
 
 # Use this directory to store build artifacts.
 # You can use "$ROOT" to indicate the root of the git repository.
@@ -525,6 +526,7 @@
 # compiler itself. This is useful for building rustc on targets that normally
 # only use static libraries. If unset, the target's default linkage is used.
 #crt-static = false
+crt-static = true
 
 # The root location of the musl installation directory. The library directory
 # will also need to contain libunwind.a for an unwinding implementation. Note
Updating only changed submodules
Submodules updated in 0.03 seconds
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
Building stage0 std artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
    Finished release [optimized] target(s) in 0.08s
Copying stage0 std from stage0 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
Building stage0 compiler artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
    Finished release [optimized] target(s) in 0.13s
Copying stage0 rustc from stage0 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
Assembling stage1 compiler (x86_64-unknown-linux-gnu)
Building stage1 std artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
    Finished release [optimized] target(s) in 0.08s
Copying stage1 std from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
Building stage1 std artifacts (x86_64-unknown-linux-gnu -> x86_64-pc-windows-msvc)
   Compiling core v0.0.0 (/home/pnkfelix/Dev/Mozilla/rust-xplat/library/core)
   Compiling libc v0.2.74
   Compiling compiler_builtins v0.1.32
The following warnings were emitted during compilation:

warning: cc: error: /Zl: No such file or directory

error: failed to run custom build command for `compiler_builtins v0.1.32`

Caused by:
  process didn't exit successfully: `/home/pnkfelix/Dev/Mozilla/rust-xplat/objdir/build/x86_64-unknown-linux-gnu/stage1-std/release/build/compiler_builtins-09dda507c59e8728/build-script-build` (exit code: 1)
  --- stdout
  cargo:rerun-if-changed=build.rs
  cargo:compiler-rt=/home/pnkfelix/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.32/compiler-rt
  cargo:rustc-cfg=feature="unstable"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/absvdi2.c
  cargo:rustc-cfg=__absvdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/absvsi2.c
  cargo:rustc-cfg=__absvsi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/absvti2.c
  cargo:rustc-cfg=__absvti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/addvdi3.c
  cargo:rustc-cfg=__addvdi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/addvsi3.c
  cargo:rustc-cfg=__addvsi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/addvti3.c
  cargo:rustc-cfg=__addvti3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/clzdi2.c
  cargo:rustc-cfg=__clzdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/clzsi2.c
  cargo:rustc-cfg=__clzsi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/clzti2.c
  cargo:rustc-cfg=__clzti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/cmpdi2.c
  cargo:rustc-cfg=__cmpdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/cmpti2.c
  cargo:rustc-cfg=__cmpti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ctzdi2.c
  cargo:rustc-cfg=__ctzdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ctzsi2.c
  cargo:rustc-cfg=__ctzsi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ctzti2.c
  cargo:rustc-cfg=__ctzti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/divdc3.c
  cargo:rustc-cfg=__divdc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/divsc3.c
  cargo:rustc-cfg=__divsc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/divxc3.c
  cargo:rustc-cfg=__divxc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/extendhfsf2.c
  cargo:rustc-cfg=__extendhfsf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ffsti2.c
  cargo:rustc-cfg=__ffsti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatdisf.c
  cargo:rustc-cfg=__floatdisf="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatdixf.c
  cargo:rustc-cfg=__floatdixf="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/int_util.c
  cargo:rustc-cfg=__int_util="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/muldc3.c
  cargo:rustc-cfg=__muldc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/mulsc3.c
  cargo:rustc-cfg=__mulsc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/mulvdi3.c
  cargo:rustc-cfg=__mulvdi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/mulvsi3.c
  cargo:rustc-cfg=__mulvsi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/mulvti3.c
  cargo:rustc-cfg=__mulvti3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/mulxc3.c
  cargo:rustc-cfg=__mulxc3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negdf2.c
  cargo:rustc-cfg=__negdf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negdi2.c
  cargo:rustc-cfg=__negdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negsf2.c
  cargo:rustc-cfg=__negsf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negti2.c
  cargo:rustc-cfg=__negti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negvdi2.c
  cargo:rustc-cfg=__negvdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negvsi2.c
  cargo:rustc-cfg=__negvsi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/negvti2.c
  cargo:rustc-cfg=__negvti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/paritydi2.c
  cargo:rustc-cfg=__paritydi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/paritysi2.c
  cargo:rustc-cfg=__paritysi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/parityti2.c
  cargo:rustc-cfg=__parityti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/popcountdi2.c
  cargo:rustc-cfg=__popcountdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/popcountsi2.c
  cargo:rustc-cfg=__popcountsi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/popcountti2.c
  cargo:rustc-cfg=__popcountti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/powixf2.c
  cargo:rustc-cfg=__powixf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/subvdi3.c
  cargo:rustc-cfg=__subvdi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/subvsi3.c
  cargo:rustc-cfg=__subvsi3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/subvti3.c
  cargo:rustc-cfg=__subvti3="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/truncdfhf2.c
  cargo:rustc-cfg=__truncdfhf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/truncdfsf2.c
  cargo:rustc-cfg=__truncdfsf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/truncsfhf2.c
  cargo:rustc-cfg=__truncsfhf2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ucmpdi2.c
  cargo:rustc-cfg=__ucmpdi2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/ucmpti2.c
  cargo:rustc-cfg=__ucmpti2="optimized-c"
  cargo:rerun-if-changed=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/apple_versioning.c
  cargo:rustc-cfg=apple_versioning="optimized-c"
  TARGET = Some("x86_64-pc-windows-msvc")
  OPT_LEVEL = Some("3")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_x86_64-pc-windows-msvc = None
  CC_x86_64_pc_windows_msvc = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-pc-windows-msvc = None
  CFLAGS_x86_64_pc_windows_msvc = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CARGO_CFG_TARGET_FEATURE = Some("crt-static,fxsr,mmx,sse,sse2")
  CC_x86_64-pc-windows-msvc = None
  CC_x86_64_pc_windows_msvc = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-pc-windows-msvc = None
  CFLAGS_x86_64_pc_windows_msvc = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  CARGO_CFG_TARGET_FEATURE = Some("crt-static,fxsr,mmx,sse,sse2")
  running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-m64" "-static" "/Zl" "-ffile-prefix-map=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt=." "-D__func__=__FUNCTION__" "-Fo/home/pnkfelix/Dev/Mozilla/rust-xplat/objdir/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-pc-windows-msvc/release/build/compiler_builtins-53591ff0903ba093/out/absvdi2.o" "-c" "/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/absvdi2.c"
  cargo:warning=cc: error: /Zl: No such file or directory
  exit code: 1

  --- stderr


  error occurred: Command "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-m64" "-static" "/Zl" "-ffile-prefix-map=/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt=." "-D__func__=__FUNCTION__" "-Fo/home/pnkfelix/Dev/Mozilla/rust-xplat/objdir/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-pc-windows-msvc/release/build/compiler_builtins-53591ff0903ba093/out/absvdi2.o" "-c" "/home/pnkfelix/Dev/Mozilla/rust-xplat/src/llvm-project/compiler-rt/lib/builtins/absvdi2.c" with args "cc" did not execute successfully (status code exit code: 1).


warning: build failed, waiting for other jobs to finish...
error: build failed
command did not execute successfully: "/home/pnkfelix/Dev/Mozilla/rust-xplat/objdir/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-pc-windows-msvc" "-Zbinary-dep-depinfo" "-j" "128" "--release" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/home/pnkfelix/Dev/Mozilla/rust-xplat/library/test/Cargo.toml" "--message-format" "json-render-diagnostics"
expected success, got: exit code: 101
failed to run: /home/pnkfelix/Dev/Mozilla/rust-xplat/objdir/build/bootstrap/debug/bootstrap build --stage 1
Build completed unsuccessfully in 0:00:10

real	0m10.967s
user	0m15.619s
sys	0m0.656s
``` 

</details>