Skip to content

Commit

Permalink
Merge pull request #25 from wravery/update-without-rustfmt
Browse files Browse the repository at this point in the history
Replace NuGet download and bindgen in build script with update-bindings tool
  • Loading branch information
wravery authored Oct 16, 2023
2 parents c490952 + 7328441 commit 26e237a
Show file tree
Hide file tree
Showing 23 changed files with 397 additions and 354 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ Rust bindings for the WebView2 COM APIs

## Crates in this repo
The root of this repo defines a virtual workspace in [Cargo.toml](./Cargo.toml) which includes three crates:
- [webview2-com](./crates/webview2-com/README.md): The main crate, which depends on the other 2.
- [webview2-com](./crates/webview2-com/README.md): The main crate, which depends on `webview2-com-macros` and `webview2-com-sys`.
- [webview2-com-macros](./crates/callback-macros/README.md): Macros used to build boilerplate implementations of all the `ICoreWebView2...Handler` interfaces declared in `WebView2.h`.
- [webview2-com-sys](./crates/bindings/README.md): Unsafe bindings built with the [Windows](https://github.com/microsoft/windows-rs) crate.
- [update-bindings](./crates/update-bindings/README.md): Utility to regenerate the bindings in `webview2-com-sys` from a new `winmd` file.

## Windows Metadata
The Windows crate requires a Windows Metadata (`winmd`) file describing the API. The one used in this repo was generated with the [webview2-win32md](https://github.com/wravery/webview2-win32md) project.
Expand All @@ -24,15 +25,20 @@ Or run the sample app from anywhere in the repo:
See the [README.md](./crates/webview2-com/README.md) in `webview2-com` for more details about using that crate in your own project.

## Cross-compilation
The `webview2-com-sys` [build.rs](./crates/bindings/build.rs) script automatically downloads and extracts the NuGet package for the SDK and links against the libraries from that package. If you build on a Windows machine, you probably won't need to do anything special to enable this, but if you are building on a Linux or macOS machine, you need to have [mono](https://www.mono-project.com/) installed and on your `$PATH` to execute the `nuget.exe` CLI tool.
The [update-bindings](./crates/update-bindings/src/main.rs) tool automatically downloads and extracts the NuGet package for the SDK and links against the libraries from that package. If you build on a Windows machine, you probably won't need to do anything special to enable this, but if you are building on a Linux or macOS machine, you need to have [mono](https://www.mono-project.com/) installed and on your `$PATH` to execute the `nuget.exe` CLI tool.

By default this crate uses the `WebView2LoaderStatic.lib` static library and does not need to redistribute `WebView2Loader.dll`. However, this doesn't work with the `*-pc-windows-gnu` targets instead of `*-pc-windows-msvc` (for example, when cross-compiling). For the `gnu` targets, it will link against the import lib for `WebView2Loader.dll` instead, and you will need to place this DLL next to your executable or in your `$PATH` so it can find the DLL at runtime.

## Updating the WebView2 SDK
You can tell the build script to use a different version by updating `WEBVIEW2_VERSION` in `build.rs`:
You can tell the `update-bindings` tool to use a different version by updating `WEBVIEW2_VERSION` in `main.rs`:
```rust
const WEBVIEW2_VERSION: &str = "1.0.1938.49";
const WEBVIEW2_VERSION: &str = "1.0.2045.28";
```
It will also regenerate [callback_interfaces.rs](./crates/bindings/src/callback_interfaces.rs) if they change in a new version. This file is used in `webview2-com`, and in particular, the tests in [callback.rs](./crates/webview2-com/src/callback.rs) verify that all of the interfaces listed in `callback_interfaces.rs` are implemented. If a new version of the SDK declared additional callback interfaces, you will need to add those interfaces to `callback.rs` using the `#[completed_callback]` (for `ICoreWebView2...CompletedHandler` interfaces) and `#[event_callback]` (for `ICoreWebView2...EventHandler` interfaces) macros.

It does not regenerate the `winmd` file automatically because that would depend on having the `dotnet` CLI installed. New versions of the SDK should be backwards compatible, but you may want to regenerate the `Microsoft.Web.WebView2.Win32.winmd` file using `webview2-win32md` if you need functionality which was added in a new version. You should then copy the file to `./crates/bindings/winmd/Microsoft.Web.WebView2.Win32.winmd`, which is where the bindings build script looks for it.
It does not regenerate the `winmd` file automatically because that would depend on having the `dotnet` CLI installed. New versions of the SDK should be backwards compatible, but you may want to regenerate the `Microsoft.Web.WebView2.Win32.winmd` file using `webview2-win32md` if you need functionality which was added in a new version. You should then copy the file to `./crates/update-bindings/winmd/Microsoft.Web.WebView2.Win32.winmd`, which is where the `update-bindings` tool looks for it.

To run the `update-bindings` tool after updating the `winmd` file, build and run the tool without any additional arguments from the root of this repo:
```cmd
> cargo run update-bindings
```
16 changes: 1 addition & 15 deletions crates/bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webview2-com-sys"
version = "0.26.0"
version = "0.27.0"
edition = "2021"
rust-version = "1.61"
description = "Bindings generated with the windows crate for the WebView2 COM APIs"
Expand All @@ -10,7 +10,6 @@ keywords = [ "win32", "webview2" ]
categories = [ "os::windows-apis" ]

[package.metadata.docs.rs]
no-default-features = true
default-target = "x86_64-pc-windows-msvc"
targets = [
"i686-pc-windows-msvc",
Expand All @@ -19,10 +18,6 @@ targets = [
"i686-pc-windows-gnu",
]

[features]
"default" = [ "nuget" ]
"nuget" = []

[dependencies]
windows-core = "0.51.1"

Expand All @@ -40,13 +35,4 @@ features = [
]

[build-dependencies]
regex = "1.5.4"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
thiserror = "1.0.26"

[build-dependencies.windows-bindgen]
version = "0.51.1"
features = [
"metadata",
]
3 changes: 0 additions & 3 deletions crates/bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ This crate implements unsafe Rust bindings for the [WebView2](https://aka.ms/web

## Getting Started
This crate has a friendlier wrapper in [webview2-com](https://crates.io/crates/webview2-com).

## Windows Metadata
The Windows crate requires a Windows Metadata (`winmd`) file describing the API. The one used in this crate was generated with the [webview2-win32md](https://github.com/wravery/webview2-win32md) project.
Binary file modified crates/bindings/arm64/WebView2Loader.dll
Binary file not shown.
Binary file modified crates/bindings/arm64/WebView2Loader.dll.lib
Binary file not shown.
Binary file modified crates/bindings/arm64/WebView2LoaderStatic.lib
Binary file not shown.
Loading

0 comments on commit 26e237a

Please sign in to comment.