Skip to content

Commit 31bc808

Browse files
authored
Fix package access modifier in XCBuild support (#7258)
`package` access modifier was previously not supported in `swift build --build-system xcode`. This causes build issues when attempting to produce Universal binaries for SwiftPM, for example in this job https://ci.swift.org/job/swift-PR-source-compat-suite-debug-macos/1297/consoleFull. The reason was that `-package-name` option was not added to `OTHER_SWIFT_FLAGS` in `PIFBuilder.swift`. Additionally, in llbuild support code we were shelling out to Swift Driver for every target to check whether `-package-name` is supported. Now with `-package-name` options calculation generalized across build systems, Swift Driver checks are done once per `BuildParameters` initialization, which reduces excessive shelling for llbuild. Resolves rdar://120925895.
1 parent 6ff5cbd commit 31bc808

File tree

17 files changed

+284
-83
lines changed

17 files changed

+284
-83
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// This source file is part of the Swift open source project
66
//
7-
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
7+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
88
// Licensed under Apache License v2.0 with Runtime Library Exception
99
//
1010
// See http://swift.org/LICENSE.txt for license information
@@ -349,7 +349,7 @@ let package = Package(
349349
.target(
350350
/** Support for building using Xcode's build system */
351351
name: "XCBuildSupport",
352-
dependencies: ["SPMBuildCore", "PackageGraph"],
352+
dependencies: ["DriverSupport", "SPMBuildCore", "PackageGraph"],
353353
exclude: ["CMakeLists.txt", "CODEOWNERS"]
354354
),
355355
.target(

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Basics
14+
1415
import Foundation
1516
import PackageGraph
1617
import PackageLoading
1718
import PackageModel
19+
20+
@_spi(SwiftPMInternal)
1821
import SPMBuildCore
1922

2023
#if USE_IMPL_ONLY_IMPORTS
@@ -419,18 +422,6 @@ public final class SwiftTargetBuildDescription {
419422
try self.fileSystem.writeIfChanged(path: path, string: content)
420423
}
421424

422-
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage, packageAccess: Bool) -> [String] {
423-
let flag = "-package-name"
424-
if pkg.manifest.usePackageNameFlag,
425-
DriverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
426-
if packageAccess {
427-
let pkgID = pkg.identity.description.spm_mangledToC99ExtendedIdentifier()
428-
return [flag, pkgID]
429-
}
430-
}
431-
return []
432-
}
433-
434425
private func macroArguments() throws -> [String] {
435426
var args = [String]()
436427

@@ -450,7 +441,14 @@ public final class SwiftTargetBuildDescription {
450441
#endif
451442

452443
// If we're using an OSS toolchain, add the required arguments bringing in the plugin server from the default toolchain if available.
453-
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, DriverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = try self.buildParameters.toolchain.swiftPluginServerPath {
444+
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain,
445+
DriverSupport.checkSupportedFrontendFlags(
446+
flags: ["-external-plugin-path"],
447+
toolchain: self.buildParameters.toolchain,
448+
fileSystem: self.fileSystem
449+
),
450+
let pluginServer = try self.buildParameters.toolchain.swiftPluginServerPath
451+
{
454452
let toolchainUsrPath = pluginServer.parentDirectory.parentDirectory
455453
let pluginPathComponents = ["lib", "swift", "host", "plugins"]
456454

@@ -631,7 +629,10 @@ public final class SwiftTargetBuildDescription {
631629
args += ["-user-module-version", version.description]
632630
}
633631

634-
args += self.packageNameArgumentIfSupported(with: self.package, packageAccess: self.target.packageAccess)
632+
args += self.package.packageNameArgument(
633+
target: self.target,
634+
isPackageNameSupported: self.buildParameters.driverParameters.isPackageAccessModifierSupported
635+
)
635636
args += try self.macroArguments()
636637

637638
// rdar://117578677
@@ -656,7 +657,12 @@ public final class SwiftTargetBuildDescription {
656657

657658
result.append("-module-name")
658659
result.append(self.target.c99name)
659-
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, packageAccess: self.target.packageAccess))
660+
result.append(
661+
contentsOf: self.package.packageNameArgument(
662+
target: self.target,
663+
isPackageNameSupported: self.buildParameters.driverParameters.isPackageAccessModifierSupported
664+
)
665+
)
660666
if !scanInvocation {
661667
result.append("-emit-dependencies")
662668

Sources/CoreCommands/SwiftTool.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -23,8 +23,11 @@ import SPMBuildCore
2323
import Workspace
2424

2525
#if USE_IMPL_ONLY_IMPORTS
26-
@_implementationOnly import DriverSupport
26+
@_implementationOnly
27+
@_spi(SwiftPMInternal)
28+
import DriverSupport
2729
#else
30+
@_spi(SwiftPMInternal)
2831
import DriverSupport
2932
#endif
3033

@@ -757,7 +760,11 @@ public final class SwiftTool {
757760
enableParseableModuleInterfaces: options.build.shouldEnableParseableModuleInterfaces,
758761
explicitTargetDependencyImportCheckingMode: options.build.explicitTargetDependencyImportCheck.modeParameter,
759762
useIntegratedSwiftDriver: options.build.useIntegratedSwiftDriver,
760-
useExplicitModuleBuild: options.build.useExplicitModuleBuild
763+
useExplicitModuleBuild: options.build.useExplicitModuleBuild,
764+
isPackageAccessModifierSupported: DriverSupport.isPackageNameSupported(
765+
toolchain: toolchain,
766+
fileSystem: self.fileSystem
767+
)
761768
),
762769
linkingParameters: .init(
763770
linkerDeadStrip: options.linker.linkerDeadStrip,

Sources/DriverSupport/DriverSupportUtils.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -20,7 +20,7 @@ import struct TSCBasic.ProcessResult
2020
public enum DriverSupport {
2121
private static var flagsMap = ThreadSafeBox<[String: Set<String>]>()
2222

23-
// This checks _frontend_ supported flags, which are not necessarily supported in the driver.
23+
/// This checks _frontend_ supported flags, which are not necessarily supported in the driver.
2424
public static func checkSupportedFrontendFlags(
2525
flags: Set<String>,
2626
toolchain: PackageModel.Toolchain,
@@ -54,7 +54,7 @@ public enum DriverSupport {
5454
// This checks if given flags are supported in the built-in toolchain driver. Currently
5555
// there's no good way to get the supported flags from it, so run `swiftc -h` directly
5656
// to get the flags and cache the result.
57-
public static func checkToolchainDriverFlags(
57+
static func checkToolchainDriverFlags(
5858
flags: Set<String>,
5959
toolchain: PackageModel.Toolchain,
6060
fileSystem: FileSystem
@@ -83,4 +83,9 @@ public enum DriverSupport {
8383
return false
8484
}
8585
}
86+
87+
@_spi(SwiftPMInternal)
88+
public static func isPackageNameSupported(toolchain: PackageModel.Toolchain, fileSystem: FileSystem) -> Bool {
89+
DriverSupport.checkToolchainDriverFlags(flags: ["-package-name"], toolchain: toolchain, fileSystem: fileSystem)
90+
}
8691
}

Sources/PackageModel/Target/SwiftTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information

Sources/SPMBuildCore/BuildParameters/BuildParameters+Driver.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2020-2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2020-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -25,13 +25,15 @@ extension BuildParameters {
2525
enableParseableModuleInterfaces: Bool = false,
2626
explicitTargetDependencyImportCheckingMode: TargetDependencyImportCheckingMode = .none,
2727
useIntegratedSwiftDriver: Bool = false,
28-
useExplicitModuleBuild: Bool = false
28+
useExplicitModuleBuild: Bool = false,
29+
isPackageAccessModifierSupported: Bool = false
2930
) {
3031
self.canRenameEntrypointFunctionName = canRenameEntrypointFunctionName
3132
self.enableParseableModuleInterfaces = enableParseableModuleInterfaces
3233
self.explicitTargetDependencyImportCheckingMode = explicitTargetDependencyImportCheckingMode
3334
self.useIntegratedSwiftDriver = useIntegratedSwiftDriver
3435
self.useExplicitModuleBuild = useExplicitModuleBuild
36+
self.isPackageAccessModifierSupported = isPackageAccessModifierSupported
3537
}
3638

3739
/// Whether to enable the entry-point-function-name feature.
@@ -45,11 +47,16 @@ extension BuildParameters {
4547
/// `.swiftmodule`s.
4648
public var enableParseableModuleInterfaces: Bool
4749

48-
/// Whether to use the integrated Swift driver rather than shelling out
50+
/// Whether to use the integrated Swift Driver rather than shelling out
4951
/// to a separate process.
5052
public var useIntegratedSwiftDriver: Bool
5153

5254
/// Whether to use the explicit module build flow (with the integrated driver).
5355
public var useExplicitModuleBuild: Bool
56+
57+
/// Whether the version of Swift Driver used in the currently selected toolchain
58+
/// supports `-package-name` options.
59+
@_spi(SwiftPMInternal)
60+
public var isPackageAccessModifierSupported: Bool
5461
}
5562
}

Sources/SPMBuildCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_library(SPMBuildCore
2323
Plugins/PluginMessages.swift
2424
Plugins/PluginScriptRunner.swift
2525
PrebuildCommandResult.swift
26+
ResolvedPackage+Extensions.swift
2627
Triple+Extensions.swift
2728
XCFrameworkMetadata.swift)
2829
# NOTE(compnerd) workaround for CMake not setting up include flags yet
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import struct PackageGraph.ResolvedPackage
14+
import struct PackageGraph.ResolvedTarget
15+
16+
extension ResolvedPackage {
17+
@_spi(SwiftPMInternal)
18+
public func packageNameArgument(target: ResolvedTarget, isPackageNameSupported: Bool) -> [String] {
19+
if self.manifest.usePackageNameFlag, target.packageAccess {
20+
["-package-name", self.identity.description.spm_mangledToC99ExtendedIdentifier()]
21+
} else {
22+
[]
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)