From 7430a55708fb78ff7e5600f94aa68ddbce78082f Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 20 Dec 2021 16:26:28 -0300 Subject: [PATCH 1/4] Add x86_64-pc-windows-msvc linker-plugin-lto instructions --- src/doc/rustc/src/linker-plugin-lto.md | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/doc/rustc/src/linker-plugin-lto.md b/src/doc/rustc/src/linker-plugin-lto.md index 18f1be6a1fa..e9586cb3e11 100644 --- a/src/doc/rustc/src/linker-plugin-lto.md +++ b/src/doc/rustc/src/linker-plugin-lto.md @@ -86,6 +86,48 @@ option: rustc -Clinker-plugin-lto="/path/to/LLVMgold.so" -L. -Copt-level=2 ./main.rs ``` +### Usage with clang-cl and --target x86_64-pc-windows-msvc + +Cross language LTO can be used with the x86_64-pc-windows-msvc target, but this requires using the +clang-cl compiler instead of the MSVC cl.exe included with Visual Studio Build Tools, and linking +with lld-link. Both clang-cl and lld-link can be downloaded from [LLVM's download page](https://releases.llvm.org/download.html). +Note that most crates in the ecosystem are likely to assume you are using cl.exe if using this target +and that some things, like for example vcpkg, [don't work very well with clang-cl](https://github.com/microsoft/vcpkg/issues/2087). + +You will want to make sure your rust major LLVM version matches your installed LLVM tooling version, +otherwise it is likely you will get linker errors: + +```bat +rustc -V --verbose +clang-cl --version +``` + +If you are compiling any proc-macros, you will get this error: + +``` +error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when +targeting Windows-like targets +``` + +This is fixed if you explicitly set the target, for example +`cargo build --target x86_64-pc-windows-msvc` +Without an explicit --target the flags will be passed to all compiler invocations (including build +scripts and proc macros), see [cargo docs on rustflags](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags) + +If you have dependencies using the `cc` crate, you will need to set these +environment variables: +```bat +set CC=clang-cl +set CXX=clang-cl +set CFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link +set CXXFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link +REM Needed because msvc's lib.exe crashes on LLVM LTO .obj files +set AR=llvm-lib +``` + +If you are specifying lld-link as your linker by setting `linker = "lld-link.exe"` in your cargo config, +you may run into issues with some crates that compile code with separate cargo invocations. You should be +able to get around this problem by setting `-Clinker=lld-link` in RUSTFLAGS ## Toolchain Compatibility From 55b61081600eb5c369115f137a24dfe5ce3b184c Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 20 Dec 2021 16:46:09 -0300 Subject: [PATCH 2/4] Update linker-plugin-lto.md --- src/doc/rustc/src/linker-plugin-lto.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/rustc/src/linker-plugin-lto.md b/src/doc/rustc/src/linker-plugin-lto.md index e9586cb3e11..9ecf42ac240 100644 --- a/src/doc/rustc/src/linker-plugin-lto.md +++ b/src/doc/rustc/src/linker-plugin-lto.md @@ -105,13 +105,13 @@ clang-cl --version If you are compiling any proc-macros, you will get this error: ``` -error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when +error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when targeting Windows-like targets ``` -This is fixed if you explicitly set the target, for example +This is fixed if you explicitly set the target, for example `cargo build --target x86_64-pc-windows-msvc` -Without an explicit --target the flags will be passed to all compiler invocations (including build +Without an explicit --target the flags will be passed to all compiler invocations (including build scripts and proc macros), see [cargo docs on rustflags](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags) If you have dependencies using the `cc` crate, you will need to set these From 032c545427efb279c85843f1434059de13785f60 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 20 Dec 2021 17:48:17 -0300 Subject: [PATCH 3/4] Update linker-plugin-lto.md --- src/doc/rustc/src/linker-plugin-lto.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/rustc/src/linker-plugin-lto.md b/src/doc/rustc/src/linker-plugin-lto.md index 9ecf42ac240..ab261319894 100644 --- a/src/doc/rustc/src/linker-plugin-lto.md +++ b/src/doc/rustc/src/linker-plugin-lto.md @@ -104,7 +104,7 @@ clang-cl --version If you are compiling any proc-macros, you will get this error: -``` +```bash error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when targeting Windows-like targets ``` From b75cb95c269231e4e065dccc27bcf63543550325 Mon Sep 17 00:00:00 2001 From: Nicolas Abram Date: Tue, 25 Jan 2022 20:19:43 -0300 Subject: [PATCH 4/4] Update src/doc/rustc/src/linker-plugin-lto.md Co-authored-by: Noah Lev --- src/doc/rustc/src/linker-plugin-lto.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/rustc/src/linker-plugin-lto.md b/src/doc/rustc/src/linker-plugin-lto.md index ab261319894..941c65922d8 100644 --- a/src/doc/rustc/src/linker-plugin-lto.md +++ b/src/doc/rustc/src/linker-plugin-lto.md @@ -86,7 +86,7 @@ option: rustc -Clinker-plugin-lto="/path/to/LLVMgold.so" -L. -Copt-level=2 ./main.rs ``` -### Usage with clang-cl and --target x86_64-pc-windows-msvc +### Usage with clang-cl and x86_64-pc-windows-msvc Cross language LTO can be used with the x86_64-pc-windows-msvc target, but this requires using the clang-cl compiler instead of the MSVC cl.exe included with Visual Studio Build Tools, and linking