Skip to content
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

Support Target Grouping #6356

Merged
merged 2 commits into from
Apr 3, 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
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:999.0
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", dependencies: ["DataManager"]),
.target(name: "DataManager", group: .package, dependencies: ["DataModel"]),
.target(name: "DataModel"),
.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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,19 @@ 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()]
}
return []
driverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
switch group {
case .package:
let pkgID = pkg.identity.description.spm_mangledToC99ExtendedIdentifier()
return [flag, pkgID]
case .excluded:
return []
}
}
return []
}

private func macroArguments() throws -> [String] {
Expand Down Expand Up @@ -513,7 +520,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 +534,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 +580,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 +626,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,11 @@ enum Serialization {
case `macro`
}

enum TargetGroup: Codable {
case package
case excluded
}

enum PluginCapability: Codable {
case buildTool
case command(intent: PluginCommandIntent, permissions: [PluginPermission])
Expand Down Expand Up @@ -196,6 +201,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,15 @@ extension Serialization.TargetType {
}
}

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

extension Serialization.PluginCapability {
init(_ capability: PackageDescription.Target.PluginCapability) {
switch capability {
Expand Down Expand Up @@ -270,6 +279,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