Skip to content

Commit fe1aa30

Browse files
authored
fix(cli): quote iOS Xcode build script arguments and set CXXFLAGS (#16113)
1 parent c1ea96a commit fe1aa30

4 files changed

Lines changed: 64 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Quote the Tauri CLI path and the Xcode variables in the iOS "Build Rust Code" build phase, fixing builds when Xcode or the CLI is installed in a path containing spaces (requires regenerating the Xcode project with `tauri ios init`). Also fixes `CXXFLAGS` not being set for iOS targets in `tauri ios xcode-script`.

‎crates/tauri-cli/src/mobile/init.rs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ fn exec(
133133

134134
map.insert("tauri-binary", binary);
135135
map.insert("tauri-binary-args", &build_args);
136-
map.insert("tauri-binary-args-str", build_args.join(" "));
136+
map.insert(
137+
"tauri-binary-args-str",
138+
build_args
139+
.iter()
140+
.map(|arg| shell_escape(arg))
141+
.collect::<Vec<_>>()
142+
.join(" "),
143+
);
137144

138145
let app = match target {
139146
// Generate Android Studio project
@@ -197,6 +204,7 @@ fn handlebars(app: &App) -> (Handlebars<'static>, JsonMap) {
197204
"quote-and-join-colon-prefix",
198205
Box::new(quote_and_join_colon_prefix),
199206
);
207+
h.register_helper("shell-escape", Box::new(shell_escape_helper));
200208
h.register_helper("snake-case", Box::new(snake_case));
201209
h.register_helper("escape-kotlin-keyword", Box::new(escape_kotlin_keyword));
202210
// don't mix these up or very bad things will happen to all of us
@@ -236,6 +244,31 @@ fn html_escape(
236244
.map_err(Into::into)
237245
}
238246

247+
/// Quotes a string so it is interpreted as a single word by a POSIX shell.
248+
fn shell_escape(s: &str) -> String {
249+
if !s.is_empty()
250+
&& s
251+
.chars()
252+
.all(|c| c.is_ascii_alphanumeric() || "-_./:@%+=,".contains(c))
253+
{
254+
s.to_string()
255+
} else {
256+
format!("'{}'", s.replace('\'', r"'\''"))
257+
}
258+
}
259+
260+
fn shell_escape_helper(
261+
helper: &Helper,
262+
_: &Handlebars,
263+
_: &Context,
264+
_: &mut RenderContext,
265+
out: &mut dyn Output,
266+
) -> HelperResult {
267+
out
268+
.write(&shell_escape(get_str(helper)))
269+
.map_err(Into::into)
270+
}
271+
239272
fn join(
240273
helper: &Helper,
241274
_: &Handlebars,
@@ -412,3 +445,24 @@ fn is_pnpm_dlx() -> bool {
412445
false
413446
})
414447
}
448+
449+
#[cfg(test)]
450+
mod tests {
451+
use super::shell_escape;
452+
453+
#[test]
454+
fn shell_escape_words() {
455+
assert_eq!(shell_escape("cargo"), "cargo");
456+
assert_eq!(shell_escape("xcode-script"), "xcode-script");
457+
assert_eq!(
458+
shell_escape("/Users/me/node_modules/.bin/tauri"),
459+
"/Users/me/node_modules/.bin/tauri"
460+
);
461+
assert_eq!(
462+
shell_escape("/Users/me/My Projects/tauri"),
463+
"'/Users/me/My Projects/tauri'"
464+
);
465+
assert_eq!(shell_escape("it's $HOME"), r"'it'\''s $HOME'");
466+
assert_eq!(shell_escape(""), "''");
467+
}
468+
}

‎crates/tauri-cli/src/mobile/ios/xcode_script.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub fn command(options: Options) -> Result<()> {
231231
let interface = AppInterface::new(&tauri_config, Some(rust_triple.into()), dirs.tauri)?;
232232

233233
let cflags = format!("CFLAGS_{env_triple}");
234-
let cxxflags = format!("CFLAGS_{env_triple}");
234+
let cxxflags = format!("CXXFLAGS_{env_triple}");
235235
let objc_include_path = format!("OBJC_INCLUDE_PATH_{env_triple}");
236236
let mut target_env = host_env.clone();
237237
target_env.insert(cflags.as_ref(), isysroot.as_ref());

‎crates/tauri-cli/templates/mobile/ios/project.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ targets:
124124
discoveredDependencyFile: {{this.discovered-dependency-file}}{{/if}}
125125
{{~/each}}
126126

127-
- script: {{ tauri-binary }} {{ tauri-binary-args-str }} -v --platform ${PLATFORM_DISPLAY_NAME:?} --sdk-root ${SDKROOT:?} --framework-search-paths "${FRAMEWORK_SEARCH_PATHS:?}" --header-search-paths "${HEADER_SEARCH_PATHS:?}" --gcc-preprocessor-definitions "${GCC_PREPROCESSOR_DEFINITIONS:-}" --configuration ${CONFIGURATION:?} ${FORCE_COLOR} ${ARCHS:?}
127+
- script: |
128+
{{ shell-escape tauri-binary }} {{ tauri-binary-args-str }} -v --platform "${PLATFORM_DISPLAY_NAME:?}" --sdk-root "${SDKROOT:?}" --framework-search-paths "${FRAMEWORK_SEARCH_PATHS:?}" --header-search-paths "${HEADER_SEARCH_PATHS:?}" --gcc-preprocessor-definitions "${GCC_PREPROCESSOR_DEFINITIONS:-}" --configuration "${CONFIGURATION:?}" ${FORCE_COLOR} ${ARCHS:?}
128129
name: Build Rust Code
129130
basedOnDependencyAnalysis: false
130131
outputFiles:

0 commit comments

Comments
 (0)