refactor(core): statically link vcruntime, closes #4122 (#4227)

This commit is contained in:
Lucas Fernandes Nogueira
2022-05-27 10:33:04 -07:00
committed by GitHub
parent 62ce02f6b5
commit bb061509fb
12 changed files with 109 additions and 49 deletions

View File

@@ -0,0 +1,8 @@
---
"tauri-build": patch
"tauri-bundler": patch
"cli.rs": patch
"cli.js": patch
---
Statically link the Visual C++ runtime instead of using a merge module on the installer.

View File

@@ -12,6 +12,8 @@ use std::path::{Path, PathBuf};
#[cfg(feature = "codegen")]
mod codegen;
#[cfg(windows)]
mod static_vcruntime;
#[cfg(feature = "codegen")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "codegen")))]
@@ -184,6 +186,9 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
)?)?
};
#[cfg(windows)]
static_vcruntime::build();
cfg_alias("dev", !has_feature("custom-protocol"));
let mut manifest = Manifest::from_path("Cargo.toml")?;

View File

@@ -0,0 +1,63 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
// taken from https://github.com/ChrisDenton/static_vcruntime/
// we're not using static_vcruntime directly because we want this for debug builds too
use std::{env, fs, io::Write, path::Path};
pub fn build() {
// Early exit if not msvc or release
if env::var("CARGO_CFG_TARGET_ENV").as_deref() != Ok("msvc") {
return;
}
override_msvcrt_lib();
// Disable conflicting libraries that aren't hard coded by Rust.
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libvcruntimed.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:vcruntime.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:vcruntimed.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libcmtd.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:msvcrt.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:msvcrtd.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libucrt.lib");
println!("cargo:rustc-link-arg=/NODEFAULTLIB:libucrtd.lib");
// Set the libraries we want.
println!("cargo:rustc-link-arg=/DEFAULTLIB:libcmt.lib");
println!("cargo:rustc-link-arg=/DEFAULTLIB:libvcruntime.lib");
println!("cargo:rustc-link-arg=/DEFAULTLIB:ucrt.lib");
}
/// Override the hard-coded msvcrt.lib by replacing it with a (mostly) empty object file.
fn override_msvcrt_lib() {
// Get the right machine type for the empty library.
let arch = std::env::var("CARGO_CFG_TARGET_ARCH");
let machine: &[u8] = if arch.as_deref() == Ok("x86_64") {
&[0x64, 0x86]
} else if arch.as_deref() == Ok("x86") {
&[0x4C, 0x01]
} else {
return;
};
let bytes: &[u8] = &[
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,
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,
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,
];
// Write the empty "msvcrt.lib" to the output directory.
let out_dir = env::var("OUT_DIR").unwrap();
let path = Path::new(&out_dir).join("msvcrt.lib");
let f = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path);
if let Ok(mut f) = f {
f.write_all(machine).unwrap();
f.write_all(bytes).unwrap();
}
// Add the output directory to the native library path.
println!("cargo:rustc-link-search=native={}", out_dir);
}

View File

@@ -2,6 +2,4 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
fn main() {
tauri_build::build()
}
fn main() {}

View File

