Skip to content

Commit 8ca2b79

Browse files
committed
Enable package-name setting for v5.9+
Support Target Grouping - Allow opting out of package boundary - Default set to package boundary Resolves rdar://107454728
1 parent 96d8497 commit 8ca2b79

File tree

30 files changed

+560
-21
lines changed

30 files changed

+560
-21
lines changed

Fixtures/Miscellaneous/PackageNameFlag/appPkg/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:999.0
1+
// swift-tools-version:5.9
22
import PackageDescription
33

44
let package = Package(

Fixtures/Miscellaneous/PackageNameFlag/fooPkg/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:999.0
1+
// swift-tools-version:5.9
22
import PackageDescription
33

44
let package = Package(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// swift-tools-version:5.9
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "libPkg",
6+
products: [
7+
.executable(name: "ExampleApp", targets: ["ExampleApp"]),
8+
.library(name: "MainLib", targets: ["MainLib"]),
9+
],
10+
targets: [
11+
.executableTarget(name: "ExampleApp", group: .excluded, dependencies: ["MainLib"]),
12+
.target(name: "MainLib", group: .package, dependencies: ["Core"]),
13+
.target(name: "Core", dependencies: ["DataManager"]),
14+
.target(name: "DataManager", group: .package, dependencies: ["DataModel"]),
15+
.target(name: "DataModel"),
16+
.testTarget(name: "MainLibTests", group: .package, dependencies: ["MainLib"]),
17+
.testTarget(name: "BlackBoxTests", group: .excluded, dependencies: ["MainLib"])
18+
]
19+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import DataManager
2+
3+
public struct PublicCore {
4+
public let publicVar: Int
5+
public init(publicVar: Int) {
6+
self.publicVar = publicVar
7+
}
8+
public func publicCoreFunc() {
9+
managePublicFunc()
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import DataModel
2+
3+
public func managePublicFunc() -> PublicData? {
4+
return nil
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class PublicData {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import MainLib
2+
3+
publicFunc()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Core
2+
3+
public func publicFunc() -> Int {
4+
print("public decl")
5+
return PublicCore(publicVar: 10).publicVar
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import MainLib
2+
import XCTest
3+
4+
class BlackBoxTests: XCTestCase {
5+
func testBlackBox() {
6+
let x = publicFunc()
7+
XCTAssertTrue(x > 0)
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import MainLib
2+
import XCTest
3+
4+
class TestMainLib: XCTestCase {
5+
func testMainLib() {
6+
let x = publicFunc()
7+
XCTAssertTrue(x > 0)
8+
}
9+
}

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,19 @@ public final class SwiftTargetBuildDescription {
385385
try self.fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
386386
}
387387

388-
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage) -> [String] {
388+
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage, group: Target.Group) -> [String] {
389+
let flag = "-package-name"
389390
if pkg.manifest.usePackageNameFlag,
390-
driverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
391-
return ["-package-name", pkg.identity.description.spm_mangledToC99ExtendedIdentifier()]
392-
}
393-
return []
391+
driverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
392+
switch group {
393+
case .package:
394+
let pkgID = pkg.identity.description.spm_mangledToC99ExtendedIdentifier()
395+
return [flag, pkgID]
396+
case .excluded:
397+
return []
398+
}
399+
}
400+
return []
394401
}
395402

396403
private func macroArguments() throws -> [String] {
@@ -513,7 +520,7 @@ public final class SwiftTargetBuildDescription {
513520
}
514521
}
515522

516-
args += self.packageNameArgumentIfSupported(with: self.package)
523+
args += self.packageNameArgumentIfSupported(with: self.package, group: self.target.group)
517524
args += try self.macroArguments()
518525

519526
return args
@@ -527,7 +534,7 @@ public final class SwiftTargetBuildDescription {
527534

528535
result.append("-module-name")
529536
result.append(self.target.c99name)
530-
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
537+
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
531538
if !scanInvocation {
532539
result.append("-emit-dependencies")
533540

@@ -573,7 +580,7 @@ public final class SwiftTargetBuildDescription {
573580
result.append("-emit-module")
574581
result.append("-emit-module-path")
575582
result.append(self.moduleOutputPath.pathString)
576-
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
583+
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
577584
result += self.buildParameters.toolchain.extraFlags.swiftCompilerFlags
578585

579586
result.append("-Xfrontend")
@@ -619,7 +626,7 @@ public final class SwiftTargetBuildDescription {
619626

620627
result.append("-module-name")
621628
result.append(self.target.c99name)
622-
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
629+
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
623630
result.append("-incremental")
624631
result.append("-emit-dependencies")
625632

Sources/Build/BuildPlan.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
282282

283283
let discoveryTarget = SwiftTarget(
284284
name: discoveryTargetName,
285+
group: .package, // test target is allowed access to package decls by default
285286
dependencies: testProduct.underlyingProduct.targets.map { .target($0, conditions: []) },
286287
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir)
287288
)
@@ -313,6 +314,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
313314

314315
let entryPointTarget = SwiftTarget(
315316
name: testProduct.name,
317+
group: .package, // test target is allowed access to package decls by default
316318
type: .library,
317319
dependencies: testProduct.underlyingProduct.targets.map { .target($0, conditions: []) } + [.target(discoveryTarget, conditions: [])],
318320
testEntryPointSources: entryPointSources
@@ -342,6 +344,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
342344
// Allow using the explicitly-specified test entry point target, but still perform test discovery and thus declare a dependency on the discovery targets.
343345
let entryPointTarget = SwiftTarget(
344346
name: entryPointResolvedTarget.underlyingTarget.name,
347+
group: entryPointResolvedTarget.group,
345348
dependencies: entryPointResolvedTarget.underlyingTarget.dependencies + [.target(discoveryTargets.target, conditions: [])],
346349
testEntryPointSources: entryPointResolvedTarget.underlyingTarget.sources
347350
)
@@ -1000,12 +1003,14 @@ private extension PackageModel.SwiftTarget {
10001003
/// Initialize a SwiftTarget representing a test entry point.
10011004
convenience init(
10021005
name: String,
1006+
group: Group,
10031007
type: PackageModel.Target.Kind? = nil,
10041008
dependencies: [PackageModel.Target.Dependency],
10051009
testEntryPointSources sources: Sources
10061010
) {
10071011
self.init(
10081012
name: name,
1013+
group: group,
10091014
type: type ?? .executable,
10101015
path: .root,
10111016
sources: sources,

Sources/CompilerPluginSupport/TargetExtensions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ public extension Target {
1616
@available(_PackageDescription, introduced: 999.0)
1717
static func macro(
1818
name: String,
19+
group: TargetGroup,
1920
dependencies: [Dependency] = [],
2021
path: String? = nil,
2122
exclude: [String] = [],
2223
sources: [String]? = nil
2324
) -> Target {
2425
return Target(name: name,
26+
group: group,
2527
dependencies: dependencies,
2628
path: path,
2729
exclude: exclude,

Sources/PackageDescription/PackageDescriptionSerialization.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ enum Serialization {
166166
case `macro`
167167
}
168168

169+
enum TargetGroup: Codable {
170+
case package
171+
case excluded
172+
}
173+
169174
enum PluginCapability: Codable {
170175
case buildTool
171176
case command(intent: PluginCommandIntent, permissions: [PluginPermission])
@@ -196,6 +201,7 @@ enum Serialization {
196201

197202
struct Target: Codable {
198203
let name: String
204+
let group: TargetGroup
199205
let path: String?
200206
let url: String?
201207
let sources: [String]?

Sources/PackageDescription/PackageDescriptionSerializationConversion.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ extension Serialization.TargetType {
219219
}
220220
}
221221

222+
extension Serialization.TargetGroup {
223+
init(_ type: PackageDescription.Target.TargetGroup) {
224+
switch type {
225+
case .package: self = .package
226+
case .excluded: self = .excluded
227+
}
228+
}
229+
}
230+
222231
extension Serialization.PluginCapability {
223232
init(_ capability: PackageDescription.Target.PluginCapability) {
224233
switch capability {
@@ -270,6 +279,7 @@ extension Serialization.PluginUsage {
270279
extension Serialization.Target {
271280
init(_ target: PackageDescription.Target) {
272281
self.name = target.name
282+
self.group = .init(target.group)
273283
self.path = target.path
274284
self.url = target.url
275285
self.sources = target.sources

0 commit comments

Comments
 (0)