diff --git a/tooling/cli/src/mobile/android.rs b/tooling/cli/src/mobile/android.rs index 2284fde41..127048242 100644 --- a/tooling/cli/src/mobile/android.rs +++ b/tooling/cli/src/mobile/android.rs @@ -26,7 +26,7 @@ use tauri_mobile::{ use super::{ ensure_init, get_app, - init::{command as init_command, init_dot_cargo}, + init::{command as init_command, configure_cargo}, log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE, }; diff --git a/tooling/cli/src/mobile/android/build.rs b/tooling/cli/src/mobile/android/build.rs index e475e96fe..f86276e8f 100644 --- a/tooling/cli/src/mobile/android/build.rs +++ b/tooling/cli/src/mobile/android/build.rs @@ -1,5 +1,5 @@ use super::{ - delete_codegen_vars, ensure_init, env, init_dot_cargo, log_finished, open_and_wait, with_config, + configure_cargo, delete_codegen_vars, ensure_init, env, log_finished, open_and_wait, with_config, MobileTarget, }; use crate::{ @@ -79,7 +79,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> { ensure_init(config.project_dir(), MobileTarget::Android)?; let mut env = env()?; - init_dot_cargo(app, Some((&env, config)))?; + configure_cargo(app, Some((&mut env, config)))?; let open = options.open; run_build(options, config, &mut env, noise_level)?; diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index 402d8d2cd..802f637a9 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -1,5 +1,5 @@ use super::{ - delete_codegen_vars, device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, + configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, open_and_wait, setup_dev_config, with_config, MobileTarget, }; use crate::{ @@ -98,7 +98,7 @@ fn run_dev( noise_level: NoiseLevel, ) -> Result<()> { setup_dev_config(&mut options.config)?; - let env = env()?; + let mut env = env()?; let device = if options.open { None } else { @@ -131,7 +131,7 @@ fn run_dev( let out_dir = bin_path.parent().unwrap(); let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?; - init_dot_cargo(app, Some((&env, config)))?; + configure_cargo(app, Some((&mut env, config)))?; // run an initial build to initialize plugins interface.build(interface_options)?; diff --git a/tooling/cli/src/mobile/init.rs b/tooling/cli/src/mobile/init.rs index dd9b97f7b..c6aeaba17 100644 --- a/tooling/cli/src/mobile/init.rs +++ b/tooling/cli/src/mobile/init.rs @@ -33,7 +33,27 @@ pub fn command(target: Target, ci: bool, reinstall_deps: bool) -> Result<()> { Ok(()) } -pub fn init_dot_cargo(app: &App, android: Option<(&AndroidEnv, &AndroidConfig)>) -> Result<()> { +pub fn configure_cargo( + app: &App, + android: Option<(&mut AndroidEnv, &AndroidConfig)>, +) -> Result<()> { + if let Some((env, config)) = android { + for target in AndroidTarget::all().values() { + let config = target.generate_cargo_config(config, env)?; + let target_var_name = target.triple.replace('-', "_").to_uppercase(); + if let Some(linker) = config.linker { + env.base.insert_env_var( + format!("CARGO_TARGET_{target_var_name}_LINKER"), + linker.into(), + ); + } + env.base.insert_env_var( + format!("CARGO_TARGET_{target_var_name}_RUSTFLAGS"), + config.rustflags.join(" ").into(), + ); + } + } + let mut dot_cargo = dot_cargo::DotCargo::load(app)?; // Mysteriously, builds that don't specify `--target` seem to fight over // the build cache with builds that use `--target`! This means that @@ -47,15 +67,6 @@ pub fn init_dot_cargo(app: &App, android: Option<(&AndroidEnv, &AndroidConfig)>) // https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags dot_cargo.set_default_target(util::host_target_triple()?); - if let Some((env, config)) = android { - for target in AndroidTarget::all().values() { - dot_cargo.insert_target( - target.triple.to_owned(), - target.generate_cargo_config(config, env)?, - ); - } - } - dot_cargo.write(app).map_err(Into::into) } @@ -110,12 +121,11 @@ pub fn exec( let app = match target { // Generate Android Studio project Target::Android => match AndroidEnv::new() { - Ok(env) => { + Ok(_env) => { let (app, config, metadata) = super::android::get_config(Some(app), tauri_config_, &Default::default()); map.insert("android", &config); super::android::project::gen(&config, &metadata, (handlebars, map), wrapper)?; - init_dot_cargo(&app, Some((&env, &config)))?; app } Err(err) => { @@ -125,7 +135,6 @@ pub fn exec( err, ) .print(wrapper); - init_dot_cargo(&app, None)?; app } else { return Err(err.into()); @@ -146,7 +155,6 @@ pub fn exec( non_interactive, reinstall_deps, )?; - init_dot_cargo(&app, None)?; app } }; diff --git a/tooling/cli/src/mobile/ios.rs b/tooling/cli/src/mobile/ios.rs index aed2ec3e3..20b49d45b 100644 --- a/tooling/cli/src/mobile/ios.rs +++ b/tooling/cli/src/mobile/ios.rs @@ -24,7 +24,7 @@ use tauri_mobile::{ use super::{ ensure_init, env, get_app, - init::{command as init_command, init_dot_cargo}, + init::{command as init_command, configure_cargo}, log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE, }; diff --git a/tooling/cli/src/mobile/ios/build.rs b/tooling/cli/src/mobile/ios/build.rs index c0b8c9484..c7091d7a6 100644 --- a/tooling/cli/src/mobile/ios/build.rs +++ b/tooling/cli/src/mobile/ios/build.rs @@ -1,5 +1,5 @@ use super::{ - detect_target_ok, ensure_init, env, init_dot_cargo, log_finished, open_and_wait, with_config, + configure_cargo, detect_target_ok, ensure_init, env, log_finished, open_and_wait, with_config, MobileTarget, }; use crate::{ @@ -71,7 +71,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> { ensure_init(config.project_dir(), MobileTarget::Ios)?; let mut env = env()?; - init_dot_cargo(app, None)?; + configure_cargo(app, None)?; let open = options.open; run_build(options, config, &mut env, noise_level)?; diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index 7efb3699a..f45eb53fd 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -1,5 +1,5 @@ use super::{ - device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, setup_dev_config, with_config, + configure_cargo, device_prompt, ensure_init, env, open_and_wait, setup_dev_config, with_config, MobileTarget, APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME, }; use crate::{ @@ -142,7 +142,7 @@ fn run_dev( let out_dir = bin_path.parent().unwrap(); let _lock = flock::open_rw(&out_dir.join("lock").with_extension("ios"), "iOS")?; - init_dot_cargo(app, None)?; + configure_cargo(app, None)?; let open = options.open; let exit_on_panic = options.exit_on_panic; diff --git a/tooling/cli/src/mobile/mod.rs b/tooling/cli/src/mobile/mod.rs index d3d7eddd3..918c2b72f 100644 --- a/tooling/cli/src/mobile/mod.rs +++ b/tooling/cli/src/mobile/mod.rs @@ -179,6 +179,7 @@ fn env_vars() -> HashMap { let k = k.to_string_lossy(); if (k.starts_with("TAURI") && k != "TAURI_PRIVATE_KEY" && k != "TAURI_KEY_PASSWORD") || k.starts_with("WRY") + || k.starts_with("CARGO_") || k == "TMPDIR" || k == "PATH" {