Skip to content

Commit 66ba054

Browse files
authored
Use prebuilts for macro tests. Add debug URL for swift-syntax. (#8229)
Re-enable use of prebuilts for tests that depend on macro targets. These are guaranteed to be built for host as the macros are. Add a hidden option to override the prebuilts download URL for swift-syntax for testing. Also added support for this to be a file URL. Add support for using the ssh GitHub URL for swift-syntax, allowing multiple PackageReferences for prebuilts. Also elevated the PackageIdentity to the root structures that need it. Fixed the build prebuilts script to generate into a versioned directory so we can support generating multiple versions at the same time.
1 parent 4bc74dd commit 66ba054

File tree

12 files changed

+311
-171
lines changed

12 files changed

+311
-171
lines changed

Sources/CoreCommands/Options.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,19 @@ public struct CachingOptions: ParsableArguments {
188188
}
189189

190190
/// Whether to use macro prebuilts or not
191-
@Flag(name: .customLong("experimental-prebuilts"),
192-
inversion: .prefixedEnableDisable,
193-
help: "Whether to use prebuilt swift-syntax libraries for macros")
191+
@Flag(
192+
name: .customLong("experimental-prebuilts"),
193+
inversion: .prefixedEnableDisable,
194+
help: "Whether to use prebuilt swift-syntax libraries for macros"
195+
)
194196
public var usePrebuilts: Bool = false
197+
198+
/// Hidden option to override the prebuilts download location for testing
199+
@Option(
200+
name: .customLong("experimental-prebuilts-download-url"),
201+
help: .hidden
202+
)
203+
public var prebuiltsDownloadURL: String?
195204
}
196205

197206
public struct LoggingOptions: ParsableArguments {

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ public final class SwiftCommandState {
472472
.init(url: $0, supportsAvailability: true)
473473
},
474474
manifestImportRestrictions: .none,
475-
usePrebuilts: options.caching.usePrebuilts
475+
usePrebuilts: options.caching.usePrebuilts,
476+
prebuiltsDownloadURL: options.caching.prebuiltsDownloadURL
476477
),
477478
cancellator: self.cancellator,
478479
initializationWarningHandler: { self.observabilityScope.emit(warning: $0) },

Sources/PackageGraph/ModulesGraph+Loading.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,22 @@ private func createResolvedPackages(
671671
}
672672
}
673673

674-
if moduleBuilder.module.type == .macro, let package = productRef.package, prebuilts[.plain(package)]?[productRef.name] != nil {
675-
// using a prebuilt instead.
676-
continue
674+
if let package = productRef.package, prebuilts[.plain(package)]?[productRef.name] != nil {
675+
// See if we're using a prebuilt instead
676+
if moduleBuilder.module.type == .macro {
677+
continue
678+
} else if moduleBuilder.module.type == .test {
679+
// use prebuilt if this is a test that depends a macro target
680+
// these are guaranteed built for host
681+
if moduleBuilder.module.dependencies.contains(where: { dep in
682+
guard let module = dep.module else {
683+
return false
684+
}
685+
return module.type == .macro
686+
}) {
687+
continue
688+
}
689+
}
677690
}
678691

679692
// Find the product in this package's dependency products.

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ public struct BinaryArtifact {
270270

271271
/// A structure representing a prebuilt library to be used instead of a source dependency
272272
public struct PrebuiltLibrary {
273-
/// The package reference.
274-
public let packageRef: PackageReference
273+
/// The package identity.
274+
public let identity: PackageIdentity
275275

276276
/// The name of the binary target the artifact corresponds to.
277277
public let libraryName: String
@@ -285,8 +285,8 @@ public struct PrebuiltLibrary {
285285
/// The C modules that need their includes directory added to the include path
286286
public let cModules: [String]
287287

288-
public init(packageRef: PackageReference, libraryName: String, path: AbsolutePath, products: [String], cModules: [String]) {
289-
self.packageRef = packageRef
288+
public init(identity: PackageIdentity, libraryName: String, path: AbsolutePath, products: [String], cModules: [String]) {
289+
self.identity = identity
290290
self.libraryName = libraryName
291291
self.path = path
292292
self.products = products
@@ -1242,9 +1242,9 @@ public final class PackageBuilder {
12421242
table.add(assignment, for: .SWIFT_ACTIVE_COMPILATION_CONDITIONS)
12431243
}
12441244

1245-
// Add in flags for prebuilts if the target is a macro.
1245+
// Add in flags for prebuilts if the target is a macro or a macro test.
12461246
// Currently we only support prebuilts for macros.
1247-
if target.type == .macro {
1247+
if target.type == .macro || target.isMacroTest(in: manifest) {
12481248
let prebuiltLibraries: [String: PrebuiltLibrary] = target.dependencies.reduce(into: .init()) {
12491249
guard case let .product(name: name, package: package, moduleAliases: _, condition: _) = $1,
12501250
let package = package,
@@ -1856,4 +1856,26 @@ extension TargetDescription {
18561856
fileprivate var usesUnsafeFlags: Bool {
18571857
settings.filter(\.kind.isUnsafeFlags).isEmpty == false
18581858
}
1859+
1860+
fileprivate func isMacroTest(in manifest: Manifest) -> Bool {
1861+
guard self.type == .test else { return false }
1862+
1863+
return self.dependencies.contains(where: {
1864+
let name: String
1865+
switch $0 {
1866+
case .byName(name: let n, condition: _):
1867+
name = n
1868+
case .target(name: let n, condition: _):
1869+
name = n
1870+
default:
1871+
return false
1872+
}
1873+
1874+
guard let target = manifest.targetMap[name] else {
1875+
return false
1876+
}
1877+
1878+
return target.type == .macro
1879+
})
1880+
}
18591881
}

Sources/Workspace/ManagedPrebuilt.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import PackageModel
1616
extension Workspace {
1717
/// A downloaded prebuilt managed by the workspace.
1818
public struct ManagedPrebuilt {
19-
/// The package reference.
20-
public let packageRef: PackageReference
19+
/// The package identity
20+
public let identity: PackageIdentity
2121

2222
/// The name of the binary target the artifact corresponds to.
2323
public let libraryName: String
@@ -35,7 +35,7 @@ extension Workspace {
3535

3636
extension Workspace.ManagedPrebuilt: CustomStringConvertible {
3737
public var description: String {
38-
return "<ManagedArtifact: \(self.packageRef.identity).\(self.libraryName)>"
38+
return "<ManagedArtifact: \(self.identity).\(self.libraryName)>"
3939
}
4040
}
4141

@@ -56,7 +56,7 @@ extension Workspace {
5656
}
5757

5858
init(_ artifacts: [ManagedPrebuilt]) throws {
59-
let artifactsByPackagePath = Dictionary(grouping: artifacts, by: { $0.packageRef.identity })
59+
let artifactsByPackagePath = Dictionary(grouping: artifacts, by: { $0.identity })
6060
self.artifactMap = try artifactsByPackagePath.mapValues{ artifacts in
6161
try Dictionary(artifacts.map { ($0.libraryName, $0) }, uniquingKeysWith: { _, _ in
6262
// should be unique
@@ -70,7 +70,7 @@ extension Workspace {
7070
}
7171

7272
public func add(_ artifact: ManagedPrebuilt) {
73-
self.artifactMap[artifact.packageRef.identity, default: [:]][artifact.libraryName] = artifact
73+
self.artifactMap[artifact.identity, default: [:]][artifact.libraryName] = artifact
7474
}
7575

7676
public func remove(packageIdentity: PackageIdentity, targetName: String) {

Sources/Workspace/Workspace+Configuration.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ public struct WorkspaceConfiguration {
792792
/// Whether or not to use prebuilt swift-syntax for macros
793793
public var usePrebuilts: Bool
794794

795+
/// String URL to allow override of the prebuilts download location
796+
public var prebuiltsDownloadURL: String?
797+
795798
public init(
796799
skipDependenciesUpdates: Bool,
797800
prefetchBasedOnResolvedFile: Bool,
@@ -805,7 +808,8 @@ public struct WorkspaceConfiguration {
805808
sourceControlToRegistryDependencyTransformation: SourceControlToRegistryDependencyTransformation,
806809
defaultRegistry: Registry?,
807810
manifestImportRestrictions: (startingToolsVersion: ToolsVersion, allowedImports: [String])?,
808-
usePrebuilts: Bool
811+
usePrebuilts: Bool,
812+
prebuiltsDownloadURL: String?
809813
) {
810814
self.skipDependenciesUpdates = skipDependenciesUpdates
811815
self.prefetchBasedOnResolvedFile = prefetchBasedOnResolvedFile
@@ -820,6 +824,7 @@ public struct WorkspaceConfiguration {
820824
self.defaultRegistry = defaultRegistry
821825
self.manifestImportRestrictions = manifestImportRestrictions
822826
self.usePrebuilts = usePrebuilts
827+
self.prebuiltsDownloadURL = prebuiltsDownloadURL
823828
}
824829

825830
/// Default instance of WorkspaceConfiguration
@@ -837,7 +842,8 @@ public struct WorkspaceConfiguration {
837842
sourceControlToRegistryDependencyTransformation: .disabled,
838843
defaultRegistry: .none,
839844
manifestImportRestrictions: .none,
840-
usePrebuilts: false
845+
usePrebuilts: false,
846+
prebuiltsDownloadURL: nil
841847
)
842848
}
843849

0 commit comments

Comments
 (0)