Skip to content

Commit 4c538f9

Browse files
authored
cache DriverSupport capabilities across targets (#7057)
motivation: DriverSupport capabilities require shelling, which was called for every target, while it can be cached across them changes: make DriverSupport a static util
1 parent 9eb51f4 commit 4c538f9

File tree

11 files changed

+26
-47
lines changed

11 files changed

+26
-47
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ import PackageModel
1616
import OrderedCollections
1717
import SPMBuildCore
1818

19-
#if USE_IMPL_ONLY_IMPORTS
20-
@_implementationOnly import DriverSupport
21-
#else
22-
import DriverSupport
23-
#endif
24-
2519
import struct TSCBasic.SortedArray
2620

2721
/// The build description for a product.

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public final class SwiftTargetBuildDescription {
5555
/// These are the resource files derived from plugins.
5656
private var pluginDerivedResources: [Resource]
5757

58-
private let driverSupport = DriverSupport()
59-
6058
/// Path to the bundle generated for this module (if any).
6159
var bundlePath: AbsolutePath? {
6260
if let bundleName = target.underlyingTarget.potentialBundleName, needsResourceBundle {
@@ -255,6 +253,7 @@ public final class SwiftTargetBuildDescription {
255253
guard target.underlyingTarget is SwiftTarget else {
256254
throw InternalError("underlying target type mismatch \(target)")
257255
}
256+
258257
self.package = package
259258
self.target = target
260259
self.toolsVersion = toolsVersion
@@ -267,13 +266,14 @@ public final class SwiftTargetBuildDescription {
267266
} else {
268267
self.testTargetRole = nil
269268
}
270-
self.fileSystem = fileSystem
269+
271270
self.tempsPath = buildParameters.buildPath.appending(component: target.c99name + ".build")
272271
self.derivedSources = Sources(paths: [], root: self.tempsPath.appending("DerivedSources"))
273272
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
274273
self.prebuildCommandResults = prebuildCommandResults
275274
self.requiredMacroProducts = requiredMacroProducts
276275
self.shouldGenerateTestObservation = shouldGenerateTestObservation
276+
self.fileSystem = fileSystem
277277
self.observabilityScope = observabilityScope
278278

279279
(self.pluginDerivedSources, self.pluginDerivedResources) = SharedTargetBuildDescription.computePluginGeneratedFiles(
@@ -411,7 +411,7 @@ public final class SwiftTargetBuildDescription {
411411
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage, packageAccess: Bool) -> [String] {
412412
let flag = "-package-name"
413413
if pkg.manifest.usePackageNameFlag,
414-
driverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
414+
DriverSupport.checkToolchainDriverFlags(flags: [flag], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem) {
415415
if packageAccess {
416416
let pkgID = pkg.identity.description.spm_mangledToC99ExtendedIdentifier()
417417
return [flag, pkgID]
@@ -439,7 +439,7 @@ public final class SwiftTargetBuildDescription {
439439
#endif
440440

441441
// If we're using an OSS toolchain, add the required arguments bringing in the plugin server from the default toolchain if available.
442-
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, driverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = try self.buildParameters.toolchain.swiftPluginServerPath {
442+
if self.buildParameters.toolchain.isSwiftDevelopmentToolchain, DriverSupport.checkSupportedFrontendFlags(flags: ["-external-plugin-path"], toolchain: self.buildParameters.toolchain, fileSystem: self.fileSystem), let pluginServer = try self.buildParameters.toolchain.swiftPluginServerPath {
443443
let toolchainUsrPath = pluginServer.parentDirectory.parentDirectory
444444
let pluginPathComponents = ["lib", "swift", "host", "plugins"]
445445

Sources/Build/BuildManifest/LLBuildManifestBuilder.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ import PackageModel
1717
import SPMBuildCore
1818

1919
#if USE_IMPL_ONLY_IMPORTS
20-
@_implementationOnly import DriverSupport
2120
@_implementationOnly import SwiftDriver
2221
#else
23-
import DriverSupport
2422
import SwiftDriver
2523
#endif
2624

Sources/Build/BuildOperation.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
101101
/// Alternative path to search for pkg-config `.pc` files.
102102
private let pkgConfigDirectories: [AbsolutePath]
103103

104-
private let driverSupport = DriverSupport()
105-
106104
public init(
107105
buildParameters: BuildParameters,
108106
cacheBuildManifest: Bool,
@@ -186,7 +184,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
186184
return
187185
}
188186
// Ensure the compiler supports the import-scan operation
189-
guard driverSupport.checkSupportedFrontendFlags(flags: ["import-prescan"], toolchain: self.buildParameters.toolchain, fileSystem: localFileSystem) else {
187+
guard DriverSupport.checkSupportedFrontendFlags(flags: ["import-prescan"], toolchain: self.buildParameters.toolchain, fileSystem: localFileSystem) else {
190188
return
191189
}
192190

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
374374

375375
// Plan the derived test targets, if necessary.
376376
if buildParameters.testingParameters.testProductStyle.requiresAdditionalDerivedTestTargets {
377-
let derivedTestTargets = try Self.makeDerivedTestTargets(buildParameters, graph, self.fileSystem, self.observabilityScope)
377+
let derivedTestTargets = try Self.makeDerivedTestTargets(
378+
buildParameters,
379+
graph,
380+
self.fileSystem,
381+
self.observabilityScope
382+
)
378383
for item in derivedTestTargets {
379384
var derivedTestTargets = [item.entryPointTargetBuildDescription.target]
380385

Sources/Commands/Utilities/SymbolGraphExtract.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public struct SymbolGraphExtract {
3636
var includeSPISymbols = false
3737
var emitExtensionBlockSymbols = false
3838
var outputFormat = OutputFormat.json(pretty: false)
39-
private let driverSupport = DriverSupport()
4039

4140
/// Access control levels.
4241
public enum AccessLevel: String, RawRepresentable, CaseIterable, ExpressibleByArgument {
@@ -82,7 +81,7 @@ public struct SymbolGraphExtract {
8281
}
8382

8483
let extensionBlockSymbolsFlag = emitExtensionBlockSymbols ? "-emit-extension-block-symbols" : "-omit-extension-block-symbols"
85-
if driverSupport.checkSupportedFrontendFlags(flags: [extensionBlockSymbolsFlag.trimmingCharacters(in: ["-"])], toolchain: buildParameters.toolchain, fileSystem: fileSystem) {
84+
if DriverSupport.checkSupportedFrontendFlags(flags: [extensionBlockSymbolsFlag.trimmingCharacters(in: ["-"])], toolchain: buildParameters.toolchain, fileSystem: fileSystem) {
8685
commandLine += [extensionBlockSymbolsFlag]
8786
} else {
8887
observabilityScope.emit(warning: "dropped \(extensionBlockSymbolsFlag) flag because it is not supported by this compiler version")

Sources/CoreCommands/SwiftTool.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ public final class SwiftTool {
253253

254254
fileprivate var buildSystemProvider: BuildSystemProvider?
255255

256-
private let driverSupport = DriverSupport()
257-
258256
/// Create an instance of this tool.
259257
///
260258
/// - parameter options: The command line options to be passed to this tool.
@@ -707,7 +705,7 @@ public final class SwiftTool {
707705
omitFramePointers: options.build.omitFramePointers
708706
),
709707
driverParameters: .init(
710-
canRenameEntrypointFunctionName: driverSupport.checkSupportedFrontendFlags(
708+
canRenameEntrypointFunctionName: DriverSupport.checkSupportedFrontendFlags(
711709
flags: ["entry-point-function-name"],
712710
toolchain: toolchain,
713711
fileSystem: self.fileSystem
@@ -859,11 +857,11 @@ public final class SwiftTool {
859857

860858
var extraManifestFlags = self.options.build.manifestFlags
861859
// Disable the implicit concurrency import if the compiler in use supports it to avoid warnings if we are building against an older SDK that does not contain a Concurrency module.
862-
if driverSupport.checkSupportedFrontendFlags(flags: ["disable-implicit-concurrency-module-import"], toolchain: try self.buildParameters().toolchain, fileSystem: self.fileSystem) {
860+
if DriverSupport.checkSupportedFrontendFlags(flags: ["disable-implicit-concurrency-module-import"], toolchain: try self.buildParameters().toolchain, fileSystem: self.fileSystem) {
863861
extraManifestFlags += ["-Xfrontend", "-disable-implicit-concurrency-module-import"]
864862
}
865863
// Disable the implicit string processing import if the compiler in use supports it to avoid warnings if we are building against an older SDK that does not contain a StringProcessing module.
866-
if driverSupport.checkSupportedFrontendFlags(flags: ["disable-implicit-string-processing-module-import"], toolchain: try self.buildParameters().toolchain, fileSystem: self.fileSystem) {
864+
if DriverSupport.checkSupportedFrontendFlags(flags: ["disable-implicit-string-processing-module-import"], toolchain: try self.buildParameters().toolchain, fileSystem: self.fileSystem) {
867865
extraManifestFlags += ["-Xfrontend", "-disable-implicit-string-processing-module-import"]
868866
}
869867

Sources/DriverSupport/DriverSupportUtils.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ import class TSCBasic.Process
1717
import enum TSCBasic.ProcessEnv
1818
import struct TSCBasic.ProcessResult
1919

20-
public class DriverSupport {
21-
private var flagsMap = ThreadSafeBox<[String: Set<String>]>()
22-
public init() {}
20+
public enum DriverSupport {
21+
private static var flagsMap = ThreadSafeBox<[String: Set<String>]>()
2322

2423
// This checks _frontend_ supported flags, which are not necessarily supported in the driver.
25-
public func checkSupportedFrontendFlags(
24+
public static func checkSupportedFrontendFlags(
2625
flags: Set<String>,
2726
toolchain: PackageModel.Toolchain,
2827
fileSystem: FileSystem
@@ -55,7 +54,7 @@ public class DriverSupport {
5554
// This checks if given flags are supported in the built-in toolchain driver. Currently
5655
// there's no good way to get the supported flags from it, so run `swiftc -h` directly
5756
// to get the flags and cache the result.
58-
public func checkToolchainDriverFlags(
57+
public static func checkToolchainDriverFlags(
5958
flags: Set<String>,
6059
toolchain: PackageModel.Toolchain,
6160
fileSystem: FileSystem

Sources/SPMTestSupport/misc.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import PackageGraph
2020
import PackageLoading
2121
import PackageModel
2222
import SourceControl
23+
import struct SPMBuildCore.BuildParameters
2324
import TSCTestSupport
2425
import Workspace
2526
import func XCTest.XCTFail

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
@testable import Basics
1414
@testable import Build
15+
import DriverSupport
1516
import PackageLoading
1617
@testable import PackageGraph
1718
@testable import PackageModel
@@ -21,20 +22,13 @@ import SwiftDriver
2122
import Workspace
2223
import XCTest
2324

24-
#if USE_IMPL_ONLY_IMPORTS
25-
@_implementationOnly import DriverSupport
26-
#else
27-
import DriverSupport
28-
#endif
29-
3025
import struct TSCBasic.ByteString
3126
import class TSCBasic.InMemoryFileSystem
3227

3328
import enum TSCUtility.Diagnostics
3429

3530
final class BuildPlanTests: XCTestCase {
3631
let inputsDir = AbsolutePath(#file).parentDirectory.appending(components: "Inputs")
37-
private let driverSupport = DriverSupport()
3832

3933
/// The j argument.
4034
private var j: String {
@@ -533,7 +527,7 @@ final class BuildPlanTests: XCTestCase {
533527
}
534528

535529
func testPackageNameFlag() throws {
536-
let isFlagSupportedInDriver = try driverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: UserToolchain.default, fileSystem: localFileSystem)
530+
let isFlagSupportedInDriver = try DriverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: UserToolchain.default, fileSystem: localFileSystem)
537531
try fixture(name: "Miscellaneous/PackageNameFlag") { fixturePath in
538532
let (stdout, _) = try executeSwiftBuild(fixturePath.appending("appPkg"), extraArgs: ["-v"])
539533
XCTAssertMatch(stdout, .contains("-module-name Foo"))
@@ -555,7 +549,7 @@ final class BuildPlanTests: XCTestCase {
555549
}
556550

557551
func testTargetsWithPackageAccess() throws {
558-
let isFlagSupportedInDriver = try driverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: UserToolchain.default, fileSystem: localFileSystem)
552+
let isFlagSupportedInDriver = try DriverSupport.checkToolchainDriverFlags(flags: ["package-name"], toolchain: UserToolchain.default, fileSystem: localFileSystem)
559553
try fixture(name: "Miscellaneous/TargetPackageAccess") { fixturePath in
560554
let (stdout, _) = try executeSwiftBuild(fixturePath.appending("libPkg"), extraArgs: ["-v"])
561555
if isFlagSupportedInDriver {

Tests/CommandsTests/APIDiffTests.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,17 @@
1313
import Basics
1414
import Build
1515
import Commands
16+
import DriverSupport
1617
import Foundation
1718
import PackageModel
1819
import SourceControl
1920
import SPMTestSupport
2021
import Workspace
2122
import XCTest
2223

23-
#if USE_IMPL_ONLY_IMPORTS
24-
@_implementationOnly import DriverSupport
25-
#else
26-
import DriverSupport
27-
#endif
28-
2924
import enum TSCBasic.ProcessEnv
3025

3126
final class APIDiffTests: CommandsTestCase {
32-
private let driverSupport = DriverSupport()
33-
3427
@discardableResult
3528
private func execute(
3629
_ args: [String],
@@ -61,7 +54,7 @@ final class APIDiffTests: CommandsTestCase {
6154
// not all of which can be tested for easily. Fortunately, we can test for the
6255
// `-disable-fail-on-error` option, and any version which supports this flag
6356
// will meet the other requirements.
64-
guard driverSupport.checkSupportedFrontendFlags(flags: ["disable-fail-on-error"], toolchain: try UserToolchain.default, fileSystem: localFileSystem) else {
57+
guard DriverSupport.checkSupportedFrontendFlags(flags: ["disable-fail-on-error"], toolchain: try UserToolchain.default, fileSystem: localFileSystem) else {
6558
throw XCTSkip("swift-api-digester is too old")
6659
}
6760
}

0 commit comments

Comments
 (0)