11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Basics
14
+ @_spi ( SwiftPMInternal)
14
15
@testable import PackageModel
15
16
import SPMTestSupport
16
17
import XCTest
@@ -56,19 +57,33 @@ private func generateBundleFiles(bundle: MockBundle) throws -> [(String, ByteStr
56
57
] + bundle. artifacts. map {
57
58
(
58
59
" \( bundle. path) / \( $0. id) / \( targetTriple. tripleString) /swift-sdk.json " ,
59
- ByteString ( json: try generateSwiftSDKMetadata ( jsonEncoder) )
60
+ ByteString ( json: try generateSwiftSDKMetadata ( jsonEncoder, createToolset : $0 . toolsetRootPath != nil ) )
60
61
)
62
+ } + bundle. artifacts. compactMap { artifact in
63
+ artifact. toolsetRootPath. map { path in
64
+ (
65
+ " \( bundle. path) / \( artifact. id) / \( targetTriple. tripleString) /toolset.json " ,
66
+ ByteString ( json: """
67
+ {
68
+ " schemaVersion " : " 1.0 " ,
69
+ " rootPath " : " \( path) "
70
+ }
71
+ """ )
72
+ )
73
+ }
61
74
}
62
75
}
63
76
64
- private func generateSwiftSDKMetadata( _ encoder: JSONEncoder ) throws -> SerializedJSON {
77
+ private func generateSwiftSDKMetadata( _ encoder: JSONEncoder , createToolset : Bool ) throws -> SerializedJSON {
65
78
try """
66
79
{
67
80
" schemaVersion " : " 4.0 " ,
68
81
" targetTriples " : \(
69
82
String (
70
83
bytes: encoder. encode ( [
71
- targetTriple. tripleString: SwiftSDKMetadataV4 . TripleProperties ( sdkRootPath: " sdk " )
84
+ targetTriple. tripleString: SwiftSDKMetadataV4 . TripleProperties ( sdkRootPath: " sdk " , toolsetPaths: createToolset ? [
85
+ " toolset.json "
86
+ ] : nil )
72
87
] ) ,
73
88
encoding: . utf8
74
89
) !
@@ -86,6 +101,7 @@ private struct MockBundle {
86
101
private struct MockArtifact {
87
102
let id : String
88
103
let supportedTriples : [ Triple ]
104
+ var toolsetRootPath : AbsolutePath ?
89
105
}
90
106
91
107
private func generateTestFileSystem( bundleArtifacts: [ MockArtifact ] ) throws -> ( some FileSystem , [ MockBundle ] , AbsolutePath ) {
@@ -341,4 +357,88 @@ final class SwiftSDKBundleTests: XCTestCase {
341
357
) ,
342
358
] )
343
359
}
360
+
361
+ func testTargetSDKDeriviation( ) async throws {
362
+ let toolsetRootPath = AbsolutePath ( " /path/to/toolpath " )
363
+ let ( fileSystem, bundles, swiftSDKsDirectory) = try generateTestFileSystem (
364
+ bundleArtifacts: [
365
+ . init( id: " \( testArtifactID) 1 " , supportedTriples: [ arm64Triple] ) ,
366
+ . init( id: " \( testArtifactID) 2 " , supportedTriples: [ arm64Triple] , toolsetRootPath: toolsetRootPath) ,
367
+ ]
368
+ )
369
+ let system = ObservabilitySystem . makeForTesting ( )
370
+ let hostSwiftSDK = try SwiftSDK . hostSwiftSDK ( )
371
+ let hostTriple = try ! Triple ( " arm64-apple-macosx14.0 " )
372
+ let archiver = MockArchiver ( )
373
+ let store = SwiftSDKBundleStore (
374
+ swiftSDKsDirectory: swiftSDKsDirectory,
375
+ fileSystem: fileSystem,
376
+ observabilityScope: system. topScope,
377
+ outputHandler: { _ in }
378
+ )
379
+ for bundle in bundles {
380
+ try await store. install ( bundlePathOrURL: bundle. path, archiver)
381
+ }
382
+
383
+ do {
384
+ let targetSwiftSDK = try SwiftSDK . deriveTargetSwiftSDK (
385
+ hostSwiftSDK: hostSwiftSDK,
386
+ hostTriple: hostTriple,
387
+ store: store,
388
+ observabilityScope: system. topScope,
389
+ fileSystem: fileSystem
390
+ )
391
+ // By default, the target SDK is the same as the host SDK.
392
+ XCTAssertEqual ( targetSwiftSDK, hostSwiftSDK)
393
+ }
394
+
395
+ do {
396
+ let targetSwiftSDK = try SwiftSDK . deriveTargetSwiftSDK (
397
+ hostSwiftSDK: hostSwiftSDK,
398
+ hostTriple: hostTriple,
399
+ swiftSDKSelector: " \( testArtifactID) 1 " ,
400
+ store: store,
401
+ observabilityScope: system. topScope,
402
+ fileSystem: fileSystem
403
+ )
404
+ // With a target SDK selector, SDK should be chosen from the store.
405
+ XCTAssertEqual ( targetSwiftSDK. targetTriple, targetTriple)
406
+ // No toolset in the SDK, so it should be the same as the host SDK.
407
+ XCTAssertEqual ( targetSwiftSDK. toolset. rootPaths, hostSwiftSDK. toolset. rootPaths)
408
+ }
409
+
410
+ do {
411
+ let targetSwiftSDK = try SwiftSDK . deriveTargetSwiftSDK (
412
+ hostSwiftSDK: hostSwiftSDK,
413
+ hostTriple: hostTriple,
414
+ swiftSDKSelector: " \( testArtifactID) 2 " ,
415
+ store: store,
416
+ observabilityScope: system. topScope,
417
+ fileSystem: fileSystem
418
+ )
419
+ // With toolset in the target SDK, it should contain the host toolset roots at the end.
420
+ XCTAssertEqual ( targetSwiftSDK. toolset. rootPaths, [ toolsetRootPath] + hostSwiftSDK. toolset. rootPaths)
421
+ }
422
+
423
+ do {
424
+ // Check explicit overriding options.
425
+ let customCompileSDK = AbsolutePath ( " /path/to/sdk " )
426
+ let archs = [ " x86_64-apple-macosx10.15 " ]
427
+ let customCompileToolchain = AbsolutePath ( " /path/to/toolchain " )
428
+ try fileSystem. createDirectory ( customCompileToolchain, recursive: true )
429
+
430
+ let targetSwiftSDK = try SwiftSDK . deriveTargetSwiftSDK (
431
+ hostSwiftSDK: hostSwiftSDK,
432
+ hostTriple: hostTriple,
433
+ customCompileToolchain: customCompileToolchain,
434
+ customCompileSDK: customCompileSDK,
435
+ architectures: archs,
436
+ store: store,
437
+ observabilityScope: system. topScope,
438
+ fileSystem: fileSystem
439
+ )
440
+ XCTAssertEqual ( targetSwiftSDK. architectures, archs)
441
+ XCTAssertEqual ( targetSwiftSDK. pathsConfiguration. sdkRootPath, customCompileSDK)
442
+ }
443
+ }
344
444
}
0 commit comments