Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Cross compilation from Linux? #41

Closed
rachtsingh opened this issue Jun 8, 2022 · 5 comments · Fixed by #46
Closed

Cross compilation from Linux? #41

rachtsingh opened this issue Jun 8, 2022 · 5 comments · Fixed by #46
Labels
enhancement New feature or request

Comments

@rachtsingh
Copy link

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!

@JasperDeSutter
Copy link

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 cfged out.

See this branch with above changes that make compilation work on linux: https://github.com/JasperDeSutter/windows-app-rs/tree/linux-support

@rachtsingh
Copy link
Author

rachtsingh commented Jun 13, 2022

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:

image

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!

@rachtsingh
Copy link
Author

rachtsingh commented Jun 14, 2022

Ok, got it to run. The above issue was solved after using the steps here: microsoft/WindowsAppSDK#1953; in particular, I:

  1. Uninstalled the v1.0.0 Windows App SDK Runtime I had installed from the link in the README
  2. Downloaded version 1.0.3 (I guess v1.1.0 doesn't work) from here
  3. 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:

  1. Please incorporate the (small) fixes in @JasperDeSutter's branch.
  2. Change the README to include a link to v1.0.3 instead of v1.0.0 (which has the known bug above)
  3. 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.
  4. (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.

@riverar
Copy link
Collaborator

riverar commented Jun 14, 2022

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.

@riverar riverar added the enhancement New feature or request label Jun 21, 2022
@rachtsingh
Copy link
Author

Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants