Skip to content

Commit bb06150

Browse files
authored
refactor(core): statically link vcruntime, closes #4122 (#4227)
1 parent 62ce02f commit bb06150

File tree

12 files changed

+109
-49
lines changed

12 files changed

+109
-49
lines changed

.changes/static-vcruntime.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-build": patch
3+
"tauri-bundler": patch
4+
"cli.rs": patch
5+
"cli.js": patch
6+
---
7+
8+
Statically link the Visual C++ runtime instead of using a merge module on the installer.

core/tauri-build/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::path::{Path, PathBuf};
1212

1313
#[cfg(feature = "codegen")]
1414
mod codegen;
15+
#[cfg(windows)]
16+
mod static_vcruntime;
1517

1618
#[cfg(feature = "codegen")]
1719
#[cfg_attr(doc_cfg, doc(cfg(feature = "codegen")))]
@@ -184,6 +186,9 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
184186
)?)?
185187
};
186188

189+
#[cfg(windows)]
190+
static_vcruntime::build();
191+
187192
cfg_alias("dev", !has_feature("custom-protocol"));
188193

189194
let mut manifest = Manifest::from_path("Cargo.toml")?;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
// taken from https://github.com/ChrisDenton/static_vcruntime/
6+
// we're not using static_vcruntime directly because we want this for debug builds too
7+
8+
use std::{env, fs, io::Write, path::Path};
9+
10+
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+
16+
override_msvcrt_lib();
17+
18+
// Disable conflicting libraries that aren't hard coded by Rust.
19+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libvcruntimed.lib");
20+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:vcruntime.lib");
21+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:vcruntimed.lib");
22+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libcmtd.lib");
23+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:msvcrt.lib");
24+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:msvcrtd.lib");
25+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libucrt.lib");
26+
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libucrtd.lib");
27+
// Set the libraries we want.
28+
println!("cargo:rustc-link-arg=/DEFAULTLIB:libcmt.lib");
29+
println!("cargo:rustc-link-arg=/DEFAULTLIB:libvcruntime.lib");
30+
println!("cargo:rustc-link-arg=/DEFAULTLIB:ucrt.lib");
31+
}
32+
33+
/// Override the hard-coded msvcrt.lib by replacing it with a (mostly) empty object file.
34+
fn override_msvcrt_lib() {
35+
// Get the right machine type for the empty library.
36+
let arch = std::env::var("CARGO_CFG_TARGET_ARCH");
37+
let machine: &[u8] = if arch.as_deref() == Ok("x86_64") {
38+
&[0x64, 0x86]
39+
} else if arch.as_deref() == Ok("x86") {
40+
&[0x4C, 0x01]
41+
} else {
42+
return;
43+
};
44+
let bytes: &[u8] = &[
45+
1, 0, 94, 3, 96, 98, 60, 0, 0, 0, 1, 0, 0, 0, 0, 0, 132, 1, 46, 100, 114, 101, 99, 116, 118,
46+
101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47+
10, 16, 0, 46, 100, 114, 101, 99, 116, 118, 101, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 4, 0, 0, 0,
48+
];
49+
50+
// Write the empty "msvcrt.lib" to the output directory.
51+
let out_dir = env::var("OUT_DIR").unwrap();
52+
let path = Path::new(&out_dir).join("msvcrt.lib");
53+
let f = fs::OpenOptions::new()
54+
.write(true)
55+
.create_new(true)
56+
.open(&path);
57+
if let Ok(mut f) = f {
58+
f.write_all(machine).unwrap();
59+
f.write_all(bytes).unwrap();
60+
}
61+
// Add the output directory to the native library path.
62+
println!("cargo:rustc-link-search=native={}", out_dir);
63+
}

core/tests/app-updater/build.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
fn main() {
6-
tauri_build::build()
7-
}
5+
fn main() {}

core/tests/app-updater/tests/update.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ fn get_cli_bin_path(cli_dir: &Path, debug: bool) -> Option<PathBuf> {
5757
fn build_app(cli_bin_path: &Path, cwd: &Path, config: &Config, bundle_updater: bool) {
5858
let mut command = Command::new(&cli_bin_path);
5959
command
60-
.arg("build")
61-
.arg("--debug")
60+
.args(["build", "--debug", "--verbose"])
6261
.arg("--config")
6362
.arg(serde_json::to_string(config).unwrap())
6463
.current_dir(&cwd);

examples/updater/src-tauri/Cargo.lock

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/bundler/src/bundle/windows/templates/main.wxs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
SummaryCodepage="!(loc.TauriCodepage)"/>
2727

2828
<!-- https://docs.microsoft.com/en-us/windows/win32/msi/reinstallmode -->
29-
<!-- reinstall if the file is missing or a different version is present; rewrite all registry entries; reinstall all shortcuts -->
30-
<Property Id="REINSTALLMODE" Value="dmus" />
29+
<!-- reinstall all files; rewrite all registry entries; reinstall all shortcuts -->
30+
<Property Id="REINSTALLMODE" Value="amus" />
3131

3232
{{#if allow_downgrades}}
3333
<MajorUpgrade AllowDowngrades="yes" Schedule="afterInstallValidate" />

tooling/cli/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ description = "Command line interface for building Tauri apps"
1515
include = [
1616
"src/",
1717
"/templates",
18-
"MergeModules/",
1918
"scripts/",
2019
"*.json",
2120
"*.rs",
-1.15 MB
Binary file not shown.

0 commit comments

Comments
 (0)