Skip to content

Build direct dependencies of tests for target triple #7350

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 2 commits into from
Feb 20, 2024
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 @@ -130,7 +130,7 @@ public final class SwiftTargetBuildDescription {
self.tempsPath.appending(component: self.target.c99name + ".swiftmodule.o")
}

/// The path to the swifinterface file after compilation.
/// The path to the swiftinterface file after compilation.
var parseableModuleInterfaceOutputPath: AbsolutePath {
self.modulesPath.appending(component: self.target.c99name + ".swiftinterface")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Build/BuildPlan/BuildPlan+Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ extension BuildPlan {

buildProduct.staticTargets = dependencies.staticTargets
buildProduct.dylibs = try dependencies.dylibs.map {
guard let product = productMap[$0.id] else {
guard let product = self.productMap[$0.id] else {
throw InternalError("unknown product \($0)")
}
return product
}
buildProduct.objects += try dependencies.staticTargets.flatMap { targetName -> [AbsolutePath] in
guard let target = targetMap[targetName.id] else {
guard let target = self.targetMap[targetName.id] else {
throw InternalError("unknown target \(targetName)")
}
return try target.objects
Expand Down
5 changes: 4 additions & 1 deletion Sources/PackageGraph/PackageGraph+Loading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ private func createResolvedPackages(

// Resolve module aliases, if specified, for targets and their dependencies
// across packages. Aliasing will result in target renaming.
let moduleAliasingUsed = try resolveModuleAliases(packageBuilders: packageBuilders, observabilityScope: observabilityScope)
let moduleAliasingUsed = try resolveModuleAliases(
packageBuilders: packageBuilders,
observabilityScope: observabilityScope
)

// Scan and validate the dependencies
for packageBuilder in packageBuilders {
Expand Down
10 changes: 7 additions & 3 deletions Sources/PackageGraph/PackageGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ public struct PackageGraph {
/// Returns true if a given target is present in root packages and is not excluded for the given build environment.
public func isInRootPackages(_ target: ResolvedTarget, satisfying buildEnvironment: BuildEnvironment) -> Bool {
// FIXME: This can be easily cached.
return rootPackages.reduce(into: IdentifiableSet<ResolvedTarget>()) { (accumulator: inout IdentifiableSet<ResolvedTarget>, package: ResolvedPackage) in
return rootPackages.reduce(
into: IdentifiableSet<ResolvedTarget>()
) { (accumulator: inout IdentifiableSet<ResolvedTarget>, package: ResolvedPackage) in
let allDependencies = package.targets.flatMap { $0.dependencies }
let unsatisfiedDependencies = allDependencies.filter { !$0.satisfies(buildEnvironment) }
let unsatisfiedDependencyTargets = unsatisfiedDependencies.compactMap { (dep: ResolvedTarget.Dependency) -> ResolvedTarget? in
let unsatisfiedDependencyTargets = unsatisfiedDependencies.compactMap { (
dep: ResolvedTarget.Dependency
) -> ResolvedTarget? in
switch dep {
case .target(let target, _):
return target
Expand Down Expand Up @@ -152,7 +156,7 @@ public struct PackageGraph {
for package in self.packages {
let targetsToInclude: [ResolvedTarget]
if rootPackages.contains(id: package.id) {
targetsToInclude = package.targets
targetsToInclude = Array(package.targets)
} else {
// Don't include tests targets from non-root packages so swift-test doesn't
// try to run them.
Expand Down
42 changes: 40 additions & 2 deletions Sources/PackageGraph/Resolution/ResolvedPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
//===----------------------------------------------------------------------===//

import Basics

import struct OrderedCollections.OrderedDictionary

import PackageModel

/// A fully resolved package. Contains resolved targets, products and dependencies of the package.
Expand Down Expand Up @@ -64,8 +67,43 @@ public struct ResolvedPackage {
platformVersionProvider: PlatformVersionProvider
) {
self.underlying = underlying
self.targets = targets
self.products = products

var processedTargets = OrderedDictionary<ResolvedTarget.ID, ResolvedTarget>(
uniqueKeysWithValues: targets.map { ($0.id, $0) }
)
var processedProducts = [ResolvedProduct]()
// Make sure that direct macro dependencies of test products are also built for the target triple.
// Without this workaround, `assertMacroExpansion` in tests can't be built, as it requires macros
// and SwiftSyntax to be built for the target triple: https://github.com/apple/swift-package-manager/pull/7349
for var product in products {
if product.type == .test {
var targets = IdentifiableSet<ResolvedTarget>()
for var target in product.targets {
var dependencies = [ResolvedTarget.Dependency]()
for dependency in target.dependencies {
switch dependency {
case .target(var target, let conditions) where target.type == .macro:
target.buildTriple = .destination
dependencies.append(.target(target, conditions: conditions))
processedTargets[target.id] = target
case .product(var product, let conditions) where product.type == .macro:
product.buildTriple = .destination
dependencies.append(.product(product, conditions: conditions))
default:
dependencies.append(dependency)
}
}
target.dependencies = dependencies
targets.insert(target)
}
product.targets = targets
}

processedProducts.append(product)
}

self.products = processedProducts
self.targets = Array(processedTargets.values)
self.dependencies = dependencies
self.defaultLocalization = defaultLocalization
self.supportedPlatforms = supportedPlatforms
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageGraph/Resolution/ResolvedProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct ResolvedProduct {
public let underlying: Product

/// The top level targets contained in this product.
public private(set) var targets: IdentifiableSet<ResolvedTarget>
public internal(set) var targets: IdentifiableSet<ResolvedTarget>

/// Executable target for test entry point file.
public let testEntryPointTarget: ResolvedTarget?
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageGraph/Resolution/ResolvedTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public struct ResolvedTarget {
public let underlying: Target

/// The dependencies of this target.
public private(set) var dependencies: [Dependency]
public internal(set) var dependencies: [Dependency]

/// The default localization for resources.
public let defaultLocalization: String?
Expand Down
11 changes: 8 additions & 3 deletions Tests/BuildTests/CrossCompilationBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
let macroProduct = try XCTUnwrap(macroProducts.first)
XCTAssertEqual(macroProduct.buildParameters.triple, toolsTriple)

// FIXME: check for *toolsTriple*
// FIXME: check for *toolsTriple*
let mmioTarget = try XCTUnwrap(plan.targets.first { try $0.swiftTarget().target.name == "MMIO" }?.swiftTarget())
let compileArguments = try mmioTarget.compileArguments()
let compileArguments = try mmioTarget.emitCommandLine()
XCTAssertMatch(
compileArguments,
["-Xfrontend", "-load-plugin-executable", "-Xfrontend", .contains(toolsTriple.tripleString)]
[
"-I", .equal(mmioTarget.moduleOutputPath.parentDirectory.pathString),
.anySequence,
"-Xfrontend", "-load-plugin-executable",
"-Xfrontend", .contains(toolsTriple.tripleString)
]
)
}
}