wravery / webview2-rs

Rust bindings for the WebView2 COM APIs
MIT License
51 stars 5 forks source link

Doesn't play nicely with cross compiling (via cargo cross) #5

Closed kdar closed 2 years ago

kdar commented 2 years ago

Hello! I was trying to compile wry with cargo cross from wsl2 to windows, and it fails on building webview2-com-sys due to this error:

error: could not find native static libraryWebView2LoaderStatic, perhaps an -L flag is missing?

When I looked at the build.rs script for that package, it seems to use the cfg attributes to test for windows or not windows. Unfortunately it doesn't play well with cross compiling (as explained here: https://kazlauskas.me/entries/writing-proper-buildrs-scripts).

Any thoughts?

wravery commented 2 years ago

Thanks for the pointer, I think that's exactly what it needs. 👍🏼

wravery commented 2 years ago

I have cross-compilation working to build the crates, but it only works for the msvc ABI. The static lib we're linking is not built with MinGW, so even though it adds the right directory to the path, the link step still says it can't find it with the gnu ABI:

> cargo build --target x86_64-pc-windows-gnu
   Compiling proc-macro2 v1.0.36
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.86
   Compiling memchr v2.4.1
   Compiling serde_derive v1.0.136
   Compiling windows_quote v0.30.0
   Compiling serde v1.0.136
   Compiling windows_reader v0.30.0
   Compiling serde_json v1.0.78
   Compiling itoa v1.0.1
   Compiling ryu v1.0.9
   Compiling windows_x86_64_gnu v0.30.0
   Compiling regex-syntax v0.6.25
   Compiling windows-bindgen v0.30.0
   Compiling windows_gen v0.30.0
   Compiling windows v0.30.0
   Compiling aho-corasick v0.7.18
   Compiling regex v1.5.4
   Compiling quote v1.0.15
   Compiling thiserror-impl v1.0.30
   Compiling webview2-com-macros v0.5.0 (/mnt/c/repos/wravery/webview2-rs/crates/callback-macros)
   Compiling windows_macros v0.30.0
   Compiling thiserror v1.0.30
   Compiling webview2-com-sys v0.12.0 (/mnt/c/repos/wravery/webview2-rs/crates/bindings)
   Compiling webview2-com v0.12.0 (/mnt/c/repos/wravery/webview2-rs/crates/webview2-com)
error: could not find native static library `WebView2LoaderStatic`, perhaps an -L flag is missing?

error: could not compile `webview2-com-sys` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed

Cross-compilation with the msvc ABI works for a lib crate, but ultimately it will complain that it can't find link.exe when it tries to build any executables:

> cargo build --target x86_64-pc-windows-msvc --example sample
   Compiling ryu v1.0.9
   Compiling regex-syntax v0.6.25
   Compiling itoa v1.0.1
   Compiling proc-macro2 v1.0.36
   Compiling syn v1.0.86
   Compiling serde_derive v1.0.136
   Compiling memchr v2.4.1
   Compiling serde v1.0.136
   Compiling serde_json v1.0.78
   Compiling windows_x86_64_msvc v0.30.0
   Compiling windows v0.30.0
   Compiling aho-corasick v0.7.18
   Compiling quote v1.0.15
   Compiling regex v1.5.4
   Compiling thiserror-impl v1.0.30
   Compiling windows_macros v0.30.0
   Compiling webview2-com-macros v0.5.0 (/mnt/c/repos/wravery/webview2-rs/crates/callback-macros)
   Compiling thiserror v1.0.30
   Compiling webview2-com-sys v0.12.0 (/mnt/c/repos/wravery/webview2-rs/crates/bindings)
   Compiling webview2-com v0.12.0 (/mnt/c/repos/wravery/webview2-rs/crates/webview2-com)
error: linker `link.exe` not found
  |
  = note: No such file or directory (os error 2)

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option

error: could not compile `webview2-com` due to previous error

It may be possible to work around that with a custom docker image for cargo cross that includes the Visual C++ build tools (it doesn't have a default docker image for this target). If so, that should make the msvc ABI a viable cross-compilation target.

Ultimately, I think the gnu ABI only works with import libs, so we'd need to switch the #[link(name = "WebView2LoaderStatic", kind = "static")] attribute on the generated bindings back to #[link(name = "WebView2Loader")] and copy the DLL and import lib in addition to the static lib. It would require a tweak to the winmd to enable that, and any cross-compiled binaries would need to carry the DLL with them wherever they went.