Skip to content

Commit a0b25d3

Browse files
authored
Support macros when cross-compiling (#7118)
### Motivation: Not supporting macros in cross-compilation is a major limitation, especially for libraries like https://github.com/apple/swift-mmio. ### Modifications: Added `enum BuildTriple { case tools, destination }` and `var buildTriple: BuildTriple` on `ResolvedTarget` and `ResolvedProduct`. We're not using "host" and "target" triple terminology in this enum, as that clashes with build system "targets" and can lead to confusion in this context. Corresponding value is assigned to this property depending on target and product type: `tools` for macros, plugins, and their dependencies, `destination` for everything else (the default). Based on this property we can choose between `buildParameters.hostTriple` and `buildParameters.targetTriple` during build plan construction. Additionally, the resolved products and resolved targets graph is now constructed in a way that allows certain targets and products to be built twice: once for host triple, once for target triple if needed. This required modifying build description and build manifest generation to distinguish these products and targets from each other that are built twice. Artifacts built for the host now have `-tools` suffix appended to their names. This cascaded into making changes throughout the code base for build tool plugins and package plugins handling, which constructed their paths in an ad-hoc manner without accounting for possible changes to these names. Also added `CrossCompilationPackageGraphTests` and `CrossCompilationBuildPlanTests` to verify the changes made are applied correctly. ### Result: Resolves #6950 Resolves rdar://105991372
1 parent 2fae93c commit a0b25d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1080
-356
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
Note: This is in reverse chronological order, so newer entries are added to the top.
22

3-
Swift Next
3+
Swift 6.0
44
-----------
55

66
* [#7202]
77

88
Package manifests can now access information about the Git repository the given package is in via the context object's
99
`gitInformation` property. This allows to determine the current tag (if any), the current commit and whether or not there are uncommited changes.
1010

11+
* [#7201]
12+
13+
`// swift-tools-version:` can now be specified on subsequent lines of `Package.swift`, for example when first few lines are required to contain a licensing comment header.
14+
15+
* [#7118]
16+
17+
Macros cross-compiled by SwiftPM with Swift SDKs are now correctly built, loaded, and evaluated for the host triple.
18+
19+
Swift 5.10
20+
-----------
21+
1122
* [#7010]
1223

1324
On macOS, `swift build` and `swift run` now produce binaries that allow backtraces in debug builds. Pass `SWIFT_BACKTRACE=enable=yes` environment variable to enable backtraces on such binaries when running them.
1425

15-
* [7101]
26+
* [#7101]
1627

1728
Binary artifacts are now cached along side repository checkouts so they do not need to be re-downloaded across projects.
1829

@@ -387,4 +398,8 @@ Swift 3.0
387398
[#6276]: https://github.com/apple/swift-package-manager/pull/6276
388399
[#6540]: https://github.com/apple/swift-package-manager/pull/6540
389400
[#6663]: https://github.com/apple/swift-package-manager/pull/6663
401+
[#7010]: https://github.com/apple/swift-package-manager/pull/7010
390402
[#7101]: https://github.com/apple/swift-package-manager/pull/7101
403+
[#7118]: https://github.com/apple/swift-package-manager/pull/7118
404+
[#7201]: https://github.com/apple/swift-package-manager/pull/7201
405+
[#7202]: https://github.com/apple/swift-package-manager/pull/7202

Sources/Basics/Collections/IdentifiableSet.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
4545
}
4646

4747
public subscript(id: Element.ID) -> Element? {
48-
self.storage[id]
48+
get {
49+
self.storage[id]
50+
}
51+
set {
52+
self.storage[id] = newValue
53+
}
4954
}
5055

5156
public func index(after i: Index) -> Index {
5257
Index(storageIndex: self.storage.index(after: i.storageIndex))
5358
}
5459

60+
public mutating func insert(_ element: Element) {
61+
self.storage[element.id] = element
62+
}
63+
5564
public func union(_ otherSequence: some Sequence<Element>) -> Self {
5665
var result = self
5766
for element in otherSequence {

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import struct SPMBuildCore.BuildParameters
1919
import struct SPMBuildCore.BuildToolPluginInvocationResult
2020
import struct SPMBuildCore.PrebuildCommandResult
2121

22+
@_spi(SwiftPMInternal)
23+
import SPMBuildCore
24+
2225
import enum TSCBasic.ProcessEnv
2326

2427
/// Target description for a Clang target i.e. C language family target.
@@ -49,7 +52,7 @@ public final class ClangTargetBuildDescription {
4952

5053
/// Path to the bundle generated for this module (if any).
5154
var bundlePath: AbsolutePath? {
52-
guard !resources.isEmpty else {
55+
guard !self.resources.isEmpty else {
5356
return .none
5457
}
5558

@@ -127,7 +130,7 @@ public final class ClangTargetBuildDescription {
127130
self.target = target
128131
self.toolsVersion = toolsVersion
129132
self.buildParameters = buildParameters
130-
self.tempsPath = buildParameters.buildPath.appending(component: target.c99name + ".build")
133+
self.tempsPath = target.tempsPath(buildParameters)
131134
self.derivedSources = Sources(paths: [], root: tempsPath.appending("DerivedSources"))
132135

133136
// We did not use to apply package plugins to C-family targets in prior tools-versions, this preserves the behavior.
@@ -219,7 +222,7 @@ public final class ClangTargetBuildDescription {
219222
if self.buildParameters.triple.isDarwin() {
220223
args += ["-fobjc-arc"]
221224
}
222-
args += try buildParameters.targetTripleArgs(for: target)
225+
args += try self.buildParameters.tripleArgs(for: target)
223226

224227
args += optimizationArguments
225228
args += activeCompilationConditions

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
311311
// setting is the package-level right now. We might need to figure out a better
312312
// answer for libraries if/when we support specifying deployment target at the
313313
// target-level.
314-
args += try self.buildParameters.targetTripleArgs(for: self.product.targets[self.product.targets.startIndex])
314+
args += try self.buildParameters.tripleArgs(for: self.product.targets[self.product.targets.startIndex])
315315

316316
// Add arguments from declared build settings.
317317
args += self.buildSettingsFlags
@@ -346,7 +346,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
346346
// Library search path for the toolchain's copy of SwiftSyntax.
347347
#if BUILD_MACROS_AS_DYLIBS
348348
if product.type == .macro {
349-
args += try ["-L", buildParameters.toolchain.hostLibDir.pathString]
349+
args += try ["-L", defaultBuildParameters.toolchain.hostLibDir.pathString]
350350
}
351351
#endif
352352

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2015-2023 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 Basics.AbsolutePath
14+
import struct PackageGraph.ResolvedTarget
15+
16+
@_spi(SwiftPMInternal)
17+
import SPMBuildCore
18+
19+
extension ResolvedTarget {
20+
func tempsPath(_ buildParameters: BuildParameters) -> AbsolutePath {
21+
buildParameters.buildPath.appending(component: self.c99name + "\(self.buildTriple.suffix).build")
22+
}
23+
}

0 commit comments

Comments
 (0)