Skip to content

Pass -disable-sandbox to Swift compiler if requested #7167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ public final class SwiftTargetBuildDescription {
/// Whether or not to generate code for test observation.
private let shouldGenerateTestObservation: Bool

/// Whether to disable sandboxing (e.g. for macros).
private let disableSandbox: Bool

/// Create a new target description with target and build parameters.
init(
package: ResolvedPackage,
Expand All @@ -250,6 +253,7 @@ public final class SwiftTargetBuildDescription {
requiredMacroProducts: [ResolvedProduct] = [],
testTargetRole: TestTargetRole? = nil,
shouldGenerateTestObservation: Bool = false,
disableSandbox: Bool,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) throws {
Expand All @@ -276,6 +280,7 @@ public final class SwiftTargetBuildDescription {
self.prebuildCommandResults = prebuildCommandResults
self.requiredMacroProducts = requiredMacroProducts
self.shouldGenerateTestObservation = shouldGenerateTestObservation
self.disableSandbox = disableSandbox
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope

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

if self.disableSandbox {
let toolchainSupportsDisablingSandbox = DriverSupport.checkSupportedFrontendFlags(flags: ["-disable-sandbox"], toolchain: self.buildParameters.toolchain, fileSystem: fileSystem)
if toolchainSupportsDisablingSandbox {
args += ["-disable-sandbox"]
} else {
// If there's at least one macro being used, we warn about our inability to disable sandboxing.
if !self.requiredMacroProducts.isEmpty {
observabilityScope.emit(warning: "cannot disable sandboxing for Swift compilation because the selected toolchain does not support it")
}
}
}

return args
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
additionalFileRules: additionalFileRules,
buildToolPluginInvocationResults: buildToolPluginInvocationResults,
prebuildCommandResults: prebuildCommandResults,
disableSandbox: self.pluginConfiguration?.disableSandbox ?? false,
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
Expand Down
5 changes: 5 additions & 0 deletions Sources/Build/BuildPlan/BuildPlan+Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension BuildPlan {
static func makeDerivedTestTargets(
_ buildParameters: BuildParameters,
_ graph: PackageGraph,
_ disableSandbox: Bool,
_ fileSystem: FileSystem,
_ observabilityScope: ObservabilityScope
) throws -> [(product: ResolvedProduct, discoveryTargetBuildDescription: SwiftTargetBuildDescription?, entryPointTargetBuildDescription: SwiftTargetBuildDescription)] {
Expand Down Expand Up @@ -95,6 +96,7 @@ extension BuildPlan {
toolsVersion: toolsVersion,
buildParameters: buildParameters,
testTargetRole: .discovery,
disableSandbox: disableSandbox,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
Expand Down Expand Up @@ -132,6 +134,7 @@ extension BuildPlan {
toolsVersion: toolsVersion,
buildParameters: buildParameters,
testTargetRole: .entryPoint(isSynthesized: true),
disableSandbox: disableSandbox,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
Expand Down Expand Up @@ -174,6 +177,7 @@ extension BuildPlan {
toolsVersion: toolsVersion,
buildParameters: buildParameters,
testTargetRole: .entryPoint(isSynthesized: false),
disableSandbox: disableSandbox,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
Expand All @@ -195,6 +199,7 @@ extension BuildPlan {
toolsVersion: toolsVersion,
buildParameters: buildParameters,
testTargetRole: .entryPoint(isSynthesized: false),
disableSandbox: disableSandbox,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Build/BuildPlan/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
/// Cache for tools information.
var externalExecutablesCache = [BinaryTarget: [ExecutableInfo]]()

/// Whether to disable sandboxing (e.g. for macros).
private let disableSandbox: Bool

/// The filesystem to operate on.
let fileSystem: any FileSystem

Expand Down Expand Up @@ -271,6 +274,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
additionalFileRules: [FileRuleDescription] = [],
buildToolPluginInvocationResults: [ResolvedTarget: [BuildToolPluginInvocationResult]] = [:],
prebuildCommandResults: [ResolvedTarget: [PrebuildCommandResult]] = [:],
disableSandbox: Bool = false,
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope
) throws {
Expand All @@ -279,6 +283,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
self.graph = graph
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
self.prebuildCommandResults = prebuildCommandResults
self.disableSandbox = disableSandbox
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Plan")

Expand Down Expand Up @@ -377,6 +382,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
prebuildCommandResults: prebuildCommandResults[target] ?? [],
requiredMacroProducts: requiredMacroProducts,
shouldGenerateTestObservation: generateTestObservation,
disableSandbox: self.disableSandbox,
fileSystem: fileSystem,
observabilityScope: observabilityScope)
)
Expand Down Expand Up @@ -423,6 +429,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
let derivedTestTargets = try Self.makeDerivedTestTargets(
productsBuildParameters,
graph,
self.disableSandbox,
self.fileSystem,
self.observabilityScope
)
Expand Down