Skip to content

Commit e249812

Browse files
authored
Rollup merge of #92134 - nico-abram:patch-1, r=michaelwoerister
Add x86_64-pc-windows-msvc linker-plugin-lto instructions I had some trouble getting cross language LTO working for this target, in part because the very few links of documentation I could find were linux-centric and because of a few very specific errors I ran into. I'm not sure if this is the correct place to document this, but this is one of the first links I found when looking for documentation so it might be the best place for it.
2 parents 75eb963 + b75cb95 commit e249812

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/doc/rustc/src/linker-plugin-lto.md

+42
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,48 @@ option:
8686
rustc -Clinker-plugin-lto="/path/to/LLVMgold.so" -L. -Copt-level=2 ./main.rs
8787
```
8888

89+
### Usage with clang-cl and x86_64-pc-windows-msvc
90+
91+
Cross language LTO can be used with the x86_64-pc-windows-msvc target, but this requires using the
92+
clang-cl compiler instead of the MSVC cl.exe included with Visual Studio Build Tools, and linking
93+
with lld-link. Both clang-cl and lld-link can be downloaded from [LLVM's download page](https://releases.llvm.org/download.html).
94+
Note that most crates in the ecosystem are likely to assume you are using cl.exe if using this target
95+
and that some things, like for example vcpkg, [don't work very well with clang-cl](https://github.com/microsoft/vcpkg/issues/2087).
96+
97+
You will want to make sure your rust major LLVM version matches your installed LLVM tooling version,
98+
otherwise it is likely you will get linker errors:
99+
100+
```bat
101+
rustc -V --verbose
102+
clang-cl --version
103+
```
104+
105+
If you are compiling any proc-macros, you will get this error:
106+
107+
```bash
108+
error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when
109+
targeting Windows-like targets
110+
```
111+
112+
This is fixed if you explicitly set the target, for example
113+
`cargo build --target x86_64-pc-windows-msvc`
114+
Without an explicit --target the flags will be passed to all compiler invocations (including build
115+
scripts and proc macros), see [cargo docs on rustflags](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags)
116+
117+
If you have dependencies using the `cc` crate, you will need to set these
118+
environment variables:
119+
```bat
120+
set CC=clang-cl
121+
set CXX=clang-cl
122+
set CFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link
123+
set CXXFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link
124+
REM Needed because msvc's lib.exe crashes on LLVM LTO .obj files
125+
set AR=llvm-lib
126+
```
127+
128+
If you are specifying lld-link as your linker by setting `linker = "lld-link.exe"` in your cargo config,
129+
you may run into issues with some crates that compile code with separate cargo invocations. You should be
130+
able to get around this problem by setting `-Clinker=lld-link` in RUSTFLAGS
89131

90132
## Toolchain Compatibility
91133

0 commit comments

Comments
 (0)