@@ -57,8 +57,7 @@ fn get_cli_bin_path(cli_dir: &Path, debug: bool) -> Option<PathBuf> {
fn build_app(cli_bin_path: &Path, cwd: &Path, config: &Config, bundle_updater: bool) {
let mut command = Command::new(&cli_bin_path);
command
.arg("build")
.arg("--debug")
.args(["build", "--debug", "--verbose"])
.arg("--config")
.arg(serde_json::to_string(config).unwrap())
.current_dir(&cwd);

View File

@@ -1511,6 +1511,15 @@ dependencies = [
"syn",
]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]]
name = "objc"
version = "0.2.7"
@@ -2539,7 +2548,7 @@ dependencies = [
[[package]]
name = "tauri"
version = "1.0.0-rc.11"
version = "1.0.0-rc.13"
dependencies = [
"anyhow",
"attohttpc",
@@ -2577,6 +2586,7 @@ dependencies = [
"tauri-utils",
"tempfile",
"thiserror",
"time",
"tokio",
"url",
"uuid 1.0.0",
@@ -2588,10 +2598,11 @@ dependencies = [
[[package]]
name = "tauri-build"
version = "1.0.0-rc.9"
version = "1.0.0-rc.11"
dependencies = [
"anyhow",
"cargo_toml",
"heck 0.4.0",
"quote",
"semver 1.0.9",
"serde_json",
@@ -2610,6 +2621,7 @@ dependencies = [
"png 0.17.5",
"proc-macro2",
"quote",
"semver 1.0.9",
"serde",
"serde_json",
"sha2",
@@ -2633,7 +2645,7 @@ dependencies = [
[[package]]
name = "tauri-runtime"
version = "0.5.1"
version = "0.6.0"
dependencies = [
"gtk",
"http",
@@ -2650,7 +2662,7 @@ dependencies = [
[[package]]
name = "tauri-runtime-wry"
version = "0.5.2"
version = "0.6.0"
dependencies = [
"cocoa",
"gtk",
@@ -2680,6 +2692,7 @@ dependencies = [
"phf 0.10.1",
"proc-macro2",
"quote",
"semver 1.0.9",
"serde",
"serde_json",
"serde_with",
@@ -2748,6 +2761,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
dependencies = [
"itoa 1.0.2",
"libc",
"num_threads",
]
[[package]]
name = "tinyvec"
version = "1.6.0"

View File

@@ -26,8 +26,8 @@
SummaryCodepage="!(loc.TauriCodepage)"/>
<!-- https://docs.microsoft.com/en-us/windows/win32/msi/reinstallmode -->
<!-- reinstall if the file is missing or a different version is present; rewrite all registry entries; reinstall all shortcuts -->
<Property Id="REINSTALLMODE" Value="dmus" />
<!-- reinstall all files; rewrite all registry entries; reinstall all shortcuts -->
<Property Id="REINSTALLMODE" Value="amus" />
{{#if allow_downgrades}}
<MajorUpgrade AllowDowngrades="yes" Schedule="afterInstallValidate" />

View File

@@ -2805,6 +2805,7 @@ dependencies = [
"memchr",
"phf 0.10.1",
"schemars",
"semver",
"serde",
"serde_json",
"serde_with",

View File

@@ -15,7 +15,6 @@ description = "Command line interface for building Tauri apps"
include = [
"src/",
"/templates",
"MergeModules/",
"scripts/",
"*.json",
"*.rs",

View File

@@ -240,43 +240,6 @@ pub fn command(options: Options) -> Result<()> {
}
if config_.tauri.bundle.active {
// move merge modules to the out dir so the bundler can load it
#[cfg(windows)]
{
let target = options
.target
.clone()
.unwrap_or_else(|| std::env::consts::ARCH.into());
let arch = if target.starts_with("x86_64") {
"x86_64"
} else if target.starts_with('i') || target.starts_with("x86") {
"x86"
} else if target.starts_with("arm") {
"arm"
} else if target.starts_with("aarch64") {
"aarch64"
} else {
panic!(
"Unexpected target architecture {}",
target.split('_').next().unwrap()
)
};
let (filename, vcruntime_msm) = if arch == "x86" {
let _ = std::fs::remove_file(out_dir.join("Microsoft_VC142_CRT_x64.msm"));
(
"Microsoft_VC142_CRT_x86.msm",
include_bytes!("../MergeModules/Microsoft_VC142_CRT_x86.msm").to_vec(),
)
} else {
let _ = std::fs::remove_file(out_dir.join("Microsoft_VC142_CRT_x86.msm"));
(
"Microsoft_VC142_CRT_x64.msm",
include_bytes!("../MergeModules/Microsoft_VC142_CRT_x64.msm").to_vec(),
)
};
std::fs::write(out_dir.join(filename), vcruntime_msm)?;
}
let package_types = if let Some(names) = options.bundles {
let mut types = vec![];
for name in names {