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
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ee34109
Keep separate build parameters for host and target
MaxDesiatov Dec 5, 2023
12a1ea0
Fix build errors
MaxDesiatov Dec 6, 2023
d020e28
Fix tests, restore SourceKit-LSP compatibility
MaxDesiatov Dec 6, 2023
1270b44
Address remaining deprecations of `buildParameters` use
MaxDesiatov Dec 6, 2023
172055c
Remove unused deprecated properties and functions
MaxDesiatov Dec 6, 2023
7592888
Deprecate old `BuildPlan` initializer with no `BuildParameters` disti…
MaxDesiatov Dec 6, 2023
0fee29a
Clean up formattin
MaxDesiatov Dec 6, 2023
fc8e92c
Fix Linux build issues in tests
MaxDesiatov Dec 6, 2023
38ffcae
Support macros when cross-compiling
MaxDesiatov Nov 23, 2023
57e9c9d
Fix build issues after rebase
MaxDesiatov Dec 6, 2023
dc18982
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Dec 15, 2023
a148910
Clean up the diff
MaxDesiatov Dec 15, 2023
e542e7a
Use `topologicalSort` with `Identifiable` on `ResolvedTarget`
MaxDesiatov Dec 20, 2023
8e167f1
Reduce number of iterations in `testRecursiveDependencies`
MaxDesiatov Dec 21, 2023
b9126a3
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Dec 21, 2023
871e653
Merge branch 'maxd/identifiable-topological-sort' of github.com:apple…
MaxDesiatov Dec 21, 2023
cde00ec
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Jan 2, 2024
4d26807
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Jan 25, 2024
ef8e36c
Update `buildTriple` in `ResolvedTarget` and `ResolvedProduct`
MaxDesiatov Jan 30, 2024
779f7e8
Remove debug logging
MaxDesiatov Jan 30, 2024
2e144e1
Refine `ResolvedTarget/description` for logging
MaxDesiatov Jan 30, 2024
83aad66
Make sure host tools dependencies are in the build plan
MaxDesiatov Jan 30, 2024
63eaf8d
Fix nodes naming collisions in the build plan
MaxDesiatov Jan 31, 2024
eabe05b
Fix remaining name collisions in llbuild manifests
MaxDesiatov Feb 6, 2024
f59aeb1
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Feb 6, 2024
a8b35ef
Fix incorrect `-tool` suffix module paths, plugins build triple
MaxDesiatov Feb 13, 2024
2cd8150
Fix package plugins built without `-tool` suffix
MaxDesiatov Feb 15, 2024
73a6a99
Fix `PluginsBuildPlanTests` failure
MaxDesiatov Feb 15, 2024
3ade20f
Assign correct triple to macro products, fix macros load path
MaxDesiatov Feb 15, 2024
d083be6
Fix stray debug printer
MaxDesiatov Feb 15, 2024
379bf0f
Restore `BuildPlan(productsBuildParameters:...)` compat
MaxDesiatov Feb 16, 2024
99b7bd6
Fix doc comment
MaxDesiatov Feb 16, 2024
b83db64
Address PR feedback
MaxDesiatov Feb 16, 2024
76dd8c4
Update `CHANGELOG.md`
MaxDesiatov Feb 16, 2024
788c7af
Fix tests for `destinationBuildParameters` property renaming
MaxDesiatov Feb 16, 2024
e858a4c
Fix `destinationBuildParameters` renaming
MaxDesiatov Feb 16, 2024
9b055aa
Merge branch 'main' of github.com:apple/swift-package-manager into ma…
MaxDesiatov Feb 18, 2024
889acfd
Fix build directory path in `BuildOperationTests`
MaxDesiatov Feb 18, 2024
cfc1b65
Update CHANGELOG.md
MaxDesiatov Feb 18, 2024
10d1aea
Update CHANGELOG.md
MaxDesiatov Feb 18, 2024
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
11 changes: 10 additions & 1 deletion Sources/Basics/Collections/IdentifiableSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
}

public subscript(id: Element.ID) -> Element? {
self.storage[id]
get {
self.storage[id]
}
set {
self.storage[id] = newValue
}
}

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

