-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathExplicitModuleBuildTests.swift
2232 lines (2091 loc) · 119 KB
/
ExplicitModuleBuildTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===------- ExplicitModuleBuildTests.swift - Swift Driver Tests ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(Testing) import SwiftDriver
import SwiftDriverExecution
import TSCBasic
import XCTest
import TestUtilities
private var testInputsPath: AbsolutePath {
get throws {
var root: AbsolutePath = try AbsolutePath(validating: #file)
while root.basename != "Tests" {
root = root.parentDirectory
}
return root.parentDirectory.appending(component: "TestInputs")
}
}
/// Check that an explicit module build job contains expected inputs and options
private func checkExplicitModuleBuildJob(job: Job,
moduleId: ModuleDependencyId,
dependencyGraph: InterModuleDependencyGraph)
throws {
let moduleInfo = try dependencyGraph.moduleInfo(of: moduleId)
switch moduleInfo.details {
case .swift(let swiftModuleDetails):
XCTAssertTrue(job.commandLine.contains(.flag(String("-disable-implicit-swift-modules"))))
let moduleInterfacePath =
TypedVirtualPath(file: swiftModuleDetails.moduleInterfacePath!.path,
type: .swiftInterface)
XCTAssertEqual(job.kind, .compileModuleFromInterface)
XCTAssertTrue(job.inputs.contains(moduleInterfacePath))
if let compiledCandidateList = swiftModuleDetails.compiledModuleCandidates {
for compiledCandidate in compiledCandidateList {
let candidatePath = compiledCandidate.path
let typedCandidatePath = TypedVirtualPath(file: candidatePath,
type: .swiftModule)
XCTAssertTrue(job.inputs.contains(typedCandidatePath))
XCTAssertTrue(job.commandLine.contains(.flag(VirtualPath.lookup(candidatePath).description)))
}
XCTAssertTrue(job.commandLine.filter {$0 == .flag("-candidate-module-file")}.count == compiledCandidateList.count)
}
case .clang(_):
XCTAssertEqual(job.kind, .generatePCM)
XCTAssertEqual(job.description, "Compiling Clang module \(moduleId.moduleName)")
case .swiftPrebuiltExternal(_):
XCTFail("Unexpected prebuilt external module dependency found.")
case .swiftPlaceholder(_):
XCTFail("Placeholder dependency found.")
}
// Ensure the frontend was prohibited from doing implicit module builds
XCTAssertTrue(job.commandLine.contains(.flag(String("-fno-implicit-modules"))))
try checkExplicitModuleBuildJobDependencies(job: job,
moduleInfo: moduleInfo,
dependencyGraph: dependencyGraph)
}
/// Checks that the build job for the specified module contains the required options and inputs
/// to build all of its dependencies explicitly
private func checkExplicitModuleBuildJobDependencies(job: Job,
moduleInfo : ModuleInfo,
dependencyGraph: InterModuleDependencyGraph
) throws {
let validateSwiftCommandLineDependency: (ModuleDependencyId, ModuleInfo) -> Void = { dependencyId, dependencyInfo in
let inputModulePath = dependencyInfo.modulePath.path
XCTAssertTrue(job.inputs.contains(TypedVirtualPath(file: inputModulePath, type: .swiftModule)))
XCTAssertTrue(job.commandLine.contains(
.flag(String("-swift-module-file=\(dependencyId.moduleName)=\(inputModulePath.description)"))))
}
let validateClangCommandLineDependency: (ModuleDependencyId,
ModuleInfo,
ClangModuleDetails) -> Void = { dependencyId, dependencyInfo, clangDependencyDetails in
let clangDependencyModulePathString = dependencyInfo.modulePath.path
let clangDependencyModulePath =
TypedVirtualPath(file: clangDependencyModulePathString, type: .pcm)
XCTAssertTrue(job.inputs.contains(clangDependencyModulePath))
XCTAssertTrue(job.commandLine.contains(
.flag(String("-fmodule-file=\(dependencyId.moduleName)=\(clangDependencyModulePathString)"))))
XCTAssertTrue(job.commandLine.contains(
.flag(String("-fmodule-map-file=\(clangDependencyDetails.moduleMapPath.path.description)"))))
}
for dependencyId in moduleInfo.directDependencies! {
let dependencyInfo = try dependencyGraph.moduleInfo(of: dependencyId)
switch dependencyInfo.details {
case .swift(_):
fallthrough
case .swiftPrebuiltExternal(_):
validateSwiftCommandLineDependency(dependencyId, dependencyInfo)
case .clang(let clangDependencyDetails):
validateClangCommandLineDependency(dependencyId, dependencyInfo, clangDependencyDetails)
case .swiftPlaceholder(_):
XCTFail("Placeholder dependency found.")
}
// Ensure all transitive dependencies got added as well.
for transitiveDependencyId in dependencyInfo.directDependencies! {
try checkExplicitModuleBuildJobDependencies(job: job,
moduleInfo: try dependencyGraph.moduleInfo(of: transitiveDependencyId),
dependencyGraph: dependencyGraph)
}
}
}
/// Test that for the given JSON module dependency graph, valid jobs are generated
final class ExplicitModuleBuildTests: XCTestCase {
func testModuleDependencyBuildCommandGeneration() throws {
do {
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
"-module-name", "testModuleDependencyBuildCommandGeneration",
"test.swift"])
let moduleDependencyGraph =
try JSONDecoder().decode(
InterModuleDependencyGraph.self,
from: ModuleDependenciesInputs.fastDependencyScannerOutput.data(using: .utf8)!)
driver.explicitDependencyBuildPlanner =
try ExplicitDependencyBuildPlanner(dependencyGraph: moduleDependencyGraph,
toolchain: driver.toolchain,
dependencyOracle: driver.interModuleDependencyOracle)
let modulePrebuildJobs =
try driver.explicitDependencyBuildPlanner!.generateExplicitModuleDependenciesBuildJobs()
XCTAssertEqual(modulePrebuildJobs.count, 4)
for job in modulePrebuildJobs {
XCTAssertEqual(job.outputs.count, 1)
XCTAssertFalse(driver.isExplicitMainModuleJob(job: job))
switch (job.outputs[0].file) {
case .relative(try .init(validating: "SwiftShims.pcm")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .clang("SwiftShims"),
dependencyGraph: moduleDependencyGraph)
case .relative(try .init(validating: "c_simd.pcm")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .clang("c_simd"),
dependencyGraph: moduleDependencyGraph)
case .relative(try .init(validating: "Swift.swiftmodule")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .swift("Swift"),
dependencyGraph: moduleDependencyGraph)
case .relative(try .init(validating: "_Concurrency.swiftmodule")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .swift("_Concurrency"),
dependencyGraph: moduleDependencyGraph)
case .relative(try .init(validating: "_StringProcessing.swiftmodule")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .swift("_StringProcessing"),
dependencyGraph: moduleDependencyGraph)
case .relative(try .init(validating: "SwiftOnoneSupport.swiftmodule")):
try checkExplicitModuleBuildJob(job: job,
moduleId: .swift("SwiftOnoneSupport"),
dependencyGraph: moduleDependencyGraph)
default:
XCTFail("Unexpected module dependency build job output: \(job.outputs[0].file)")
}
}
}
}
func testModuleDependencyBuildCommandGenerationWithExternalFramework() throws {
do {
let externalDetails: ExternalTargetModuleDetailsMap =
[.swiftPrebuiltExternal("A"): ExternalTargetModuleDetails(path: try AbsolutePath(validating: "/tmp/A.swiftmodule"),
isFramework: true),
.swiftPrebuiltExternal("K"): ExternalTargetModuleDetails(path: try AbsolutePath(validating: "/tmp/K.swiftmodule"),
isFramework: true),
.swiftPrebuiltExternal("simpleTestModule"): ExternalTargetModuleDetails(path: try AbsolutePath(validating: "/tmp/simpleTestModule.swiftmodule"),
isFramework: true)]
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
"-module-name", "simpleTestModule",
"test.swift"])
var moduleDependencyGraph =
try JSONDecoder().decode(
InterModuleDependencyGraph.self,
from: ModuleDependenciesInputs.simpleDependencyGraphInput.data(using: .utf8)!)
// Key part of this test, using the external info to generate dependency pre-build jobs
try moduleDependencyGraph.resolveExternalDependencies(for: externalDetails)
// Ensure the main module was not overriden by an external dependency
XCTAssertNotNil(moduleDependencyGraph.modules[.swift("simpleTestModule")])
// Ensure the "K" module's framework status got resolved via `externalDetails`
guard case .swiftPrebuiltExternal(let kPrebuiltDetails) = moduleDependencyGraph.modules[.swiftPrebuiltExternal("K")]?.details else {
XCTFail("Expected prebuilt module details for module \"K\"")
return
}
XCTAssertTrue(kPrebuiltDetails.isFramework)
let jobsInPhases = try driver.computeJobsForPhasedStandardBuild(with: moduleDependencyGraph)
let job = try XCTUnwrap(jobsInPhases.allJobs.first(where: { $0.kind == .compile }))
// Load the dependency JSON and verify this dependency was encoded correctly
let explicitDepsFlag =
SwiftDriver.Job.ArgTemplate.flag(String("-explicit-swift-module-map-file"))
XCTAssert(job.commandLine.contains(explicitDepsFlag))
let jsonDepsPathIndex = job.commandLine.firstIndex(of: explicitDepsFlag)
let jsonDepsPathArg = job.commandLine[jsonDepsPathIndex! + 1]
guard case .path(let jsonDepsPath) = jsonDepsPathArg else {
XCTFail("No JSON dependency file path found.")
return
}
guard case let .temporaryWithKnownContents(_, contents) = jsonDepsPath else {
XCTFail("Unexpected path type")
return
}
let dependencyInfoList = try JSONDecoder().decode(Array<SwiftModuleArtifactInfo>.self,
from: contents)
XCTAssertEqual(dependencyInfoList.count, 2)
let dependencyArtifacts =
dependencyInfoList.first(where:{ $0.moduleName == "A" })!
// Ensure this is a framework, as specified by the externalDetails above.
XCTAssertEqual(dependencyArtifacts.isFramework, true)
}
}
func testModuleDependencyBuildCommandUniqueDepFile() throws {
try withTemporaryDirectory { path in
let source0 = path.appending(component: "testModuleDependencyBuildCommandUniqueDepFile1.swift")
let source1 = path.appending(component: "testModuleDependencyBuildCommandUniqueDepFile2.swift")
try localFileSystem.writeFileContents(source0, bytes:
"""
import C;
"""
)
try localFileSystem.writeFileContents(source1, bytes:
"""
import G;
"""
)
let cHeadersPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "CHeaders")
let bridgingHeaderpath: AbsolutePath =
cHeadersPath.appending(component: "Bridging.h")
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
var driver = try Driver(args: ["swiftc",
"-target", "x86_64-apple-macosx11.0",
"-I", cHeadersPath.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-explicit-module-build",
"-import-objc-header", bridgingHeaderpath.nativePathString(escaped: true),
source0.nativePathString(escaped: true),
source1.nativePathString(escaped: true)] + sdkArgumentsForTesting)
let jobs = try driver.planBuild()
let compileJobs = jobs.filter({ $0.kind == .compile })
XCTAssertEqual(compileJobs.count, 2)
let compileJob0 = compileJobs[0]
let compileJob1 = compileJobs[1]
let explicitDepsFlag = SwiftDriver.Job.ArgTemplate.flag(String("-explicit-swift-module-map-file"))
XCTAssert(compileJob0.commandLine.contains(explicitDepsFlag))
XCTAssert(compileJob1.commandLine.contains(explicitDepsFlag))
let jsonDeps0PathIndex = compileJob0.commandLine.firstIndex(of: explicitDepsFlag)
let jsonDeps0PathArg = compileJob0.commandLine[jsonDeps0PathIndex! + 1]
let jsonDeps1PathIndex = compileJob1.commandLine.firstIndex(of: explicitDepsFlag)
let jsonDeps1PathArg = compileJob1.commandLine[jsonDeps1PathIndex! + 1]
XCTAssertEqual(jsonDeps0PathArg, jsonDeps1PathArg)
}
}
private func pathMatchesSwiftModule(path: VirtualPath, _ name: String) -> Bool {
return path.basenameWithoutExt.starts(with: "\(name)-") &&
path.extension! == FileType.swiftModule.rawValue
}
/// Test generation of explicit module build jobs for dependency modules when the driver
/// is invoked with -explicit-module-build
func testExplicitModuleBuildJobs() throws {
try withTemporaryDirectory { path in
let main = path.appending(component: "testExplicitModuleBuildJobs.swift")
try localFileSystem.writeFileContents(main, bytes:
"""
import C;\
import E;\
import G;
"""
)
let cHeadersPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "CHeaders")
let bridgingHeaderpath: AbsolutePath =
cHeadersPath.appending(component: "Bridging.h")
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
var driver = try Driver(args: ["swiftc",
"-target", "x86_64-apple-macosx11.0",
"-I", cHeadersPath.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-explicit-module-build",
"-import-objc-header", bridgingHeaderpath.nativePathString(escaped: true),
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
let jobs = try driver.planBuild()
// Figure out which Triples to use.
let dependencyGraph = try driver.gatherModuleDependencies()
let mainModuleInfo = try dependencyGraph.moduleInfo(of: .swift("testExplicitModuleBuildJobs"))
guard case .swift(_) = mainModuleInfo.details else {
XCTFail("Main module does not have Swift details field")
return
}
for job in jobs {
XCTAssertEqual(job.outputs.count, 1)
let outputFilePath = job.outputs[0].file
// Swift dependencies
if outputFilePath.extension != nil,
outputFilePath.extension! == FileType.swiftModule.rawValue {
if pathMatchesSwiftModule(path: outputFilePath, "A") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("A"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "E") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("E"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "G") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("G"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "Swift") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("Swift"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_Concurrency") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_Concurrency"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_StringProcessing") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_StringProcessing"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "SwiftOnoneSupport") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("SwiftOnoneSupport"),
dependencyGraph: dependencyGraph)
}
// Clang Dependencies
} else if let outputExtension = outputFilePath.extension,
outputExtension == FileType.pcm.rawValue {
let relativeOutputPathFileName = outputFilePath.basename
if relativeOutputPathFileName.starts(with: "A-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("A"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "B-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("B"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "C-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("C"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "G-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("G"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "F-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("F"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "SwiftShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("SwiftShims"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "_SwiftConcurrencyShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("_SwiftConcurrencyShims"),
dependencyGraph: dependencyGraph)
}
else {
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
} else {
switch (outputFilePath) {
case .relative(try .init(validating: "testExplicitModuleBuildJobs")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .link)
case .temporary(_):
let baseName = "testExplicitModuleBuildJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink") ||
matchTemporary(outputFilePath, basename: "Bridging-", fileExtension: "pch"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
}
}
}
}
/// Test generation of explicit module build jobs for dependency modules when the driver
/// is invoked with -explicit-module-build, -verify-emitted-module-interface and -enable-library-evolution.
func testExplicitModuleVerifyInterfaceJobs() throws {
try withTemporaryDirectory { path in
let main = path.appending(component: "testExplicitModuleVerifyInterfaceJobs.swift")
try localFileSystem.writeFileContents(main) {
$0.send("import C;import E;import G;")
}
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let cHeadersPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "CHeaders")
let swiftInterfacePath: AbsolutePath = path.appending(component: "testExplicitModuleVerifyInterfaceJobs.swiftinterface")
let privateSwiftInterfacePath: AbsolutePath = path.appending(component: "testExplicitModuleVerifyInterfaceJobs.private.swiftinterface")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
var driver = try Driver(args: ["swiftc",
"-target", "x86_64-apple-macosx11.0",
"-I", cHeadersPath.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-emit-module-interface-path", swiftInterfacePath.nativePathString(escaped: true),
"-emit-private-module-interface-path", privateSwiftInterfacePath.nativePathString(escaped: true),
"-explicit-module-build", "-verify-emitted-module-interface",
"-enable-library-evolution",
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
guard driver.supportExplicitModuleVerifyInterface() else {
throw XCTSkip("-typecheck-module-from-interface doesn't support explicit build.")
}
let jobs = try driver.planBuild()
// Figure out which Triples to use.
let dependencyGraph = try driver.gatherModuleDependencies()
let mainModuleInfo = try dependencyGraph.moduleInfo(of: .swift("testExplicitModuleVerifyInterfaceJobs"))
guard case .swift(_) = mainModuleInfo.details else {
XCTFail("Main module does not have Swift details field")
return
}
for job in jobs {
if (job.outputs.count == 0) {
// This is the verify module job as it should be the only job scheduled to have no output.
XCTAssertTrue(job.kind == .verifyModuleInterface)
// Check the explicit module flags exists.
XCTAssertTrue(job.commandLine.contains(.flag(String("-explicit-interface-module-build"))))
XCTAssertTrue(job.commandLine.contains(.flag(String("-explicit-swift-module-map-file"))))
XCTAssertTrue(job.commandLine.contains(.flag(String("-disable-implicit-swift-modules"))))
continue
}
let outputFilePath = job.outputs[0].file
// Swift dependencies
if outputFilePath.extension != nil,
outputFilePath.extension! == FileType.swiftModule.rawValue {
if pathMatchesSwiftModule(path: outputFilePath, "A") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("A"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "E") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("E"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "G") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("G"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "Swift") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("Swift"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_Concurrency") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_Concurrency"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_StringProcessing") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_StringProcessing"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "SwiftOnoneSupport") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("SwiftOnoneSupport"),
dependencyGraph: dependencyGraph)
}
// Clang Dependencies
} else if let outputExtension = outputFilePath.extension,
outputExtension == FileType.pcm.rawValue {
let relativeOutputPathFileName = outputFilePath.basename
if relativeOutputPathFileName.starts(with: "A-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("A"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "B-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("B"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "C-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("C"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "G-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("G"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "F-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("F"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "SwiftShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("SwiftShims"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "_SwiftConcurrencyShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("_SwiftConcurrencyShims"),
dependencyGraph: dependencyGraph)
}
else {
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
} else {
switch (outputFilePath) {
case .relative(try .init(validating: "testExplicitModuleVerifyInterfaceJobs")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .link)
case .temporary(_):
let baseName = "testExplicitModuleVerifyInterfaceJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
}
}
}
}
/// Test generation of explicit module build jobs for dependency modules when the driver
/// is invoked with -explicit-module-build and -pch-output-dir
func testExplicitModuleBuildPCHOutputJobs() throws {
try withTemporaryDirectory { path in
let main = path.appending(component: "testExplicitModuleBuildPCHOutputJobs.swift")
try localFileSystem.writeFileContents(main, bytes:
"""
import C;\
import E;\
import G;
"""
)
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let cHeadersPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "CHeaders")
let bridgingHeaderpath: AbsolutePath =
cHeadersPath.appending(component: "Bridging.h")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
let pchOutputDir: AbsolutePath = path
var driver = try Driver(args: ["swiftc",
"-target", "x86_64-apple-macosx11.0",
"-I", cHeadersPath.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-explicit-module-build",
"-import-objc-header", bridgingHeaderpath.nativePathString(escaped: true),
"-pch-output-dir", pchOutputDir.nativePathString(escaped: true),
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
let jobs = try driver.planBuild()
// Figure out which Triples to use.
let dependencyGraph = try driver.gatherModuleDependencies()
let mainModuleInfo = try dependencyGraph.moduleInfo(of: .swift("testExplicitModuleBuildPCHOutputJobs"))
guard case .swift(_) = mainModuleInfo.details else {
XCTFail("Main module does not have Swift details field")
return
}
for job in jobs {
XCTAssertEqual(job.outputs.count, 1)
let outputFilePath = job.outputs[0].file
// Swift dependencies
if outputFilePath.extension != nil,
outputFilePath.extension! == FileType.swiftModule.rawValue {
if pathMatchesSwiftModule(path: outputFilePath, "A") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("A"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "E") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("E"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "G") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("G"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "Swift") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("Swift"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_Concurrency") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_Concurrency"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_StringProcessing") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_StringProcessing"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "SwiftOnoneSupport") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("SwiftOnoneSupport"),
dependencyGraph: dependencyGraph)
}
// Clang Dependencies
} else if let outputExtension = outputFilePath.extension,
outputExtension == FileType.pcm.rawValue {
let relativeOutputPathFileName = outputFilePath.basename
if relativeOutputPathFileName.starts(with: "A-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("A"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "B-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("B"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "C-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("C"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "G-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("G"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "F-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("F"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "SwiftShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("SwiftShims"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "_SwiftConcurrencyShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("_SwiftConcurrencyShims"),
dependencyGraph: dependencyGraph)
}
else {
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
// Bridging header
} else if let outputExtension = outputFilePath.extension,
outputExtension == FileType.pch.rawValue {
switch (outputFilePath) {
case .absolute:
// pch output is a computed absolute path.
XCTAssertFalse(job.commandLine.contains("-pch-output-dir"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
} else {
// Check we don't use `-pch-output-dir` anymore during main module job.
XCTAssertFalse(job.commandLine.contains("-pch-output-dir"))
switch (outputFilePath) {
case .relative(try .init(validating: "testExplicitModuleBuildPCHOutputJobs")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .link)
case .temporary(_):
let baseName = "testExplicitModuleBuildPCHOutputJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
}
}
}
}
func testImmediateModeExplicitModuleBuild() throws {
try withTemporaryDirectory { path in
let main = path.appending(component: "testExplicitModuleBuildJobs.swift")
try localFileSystem.writeFileContents(main, bytes: "import C\n")
let cHeadersPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "CHeaders")
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
var driver = try Driver(args: ["swift",
"-target", "x86_64-apple-macosx11.0",
"-I", cHeadersPath.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-explicit-module-build",
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
let jobs = try driver.planBuild()
let interpretJobs = jobs.filter { $0.kind == .interpret }
XCTAssertEqual(interpretJobs.count, 1)
let interpretJob = interpretJobs[0]
XCTAssertTrue(interpretJob.requiresInPlaceExecution)
XCTAssertTrue(interpretJob.commandLine.contains(subsequence: ["-frontend", "-interpret"]))
//XCTAssertTrue(interpretJob.commandLine.contains("-disable-implicit-swift-modules"))
XCTAssertTrue(interpretJob.commandLine.contains(subsequence: ["-Xcc", "-fno-implicit-modules"]))
// Figure out which Triples to use.
let dependencyGraph = try driver.gatherModuleDependencies()
let mainModuleInfo = try dependencyGraph.moduleInfo(of: .swift("testExplicitModuleBuildJobs"))
guard case .swift(_) = mainModuleInfo.details else {
XCTFail("Main module does not have Swift details field")
return
}
for job in jobs {
guard job.kind != .interpret else { continue }
XCTAssertEqual(job.outputs.count, 1)
let outputFilePath = job.outputs[0].file
// Swift dependencies
if outputFilePath.extension != nil,
outputFilePath.extension! == FileType.swiftModule.rawValue {
if pathMatchesSwiftModule(path: outputFilePath, "A") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("A"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "Swift") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("Swift"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_Concurrency") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_Concurrency"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "_StringProcessing") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("_StringProcessing"),
dependencyGraph: dependencyGraph)
} else if pathMatchesSwiftModule(path: outputFilePath, "SwiftOnoneSupport") {
try checkExplicitModuleBuildJob(job: job, moduleId: .swift("SwiftOnoneSupport"),
dependencyGraph: dependencyGraph)
}
// Clang Dependencies
} else if outputFilePath.extension != nil,
outputFilePath.extension! == FileType.pcm.rawValue {
let relativeOutputPathFileName = outputFilePath.basename
if relativeOutputPathFileName.starts(with: "A-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("A"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "B-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("B"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "C-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("C"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "SwiftShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("SwiftShims"),
dependencyGraph: dependencyGraph)
}
else if relativeOutputPathFileName.starts(with: "_SwiftConcurrencyShims-") {
try checkExplicitModuleBuildJob(job: job, moduleId: .clang("_SwiftConcurrencyShims"),
dependencyGraph: dependencyGraph)
}
else {
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
} else {
switch (outputFilePath) {
case .relative(try .init(validating: "testExplicitModuleBuildJobs")):
XCTAssertTrue(driver.isExplicitMainModuleJob(job: job))
XCTAssertEqual(job.kind, .link)
case .temporary(_):
let baseName = "testExplicitModuleBuildJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
}
}
}
}
func testModuleAliasingPrebuiltWithScanDeps() throws {
try withTemporaryDirectory { path in
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
let (stdLibPath, shimsPath, _, _) = try getDriverArtifactsForScanning()
let srcBar = path.appending(component: "bar.swift")
let moduleBarPath = path.appending(component: "Bar.swiftmodule").nativePathString(escaped: true)
try localFileSystem.writeFileContents(srcBar, bytes: "public class KlassBar {}")
// Create Bar.swiftmodule
var driver = try Driver(args: ["swiftc",
"-explicit-module-build",
"-working-directory",
path.nativePathString(escaped: true),
srcBar.nativePathString(escaped: true),
"-module-name",
"Bar",
"-emit-module",
"-emit-module-path", moduleBarPath,
"-module-cache-path", path.nativePathString(escaped: true),
"-I", stdLibPath.nativePathString(escaped: true),
"-I", shimsPath.nativePathString(escaped: true),
] + sdkArgumentsForTesting,
env: ProcessEnv.vars)
guard driver.isFrontendArgSupported(.moduleAlias) else {
throw XCTSkip("Skipping: compiler does not support '-module-alias'")
}
let jobs = try driver.planBuild()
try driver.run(jobs: jobs)
XCTAssertFalse(driver.diagnosticEngine.hasErrors)
XCTAssertTrue(FileManager.default.fileExists(atPath: moduleBarPath))
// Foo imports Car which is mapped to the real module Bar via
// `-module-alias Car=Bar`; it allows Car (alias) to be referenced
// in source files, while its contents are compiled as Bar (real
// name on disk).
let srcFoo = path.appending(component: "Foo.swift")
try localFileSystem.writeFileContents(srcFoo, bytes:
"""
import Car
func run() -> Car.KlassBar? { return nil }
"""
)
// Module alias with the fallback scanner (frontend scanner)
var driverA = try Driver(args: ["swiftc",
"-nonlib-dependency-scanner",
"-explicit-module-build",
"-working-directory",
path.nativePathString(escaped: true),
srcFoo.nativePathString(escaped: true),
"-module-alias", "Car=Bar",
"-I", path.nativePathString(escaped: true),
"-I", stdLibPath.nativePathString(escaped: true),
"-I", shimsPath.nativePathString(escaped: true),
] + sdkArgumentsForTesting)
// Resulting graph should contain the real module name Bar
let dependencyGraphA = try driverA.gatherModuleDependencies()
XCTAssertTrue(dependencyGraphA.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Bar"
})
XCTAssertFalse(dependencyGraphA.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Car"
})
let plannedJobsA = try driverA.planBuild()
XCTAssertTrue(plannedJobsA.contains { job in
job.commandLine.contains(.flag("-module-alias")) &&
job.commandLine.contains(.flag("Car=Bar"))
})
// Module alias with the default scanner (driver scanner)
var driverB = try Driver(args: ["swiftc",
"-explicit-module-build",
"-working-directory",
path.nativePathString(escaped: true),
srcFoo.nativePathString(escaped: true),
"-module-alias", "Car=Bar",
"-I", path.nativePathString(escaped: true),
"-I", stdLibPath.nativePathString(escaped: true),
"-I", shimsPath.nativePathString(escaped: true),
] + sdkArgumentsForTesting)
// Resulting graph should contain the real module name Bar
let dependencyGraphB = try driverB.gatherModuleDependencies()
XCTAssertTrue(dependencyGraphB.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Bar"
})
XCTAssertFalse(dependencyGraphB.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Car"
})
let plannedJobsB = try driverB.planBuild()
XCTAssertTrue(plannedJobsB.contains { job in
job.commandLine.contains(.flag("-module-alias")) &&
job.commandLine.contains(.flag("Car=Bar"))
})
}
}
func testModuleAliasingInterfaceWithScanDeps() throws {
try withTemporaryDirectory { path in
let swiftModuleInterfacesPath: AbsolutePath =
try testInputsPath.appending(component: "ExplicitModuleBuilds")
.appending(component: "Swift")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
let (stdLibPath, shimsPath, _, _) = try getDriverArtifactsForScanning()
// Foo imports Car which is mapped to the real module Bar via
// `-module-alias Car=E`; it allows Car (alias) to be referenced
// in source files, while its contents are compiled as E (real
// name on disk).
let srcFoo = path.appending(component: "Foo.swift")
try localFileSystem.writeFileContents(srcFoo, bytes: "import Car\n")
// Module alias with the fallback scanner (frontend scanner)
var driverA = try Driver(args: ["swiftc",
"-nonlib-dependency-scanner",
"-explicit-module-build",
srcFoo.nativePathString(escaped: true),
"-module-alias", "Car=E",
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-I", stdLibPath.nativePathString(escaped: true),
"-I", shimsPath.nativePathString(escaped: true),
] + sdkArgumentsForTesting)
guard driverA.isFrontendArgSupported(.moduleAlias) else {
throw XCTSkip("Skipping: compiler does not support '-module-alias'")
}
// Resulting graph should contain the real module name Bar
let dependencyGraphA = try driverA.gatherModuleDependencies()
XCTAssertTrue(dependencyGraphA.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "E"
})
XCTAssertFalse(dependencyGraphA.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Car"
})
let plannedJobsA = try driverA.planBuild()
XCTAssertTrue(plannedJobsA.contains { job in
job.commandLine.contains(.flag("-module-alias")) &&
job.commandLine.contains(.flag("Car=E"))
})
// Module alias with the default scanner (driver scanner)
var driverB = try Driver(args: ["swiftc",
"-explicit-module-build",
srcFoo.nativePathString(escaped: true),
"-module-alias", "Car=E",
"-working-directory", path.nativePathString(escaped: true),
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
"-I", stdLibPath.nativePathString(escaped: true),
"-I", shimsPath.nativePathString(escaped: true),
] + sdkArgumentsForTesting)
// Resulting graph should contain the real module name Bar
let dependencyGraphB = try driverB.gatherModuleDependencies()
XCTAssertTrue(dependencyGraphB.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "E"
})
XCTAssertFalse(dependencyGraphB.modules.contains { (key: ModuleDependencyId, value: ModuleInfo) in
key.moduleName == "Car"
})
let plannedJobsB = try driverB.planBuild()
XCTAssertTrue(plannedJobsB.contains { job in
job.commandLine.contains(.flag("-module-alias")) &&
job.commandLine.contains(.flag("Car=E"))
})
}
}
func testModuleAliasingWithImportPrescan() throws {
let (_, _, toolchain, _) = try getDriverArtifactsForScanning()
let dummyDriver = try Driver(args: ["swiftc", "-module-name", "dummyDriverCheck", "test.swift"])
guard dummyDriver.isFrontendArgSupported(.moduleAlias) else {
throw XCTSkip("Skipping: compiler does not support '-module-alias'")
}
// The dependency oracle wraps an instance of libSwiftScan and ensures thread safety across
// queries.
let dependencyOracle = InterModuleDependencyOracle()
let scanLibPath = try XCTUnwrap(toolchain.lookupSwiftScanLib())
try dependencyOracle.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
swiftScanLibPath: scanLibPath)
try withTemporaryDirectory { path in
let main = path.appending(component: "foo.swift")
try localFileSystem.writeFileContents(main, bytes:
"""
import Car;\
import Jet;
"""
)
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
let scannerCommand = ["-scan-dependencies",
"-import-prescan",
"-module-alias",
"Car=Bar",
main.nativePathString(escaped: true)] + sdkArgumentsForTesting
var scanDiagnostics: [ScannerDiagnosticPayload] = []
let deps =
try dependencyOracle.getImports(workingDirectory: path,
moduleAliases: ["Car": "Bar"],
commandLine: scannerCommand,
diagnostics: &scanDiagnostics)
XCTAssertTrue(deps.imports.contains("Bar"))
XCTAssertFalse(deps.imports.contains("Car"))
XCTAssertTrue(deps.imports.contains("Jet"))
}
}
func testModuleAliasingWithExplicitBuild() throws {
try withTemporaryDirectory { path in
try localFileSystem.changeCurrentWorkingDirectory(to: path)
let moduleCachePath = path.appending(component: "ModuleCache")
try localFileSystem.createDirectory(moduleCachePath)
let srcBar = path.appending(component: "bar.swift")
let moduleBarPath = path.appending(component: "Bar.swiftmodule").nativePathString(escaped: true)
try localFileSystem.writeFileContents(srcBar, bytes: "public class KlassBar {}")
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
let (stdLibPath, shimsPath, _, _) = try getDriverArtifactsForScanning()
var driver1 = try Driver(args: ["swiftc",
"-explicit-module-build",
"-module-name",
"Bar",