Cross compilation from Linux?
Closed this issue · 5 comments
I know this project is currently experimental, and this isn't a priority, but I figured I'd document in case someone else runs into this.
I'm trying to compile the example in the README from WSL using Linux cross compilation, which includes installing MinGW:
rustup target add x86_64-pc-windows-gnu
rustup toolchain install stable-x86_64-pc-windows-gnu
sudo apt-get install mingw-w64
This seems to almost work (because windows-rs
supports cross compilation, it seems) but fails when the linker tries to find the app runtime:
= note: /usr/bin/x86_64-w64-mingw32-ld: cannot find -lmicrosoft.windowsappruntime.bootstrap
collect2: error: ld returned 1 exit status
error: could not compile `rust_windows` due to previous error
I'm really not sure how this could be solved, but it would be nice to have the workflow in WSL more clear. Thanks!
There issue is a build script needs to be run on the host system (linux), but it contains Windows specific code. It can be fixed with these changes:
- Link names should be case sensitive matches with the dll that is written from the build script (
Microsoft.WindowsAppRuntime.Bootstrap.dll
) - The code to be executed from a build script should not be inside the main crate which contains Windows dependencies, or the main "runtime" code should be
cfg
ed out.
See this branch with above changes that make compilation work on linux: https://github.com/JasperDeSutter/windows-app-rs/tree/linux-support
This is super helpful, I appreciate it a lot. I cloned that branch of your repo. I'm still running into issues running the executable, but here's what I tried:
GNU toolchain
Note that I needed to update to the nightly (i.e. rustup install nightly
+ the above target adds for people new to Rust like me :) ) to remove build errors in the windows crate.
$ cargo build --target x86_64-pc-windows-gnu
...
= note: /usr/bin/x86_64-w64-mingw32-ld: cannot find -lMicrosoft.WindowsAppRuntime.Bootstrap
collect2: error: ld returned 1 exit status
I do note that I have the right file showing up in the folder, but I guess I just can't link to it?
$ file target/x86_64-pc-windows-gnu/debug/Microsoft.WindowsAppRuntime.Bootstrap.dll
target/x86_64-pc-windows-gnu/debug/Microsoft.WindowsAppRuntime.Bootstrap.dll: PE32+ executable (DLL) (GUI) x86-64, for MS Windows
So I added the location manually to RUSTFLAGS (which is a bit hacky, so probably not the final solution):
Now this builds!
$ cargo build --target x86_64-pc-windows-gnu
...
Finished dev [unoptimized + debuginfo] target(s) in 41.77s
This is great, but the final .exe isn't runnable yet from WSL:
$ ./target/x86_64-pc-windows-gnu/debug/sample_xamlapp.exe
Error: Error { code: 0x80070491, message: There was no match for the specified key in the index.
}
and if I copy it out into my Windows installation and try to run it, I get:
Note that I have installed the Windows App Runtime as indicated on the README.
I tried statically linking, by adding this to my .cargo/config.toml
:
[target.x86_64-pc-windows-gnu]
rustflags = ["-C", "target-feature=+crt-static"]
but no cigar.
MSVC toolchain
I was able to compile with the MSVC toolchain, after installing xwin
as described here:
$ cargo build --target x86_64-pc-windows-msvc
...
Finished dev [unoptimized + debuginfo] target(s) in 41.41s
But this fails for the above reasons as well.
I kind of get the sense that these are basic issues, but unfortunately I'm unable to figure it out, so if anyone has an idea I would appreciate the help!
Ok, got it to run. The above issue was solved after using the steps here: microsoft/WindowsAppSDK#1953; in particular, I:
- Uninstalled the v1.0.0 Windows App SDK Runtime I had installed from the link in the README
- Downloaded version 1.0.3 (I guess v1.1.0 doesn't work) from here
- Ran the installer as administrator
And the expected app ran both in the WSL environment and in Windows, as expected.
Ideas for improving this process:
- Please incorporate the (small) fixes in @JasperDeSutter's branch.
- Change the README to include a link to v1.0.3 instead of v1.0.0 (which has the known bug above)
- Include some details somewhere on the cross compilation process (maybe using my steps as a guide?), including a description of how to set up either of the MSVC or GNU targets.
- (which I don't know anything about) include details on how the GNU toolchain finds the appropriate DLL (I guess it is part of MINGW?)
I'm happy to do any of those (except 4) if you'd prefer a pull request.
Hey all, sorry I didn't reply sooner--lots of things moving around on my side. The README is pointing to a link that I thought was going to continue getting updates but I see I was wrong. It should be pointing to the latest 1.0.3 bits as you painfully discovered.
I'll get the crate updated right now.
Thank you!