Skip to content

Commit 2476586

Browse files
authored
Merge pull request #10052 from owenv/owenv/productsdir
[6.4] Query the underlying build system when determining the build products dir
2 parents 290b19e + 316f1dd commit 2476586

35 files changed

Lines changed: 381 additions & 270 deletions

Sources/Build/BuildDescription/ClangModuleBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public final class ClangModuleBuildDescription {
335335

336336
// Only add the build path to the framework search path if there are binary frameworks to link against.
337337
if !libraryBinaryPaths.isEmpty {
338-
args += ["-F", buildParameters.buildPath.pathString]
338+
args += ["-F", BuildOperation.buildProductsPath(for: buildParameters).pathString]
339339
}
340340

341341
args += ["-I", clangTarget.includeDir.pathString]

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
6767
/// Paths to tools shipped in binary dependencies
6868
var availableTools: [String: AbsolutePath] = [:]
6969

70+
/// Path to the build products directory.
71+
public var productsPath: AbsolutePath {
72+
BuildOperation.buildProductsPath(for: self.buildParameters)
73+
}
74+
7075
/// Path to the temporary directory for this product.
7176
var tempsPath: AbsolutePath {
7277
let suffix = buildParameters.suffix
73-
return self.buildParameters.buildPath.appending(component: "\(self.product.name)\(suffix).product")
78+
return self.productsPath.appending(component: "\(self.product.name)\(suffix).product")
7479
}
7580

7681
/// Path to the link filelist file.
@@ -161,10 +166,10 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
161166

162167
// Only add the build path to the framework search path if there are binary frameworks to link against.
163168
if !self.libraryBinaryPaths.isEmpty {
164-
args += ["-F", self.buildParameters.buildPath.pathString]
169+
args += ["-F", BuildOperation.buildProductsPath(for: self.buildParameters).pathString]
165170
}
166171

167-
args += ["-L", self.buildParameters.buildPath.pathString]
172+
args += ["-L", BuildOperation.buildProductsPath(for: self.buildParameters).pathString]
168173
args += try ["-o", binaryPath.pathString]
169174
args += ["-module-name", self.product.name.spm_mangledToC99ExtendedIdentifier()]
170175
args += self.dylibs.map { "-l" + $0.product.name + $0.buildParameters.suffix }

Sources/Build/BuildDescription/ResolvedModule+BuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ import SPMBuildCore
1818
extension ResolvedModule {
1919
func tempsPath(_ buildParameters: BuildParameters) -> AbsolutePath {
2020
let suffix = buildParameters.suffix
21-
return buildParameters.buildPath.appending(component: "\(self.c99name)\(suffix).build")
21+
return BuildOperation.buildProductsPath(for: buildParameters).appending(component: "\(self.c99name)\(suffix).build")
2222
}
2323
}

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public final class SwiftModuleBuildDescription {
138138

139139
var modulesPath: AbsolutePath {
140140
let suffix = self.buildParameters.suffix
141-
return self.buildParameters.buildPath.appending(component: "Modules\(suffix)")
141+
return BuildOperation.buildProductsPath(for: self.buildParameters).appending(component: "Modules\(suffix)")
142142
}
143143

144144
/// The path to the swiftmodule file after compilation.
@@ -459,13 +459,13 @@ public final class SwiftModuleBuildDescription {
459459
try self.requiredMacros.forEach { macro in
460460
args += [
461461
"-Xfrontend", "-load-plugin-library",
462-
"-Xfrontend", macroBuildParameters.macroBinaryPath(macro).pathString
462+
"-Xfrontend", BuildOperation.macroBinaryPath(for: macro, parameters: macroBuildParameters).pathString
463463
]
464464
}
465465
#else
466466
let macroModules = try self.requiredMacros
467467
try macroModules.forEach { macro in
468-
let executablePath = try macroBuildParameters.macroBinaryPath(macro).pathString
468+
let executablePath = try BuildOperation.macroBinaryPath(for: macro, parameters: macroBuildParameters).pathString
469469
args += ["-Xfrontend", "-load-plugin-executable", "-Xfrontend", "\(executablePath)#\(macro.c99name)"]
470470
}
471471
#endif
@@ -563,7 +563,7 @@ public final class SwiftModuleBuildDescription {
563563

564564
// Only add the build path to the framework search path if there are binary frameworks to link against.
565565
if !self.libraryBinaryPaths.isEmpty {
566-
args += ["-F", self.buildParameters.buildPath.pathString]
566+
args += ["-F", BuildOperation.buildProductsPath(for: self.buildParameters).pathString]
567567
}
568568

569569
// Emit the ObjC compatibility header if enabled.

Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ extension LLBuildManifestBuilder {
381381

382382
// Otherwise, come up with a path for the new file and generate a command to populate it.
383383
let swiftCompilerPathHash = String(swiftCompilerPath.pathString.hash, radix: 16, uppercase: true)
384-
let swiftVersionFilePath = buildParameters.buildPath.appending(component: "swift-version-\(swiftCompilerPathHash).txt")
384+
let swiftVersionFilePath = BuildOperation.buildProductsPath(for: buildParameters).appending(component: "swift-version-\(swiftCompilerPathHash).txt")
385385
self.manifest.addSwiftGetVersionCommand(swiftCompilerPath: swiftCompilerPath, swiftVersionFilePath: swiftVersionFilePath)
386386
swiftGetVersionFiles[swiftCompilerPath] = swiftVersionFilePath
387387
return swiftVersionFilePath

Sources/Build/BuildManifest/LLBuildManifestBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ extension LLBuildManifestBuilder {
342342

343343
extension BuildParameters {
344344
func destinationPath(forBinaryAt path: AbsolutePath) -> AbsolutePath {
345-
self.buildPath.appending(component: path.basename)
345+
BuildOperation.buildProductsPath(for: self).appending(component: path.basename)
346346
}
347347

348348
var buildConfig: String { self.configuration.dirname }

Sources/Build/BuildOperation.swift

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ package struct LLBuildSystemConfiguration {
7474
self.traitConfiguration = traitConfiguration
7575
self.manifestPath = manifestPath ?? destinationBuildParameters.llbuildManifest
7676
self.databasePath = databasePath ?? scratchDirectory.appending("build.db")
77-
self.buildDescriptionPath = buildDescriptionPath ?? destinationBuildParameters.buildDescriptionPath
77+
self.buildDescriptionPath = buildDescriptionPath ?? BuildOperation.buildDescriptionPath(for: destinationBuildParameters)
7878
self.fileSystem = fileSystem
7979
self.logLevel = logLevel
8080
self.outputStream = outputStream
@@ -109,13 +109,6 @@ package struct LLBuildSystemConfiguration {
109109
}
110110
}
111111

112-
func buildPath(for description: BuildParameters.Destination) -> AbsolutePath {
113-
switch description {
114-
case .host: self.toolsBuildParameters.buildPath
115-
case .target: self.destinationBuildParameters.buildPath
116-
}
117-
}
118-
119112
func dataPath(for description: BuildParameters.Destination) -> AbsolutePath {
120113
switch description {
121114
case .host: self.toolsBuildParameters.dataPath
@@ -125,8 +118,8 @@ package struct LLBuildSystemConfiguration {
125118

126119
func buildDescriptionPath(for description: BuildParameters.Destination) -> AbsolutePath {
127120
switch description {
128-
case .host: self.toolsBuildParameters.buildDescriptionPath
129-
case .target: self.destinationBuildParameters.buildDescriptionPath
121+
case .host: BuildOperation.buildDescriptionPath(for: self.toolsBuildParameters)
122+
case .target: BuildOperation.buildDescriptionPath(for: self.destinationBuildParameters)
130123
}
131124
}
132125

@@ -200,6 +193,44 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
200193

201194
public var hasIntegratedAPIDigesterSupport: Bool { false }
202195

196+
public func buildProductsPath(for parameters: BuildParameters) async throws -> AbsolutePath {
197+
Self.buildProductsPath(for: parameters)
198+
}
199+
200+
public static func buildProductsPath(for parameters: BuildParameters) -> AbsolutePath {
201+
parameters.dataPath.appending(component: parameters.configuration.dirname)
202+
}
203+
204+
/// The path to the index store directory for the given build parameters under the native build system.
205+
public static func indexStore(for parameters: BuildParameters) -> AbsolutePath {
206+
buildProductsPath(for: parameters).appending(components: "index", "store")
207+
}
208+
209+
/// The path to the build description for the given build parameters under the native build system.
210+
public static func buildDescriptionPath(for parameters: BuildParameters) -> AbsolutePath {
211+
buildProductsPath(for: parameters).appending(components: "description.json")
212+
}
213+
214+
/// The path to the test output file for the given build parameters under the native build system.
215+
public static func testOutputPath(for parameters: BuildParameters) -> AbsolutePath {
216+
buildProductsPath(for: parameters).appending(component: "testOutput.txt")
217+
}
218+
219+
/// Returns the path to the binary of a product for the given build parameters under the native build system.
220+
public static func binaryPath(for product: ResolvedProduct, parameters: BuildParameters) throws -> AbsolutePath {
221+
try buildProductsPath(for: parameters).appending(parameters.binaryRelativePath(for: product))
222+
}
223+
224+
/// Returns the path to the built binary for a macro module under the native build system.
225+
public static func macroBinaryPath(for module: ResolvedModule, parameters: BuildParameters) throws -> AbsolutePath {
226+
assert(module.type == .macro)
227+
#if BUILD_MACROS_AS_DYLIBS
228+
return try buildProductsPath(for: parameters).appending(parameters.dynamicLibraryPath(for: module.name))
229+
#else
230+
return try buildProductsPath(for: parameters).appending(parameters.executablePath(for: module.name))
231+
#endif
232+
}
233+
203234
public convenience init(
204235
productsBuildParameters: BuildParameters,
205236
toolsBuildParameters: BuildParameters,
@@ -524,7 +555,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
524555
do {
525556
try self.fileSystem.createSymbolicLink(
526557
oldBuildPath,
527-
pointingAt: self.config.buildPath(for: .target),
558+
pointingAt: Self.buildProductsPath(for: self.config.destinationBuildParameters),
528559
relative: true
529560
)
530561
} catch {
@@ -984,7 +1015,7 @@ extension BuildOperation {
9841015
// `buildPackageStructure` to recognize the split.
9851016
config.databasePath = config.scratchDirectory.appending("plugin-tools.db")
9861017

987-
config.buildDescriptionPath = config.buildPath(for: .host).appending(
1018+
config.buildDescriptionPath = Self.buildProductsPath(for: self.config.toolsBuildParameters).appending(
9881019
component: "plugin-tools-description.json"
9891020
)
9901021

@@ -1034,7 +1065,7 @@ extension BuildOperation {
10341065
if let result = try await buildToolBuilder(name, path) {
10351066
return result
10361067
} else {
1037-
return config.buildPath(for: .host).appending(path)
1068+
return Self.buildProductsPath(for: config.toolsBuildParameters).appending(path)
10381069
}
10391070
}
10401071

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension BuildPlan {
7777
/// Generates test discovery modules, which contain derived sources listing the discovered tests.
7878
func generateDiscoveryTargets() throws -> (target: SwiftModule, resolved: ResolvedModule, buildDescription: SwiftModuleBuildDescription) {
7979
let discoveryTargetName = "\(package.manifest.displayName)PackageDiscoveredTests"
80-
let discoveryDerivedDir = destinationBuildParameters.buildPath.appending(components: "\(discoveryTargetName).derived")
80+
let discoveryDerivedDir = BuildOperation.buildProductsPath(for: destinationBuildParameters).appending(components: "\(discoveryTargetName).derived")
8181
let discoveryMainFile = discoveryDerivedDir.appending(component: TestDiscoveryTool.mainFileName)
8282

8383
var discoveryPaths: [AbsolutePath] = []
@@ -136,7 +136,7 @@ extension BuildPlan {
136136
swiftTargetDependencies: [Module.Dependency],
137137
resolvedTargetDependencies: [ResolvedModule.Dependency]
138138
) throws -> SwiftModuleBuildDescription {
139-
let entryPointDerivedDir = destinationBuildParameters.buildPath.appending(components: "\(testProduct.name).derived")
139+
let entryPointDerivedDir = BuildOperation.buildProductsPath(for: destinationBuildParameters).appending(components: "\(testProduct.name).derived")
140140
let entryPointMainFileName = TestEntryPointTool.mainFileName
141141
let entryPointMainFile = entryPointDerivedDir.appending(component: entryPointMainFileName)
142142
let entryPointSources = Sources(paths: [entryPointMainFile], root: entryPointDerivedDir)

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension BuildParameters {
4545
if let path = Environment.current["SWIFTPM_TESTS_MODULECACHE"] {
4646
return try AbsolutePath(validating: path)
4747
}
48-
return buildPath.appending("ModuleCache")
48+
return BuildOperation.buildProductsPath(for: self).appending("ModuleCache")
4949
}
5050
}
5151

@@ -69,7 +69,7 @@ extension BuildParameters {
6969
}
7070

7171
if addIndexStoreArguments {
72-
return ["-index-store-path", indexStore.pathString]
72+
return ["-index-store-path", BuildOperation.indexStore(for: self).pathString]
7373
}
7474
return []
7575
}
@@ -588,7 +588,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
588588
/// Creates arguments required to launch the Swift REPL that will allow
589589
/// importing the modules in the package graph.
590590
public func createREPLArguments() throws -> CLIArguments {
591-
let buildPath = self.toolsBuildParameters.buildPath.pathString
591+
let buildPath = BuildOperation.buildProductsPath(for: self.toolsBuildParameters).pathString
592592
var arguments = ["repl", "-I" + buildPath, "-L" + buildPath]
593593

594594
// Link the special REPL product that contains all of the library targets.
@@ -1336,7 +1336,7 @@ extension Basics.Diagnostic {
13361336
extension BuildParameters {
13371337
/// Returns a named bundle's path inside the build directory.
13381338
func bundlePath(named name: String) -> Basics.AbsolutePath {
1339-
self.buildPath.appending(component: name + self.triple.nsbundleExtension)
1339+
BuildOperation.buildProductsPath(for: self).appending(component: name + self.triple.nsbundleExtension)
13401340
}
13411341
}
13421342

Sources/Build/LLBuildCommands.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
121121
return
122122
}
123123

124-
let index = self.context.productsBuildParameters.indexStore
124+
let index = BuildOperation.indexStore(for: self.context.productsBuildParameters)
125125
let api = try self.context.indexStoreAPI.get()
126126
let store = try IndexStore.open(store: TSCAbsolutePath(index), api: api)
127127

0 commit comments

Comments
 (0)