|
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | use std::{ |
| 6 | + collections::HashMap, |
6 | 7 | ffi::OsStr, |
7 | 8 | fs::{File, FileType}, |
8 | 9 | io::{Read, Write}, |
@@ -123,7 +124,7 @@ pub struct Rust { |
123 | 124 | impl Interface for Rust { |
124 | 125 | type AppSettings = RustAppSettings; |
125 | 126 |
|
126 | | - fn new(config: &Config) -> crate::Result<Self> { |
| 127 | + fn new(config: &Config, target: Option<String>) -> crate::Result<Self> { |
127 | 128 | let manifest = { |
128 | 129 | let (tx, rx) = sync_channel(1); |
129 | 130 | let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| { |
@@ -154,7 +155,7 @@ impl Interface for Rust { |
154 | 155 | } |
155 | 156 |
|
156 | 157 | Ok(Self { |
157 | | - app_settings: RustAppSettings::new(config, manifest)?, |
| 158 | + app_settings: RustAppSettings::new(config, manifest, target)?, |
158 | 159 | config_features: config.build.features.clone().unwrap_or_default(), |
159 | 160 | product_name: config.package.product_name.clone(), |
160 | 161 | available_targets: None, |
@@ -206,6 +207,52 @@ impl Interface for Rust { |
206 | 207 | self.run_dev_watcher(child, options, on_exit) |
207 | 208 | } |
208 | 209 | } |
| 210 | + |
| 211 | + fn env(&self) -> HashMap<&str, String> { |
| 212 | + let mut env = HashMap::new(); |
| 213 | + env.insert( |
| 214 | + "TAURI_TARGET_TRIPLE", |
| 215 | + self.app_settings.target_triple.clone(), |
| 216 | + ); |
| 217 | + |
| 218 | + let mut s = self.app_settings.target_triple.split('-'); |
| 219 | + let (arch, _, host) = (s.next().unwrap(), s.next().unwrap(), s.next().unwrap()); |
| 220 | + env.insert( |
| 221 | + "TAURI_ARCH", |
| 222 | + match arch { |
| 223 | + // keeps compatibility with old `std::env::consts::ARCH` implementation |
| 224 | + "i686" | "i586" => "x86".into(), |
| 225 | + a => a.into(), |
| 226 | + }, |
| 227 | + ); |
| 228 | + env.insert( |
| 229 | + "TAURI_PLATFORM", |
| 230 | + match host { |
| 231 | + // keeps compatibility with old `std::env::consts::OS` implementation |
| 232 | + "darwin" => "macos".into(), |
| 233 | + "ios-sim" => "ios".into(), |
| 234 | + "androideabi" => "android".into(), |
| 235 | + h => h.into(), |
| 236 | + }, |
| 237 | + ); |
| 238 | + |
| 239 | + env.insert( |
| 240 | + "TAURI_FAMILY", |
| 241 | + match host { |
| 242 | + "windows" => "windows".into(), |
| 243 | + _ => "unix".into(), |
| 244 | + }, |
| 245 | + ); |
| 246 | + |
| 247 | + match host { |
| 248 | + "linux" => env.insert("TAURI_PLATFORM_TYPE", "Linux".into()), |
| 249 | + "windows" => env.insert("TAURI_PLATFORM_TYPE", "Windows_NT".into()), |
| 250 | + "darwin" => env.insert("TAURI_PLATFORM_TYPE", "Darwin".into()), |
| 251 | + _ => None, |
| 252 | + }; |
| 253 | + |
| 254 | + env |
| 255 | + } |
209 | 256 | } |
210 | 257 |
|
211 | 258 | fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) { |
@@ -437,6 +484,7 @@ pub struct RustAppSettings { |
437 | 484 | cargo_package_settings: CargoPackageSettings, |
438 | 485 | package_settings: PackageSettings, |
439 | 486 | cargo_config: CargoConfig, |
| 487 | + target_triple: String, |
440 | 488 | } |
441 | 489 |
|
442 | 490 | impl AppSettings for RustAppSettings { |
@@ -468,13 +516,8 @@ impl AppSettings for RustAppSettings { |
468 | 516 | let out_dir = self |
469 | 517 | .out_dir(options.target.clone(), options.debug) |
470 | 518 | .with_context(|| "failed to get project out directory")?; |
471 | | - let target: String = if let Some(target) = options.target.clone() { |
472 | | - target |
473 | | - } else { |
474 | | - tauri_utils::platform::target_triple()? |
475 | | - }; |
476 | 519 |
|
477 | | - let binary_extension: String = if target.contains("windows") { |
| 520 | + let binary_extension: String = if self.target_triple.contains("windows") { |
478 | 521 | "exe" |
479 | 522 | } else { |
480 | 523 | "" |
@@ -590,7 +633,7 @@ impl AppSettings for RustAppSettings { |
590 | 633 | } |
591 | 634 |
|
592 | 635 | impl RustAppSettings { |
593 | | - pub fn new(config: &Config, manifest: Manifest) -> crate::Result<Self> { |
| 636 | + pub fn new(config: &Config, manifest: Manifest, target: Option<String>) -> crate::Result<Self> { |
594 | 637 | let cargo_settings = |
595 | 638 | CargoSettings::load(&tauri_dir()).with_context(|| "failed to load cargo settings")?; |
596 | 639 | let cargo_package_settings = match &cargo_settings.package { |
@@ -626,12 +669,31 @@ impl RustAppSettings { |
626 | 669 |
|
627 | 670 | let cargo_config = CargoConfig::load(&tauri_dir())?; |
628 | 671 |
|
| 672 | + let target_triple = target.unwrap_or_else(|| { |
| 673 | + cargo_config |
| 674 | + .build() |
| 675 | + .target() |
| 676 | + .map(|t| t.to_string()) |
| 677 | + .unwrap_or_else(|| { |
| 678 | + let output = Command::new("rustc").args(&["-vV"]).output().unwrap(); |
| 679 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 680 | + stdout |
| 681 | + .split('\n') |
| 682 | + .find(|l| l.starts_with("host:")) |
| 683 | + .unwrap() |
| 684 | + .replace("host:", "") |
| 685 | + .trim() |
| 686 | + .to_string() |
| 687 | + }) |
| 688 | + }); |
| 689 | + |
629 | 690 | Ok(Self { |
630 | 691 | manifest, |
631 | 692 | cargo_settings, |
632 | 693 | cargo_package_settings, |
633 | 694 | package_settings, |
634 | 695 | cargo_config, |
| 696 | + target_triple, |
635 | 697 | }) |
636 | 698 | } |
637 | 699 |
|
|
0 commit comments