Skip to content

Commit f6c1eb2

Browse files
Reland "feat: add Windows VC runtime linking and bundling options" (#15401)
* feat: add Windows VC runtime linking and bundling options (#15372) * Add `@tauri-apps/cli` in change files --------- Co-authored-by: Amr Bashir <github@amrbashir.me>
1 parent 24cb6de commit f6c1eb2

20 files changed

Lines changed: 475 additions & 28 deletions

File tree

‎.changes/bundle-vc-runtime.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-bundler": "minor:feat"
3+
"tauri-cli": "minor:feat"
4+
"@tauri-apps/cli": "minor:feat"
5+
"tauri-utils": "minor:feat"
6+
---
7+
8+
Added `bundle.windows.bundleVCRuntime` to copy the Visual C++ runtime DLLs into Windows MSI and NSIS installers. The bundler locates the runtime through `VCTOOLS_REDIST_DIR` or the bundled `vswhere.exe`.

‎.changes/static-vc-runtime.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-cli": "minor:feat"
3+
"@tauri-apps/cli": "minor:feat"
4+
"tauri-utils": "minor:feat"
5+
---
6+
7+
Added `build.windows.staticVCRuntime` to control MSVC static runtime linking. The `STATIC_VCRUNTIME` environment variable is now deprecated and emits a migration warning when used.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": "minor:feat"
3+
---
4+
5+
Added `tauri_build::WindowsAttributes::static_vc_runtime` to control MSVC static runtime linking from build scripts.

‎crates/tauri-build/src/lib.rs‎

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ fn cfg_alias(alias: &str, has_feature: bool) {
215215
#[derive(Debug)]
216216
pub struct WindowsAttributes {
217217
window_icon_path: Option<PathBuf>,
218+
/// Whether to statically link the Visual C++ runtime into the application binary on Windows MSVC targets
219+
static_vc_runtime: Option<bool>,
218220
/// A string containing an [application manifest] to be included with the application on Windows.
219221
///
220222
/// Defaults to:
@@ -257,6 +259,7 @@ impl WindowsAttributes {
257259
pub fn new() -> Self {
258260
Self {
259261
window_icon_path: Default::default(),
262+
static_vc_runtime: None,
260263
app_manifest: Some(include_str!("windows-app-manifest.xml").into()),
261264
append_rc_content: Vec::new(),
262265
}
@@ -268,6 +271,7 @@ impl WindowsAttributes {
268271
Self {
269272
app_manifest: None,
270273
window_icon_path: Default::default(),
274+
static_vc_runtime: None,
271275
append_rc_content: Vec::new(),
272276
}
273277
}
@@ -282,6 +286,15 @@ impl WindowsAttributes {
282286
self
283287
}
284288

289+
/// Sets whether to statically link the Visual C++ runtime into the application binary on Windows MSVC targets.
290+
///
291+
/// If unset, this is read from `build > windows > staticVCRuntime` in the Tauri configuration.
292+
#[must_use]
293+
pub fn static_vc_runtime(mut self, static_vc_runtime: bool) -> Self {
294+
self.static_vc_runtime.replace(static_vc_runtime);
295+
self
296+
}
297+
285298
/// Sets the [application manifest] to be included with the application on Windows.
286299
///
287300
/// Defaults to:
@@ -489,6 +502,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
489502
json_patch::merge(&mut config, &merge_config);
490503
}
491504
let config: Config = serde_json::from_value(config)?;
505+
let static_vc_runtime = should_static_link_vc_runtime(&config, &attributes);
492506

493507
let s = config.identifier.split('.');
494508
let last = s.clone().count() - 1;
@@ -705,7 +719,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
705719
}
706720
}
707721
}
708-
"msvc" if env::var_os("STATIC_VCRUNTIME").is_some_and(|v| v == "true") => {
722+
"msvc" if static_vc_runtime => {
709723
static_vcruntime::build();
710724
}
711725
_ => (),
@@ -726,6 +740,20 @@ fn to_winres_version(v: &semver::Version) -> u64 {
726740
(v.major << 48) | (v.minor << 32) | (v.patch << 16) | build
727741
}
728742

743+
fn should_static_link_vc_runtime(config: &Config, attributes: &Attributes) -> bool {
744+
if let Some(value) = env::var_os("STATIC_VCRUNTIME") {
745+
println!(
746+
"cargo:warning=STATIC_VCRUNTIME is deprecated; use build.windows.staticVCRuntime in tauri.conf.json or tauri_build::WindowsAttributes::static_vc_runtime instead."
747+
);
748+
value != "false"
749+
} else {
750+
attributes
751+
.windows_attributes
752+
.static_vc_runtime
753+
.unwrap_or(config.build.windows.static_vc_runtime)
754+
}
755+
}
756+
729757
#[cfg(test)]
730758
mod tests {
731759
use semver::Version;
@@ -769,4 +797,86 @@ mod tests {
769797
(1 << 48) | (2 << 32) | (3 << 16)
770798
);
771799
}
800+
801+
#[test]
802+
fn static_vc_runtime_chain() {
803+
// 1. Nothing is set, should default to true
804+
let config = tauri_utils::config::Config::default();
805+
let attributes = crate::Attributes::new();
806+
assert!(crate::should_static_link_vc_runtime(&config, &attributes));
807+
808+
// 2. Set to anything but "false" in env, should be true
809+
std::env::set_var("STATIC_VCRUNTIME", "qweqe");
810+
let config = tauri_utils::config::Config::default();
811+
let attributes = crate::Attributes::new();
812+
assert!(crate::should_static_link_vc_runtime(&config, &attributes));
813+
std::env::remove_var("STATIC_VCRUNTIME");
814+
815+
// 3. Set to "false" in env, should be false
816+
std::env::set_var("STATIC_VCRUNTIME", "false");
817+
let config = tauri_utils::config::Config::default();
818+
let attributes = crate::Attributes::new();
819+
assert!(!crate::should_static_link_vc_runtime(&config, &attributes));
820+
std::env::remove_var("STATIC_VCRUNTIME");
821+
822+
// 4. Set to true in attributes, should be true
823+
let config = tauri_utils::config::Config::default();
824+
let attributes = crate::Attributes::new()
825+
.windows_attributes(crate::WindowsAttributes::new().static_vc_runtime(true));
826+
assert!(crate::should_static_link_vc_runtime(&config, &attributes));
827+
828+
// 5. Set to false in attributes, should be false
829+
let config = tauri_utils::config::Config::default();
830+
let attributes = crate::Attributes::new()
831+
.windows_attributes(crate::WindowsAttributes::new().static_vc_runtime(false));
832+
assert!(!crate::should_static_link_vc_runtime(&config, &attributes));
833+
834+
// 6. Set to true in config, should be true
835+
let config = tauri_utils::config::Config {
836+
build: tauri_utils::config::BuildConfig {
837+
windows: tauri_utils::config::WindowsBuildConfig {
838+
static_vc_runtime: true,
839+
},
840+
..Default::default()
841+
},
842+
..Default::default()
843+
};
844+
let attributes = crate::Attributes::new();
845+
assert!(crate::should_static_link_vc_runtime(&config, &attributes));
846+
847+
// 7. Set to false in config, should be false
848+
let config = tauri_utils::config::Config {
849+
build: tauri_utils::config::BuildConfig {
850+
windows: tauri_utils::config::WindowsBuildConfig {
851+
static_vc_runtime: false,
852+
},
853+
..Default::default()
854+
},
855+
..Default::default()
856+
};
857+
let attributes = crate::Attributes::new();
858+
assert!(!crate::should_static_link_vc_runtime(&config, &attributes));
859+
860+
// 8. Set to true in config and false in attributes, should be false because attributes takes precedence over config
861+
let config = tauri_utils::config::Config {
862+
build: tauri_utils::config::BuildConfig {
863+
windows: tauri_utils::config::WindowsBuildConfig {
864+
static_vc_runtime: true,
865+
},
866+
..Default::default()
867+
},
868+
..Default::default()
869+
};
870+
let attributes = crate::Attributes::new()
871+
.windows_attributes(crate::WindowsAttributes::new().static_vc_runtime(false));
872+
assert!(!crate::should_static_link_vc_runtime(&config, &attributes));
873+
874+
// 9. Set to false in env and true in attributes, should be false because env takes precedence over attributes
875+
std::env::set_var("STATIC_VCRUNTIME", "false");
876+
let config = tauri_utils::config::Config::default();
877+
let attributes = crate::Attributes::new()
878+
.windows_attributes(crate::WindowsAttributes::new().static_vc_runtime(true));
879+
assert!(!crate::should_static_link_vc_runtime(&config, &attributes));
880+
std::env::remove_var("STATIC_VCRUNTIME");
881+
}
772882
}

‎crates/tauri-bundler/Cargo.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ plist = "1"
5050
[target."cfg(target_os = \"windows\")".dependencies]
5151
bitness = "0.4"
5252
windows-registry = "0.5"
53-
glob = "0.3"
5453

5554
[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
5655
version = "0.60"
@@ -67,6 +66,9 @@ ar = "0.9"
6766
md5 = "0.8"
6867
rpm = { version = "0.16", features = ["bzip2-compression"] }
6968

69+
[target."cfg(any(target_os = \"windows\", target_os = \"macos\", target_os = \"linux\"))".dependencies]
70+
glob = "0.3"
71+
7072
[target."cfg(unix)".dependencies]
7173
which = "8"
7274

‎crates/tauri-bundler/src/bundle.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub use {
3131
PackageType, PlistKind, Position, RpmSettings, Settings, SettingsBuilder, Size,
3232
UpdaterSettings, WindowsSettings, WixLanguage, WixLanguageConfig, WixSettings,
3333
},
34+
windows::vswhere_path,
3435
};
3536

3637
const BUNDLE_VAR_TOKEN: &[u8] = b"__TAURI_BUNDLE_TYPE_VAR_UNK";

‎crates/tauri-bundler/src/bundle/settings.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,13 @@ pub struct WindowsSettings {
603603
/// if the user's WebView2 is older than this version,
604604
/// the installer will try to trigger a WebView2 update.
605605
pub minimum_webview2_version: Option<String>,
606+
/// Whether to bundle the Visual C++ runtime DLLs alongside the application.
607+
///
608+
/// This can be particularly useful when the application includes sidecars or DLLs that do not
609+
/// statically link the Visual C++ runtime and require the runtime DLLs at runtime, and users
610+
/// should not be required to install the Visual C++ Redistributable. This can also be useful
611+
/// when `static_vc_runtime` is set to `false`.
612+
pub bundle_vc_runtime: bool,
606613
}
607614

608615
impl WindowsSettings {
@@ -629,6 +636,7 @@ mod _default {
629636
allow_downgrades: true,
630637
sign_command: None,
631638
minimum_webview2_version: None,
639+
bundle_vc_runtime: false,
632640
}
633641
}
634642
}

‎crates/tauri-bundler/src/bundle/windows/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ pub mod sign;
1111

1212
mod util;
1313
pub use util::{
14-
NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME, WIX_OUTPUT_FOLDER_NAME,
14+
vswhere_path, NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME, WIX_OUTPUT_FOLDER_NAME,
1515
WIX_UPDATER_OUTPUT_FOLDER_NAME,
1616
};

‎crates/tauri-bundler/src/bundle/windows/msi/mod.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
windows::{
1010
sign::{should_sign, try_sign},
1111
util::{
12-
download_webview2_bootstrapper, download_webview2_offline_installer,
12+
download_webview2_bootstrapper, download_webview2_offline_installer, vc_runtime_dlls,
1313
WIX_OUTPUT_FOLDER_NAME, WIX_UPDATER_OUTPUT_FOLDER_NAME,
1414
},
1515
},
@@ -1066,6 +1066,21 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
10661066

10671067
let mut dlls = Vec::new();
10681068

1069+
if settings.windows().bundle_vc_runtime {
1070+
for dll in vc_runtime_dlls(settings.binary_arch())? {
1071+
let resource_path = dunce::simplified(&dll);
1072+
if added_resources.contains(&resource_path.to_path_buf()) {
1073+
continue;
1074+
}
1075+
added_resources.push(resource_path.to_path_buf());
1076+
dlls.push(ResourceFile {
1077+
id: format!("I{}", Uuid::new_v4().as_simple()),
1078+
guid: Uuid::new_v4().to_string(),
1079+
path: resource_path.to_path_buf(),
1080+
});
1081+
}
1082+
}
1083+
10691084
// TODO: The bundler should not include all DLLs it finds. Instead it should only include WebView2Loader.dll if present and leave the rest to the resources config.
10701085
let out_dir = settings.project_out_directory();
10711086
for dll in glob::glob(

‎crates/tauri-bundler/src/bundle/windows/nsis/mod.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
windows::{
99
sign::{should_sign, sign_command, try_sign},
1010
util::{
11-
download_webview2_bootstrapper, download_webview2_offline_installer,
11+
download_webview2_bootstrapper, download_webview2_offline_installer, vc_runtime_dlls,
1212
NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME,
1313
},
1414
},
@@ -802,6 +802,22 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourcesMap> {
802802
}
803803
}
804804

805+
if settings.windows().bundle_vc_runtime {
806+
for dll in vc_runtime_dlls(settings.binary_arch())? {
807+
let dll = dunce::simplified(&dll).to_path_buf();
808+
if added_resources.contains(&dll) {
809+
continue;
810+
}
811+
let target = PathBuf::from(
812+
dll
813+
.file_name()
814+
.expect("failed to extract Visual C++ runtime DLL filename"),
815+
);
816+
added_resources.push(dll.clone());
817+
resources.insert(dll, (PathBuf::new(), target));
818+
}
819+
}
820+
805821
for resource in settings.resource_files().iter() {
806822
let resource = resource?;
807823

0 commit comments

Comments
 (0)