Skip to content

Commit

Permalink
fix(cli): do not generate .cargo/config file (#10785)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 27, 2024
1 parent dc0612b commit fd68b7f
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 73 deletions.
6 changes: 6 additions & 0 deletions .changes/remove-cargo-config-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:enhance
"@tauri-apps/cli": patch:enhance
---

Remove the `.cargo/config` file creation that used to fix mobile build caches.
1 change: 0 additions & 1 deletion examples/api/src-tauri/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
/target/

# cargo-mobile
.cargo/
/gen
6 changes: 3 additions & 3 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
tauri_utils::platform::Target::Android,
options.config.as_ref().map(|c| &c.0),
)?;
let (interface, app, config, metadata) = {
let (interface, config, metadata) = {
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();

Expand All @@ -121,7 +121,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
build_options.features.as_ref(),
&Default::default(),
);
(interface, app, config, metadata)
(interface, config, metadata)
};

let profile = if options.debug {
Expand All @@ -141,7 +141,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
)?;

let mut env = env()?;
configure_cargo(&app, Some((&mut env, &config)))?;
configure_cargo(&mut env, &config)?;

crate::build::setup(&interface, &mut build_options, tauri_config.clone(), true)?;

Expand Down
9 changes: 3 additions & 6 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use cargo_mobile2::{
env::Env,
target::Target,
},
config::app::App,
opts::{FilterLevel, NoiseLevel, Profile},
target::TargetTrait,
};
Expand Down Expand Up @@ -128,7 +127,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
.unwrap_or_else(|| Target::all().values().next().unwrap().triple.into());
dev_options.target = Some(target_triple.clone());

let (interface, app, config, metadata) = {
let (interface, config, metadata) = {
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();

Expand All @@ -141,7 +140,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
dev_options.features.as_ref(),
&Default::default(),
);
(interface, app, config, metadata)
(interface, config, metadata)
};

let tauri_path = tauri_dir();
Expand All @@ -160,7 +159,6 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
tauri_config,
device,
env,
&app,
&config,
&metadata,
noise_level,
Expand All @@ -175,7 +173,6 @@ fn run_dev(
tauri_config: ConfigHandle,
device: Option<Device>,
mut env: Env,
app: &App,
config: &AndroidConfig,
metadata: &AndroidMetadata,
noise_level: NoiseLevel,
Expand All @@ -193,7 +190,7 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;

configure_cargo(app, Some((&mut env, config)))?;
configure_cargo(&mut env, config)?;

// run an initial build to initialize plugins
let target_triple = dev_options.target.as_ref().unwrap();
Expand Down
26 changes: 22 additions & 4 deletions tooling/cli/src/mobile/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cargo_mobile2::{
config::app::{App, DEFAULT_ASSET_DIR},
opts::{FilterLevel, NoiseLevel},
os,
target::TargetTrait,
util::prompt,
};
use clap::{Parser, Subcommand};
Expand All @@ -28,10 +29,8 @@ use sublime_fuzzy::best_match;
use tauri_utils::resources::ResourcePaths;

use super::{
ensure_init, get_app,
init::{command as init_command, configure_cargo},
log_finished, read_options, CliOptions, OptionsHandle, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
ensure_init, get_app, init::command as init_command, log_finished, read_options, CliOptions,
OptionsHandle, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::config::{BundleResources, Config as TauriConfig},
Expand Down Expand Up @@ -327,3 +326,22 @@ fn inject_resources(config: &AndroidConfig, tauri_config: &TauriConfig) -> Resul

Ok(())
}

fn configure_cargo(env: &mut Env, config: &AndroidConfig) -> Result<()> {
for target in Target::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(),
);
}

Ok(())
}
43 changes: 1 addition & 42 deletions tooling/cli/src/mobile/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ use crate::{
Result,
};
use cargo_mobile2::{
android::{
config::Config as AndroidConfig, env::Env as AndroidEnv, target::Target as AndroidTarget,
},
android::env::Env as AndroidEnv,
config::app::App,
dot_cargo,
reserved_names::KOTLIN_ONLY_KEYWORDS,
target::TargetTrait as _,
util::{
self,
cli::{Report, TextWrapper},
Expand All @@ -40,43 +36,6 @@ pub fn command(
Ok(())
}

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
// alternating between i.e. `cargo run` and `cargo apple run` would
// result in clean builds being made each time you switched... which is
// pretty nightmarish. Specifying `build.target` in `.cargo/config`
// fortunately has the same effect as specifying `--target`, so now we can
// `cargo run` with peace of mind!
//
// This behavior could be explained here:
// https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
dot_cargo.set_default_target(util::host_target_triple()?);

dot_cargo.write(app).map_err(Into::into)
}

pub fn exec(
target: Target,
wrapper: &TextWrapper,
Expand Down
7 changes: 3 additions & 4 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, detect_target_ok, ensure_init, env, get_app, get_config, inject_resources,
load_pbxproj, log_finished, merge_plist, open_and_wait, project_config,
synchronize_project_config, MobileTarget, OptionsHandle,
detect_target_ok, ensure_init, env, get_app, get_config, inject_resources, load_pbxproj,
log_finished, merge_plist, open_and_wait, project_config, synchronize_project_config,
MobileTarget, OptionsHandle,
};
use crate::{
build::Options as BuildOptions,
Expand Down Expand Up @@ -186,7 +186,6 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
merged_info_plist.to_file_xml(&info_plist_path)?;

let mut env = env()?;
configure_cargo(&app, None)?;

let mut export_options_plist = plist::Dictionary::new();
if let Some(method) = options.export_method {
Expand Down
13 changes: 4 additions & 9 deletions tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, device_prompt, ensure_init, env, get_app, get_config, inject_resources,
merge_plist, open_and_wait, MobileTarget,
device_prompt, ensure_init, env, get_app, get_config, inject_resources, merge_plist,
open_and_wait, MobileTarget,
};
use crate::{
dev::Options as DevOptions,
Expand All @@ -25,7 +25,6 @@ use cargo_mobile2::{
config::Config as AppleConfig,
device::{Device, DeviceKind},
},
config::app::App,
env::Env,
opts::{NoiseLevel, Profile},
};
Expand Down Expand Up @@ -153,7 +152,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
tauri_utils::platform::Target::Ios,
options.config.as_ref().map(|c| &c.0),
)?;
let (interface, app, config) = {
let (interface, config) = {
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();

Expand All @@ -167,7 +166,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
&Default::default(),
);

(interface, app, config)
(interface, config)
};

let tauri_path = tauri_dir();
Expand Down Expand Up @@ -199,7 +198,6 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
tauri_config,
device,
env,
&app,
&config,
noise_level,
)
Expand Down Expand Up @@ -344,7 +342,6 @@ fn run_dev(
tauri_config: ConfigHandle,
device: Option<Device>,
env: Env,
app: &App,
config: &AppleConfig,
noise_level: NoiseLevel,
) -> Result<()> {
Expand All @@ -371,8 +368,6 @@ fn run_dev(

let set_host = options.host.is_some();

configure_cargo(app, None)?;

let open = options.open;
let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
Expand Down
6 changes: 2 additions & 4 deletions tooling/cli/src/mobile/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ use sublime_fuzzy::best_match;
use tauri_utils::resources::ResourcePaths;

use super::{
ensure_init, env, get_app,
init::{command as init_command, configure_cargo},
log_finished, read_options, CliOptions, OptionsHandle, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
ensure_init, env, get_app, init::command as init_command, log_finished, read_options, CliOptions,
OptionsHandle, Target as MobileTarget, MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::{
Expand Down

0 comments on commit fd68b7f

Please sign in to comment.