Skip to content

Commit 07898ee

Browse files
committed
[Build] Add destination to the build parameters
`BuildParameters` could either be used for `host` to `target` builds, add an ability to indicate that and switch `suffix` to use that information instead of requiring triple to be passed in. This helps avoid mismatches and makes it harder to mix us parameter's intended use.
1 parent 346beea commit 07898ee

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

Sources/Build/BuildDescription/ResolvedModule+BuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SPMBuildCore
1717

1818
extension ResolvedModule {
1919
func tempsPath(_ buildParameters: BuildParameters) -> AbsolutePath {
20-
let suffix = buildParameters.suffix(triple: self.buildTriple)
20+
let suffix = buildParameters.suffix
2121
return buildParameters.buildPath.appending(component: "\(self.c99name)\(suffix).build")
2222
}
2323
}

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class SwiftTargetBuildDescription {
6666
/// Path to the bundle generated for this module (if any).
6767
var bundlePath: AbsolutePath? {
6868
if let bundleName = target.underlying.potentialBundleName, needsResourceBundle {
69-
let suffix = self.defaultBuildParameters.suffix(triple: self.target.buildTriple)
69+
let suffix = self.defaultBuildParameters.suffix
7070
return self.defaultBuildParameters.bundlePath(named: bundleName + suffix)
7171
} else {
7272
return nil
@@ -113,7 +113,7 @@ public final class SwiftTargetBuildDescription {
113113
}
114114

115115
var modulesPath: AbsolutePath {
116-
let suffix = self.defaultBuildParameters.suffix(triple: self.target.buildTriple)
116+
let suffix = self.defaultBuildParameters.suffix
117117
return self.defaultBuildParameters.buildPath.appending(component: "Modules\(suffix)")
118118
}
119119

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,10 @@ public final class SwiftCommandState {
725725
when building on macOS.
726726
"""
727727

728-
private func _buildParams(toolchain: UserToolchain) throws -> BuildParameters {
728+
private func _buildParams(
729+
toolchain: UserToolchain,
730+
destination: BuildParameters.Destination
731+
) throws -> BuildParameters {
729732
let triple = toolchain.targetTriple
730733

731734
let dataPath = self.scratchDirectory.appending(
@@ -737,6 +740,7 @@ public final class SwiftCommandState {
737740
}
738741

739742
return try BuildParameters(
743+
destination: destination,
740744
dataPath: dataPath,
741745
configuration: options.build.configuration,
742746
toolchain: toolchain,
@@ -796,7 +800,7 @@ public final class SwiftCommandState {
796800

797801
private lazy var _toolsBuildParameters: Result<BuildParameters, Swift.Error> = {
798802
Result(catching: {
799-
try _buildParams(toolchain: self.getHostToolchain())
803+
try _buildParams(toolchain: self.getHostToolchain(), destination: .host)
800804
})
801805
}()
802806

@@ -808,7 +812,7 @@ public final class SwiftCommandState {
808812

809813
private lazy var _productsBuildParameters: Result<BuildParameters, Swift.Error> = {
810814
Result(catching: {
811-
try _buildParams(toolchain: self.getTargetToolchain())
815+
try _buildParams(toolchain: self.getTargetToolchain(), destination: .target)
812816
})
813817
}()
814818

Sources/SPMBuildCore/BuildParameters/BuildParameters.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ public struct BuildParameters: Encodable {
2626
case auto
2727
}
2828

29+
/// The destination for which code should be compiled for.
30+
public enum Destination: Encodable {
31+
/// The destination for which build tools are compiled.
32+
case host
33+
34+
/// The destination for which end products are compiled.
35+
case target
36+
}
37+
38+
/// The destination these parameters are going to be used for.
39+
public let destination: Destination
40+
2941
/// The path to the data directory.
3042
public var dataPath: AbsolutePath
3143

@@ -118,6 +130,7 @@ public struct BuildParameters: Encodable {
118130
public var testingParameters: Testing
119131

120132
public init(
133+
destination: Destination,
121134
dataPath: AbsolutePath,
122135
configuration: BuildConfiguration,
123136
toolchain: Toolchain,
@@ -144,6 +157,7 @@ public struct BuildParameters: Encodable {
144157
omitFramePointers: nil
145158
)
146159

160+
self.destination = destination
147161
self.dataPath = dataPath
148162
self.configuration = configuration
149163
self._toolchain = _Toolchain(toolchain: toolchain)
@@ -243,18 +257,18 @@ public struct BuildParameters: Encodable {
243257

244258
/// Returns the path to the dynamic library of a product for the current build parameters.
245259
func potentialDynamicLibraryPath(for product: ResolvedProduct) throws -> RelativePath {
246-
try RelativePath(validating: "\(self.triple.dynamicLibraryPrefix)\(product.name)\(self.suffix(triple: product.buildTriple))\(self.triple.dynamicLibraryExtension)")
260+
try RelativePath(validating: "\(self.triple.dynamicLibraryPrefix)\(product.name)\(self.suffix)\(self.triple.dynamicLibraryExtension)")
247261
}
248262

249263
/// Returns the path to the binary of a product for the current build parameters, relative to the build directory.
250264
public func binaryRelativePath(for product: ResolvedProduct) throws -> RelativePath {
251-
let potentialExecutablePath = try RelativePath(validating: "\(product.name)\(self.suffix(triple: product.buildTriple))\(self.triple.executableExtension)")
265+
let potentialExecutablePath = try RelativePath(validating: "\(product.name)\(self.suffix)\(self.triple.executableExtension)")
252266

253267
switch product.type {
254268
case .executable, .snippet:
255269
return potentialExecutablePath
256270
case .library(.static):
257-
return try RelativePath(validating: "lib\(product.name)\(self.suffix(triple: product.buildTriple))\(self.triple.staticLibraryExtension)")
271+
return try RelativePath(validating: "lib\(product.name)\(self.suffix)\(self.triple.staticLibraryExtension)")
258272
case .library(.dynamic):
259273
return try potentialDynamicLibraryPath(for: product)
260274
case .library(.automatic), .plugin:
@@ -333,7 +347,7 @@ extension Triple {
333347
extension BuildParameters {
334348
/// Suffix appended to build manifest nodes to distinguish nodes created for tools from nodes created for
335349
/// end products, i.e. nodes for host vs target triples.
336-
package func suffix(triple: BuildTriple) -> String {
337-
if triple == .tools { "-tool" } else { "" }
350+
package var suffix: String {
351+
if destination == .host { "-tool" } else { "" }
338352
}
339353
}

Sources/swift-bootstrap/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct SwiftBootstrapBuildTool: ParsableCommand {
282282
)
283283

284284
let buildParameters = try BuildParameters(
285+
destination: .target,
285286
dataPath: dataPath,
286287
configuration: configuration,
287288
toolchain: self.targetToolchain,

0 commit comments

Comments
 (0)