Skip to content

Commit

Permalink
Enable package-name setting for v5.9+
Browse files Browse the repository at this point in the history
Support Target Grouping
- Allow opting out of package boundary
- Allow subgrouping within a package
- Default set to package boundary
  • Loading branch information
elsh committed Mar 31, 2023
1 parent 96d8497 commit a933f7a
Show file tree
Hide file tree
Showing 30 changed files with 580 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:999.0
// swift-tools-version:5.9
import PackageDescription

let package = Package(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:999.0
// swift-tools-version:5.9
import PackageDescription

let package = Package(
Expand Down
19 changes: 19 additions & 0 deletions Fixtures/Miscellaneous/TargetGrouping/libPkg/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "libPkg",
products: [
.executable(name: "ExampleApp", targets: ["ExampleApp"]),
.library(name: "MainLib", targets: ["MainLib"]),
],
targets: [
.executableTarget(name: "ExampleApp", group: .excluded, dependencies: ["MainLib"]),
.target(name: "MainLib", group: .package, dependencies: ["Core"]),
.target(name: "Core", group: .package, dependencies: ["DataManager"]),
.target(name: "DataManager", group: .named("DataGroup"), dependencies: ["DataModel"]),
.target(name: "DataModel", group: .named("DataGroup")),
.testTarget(name: "MainLibTests", group: .package, dependencies: ["MainLib"]),
.testTarget(name: "BlackBoxTests", group: .excluded, dependencies: ["MainLib"])
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DataManager

public struct PublicCore {
public let publicVar: Int
public init(publicVar: Int) {
self.publicVar = publicVar
}
public func publicCoreFunc() {
managePublicFunc()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DataModel

public func managePublicFunc() -> PublicData? {
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class PublicData {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MainLib

publicFunc()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Core

public func publicFunc() -> Int {
print("public decl")
return PublicCore(publicVar: 10).publicVar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import MainLib
import XCTest

class BlackBoxTests: XCTestCase {
func testBlackBox() {
let x = publicFunc()
XCTAssertTrue(x > 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import MainLib
import XCTest

class TestMainLib: XCTestCase {
func testMainLib() {
let x = publicFunc()
XCTAssertTrue(x > 0)
}
}
25 changes: 18 additions & 7 deletions Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,21 @@ public final class SwiftTargetBuildDescription {
try self.fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
}

private func packageNameArgumentIfSupported(with pkg: ResolvedPackage) -> [String] {
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage, group: Target.Group) -> [String] {
let flag = "-package-name"
if pkg.manifest.usePackageNameFlag,
driverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
return ["-package-name", pkg.identity.description.spm_mangledToC99ExtendedIdentifier()]
driverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {

let pkgID = pkg.identity.description.spm_mangledToC99ExtendedIdentifier()
switch group {
case .package:
return [flag, pkgID]
case .excluded:
return []
case .named(let groupName):
let groupID = groupName.spm_mangledToC99ExtendedIdentifier()
return [flag, pkgID + "_" + groupID]
}
}
return []
}
Expand Down Expand Up @@ -513,7 +524,7 @@ public final class SwiftTargetBuildDescription {
}
}

args += self.packageNameArgumentIfSupported(with: self.package)
args += self.packageNameArgumentIfSupported(with: self.package, group: self.target.group)
args += try self.macroArguments()

return args
Expand All @@ -527,7 +538,7 @@ public final class SwiftTargetBuildDescription {

result.append("-module-name")
result.append(self.target.c99name)
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
if !scanInvocation {
result.append("-emit-dependencies")

Expand Down Expand Up @@ -573,7 +584,7 @@ public final class SwiftTargetBuildDescription {
result.append("-emit-module")
result.append("-emit-module-path")
result.append(self.moduleOutputPath.pathString)
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
result += self.buildParameters.toolchain.extraFlags.swiftCompilerFlags

result.append("-Xfrontend")
Expand Down Expand Up @@ -619,7 +630,7 @@ public final class SwiftTargetBuildDescription {

result.append("-module-name")
result.append(self.target.c99name)
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package))
result.append(contentsOf: packageNameArgumentIfSupported(with: self.package, group: self.target.group))
result.append("-incremental")
result.append("-emit-dependencies")

Expand Down
5 changes: 5 additions & 0 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {

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

let entryPointTarget = SwiftTarget(
name: testProduct.name,
group: .package, // test target is allowed access to package decls by default
type: .library,
dependencies: testProduct.underlyingProduct.targets.map { .target($0, conditions: []) } + [.target(discoveryTarget, conditions: [])],
testEntryPointSources: entryPointSources
Expand Down Expand Up @@ -342,6 +344,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
// Allow using the explicitly-specified test entry point target, but still perform test discovery and thus declare a dependency on the discovery targets.
let entryPointTarget = SwiftTarget(
name: entryPointResolvedTarget.underlyingTarget.name,
group: entryPointResolvedTarget.group,
dependencies: entryPointResolvedTarget.underlyingTarget.dependencies + [.target(discoveryTargets.target, conditions: [])],
testEntryPointSources: entryPointResolvedTarget.underlyingTarget.sources
)
Expand Down Expand Up @@ -1000,12 +1003,14 @@ private extension PackageModel.SwiftTarget {
/// Initialize a SwiftTarget representing a test entry point.
convenience init(
name: String,
group: Group,
type: PackageModel.Target.Kind? = nil,
dependencies: [PackageModel.Target.Dependency],
testEntryPointSources sources: Sources
) {
self.init(
name: name,
group: group,
type: type ?? .executable,
path: .root,
sources: sources,
Expand Down
2 changes: 2 additions & 0 deletions Sources/CompilerPluginSupport/TargetExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public extension Target {
@available(_PackageDescription, introduced: 999.0)
static func macro(
name: String,
group: TargetGroup,
dependencies: [Dependency] = [],
path: String? = nil,
exclude: [String] = [],
sources: [String]? = nil
) -> Target {
return Target(name: name,
group: group,
dependencies: dependencies,
path: path,
exclude: exclude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ enum Serialization {
case `macro`
}

enum TargetGroup: Codable {
case package
case excluded
case named(String)
}

enum PluginCapability: Codable {
case buildTool
case command(intent: PluginCommandIntent, permissions: [PluginPermission])
Expand Down Expand Up @@ -196,6 +202,7 @@ enum Serialization {

struct Target: Codable {
let name: String
let group: TargetGroup
let path: String?
let url: String?
let sources: [String]?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ extension Serialization.TargetType {
}
}

extension Serialization.TargetGroup {
init(_ type: PackageDescription.Target.TargetGroup) {
switch type {
case .package: self = .package
case .excluded: self = .excluded
case .named(let groupName): self = .named(groupName)
}
}
}

extension Serialization.PluginCapability {
init(_ capability: PackageDescription.Target.PluginCapability) {
switch capability {
Expand Down Expand Up @@ -270,6 +280,7 @@ extension Serialization.PluginUsage {
extension Serialization.Target {
init(_ target: PackageDescription.Target) {
self.name = target.name
self.group = .init(target.group)
self.path = target.path
self.url = target.url
self.sources = target.sources
Expand Down
Loading

0 comments on commit a933f7a

Please sign in to comment.