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 macros when cross-compiling #7118

Merged
merged 40 commits into from
Feb 18, 2024
Merged

Conversation

MaxDesiatov
Copy link
Contributor

@MaxDesiatov MaxDesiatov commented Nov 23, 2023

Motivation:

Not supporting macros in cross-compilation is a major limitation, especially for libraries like https://github.com/apple/swift-mmio, but even more crucially for https://github.com/apple/swift-foundation and https://github.com/apple/swift-testing, all of those increasingly use macros for their core functionality.

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

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

Sources/PackageGraph/BuildTriple.swift Outdated Show resolved Hide resolved
Sources/PackageGraph/ResolvedTarget.swift Outdated Show resolved Hide resolved
Sources/PackageGraph/ResolvedTarget.swift Outdated Show resolved Hide resolved
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@neonichu
Copy link
Contributor

What happens if we need to build a target for both triples?

Separately, it feels like eventually we have to resolve the idea that there is a singular destination triple since that doesn't actually match the reality on Apple platforms.

@MaxDesiatov
Copy link
Contributor Author

I don't have answers to these questions. This is a draft PR that doesn't fully work yet.

@MaxDesiatov MaxDesiatov added the WIP Work in progress label Nov 29, 2023
@MaxDesiatov MaxDesiatov changed the base branch from main to maxd/packagegraph-value-types December 2, 2023 23:18
@MaxDesiatov MaxDesiatov added the modules graph Modules dependency resolution label Dec 3, 2023
@MaxDesiatov MaxDesiatov changed the base branch from maxd/packagegraph-value-types to main December 4, 2023 15:20
MaxDesiatov added a commit that referenced this pull request Dec 5, 2023
Unblocks `PackageGraph` value types refactoring in
#7160 and macros
cross-compilation fix in
#7118.

Multiple places in our codebase pass around and store `[any
PackageConditionProtocol]` arrays of existentials with `FIXME` notes
that those should be sets and not arrays.

We also can't convert types containing these arrays to value types with
auto-generated `Hashable` conformance, since neither elements of these
arrays nor arrays themselves are `Hashable`. Adding `Hashable`
requirement to `PackageConditionProtocol` doesn't fix the issue, since
existentials of this protocol still wouldn't conform to `Hashable`.

A simple alternative is to create `enum PackageCondition` with cases for
each condition type. We only have two of those types and they're always
declared in the same module. There's no use case for externally defined
types for this protocol. That allows us to convert uses of `[any
PackageConditionProtocol]` to `[PackageCondition]`. Existing protocol is
kept around until clients of SwiftPM can migrate to the new
`PackageCondition` enum.

This also allows us to remove a custom conformance to `Hashable` on
`ResolvedTarget`, unblocking a conversion of `ResolvedTarget` to a value
type and making it `Sendable` in the future.
`BuildParameters` to date explicitly mentioned both host and target triples, but assumed you can use the same toolchain for both, which is not true. Additionally, it would assume that all other build parameters can be shared between the host and target, like debug info format, build configuration, sanitizers etc. That's not true either, one would use CodeView for build tools on Windows when compiling targets with DWARF for Linux, or build macros in release mode for performance when cross-compiling code that uses debug configuration itself.
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

1 similar comment
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov MaxDesiatov merged commit a0b25d3 into main Feb 18, 2024
5 checks passed
@MaxDesiatov MaxDesiatov deleted the maxd/cross-compile-macros branch February 18, 2024 19:29
DougGregor added a commit that referenced this pull request Feb 19, 2024
MaxDesiatov added a commit that referenced this pull request Feb 20, 2024
MaxDesiatov added a commit that referenced this pull request Feb 20, 2024
MaxDesiatov added a commit that referenced this pull request Apr 17, 2024
…#7353)

Reverts #7352.

Modified to build tests for the host triple when any macros are added to test modules directly as dependencies.
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
### 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 swiftlang#6950
Resolves rdar://105991372
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…swiftlang#7352)" (swiftlang#7353)

Reverts swiftlang#7352.

Modified to build tests for the host triple when any macros are added to test modules directly as dependencies.
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
### 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 swiftlang#6950
Resolves rdar://105991372
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
furby-tm pushed a commit to wabiverse/swift-package-manager that referenced this pull request May 15, 2024
…swiftlang#7352)" (swiftlang#7353)

Reverts swiftlang#7352.

Modified to build tests for the host triple when any macros are added to test modules directly as dependencies.
MaxDesiatov added a commit that referenced this pull request Jun 6, 2024
…#7353)

Reverts #7352.

Modified to build tests for the host triple when any macros are added to test modules directly as dependencies.

(cherry picked from commit cb3b085)

# Conflicts:
#	CHANGELOG.md
#	Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
#	Sources/Build/BuildManifest/LLBuildManifestBuilder.swift
#	Sources/Build/BuildPlan/BuildPlan.swift
#	Sources/Commands/SwiftTestCommand.swift
#	Sources/Commands/Utilities/PluginDelegate.swift
#	Sources/Commands/Utilities/TestingSupport.swift
#	Sources/PackageGraph/ModulesGraph+Loading.swift
#	Sources/PackageGraph/ModulesGraph.swift
#	Sources/SPMTestSupport/MockBuildTestHelper.swift
#	Sources/SPMTestSupport/MockPackageGraphs.swift
#	Sources/SPMTestSupport/PackageGraphTester.swift
#	Sources/SPMTestSupport/ResolvedTarget+Mock.swift
#	Tests/BuildTests/ClangTargetBuildDescriptionTests.swift
#	Tests/BuildTests/CrossCompilationBuildPlanTests.swift
#	Tests/SourceKitLSPAPITests/SourceKitLSPAPITests.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cross-compiling packages which contain macros doesn't work
3 participants