public mutating func insert(_ element: Element) {
self.storage[element.id] = element
}

public func union(_ otherSequence: some Sequence<Element>) -> Self {
var result = self
for element in otherSequence {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import struct SPMBuildCore.BuildParameters
import struct SPMBuildCore.BuildToolPluginInvocationResult
import struct SPMBuildCore.PrebuildCommandResult

@_spi(SwiftPMInternal)
import SPMBuildCore

import enum TSCBasic.ProcessEnv

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

/// Path to the bundle generated for this module (if any).
var bundlePath: AbsolutePath? {
guard !resources.isEmpty else {
guard !self.resources.isEmpty else {
return .none
}

Expand Down Expand Up @@ -127,7 +130,7 @@ public final class ClangTargetBuildDescription {
self.target = target
self.toolsVersion = toolsVersion
self.buildParameters = buildParameters
self.tempsPath = buildParameters.buildPath.appending(component: target.c99name + ".build")
self.tempsPath = target.tempsPath(buildParameters)
self.derivedSources = Sources(paths: [], root: tempsPath.appending("DerivedSources"))

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

args += optimizationArguments
args += activeCompilationConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// setting is the package-level right now. We might need to figure out a better
// answer for libraries if/when we support specifying deployment target at the
// target-level.
args += try self.buildParameters.targetTripleArgs(for: self.product.targets[self.product.targets.startIndex])
args += try self.buildParameters.tripleArgs(for: self.product.targets[self.product.targets.startIndex])

// Add arguments from declared build settings.
args += self.buildSettingsFlags
Expand Down Expand Up @@ -346,7 +346,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// Library search path for the toolchain's copy of SwiftSyntax.
#if BUILD_MACROS_AS_DYLIBS
if product.type == .macro {
args += try ["-L", buildParameters.toolchain.hostLibDir.pathString]
args += try ["-L", destinationBuildParameters.toolchain.hostLibDir.pathString]
}
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2015-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import struct Basics.AbsolutePath
import struct PackageGraph.ResolvedTarget

@_spi(SwiftPMInternal)
import SPMBuildCore

extension ResolvedTarget {
func tempsPath(_ buildParameters: BuildParameters) -> AbsolutePath {
buildParameters.buildPath.appending(component: self.c99name + "\(self.buildTriple.suffix).build")
}
}
124 changes: 66 additions & 58 deletions Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public enum TargetBuildDescription {
var buildParameters: BuildParameters {
switch self {
case .swift(let swiftTargetBuildDescription):
return swiftTargetBuildDescription.buildParameters
return swiftTargetBuildDescription.destinationBuildParameters
case .clang(let clangTargetBuildDescription):
return clangTargetBuildDescription.buildParameters
}
Expand Down
36 changes: 19 additions & 17 deletions Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension LLBuildManifestBuilder {
let moduleNode = Node.file(target.moduleOutputPath)
let cmdOutputs = objectNodes + [moduleNode]

if target.buildParameters.driverParameters.useIntegratedSwiftDriver {
if target.destinationBuildParameters.driverParameters.useIntegratedSwiftDriver {
try self.addSwiftCmdsViaIntegratedDriver(
target,
inputs: inputs,
Expand All @@ -68,7 +68,7 @@ extension LLBuildManifestBuilder {
// jobs needed to build this Swift target.
var commandLine = try target.emitCommandLine()
commandLine.append("-driver-use-frontend-path")
commandLine.append(target.buildParameters.toolchain.swiftCompilerPath.pathString)
commandLine.append(target.destinationBuildParameters.toolchain.swiftCompilerPath.pathString)
// FIXME: At some point SwiftPM should provide its own executor for
// running jobs/launching processes during planning
let resolver = try ArgsResolver(fileSystem: target.fileSystem)
Expand Down Expand Up @@ -132,7 +132,7 @@ extension LLBuildManifestBuilder {
// common intermediate dependency modules, such dependencies can lead
// to cycles in the resulting manifest.
var manifestNodeInputs: [Node] = []
if targetDescription.buildParameters.driverParameters.useExplicitModuleBuild && !isMainModule(job) {
if targetDescription.destinationBuildParameters.driverParameters.useExplicitModuleBuild && !isMainModule(job) {
manifestNodeInputs = jobInputs
} else {
manifestNodeInputs = (inputs + jobInputs).uniqued()
Expand Down Expand Up @@ -192,7 +192,9 @@ extension LLBuildManifestBuilder {
// Sort the product targets in topological order in order to collect and "bubble up"
// their respective dependency graphs to the depending targets.
let nodes: [ResolvedTarget.Dependency] = try self.plan.targetMap.keys.compactMap {
guard let target = self.plan.graph.allTargets[$0] else { throw InternalError("unknown target \($0)") }
guard let target = self.plan.graph.allTargets[$0] else {
throw InternalError("unknown target \($0)")
}
return ResolvedTarget.Dependency.target(target, conditions: [])
}
let allPackageDependencies = try topologicalSort(nodes, successors: { $0.dependencies })
Expand Down Expand Up @@ -285,7 +287,7 @@ extension LLBuildManifestBuilder {
// jobs needed to build this Swift target.
var commandLine = try targetDescription.emitCommandLine()
commandLine.append("-driver-use-frontend-path")
commandLine.append(targetDescription.buildParameters.toolchain.swiftCompilerPath.pathString)
commandLine.append(targetDescription.destinationBuildParameters.toolchain.swiftCompilerPath.pathString)
commandLine.append("-experimental-explicit-module-build")
let resolver = try ArgsResolver(fileSystem: self.fileSystem)
let executor = SPMSwiftDriverExecutor(
Expand Down Expand Up @@ -376,14 +378,14 @@ extension LLBuildManifestBuilder {
cmdOutputs: [Node]
) throws {
let isLibrary = target.target.type == .library || target.target.type == .test
let cmdName = target.target.getCommandName(config: target.buildParameters.buildConfig)
let cmdName = target.target.getCommandName(config: target.destinationBuildParameters.buildConfig)

self.manifest.addWriteSourcesFileListCommand(sources: target.sources, sourcesFileListPath: target.sourcesFileListPath)
self.manifest.addSwiftCmd(
name: cmdName,
inputs: inputs + [Node.file(target.sourcesFileListPath)],
outputs: cmdOutputs,
executable: target.buildParameters.toolchain.swiftCompilerPath,
executable: target.destinationBuildParameters.toolchain.swiftCompilerPath,
moduleName: target.target.c99name,
moduleAliases: target.target.moduleAliases,
moduleOutputPath: target.moduleOutputPath,
Expand All @@ -394,7 +396,7 @@ extension LLBuildManifestBuilder {
sources: target.sources,
fileList: target.sourcesFileListPath,
isLibrary: isLibrary,
wholeModuleOptimization: target.buildParameters.configuration == .release,
wholeModuleOptimization: target.destinationBuildParameters.configuration == .release,
outputFileMapPath: try target.writeOutputFileMap() // FIXME: Eliminate side effect.
)
}
Expand All @@ -404,7 +406,7 @@ extension LLBuildManifestBuilder {
) throws -> [Node] {
var inputs = target.sources.map(Node.file)

let swiftVersionFilePath = addSwiftGetVersionCommand(buildParameters: target.buildParameters)
let swiftVersionFilePath = addSwiftGetVersionCommand(buildParameters: target.destinationBuildParameters)
inputs.append(.file(swiftVersionFilePath))

// Add resources node as the input to the target. This isn't great because we
Expand Down Expand Up @@ -450,7 +452,7 @@ extension LLBuildManifestBuilder {
}
}

for dependency in target.target.dependencies(satisfying: target.buildParameters.buildEnvironment) {
for dependency in target.target.dependencies(satisfying: target.destinationBuildParameters.buildEnvironment) {
switch dependency {
case .target(let target, _):
try addStaticTargetInputs(target)
Expand All @@ -477,7 +479,7 @@ extension LLBuildManifestBuilder {
}

for binaryPath in target.libraryBinaryPaths {
let path = target.buildParameters.destinationPath(forBinaryAt: binaryPath)
let path = target.destinationBuildParameters.destinationPath(forBinaryAt: binaryPath)
if self.fileSystem.isDirectory(binaryPath) {
inputs.append(directory: path)
} else {
Expand All @@ -489,7 +491,7 @@ extension LLBuildManifestBuilder {

// Depend on any required macro product's output.
try target.requiredMacroProducts.forEach { macro in
try inputs.append(.virtual(macro.getLLBuildTargetName(config: target.buildParameters.buildConfig)))
try inputs.append(.virtual(macro.getLLBuildTargetName(config: target.destinationBuildParameters.buildConfig)))
}

return inputs + additionalInputs
Expand All @@ -498,7 +500,7 @@ extension LLBuildManifestBuilder {
/// Adds a top-level phony command that builds the entire target.
private func addTargetCmd(_ target: SwiftTargetBuildDescription, cmdOutputs: [Node]) {
// Create a phony node to represent the entire target.
let targetName = target.target.getLLBuildTargetName(config: target.buildParameters.buildConfig)
let targetName = target.target.getLLBuildTargetName(config: target.destinationBuildParameters.buildConfig)
let targetOutput: Node = .virtual(targetName)

self.manifest.addNode(targetOutput, toTarget: targetName)
Expand All @@ -507,7 +509,7 @@ extension LLBuildManifestBuilder {
inputs: cmdOutputs,
outputs: [targetOutput]
)
if self.plan.graph.isInRootPackages(target.target, satisfying: target.buildParameters.buildEnvironment) {
if self.plan.graph.isInRootPackages(target.target, satisfying: target.destinationBuildParameters.buildEnvironment) {
if !target.isTestTarget {
self.addNode(targetOutput, toTarget: .main)
}
Expand All @@ -517,13 +519,13 @@ extension LLBuildManifestBuilder {

private func addModuleWrapCmd(_ target: SwiftTargetBuildDescription) throws {
// Add commands to perform the module wrapping Swift modules when debugging strategy is `modulewrap`.
guard target.buildParameters.debuggingStrategy == .modulewrap else { return }
guard target.destinationBuildParameters.debuggingStrategy == .modulewrap else { return }
var moduleWrapArgs = [
target.buildParameters.toolchain.swiftCompilerPath.pathString,
target.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-modulewrap", target.moduleOutputPath.pathString,
"-o", target.wrappedModuleOutputPath.pathString,
]
moduleWrapArgs += try target.buildParameters.targetTripleArgs(for: target.target)
moduleWrapArgs += try target.destinationBuildParameters.tripleArgs(for: target.target)
self.manifest.addShellCmd(
name: target.wrappedModuleOutputPath.pathString,
description: "Wrapping AST for \(target.target.name) for debugging",
Expand Down
16 changes: 9 additions & 7 deletions Sources/Build/BuildManifest/LLBuildManifestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Basics
import LLBuildManifest
import PackageGraph
import PackageModel

@_spi(SwiftPMInternal)
import SPMBuildCore

#if USE_IMPL_ONLY_IMPORTS
Expand Down Expand Up @@ -322,26 +324,26 @@ extension ResolvedTarget {
}

public func getLLBuildTargetName(config: String) -> String {
"\(name)-\(config).module"
"\(self.name)-\(config)\(self.buildTriple.suffix).module"
}

public func getLLBuildResourcesCmdName(config: String) -> String {
"\(name)-\(config).module-resources"
"\(self.name)-\(config).module-resources"
}
}

extension ResolvedProduct {
public func getLLBuildTargetName(config: String) throws -> String {
let potentialExecutableTargetName = "\(name)-\(config).exe"
let potentialLibraryTargetName = "\(name)-\(config).dylib"
let potentialExecutableTargetName = "\(name)-\(config)\(self.buildTriple.suffix).exe"
let potentialLibraryTargetName = "\(name)-\(config)\(self.buildTriple.suffix).dylib"

switch type {
case .library(.dynamic):
return potentialLibraryTargetName
case .test:
return "\(name)-\(config).test"
return "\(name)-\(config)\(self.buildTriple.suffix).test"
case .library(.static):
return "\(name)-\(config).a"
return "\(name)-\(config)\(self.buildTriple.suffix).a"
case .library(.automatic):
throw InternalError("automatic library not supported")
case .executable, .snippet:
Expand All @@ -358,7 +360,7 @@ extension ResolvedProduct {
}

public func getCommandName(config: String) throws -> String {
try "C." + self.getLLBuildTargetName(config: config)
try "C.\(self.getLLBuildTargetName(config: config))\(self.buildTriple.suffix)"
}
}

Expand Down
15 changes: 10 additions & 5 deletions Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

import Basics
import LLBuildManifest

@_spi(SwiftPMInternal)
import PackageGraph

import PackageLoading
import PackageModel
import SPMBuildCore
Expand Down Expand Up @@ -455,25 +458,28 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
// Invoke any build tool plugins in the graph to generate prebuild commands and build commands.
if let pluginConfiguration, !self.productsBuildParameters.shouldSkipBuilding {
// Hacky workaround for rdar://120560817, but it replicates precisely enough the original behavior before
// products/tools build parameters were split. Ideally we want to have specify the correct path at the time
// products/tools build parameters were split. Ideally we want to specify the correct path at the time
// when `toolsBuildParameters` is initialized, but we have too many places in the codebase where that's
// done, which makes it hard to realign them all at once.
var pluginsBuildParameters = self.toolsBuildParameters
pluginsBuildParameters.dataPath = pluginsBuildParameters.dataPath.parentDirectory.appending(components: ["plugins", "tools"])
var buildToolsGraph = graph
try buildToolsGraph.updateBuildTripleRecursively(.tools)

let buildOperationForPluginDependencies = BuildOperation(
// FIXME: this doesn't maintain the products/tools split cleanly
productsBuildParameters: pluginsBuildParameters,
toolsBuildParameters: pluginsBuildParameters,
cacheBuildManifest: false,
packageGraphLoader: { return graph },
packageGraphLoader: { buildToolsGraph },
additionalFileRules: self.additionalFileRules,
pkgConfigDirectories: self.pkgConfigDirectories,
outputStream: self.outputStream,
logLevel: self.logLevel,
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
buildToolPluginInvocationResults = try graph.invokeBuildToolPlugins(
buildToolPluginInvocationResults = try buildToolsGraph.invokeBuildToolPlugins(
outputDir: pluginConfiguration.workDirectory.appending("outputs"),
buildParameters: pluginsBuildParameters,
additionalFileRules: self.additionalFileRules,
Expand All @@ -491,7 +497,6 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
}
}


// Surface any diagnostics from build tool plugins.
var succeeded = true
for (_, (target, results)) in buildToolPluginInvocationResults {
Expand Down Expand Up @@ -571,7 +576,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS

// Create the build plan based, on the graph and any information from plugins.
let plan = try BuildPlan(
productsBuildParameters: self.productsBuildParameters,
destinationBuildParameters: self.productsBuildParameters,
toolsBuildParameters: self.toolsBuildParameters,
graph: graph,
additionalFileRules: additionalFileRules,
Expand Down
8 changes: 5 additions & 3 deletions Sources/Build/BuildPlan/BuildPlan+Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension BuildPlan {
switch target.underlying {
case is SwiftTarget:
// Swift targets are guaranteed to have a corresponding Swift description.
guard case .swift(let description) = targetMap[target.id] else {
guard case .swift(let description) = self.targetMap[target.id] else {
throw InternalError("unknown target \(target)")
}

Expand Down Expand Up @@ -220,9 +220,11 @@ extension BuildPlan {
if product.targets.contains(id: target.id) {
staticTargets.append(target)
}
// Library targets should always be included.
// Library targets should always be included for the same build triple.
case .library:
staticTargets.append(target)
if target.buildTriple == product.buildTriple {
staticTargets.append(target)
}
// Add system target to system targets array.
case .systemModule:
systemModules.append(target)
Expand Down
Loading