Skip to content

Commit c4a891f

Browse files
authored
[5.10] Pass -disable-sandbox to Swift compiler if requested (#7227)
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. (cherry picked from commit 23042d6)
1 parent 1ce90f2 commit c4a891f

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ public final class SwiftTargetBuildDescription {
235235
/// Whether or not to generate code for test observation.
236236
private let shouldGenerateTestObservation: Bool
237237

238+
/// Whether to disable sandboxing (e.g. for macros).
239+
private let disableSandbox: Bool
240+
238241
/// Create a new target description with target and build parameters.
239242
init(
240243
package: ResolvedPackage,
@@ -247,6 +250,7 @@ public final class SwiftTargetBuildDescription {
247250
requiredMacroProducts: [ResolvedProduct] = [],
248251
testTargetRole: TestTargetRole? = nil,
249252
shouldGenerateTestObservation: Bool = false,
253+
disableSandbox: Bool,
250254
fileSystem: FileSystem,
251255
observabilityScope: ObservabilityScope
252256
) throws {
@@ -273,6 +277,7 @@ public final class SwiftTargetBuildDescription {
273277
self.prebuildCommandResults = prebuildCommandResults
274278
self.requiredMacroProducts = requiredMacroProducts
275279
self.shouldGenerateTestObservation = shouldGenerateTestObservation
280+
self.disableSandbox = disableSandbox
276281
self.fileSystem = fileSystem
277282
self.observabilityScope = observabilityScope
278283

@@ -450,6 +455,18 @@ public final class SwiftTargetBuildDescription {
450455
args += ["-Xfrontend", "-external-plugin-path", "-Xfrontend", "\(localPluginPath)#\(pluginServer.pathString)"]
451456
}
452457

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

Sources/Build/BuildOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
531531
additionalFileRules: additionalFileRules,
532532
buildToolPluginInvocationResults: buildToolPluginInvocationResults,
533533
prebuildCommandResults: prebuildCommandResults,
534+
disableSandbox: self.pluginConfiguration?.disableSandbox ?? false,
534535
fileSystem: self.fileSystem,
535536
observabilityScope: self.observabilityScope
536537
)

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 5 additions & 0 deletions
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
)
@@ -128,6 +130,7 @@ extension BuildPlan {
128130
toolsVersion: toolsVersion,
129131
buildParameters: buildParameters,
130132
testTargetRole: .entryPoint(isSynthesized: true),
133+
disableSandbox: disableSandbox,
131134
fileSystem: fileSystem,
132135
observabilityScope: observabilityScope
133136
)
@@ -157,6 +160,7 @@ extension BuildPlan {
157160
toolsVersion: toolsVersion,
158161
buildParameters: buildParameters,
159162
testTargetRole: .entryPoint(isSynthesized: false),
163+
disableSandbox: disableSandbox,
160164
fileSystem: fileSystem,
161165
observabilityScope: observabilityScope
162166
)
@@ -175,6 +179,7 @@ extension BuildPlan {
175179
toolsVersion: toolsVersion,
176180
buildParameters: buildParameters,
177181
testTargetRole: .entryPoint(isSynthesized: false),
182+
disableSandbox: disableSandbox,
178183
fileSystem: fileSystem,
179184
observabilityScope: observabilityScope
180185
)

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
229229
/// Cache for tools information.
230230
var externalExecutablesCache = [BinaryTarget: [ExecutableInfo]]()
231231

232+
/// Whether to disable sandboxing (e.g. for macros).
233+
private let disableSandbox: Bool
234+
232235
/// The filesystem to operate on.
233236
let fileSystem: FileSystem
234237

@@ -242,13 +245,15 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
242245
additionalFileRules: [FileRuleDescription] = [],
243246
buildToolPluginInvocationResults: [ResolvedTarget: [BuildToolPluginInvocationResult]] = [:],
244247
prebuildCommandResults: [ResolvedTarget: [PrebuildCommandResult]] = [:],
248+
disableSandbox: Bool = false,
245249
fileSystem: FileSystem,
246250
observabilityScope: ObservabilityScope
247251
) throws {
248252
self.buildParameters = buildParameters
249253
self.graph = graph
250254
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
251255
self.prebuildCommandResults = prebuildCommandResults
256+
self.disableSandbox = disableSandbox
252257
self.fileSystem = fileSystem
253258
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Plan")
254259

@@ -331,6 +336,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
331336
prebuildCommandResults: prebuildCommandResults[target] ?? [],
332337
requiredMacroProducts: requiredMacroProducts,
333338
shouldGenerateTestObservation: generateTestObservation,
339+
disableSandbox: self.disableSandbox,
334340
fileSystem: fileSystem,
335341
observabilityScope: observabilityScope)
336342
)
@@ -377,6 +383,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
377383
let derivedTestTargets = try Self.makeDerivedTestTargets(
378384
buildParameters,
379385
graph,
386+
self.disableSandbox,
380387
self.fileSystem,
381388
self.observabilityScope
382389
)

0 commit comments

Comments
 (0)