Skip to content

Commit 23042d6

Browse files
authored
Pass -disable-sandbox to Swift compiler if requested (#7167)
The Swift compiler supports disabling sandboxing for macros now, so we should opt-in to that if the selected toolchain supports it and the user has disabled sandboxing. We warn if macros are being used and disabling sandboxing was requested but isn't available. rdar://118851130
1 parent 380b953 commit 23042d6

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

+17
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ public final class SwiftTargetBuildDescription {
238238
/// Whether or not to generate code for test observation.
239239
private let shouldGenerateTestObservation: Bool
240240

241+
/// Whether to disable sandboxing (e.g. for macros).
242+
private let disableSandbox: Bool
243+
241244
/// Create a new target description with target and build parameters.
242245
init(
243246
package: ResolvedPackage,
@@ -250,6 +253,7 @@ public final class SwiftTargetBuildDescription {
250253
requiredMacroProducts: [ResolvedProduct] = [],
251254
testTargetRole: TestTargetRole? = nil,
252255
shouldGenerateTestObservation: Bool = false,
256+
disableSandbox: Bool,
253257
fileSystem: FileSystem,
254258
observabilityScope: ObservabilityScope
255259
) throws {
@@ -276,6 +280,7 @@ public final class SwiftTargetBuildDescription {
276280
self.prebuildCommandResults = prebuildCommandResults
277281
self.requiredMacroProducts = requiredMacroProducts
278282
self.shouldGenerateTestObservation = shouldGenerateTestObservation
283+
self.disableSandbox = disableSandbox
279284
self.fileSystem = fileSystem
280285
self.observabilityScope = observabilityScope
281286

@@ -453,6 +458,18 @@ public final class SwiftTargetBuildDescription {
453458
args += ["-Xfrontend", "-external-plugin-path", "-Xfrontend", "\(localPluginPath)#\(pluginServer.pathString)"]
454459
}
455460

461+
if self.disableSandbox {
462+
let toolchainSupportsDisablingSandbox = DriverSupport.checkSupportedFrontendFlags(flags: ["-disable-sandbox"], toolchain: self.buildParameters.toolchain, fileSystem: fileSystem)
463+
if toolchainSupportsDisablingSandbox {
464+
args += ["-disable-sandbox"]
465+
} else {
466+
// If there's at least one macro being used, we warn about our inability to disable sandboxing.
467+
if !self.requiredMacroProducts.isEmpty {
468+
observabilityScope.emit(warning: "cannot disable sandboxing for Swift compilation because the selected toolchain does not support it")
469+
}
470+
}
471+
}
472+
456473
return args
457474
}
458475

Sources/Build/BuildOperation.swift

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
545545
additionalFileRules: additionalFileRules,
546546
buildToolPluginInvocationResults: buildToolPluginInvocationResults,
547547
prebuildCommandResults: prebuildCommandResults,
548+
disableSandbox: self.pluginConfiguration?.disableSandbox ?? false,
548549
fileSystem: self.fileSystem,
549550
observabilityScope: self.observabilityScope
550551
)

Sources/Build/BuildPlan/BuildPlan+Test.swift

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extension BuildPlan {
2828
static func makeDerivedTestTargets(
2929
_ buildParameters: BuildParameters,
3030
_ graph: PackageGraph,
31+
_ disableSandbox: Bool,
3132
_ fileSystem: FileSystem,
3233
_ observabilityScope: ObservabilityScope
3334
) throws -> [(product: ResolvedProduct, discoveryTargetBuildDescription: SwiftTargetBuildDescription?, entryPointTargetBuildDescription: SwiftTargetBuildDescription)] {
@@ -95,6 +96,7 @@ extension BuildPlan {
9596
toolsVersion: toolsVersion,
9697
buildParameters: buildParameters,
9798
testTargetRole: .discovery,
99+
disableSandbox: disableSandbox,
98100
fileSystem: fileSystem,
99101
observabilityScope: observabilityScope
100102
)
@@ -132,6 +134,7 @@ extension BuildPlan {
132134
toolsVersion: toolsVersion,
133135
buildParameters: buildParameters,
134136
testTargetRole: .entryPoint(isSynthesized: true),
137+
disableSandbox: disableSandbox,
135138
fileSystem: fileSystem,
136139
observabilityScope: observabilityScope
137140
)
@@ -174,6 +177,7 @@ extension BuildPlan {
174177
toolsVersion: toolsVersion,
175178
buildParameters: buildParameters,
176179
testTargetRole: .entryPoint(isSynthesized: false),
180+
disableSandbox: disableSandbox,
177181
fileSystem: fileSystem,
178182
observabilityScope: observabilityScope
179183
)
@@ -195,6 +199,7 @@ extension BuildPlan {
195199
toolsVersion: toolsVersion,
196200
buildParameters: buildParameters,
197201
testTargetRole: .entryPoint(isSynthesized: false),
202+
disableSandbox: disableSandbox,
198203
fileSystem: fileSystem,
199204
observabilityScope: observabilityScope
200205
)

Sources/Build/BuildPlan/BuildPlan.swift

+7
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
235235
/// Cache for tools information.
236236
var externalExecutablesCache = [BinaryTarget: [ExecutableInfo]]()
237237

238+
/// Whether to disable sandboxing (e.g. for macros).
239+
private let disableSandbox: Bool
240+
238241
/// The filesystem to operate on.
239242
let fileSystem: any FileSystem
240243

@@ -271,6 +274,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
271274
additionalFileRules: [FileRuleDescription] = [],
272275
buildToolPluginInvocationResults: [ResolvedTarget: [BuildToolPluginInvocationResult]] = [:],
273276
prebuildCommandResults: [ResolvedTarget: [PrebuildCommandResult]] = [:],
277+
disableSandbox: Bool = false,
274278
fileSystem: any FileSystem,
275279
observabilityScope: ObservabilityScope
276280
) throws {
@@ -279,6 +283,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
279283
self.graph = graph
280284
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
281285
self.prebuildCommandResults = prebuildCommandResults
286+
self.disableSandbox = disableSandbox
282287
self.fileSystem = fileSystem
283288
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Plan")
284289

@@ -377,6 +382,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
377382
prebuildCommandResults: prebuildCommandResults[target] ?? [],
378383
requiredMacroProducts: requiredMacroProducts,
379384
shouldGenerateTestObservation: generateTestObservation,
385+
disableSandbox: self.disableSandbox,
380386
fileSystem: fileSystem,
381387
observabilityScope: observabilityScope)
382388
)
@@ -423,6 +429,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
423429
let derivedTestTargets = try Self.makeDerivedTestTargets(
424430
productsBuildParameters,
425431
graph,
432+
self.disableSandbox,
426433
self.fileSystem,
427434
self.observabilityScope
428435
)

0 commit comments

Comments
 (0)