From 83f52fdbe3a9ffd98dffc752e5f9e14322b56e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didrik=20Nordstr=C3=B6m?= Date: Fri, 4 Feb 2022 13:56:57 -0800 Subject: [PATCH] feat: Add `universal-darwin-macos` build target, closes #3317 (#3318) Co-authored-by: Lucas Nogueira --- .changes/universal-apple-target.md | 5 ++++ tooling/bundler/src/bundle/settings.rs | 2 ++ tooling/cli.rs/src/build.rs | 41 +++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 .changes/universal-apple-target.md diff --git a/.changes/universal-apple-target.md b/.changes/universal-apple-target.md new file mode 100644 index 000000000..fcd7eead2 --- /dev/null +++ b/.changes/universal-apple-target.md @@ -0,0 +1,5 @@ +--- +"cli.rs": patch +--- + +Add support to building Universal macOS Binaries through the virtual target `universal-apple-darwin` (run `tauri build --target universal-apple-darwin`). diff --git a/tooling/bundler/src/bundle/settings.rs b/tooling/bundler/src/bundle/settings.rs index 95ff5c605..41f3e3d03 100644 --- a/tooling/bundler/src/bundle/settings.rs +++ b/tooling/bundler/src/bundle/settings.rs @@ -467,6 +467,8 @@ impl Settings { "arm" } else if self.target.starts_with("aarch64") { "aarch64" + } else if self.target.starts_with("universal") { + "universal" } else { panic!("Unexpected target triple {}", self.target) } diff --git a/tooling/cli.rs/src/build.rs b/tooling/cli.rs/src/build.rs index 94dc46896..7cdcea723 100644 --- a/tooling/cli.rs/src/build.rs +++ b/tooling/cli.rs/src/build.rs @@ -31,7 +31,9 @@ pub struct Options { /// Enables verbose logging #[clap(short, long)] verbose: bool, - /// Target triple to build against + /// Target triple to build against. + /// It must be one of the values outputted by `$rustc --print target-list` or `universal-apple-darwin` for an universal macOS application. + /// Note that compiling an universal macOS application requires both `aarch64-apple-darwin` and `x86_64-apple-darwin` targets to be installed. #[clap(short, long)] target: Option, /// List of cargo features to activate @@ -132,9 +134,6 @@ pub fn command(options: Options) -> Result<()> { cargo_features.extend(features); } - crate::interface::rust::build_project(runner, &options.target, cargo_features, options.debug) - .with_context(|| "failed to build app")?; - let app_settings = crate::interface::rust::AppSettings::new(config_)?; let out_dir = app_settings @@ -151,6 +150,40 @@ pub fn command(options: Options) -> Result<()> { #[cfg(not(windows))] let bin_path = out_dir.join(&bin_name); + if options.target == Some("universal-apple-darwin".into()) { + std::fs::create_dir_all(&out_dir).with_context(|| "failed to create project out directory")?; + + let mut lipo_cmd = Command::new("lipo"); + lipo_cmd + .arg("-create") + .arg("-output") + .arg(out_dir.join(&bin_name)); + for triple in ["aarch64-apple-darwin", "x86_64-apple-darwin"] { + crate::interface::rust::build_project( + runner.clone(), + &Some(triple.into()), + cargo_features.clone(), + options.debug, + ) + .with_context(|| format!("failed to build {} binary", triple))?; + let triple_out_dir = app_settings + .get_out_dir(Some(triple.into()), options.debug) + .with_context(|| format!("failed to get {} out dir", triple))?; + lipo_cmd.arg(triple_out_dir.join(&bin_name)); + } + + let lipo_status = lipo_cmd.status()?; + if !lipo_status.success() { + return Err(anyhow::anyhow!(format!( + "Result of `lipo` command was unsuccessful: {}. (Is `lipo` installed?)", + lipo_status + ))); + } + } else { + crate::interface::rust::build_project(runner, &options.target, cargo_features, options.debug) + .with_context(|| "failed to build app")?; + } + #[cfg(unix)] if !options.debug { strip(&bin_path, &logger)?;