Skip to content

Commit 58a6879

Browse files
authored
feat(tauri-build): improve Windows GNU toolchain usage, closes #4319 (#4323)
1 parent 76d1eaa commit 58a6879

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch
3+
---
4+
5+
Improve usage of the GNU toolchain on Windows by copying the Webview2Loader.dll file to the target directory.

core/tauri-build/src/lib.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ fn cfg_alias(alias: &str, has_feature: bool) {
8989
#[derive(Debug, Default)]
9090
pub struct WindowsAttributes {
9191
window_icon_path: Option<PathBuf>,
92-
/// The path to the sdk location. This can be a absolute or relative path. If not supplied
93-
/// this defaults to whatever `winres` crate determines is the best. See the
94-
/// [winres documentation](https://docs.rs/winres/*/winres/struct.WindowsResource.html#method.set_toolkit_path)
92+
/// The path to the sdk location.
93+
///
94+
/// For the GNU toolkit this has to be the path where MinGW put windres.exe and ar.exe.
95+
/// This could be something like: "C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin"
96+
///
97+
/// For MSVC the Windows SDK has to be installed. It comes with the resource compiler rc.exe.
98+
/// This should be set to the root directory of the Windows SDK, e.g., "C:\Program Files (x86)\Windows Kits\10" or,
99+
/// if multiple 10 versions are installed, set it directly to the corret bin directory "C:\Program Files (x86)\Windows Kits\10\bin\10.0.14393.0\x64"
100+
///
101+
/// If it is left unset, it will look up a path in the registry, i.e. HKLM\SOFTWARE\Microsoft\Windows Kits\Installed Roots
95102
sdk_dir: Option<PathBuf>,
96103
}
97104

@@ -186,11 +193,6 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
186193
)?)?
187194
};
188195

189-
#[cfg(windows)]
190-
if std::env::var("STATIC_VCRUNTIME").map_or(false, |v| v == "true") {
191-
static_vcruntime::build();
192-
}
193-
194196
cfg_alias("dev", !has_feature("custom-protocol"));
195197

196198
let mut manifest = Manifest::from_path("Cargo.toml")?;
@@ -246,9 +248,9 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
246248
}
247249

248250
let target_triple = std::env::var("TARGET").unwrap();
249-
let out_dir = std::env::var("OUT_DIR").unwrap();
251+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
250252
// TODO: far from ideal, but there's no other way to get the target dir, see <https://github.com/rust-lang/cargo/issues/5457>
251-
let target_dir = Path::new(&out_dir)
253+
let target_dir = out_dir
252254
.parent()
253255
.unwrap()
254256
.parent()
@@ -344,6 +346,38 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
344346
window_icon_path.display()
345347
)));
346348
}
349+
350+
let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
351+
match target_env.as_str() {
352+
"gnu" => {
353+
let target_arch = match std::env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
354+
"x86_64" => Some("x64"),
355+
"x86" => Some("x86"),
356+
"aarch64" => Some("arm64"),
357+
arch => None,
358+
};
359+
if let Some(target_arch) = target_arch {
360+
for entry in std::fs::read_dir(target_dir.join("build"))? {
361+
let path = entry?.path();
362+
let webview2_loader_path = path
363+
.join("out")
364+
.join(target_arch)
365+
.join("WebView2Loader.dll");
366+
if path.to_string_lossy().contains("webview2-com-sys") && webview2_loader_path.exists()
367+
{
368+
std::fs::copy(webview2_loader_path, target_dir.join("WebView2Loader.dll"))?;
369+
break;
370+
}
371+
}
372+
}
373+
}
374+
"msvc" => {
375+
if std::env::var("STATIC_VCRUNTIME").map_or(false, |v| v == "true") {
376+
static_vcruntime::build();
377+
}
378+
}
379+
_ => (),
380+
}
347381
}
348382

349383
Ok(())

core/tauri-build/src/static_vcruntime.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
use std::{env, fs, io::Write, path::Path};
99

1010
pub fn build() {
11-
// Early exit if not msvc or release
12-
if env::var("CARGO_CFG_TARGET_ENV").as_deref() != Ok("msvc") {
13-
return;
14-
}
15-
1611
override_msvcrt_lib();
1712

1813
// Disable conflicting libraries that aren't hard coded by Rust.

0 commit comments

Comments
 (0)