Search code examples
rustcross-compilingvst

How to cross-compile Rust to Windows using xwin and xtask


I'm trying to cross-compile an example audio plugin from Nih-plug from Linux (Ubuntu) to Windows.

Cross-compiling works when using the target x86_64-pc-windows-gnu (Mingw-w64), but the binaries produced aren't compatible with Win7, because they call the entry point waitonaddress api-ms-win-core-synch-l1-2-0.dll. I think Mingw-w64 is the culprit.

I would like to try to cross-compile using msvc instead of gnu; cargo-xwin offers to do this.

However, Nih-plug uses xtask to create specific binaries (vst3 and clap) via a "bundle" task, and it seems xwin only lets one use the standard cargo build command.

How would one proceed to use cargo-xwin with xtask?


Solution

  • Trying to use cargo-xwin combined with xtask is probably possible but quite difficult, esp. when one cannot directly access the xtask script to change the cargo command. (Both GPT4o and Gemini 1.5 couldn't find a solution.)

    The approach is in fact quite straightforward, it's to NOT use cargo-xwin at all, but instead target x86_64-pc-windows-msvc in the compilation command.

    But just installing the target with rustup isn't enough (that will result in the error linker 'link.exe' not found). The proper approach is documented here and is as follows:

    • add the target to rust rustup target add x86_64-pc-windows-msvc

    • install xwin cargo install xwin

    • install xwin to the user's home folder xwin --accept-license splat --output ~/.xwin

    • declare the linker: add the following to .cargo/config (not cargo.toml), and using absolute paths (not ~ for home; replace "me" with your user name)

      [target.x86_64-pc-windows-msvc]
      linker = "lld"
      rustflags = [
        "-Lnative=/home/me/.xwin/crt/lib/x86_64",
        "-Lnative=/home/me/.xwin/sdk/lib/um/x86_64",
        "-Lnative=/home/me/.xwin/sdk/lib/ucrt/x86_64"
      ]
      

    And then it just works!

    (It doesn't solve my problem though because at present the whole chain seems to target Win8.1 (?), and the executables produced aren't compatible with Win7; but at least it compiles with no error.)