Skip to content

Commit 8fc580a

Browse files
feat: allow optionally disabling Swift sandbox (#2587)
If `cargo` is invoked inside a sandbox on Darwin, the build will fail when it invokes `swift`. This is because `swift` builds within its own sandbox by default, but nested sandboxes are not allowed on Darwin. This is the case, for example, when building `sentry-cli` inside Homebrew. For builds that already happen inside a sandbox, allow setting an environment variable `SWIFT_DISABLE_SANDBOX` which then results in `swift` being invoked with `--disable-sandbox` to enable the build to succeed. --------- Co-authored-by: Daniel Szoke <daniel.szoke@sentry.io> Co-authored-by: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com>
1 parent 3fb1353 commit 8fc580a

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

apple-catalog-parsing/build.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use std::env;
22
use std::process::Command;
33

44
fn main() {
5+
/// Environment variable to disable Swift sandboxing.
6+
const SWIFT_DISABLE_SANDBOX: &str = "SWIFT_DISABLE_SANDBOX";
7+
58
let target = env::var("TARGET").expect("TARGET is set for build scripts");
69
let mut target_bits = target.split('-');
710

@@ -20,21 +23,34 @@ fn main() {
2023

2124
println!("cargo:rerun-if-changed=native/swift/AssetCatalogParser");
2225

26+
// Allow swift to be run with `--disable-sandbox` in case cargo has been invoked inside a
27+
// sandbox already. Nested sandboxes are not allowed on Darwin.
28+
println!("cargo:rerun-if-env-changed={SWIFT_DISABLE_SANDBOX}");
29+
2330
let out_dir = env::var("OUT_DIR").expect("OUT_DIR is set for build scripts");
2431

2532
// Compile Swift code
2633
let status = Command::new("swift")
27-
.args([
28-
"build",
29-
"-c",
30-
"release",
31-
"--package-path",
32-
"native/swift/AssetCatalogParser",
33-
"--scratch-path",
34-
&format!("{out_dir}/swift-scratch"),
35-
"--triple",
36-
&format!("{arch}-apple-macosx10.12"),
37-
])
34+
.args(
35+
[
36+
"build",
37+
"-c",
38+
"release",
39+
"--package-path",
40+
"native/swift/AssetCatalogParser",
41+
"--scratch-path",
42+
&format!("{out_dir}/swift-scratch"),
43+
"--triple",
44+
&format!("{arch}-apple-macosx10.12"),
45+
]
46+
.into_iter()
47+
.chain(
48+
env::var(SWIFT_DISABLE_SANDBOX)
49+
.ok()
50+
.filter(|s| s == "1")
51+
.map(|_| "--disable-sandbox"),
52+
),
53+
)
3854
.status()
3955
.expect("Failed to compile SPM");
4056

0 commit comments

Comments
 (0)