-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Feature/binary patch bundle types #13209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 11 commits
3d51f6a
9388de0
0f24bd9
776e231
dba5fe5
89fb9a3
7316de3
ba5f400
98771b2
dd04575
e06b3ea
455b2c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
tauri-cli: patch:enhance | ||
--- | ||
|
||
Binaries are patched before bundling to add the type of a bundle they will placed in. This information will be used during update process to select the correct target. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,55 @@ | ||
# dependency directories | ||
node_modules/ | ||
|
||
# Optional npm and yarn cache directory | ||
.npm/ | ||
.yarn/ | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# .vscode workspace settings file | ||
.vscode/settings.json | ||
.vscode/launch.json | ||
.vscode/tasks.json | ||
|
||
# npm, yarn and bun lock files | ||
package-lock.json | ||
yarn.lock | ||
bun.lockb | ||
|
||
# rust compiled folders | ||
target/ | ||
|
||
# test video for streaming example | ||
streaming_example_test_video.mp4 | ||
|
||
# examples /gen directory | ||
/examples/**/src-tauri/gen/ | ||
/bench/**/src-tauri/gen/ | ||
|
||
# logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# miscellaneous | ||
/.vs | ||
.DS_Store | ||
.Thumbs.db | ||
*.sublime* | ||
.idea | ||
debug.log | ||
TODO.md | ||
# dependency directories | ||
node_modules/ | ||
|
||
# Optional npm and yarn cache directory | ||
.npm/ | ||
.yarn/ | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# .vscode workspace settings file | ||
.vscode/settings.json | ||
.vscode/launch.json | ||
.vscode/tasks.json | ||
|
||
# npm, yarn and bun lock files | ||
package-lock.json | ||
yarn.lock | ||
bun.lockb | ||
|
||
# rust compiled folders | ||
target/ | ||
|
||
# test video for streaming example | ||
streaming_example_test_video.mp4 | ||
|
||
# examples /gen directory | ||
/examples/**/src-tauri/gen/ | ||
/bench/**/src-tauri/gen/ | ||
|
||
# logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# miscellaneous | ||
/.vs | ||
.DS_Store | ||
.Thumbs.db | ||
*.sublime* | ||
.idea | ||
debug.log | ||
TODO.md | ||
.aider* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,28 @@ mod settings; | |
mod updater_bundle; | ||
mod windows; | ||
|
||
impl From<goblin::error::Error> for crate::error::Error { | ||
fn from(value: goblin::error::Error) -> Self { | ||
crate::Error::BinaryParseError(value.to_string()) | ||
} | ||
} | ||
|
||
/// Patch a binary with bundle type information | ||
fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<()> { | ||
log::info!( | ||
"Patching binary {:?} for type {}", | ||
binary, | ||
package_type.short_name() | ||
); | ||
#[cfg(target_os = "linux")] | ||
linux::patch_binary(binary, package_type)?; | ||
|
||
#[cfg(target_os = "windows")] | ||
windows::patch_binary(binary, package_type)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
use tauri_utils::display_path; | ||
|
||
pub use self::{ | ||
|
@@ -92,11 +114,21 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<Bundle>> { | |
} | ||
} | ||
|
||
let main_binary = settings | ||
.binaries() | ||
.iter() | ||
.find(|b| b.main()) | ||
.expect("Main binary missing in settings"); | ||
|
||
let mut bundles = Vec::<Bundle>::new(); | ||
for package_type in &package_types { | ||
// bundle was already built! e.g. DMG already built .app | ||
if bundles.iter().any(|b| b.package_type == *package_type) { | ||
continue; | ||
} // REMOVE THIS? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this. This only applies to DMG, right? We don't want to skip anything on linux or windows as the binary needs to be patched for each type anyway. |
||
|
||
if settings.updater().is_some() { | ||
patch_binary(&settings.binary_path(main_binary), package_type)?; | ||
} | ||
|
||
let bundle_paths = match package_type { | ||
|
@@ -119,6 +151,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<Bundle>> { | |
|
||
#[cfg(target_os = "windows")] | ||
PackageType::WindowsMsi => windows::msi::bundle_project(settings, false)?, | ||
#[cfg(target_os = "windows")] | ||
PackageType::Nsis => windows::nsis::bundle_project(settings, false)?, | ||
|
||
#[cfg(target_os = "linux")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
/// Change value of __TAURI_BUNDLE_TYPE static variable to mark which package type it was bundled in | ||
#[cfg(target_os = "linux")] | ||
pub fn patch_binary( | ||
binary_path: &std::path::PathBuf, | ||
package_type: &crate::PackageType, | ||
) -> crate::Result<()> { | ||
let mut file_data = std::fs::read(binary_path).expect("Could not binary read file."); | ||
|
||
let elf = match goblin::Object::parse(&file_data)? { | ||
goblin::Object::Elf(elf) => elf, | ||
_ => return Err(crate::Error::BinaryParseError("Not an ELF file".into())), | ||
}; | ||
|
||
if let Some(offset) = find_bundle_type_symbol(elf) { | ||
let offset = offset as usize; | ||
if offset + 3 <= file_data.len() { | ||
let chars = &mut file_data[offset..offset + 3]; | ||
match package_type { | ||
crate::PackageType::Deb => chars.copy_from_slice(b"DEB"), | ||
crate::PackageType::Rpm => chars.copy_from_slice(b"RPM"), | ||
crate::PackageType::AppImage => chars.copy_from_slice(b"APP"), | ||
_ => { | ||
return Err(crate::Error::InvalidPackageType( | ||
package_type.short_name().to_owned(), | ||
"linux".to_owned(), | ||
)) | ||
} | ||
} | ||
if let Err(error) = std::fs::write(binary_path, &file_data) { | ||
return Err(crate::Error::BinaryWriteError(error.to_string())); | ||
} | ||
} else { | ||
return Err(crate::Error::BinaryOffsetOutOfRange); | ||
} | ||
} else { | ||
return Err(crate::Error::MissingBundleTypeVar); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Find address of a symbol in relocations table | ||
#[cfg(target_os = "linux")] | ||
fn find_bundle_type_symbol(elf: goblin::elf::Elf<'_>) -> Option<i64> { | ||
for sym in elf.syms.iter() { | ||
if let Some(name) = elf.strtab.get_at(sym.st_name) { | ||
if name == "__TAURI_BUNDLE_TYPE" { | ||
for reloc in elf.dynrelas.iter() { | ||
if reloc.r_offset == sym.st_value { | ||
return Some(reloc.r_addend.unwrap()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
None | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be done inside crate::error::Error::BinaryParseError by leverating thiserror
#[from]
attribute