-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathSwiftDriverTests.swift
7365 lines (6443 loc) · 346 KB
/
SwiftDriverTests.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
//===--------------- SwiftDriverTests.swift - Swift Driver Tests -======---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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 SwiftOptions
import TSCBasic
import XCTest
import TestUtilities
private func executableName(_ name: String) -> String {
#if os(Windows)
if name.count > 4, name.suffix(from: name.index(name.endIndex, offsetBy: -4)) == ".exe" {
return name
}
return "\(name).exe"
#else
return name
#endif
}
private func rebase(_ arc: String, at base: AbsolutePath) -> String {
base.appending(component: arc).nativePathString(escaped: false)
}
private func rebase(_ arcs: String..., at base: AbsolutePath) -> String {
base.appending(components: arcs).nativePathString(escaped: false)
}
private var testInputsPath: AbsolutePath = {
var root: AbsolutePath = AbsolutePath(#file)
while root.basename != "Tests" {
root = root.parentDirectory
}
return root.parentDirectory.appending(component: "TestInputs")
}()
final class SwiftDriverTests: XCTestCase {
private var ld: AbsolutePath!
override func setUp() {
do {
self.ld = try withTemporaryDirectory(removeTreeOnDeinit: false) {
let ld = $0.appending(component: executableName("ld64.lld"))
try localFileSystem.writeFileContents(ld, bytes: "")
try localFileSystem.chmod(.executable, path: AbsolutePath(ld.nativePathString(escaped: false)))
return ld
}
} catch {
fatalError("unable to create stub 'ld' tool")
}
}
override func tearDown() {
try? localFileSystem.removeFileTree(AbsolutePath(validating: self.ld.dirname))
}
private var envWithFakeSwiftHelp: [String: String] {
// During build-script builds, build products are not installed into the toolchain
// until a project's tests pass. However, we're in the middle of those tests,
// so there is no swift-help in the toolchain yet. Set the environment variable
// as if we had found it for the purposes of testing build planning.
var env = ProcessEnv.vars
env["SWIFT_DRIVER_SWIFT_HELP_EXEC"] = "/tmp/.test-swift-help"
return env
}
/// Determine if the test's execution environment has LLDB
/// Used to skip tests that rely on LLDB in such environments.
private func testEnvHasLLDB() throws -> Bool {
let executor = try SwiftDriverExecutor(diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.vars)
let toolchain: Toolchain
#if os(macOS)
toolchain = DarwinToolchain(env: ProcessEnv.vars, executor: executor)
#elseif os(Windows)
toolchain = WindowsToolchain(env: ProcessEnv.vars, executor: executor)
#else
toolchain = GenericUnixToolchain(env: ProcessEnv.vars, executor: executor)
#endif
do {
_ = try toolchain.getToolPath(.lldb)
} catch ToolchainError.unableToFind {
return false
}
return true
}
func testInvocationRunModes() throws {
let driver1 = try Driver.invocationRunMode(forArgs: ["swift"])
XCTAssertEqual(driver1.mode, .normal(isRepl: false))
XCTAssertEqual(driver1.args, ["swift"])
let driver2 = try Driver.invocationRunMode(forArgs: ["swift", "-buzz"])
XCTAssertEqual(driver2.mode, .normal(isRepl: false))
XCTAssertEqual(driver2.args, ["swift", "-buzz"])
let driver3 = try Driver.invocationRunMode(forArgs: ["swift", "/"])
XCTAssertEqual(driver3.mode, .normal(isRepl: false))
XCTAssertEqual(driver3.args, ["swift", "/"])
let driver4 = try Driver.invocationRunMode(forArgs: ["swift", "./foo"])
XCTAssertEqual(driver4.mode, .normal(isRepl: false))
XCTAssertEqual(driver4.args, ["swift", "./foo"])
let driver5 = try Driver.invocationRunMode(forArgs: ["swift", "repl"])
XCTAssertEqual(driver5.mode, .normal(isRepl: true))
XCTAssertEqual(driver5.args, ["swift", "-repl"])
let driver6 = try Driver.invocationRunMode(forArgs: ["swift", "foo", "bar"])
XCTAssertEqual(driver6.mode, .subcommand(executableName("swift-foo")))
XCTAssertEqual(driver6.args, [executableName("swift-foo"), "bar"])
let driver7 = try Driver.invocationRunMode(forArgs: ["swift", "-frontend", "foo", "bar"])
XCTAssertEqual(driver7.mode, .subcommand(executableName("swift-frontend")))
XCTAssertEqual(driver7.args, [executableName("swift-frontend"), "foo", "bar"])
let driver8 = try Driver.invocationRunMode(forArgs: ["swift", "-modulewrap", "foo", "bar"])
XCTAssertEqual(driver8.mode, .subcommand(executableName("swift-frontend")))
XCTAssertEqual(driver8.args, [executableName("swift-frontend"), "-modulewrap", "foo", "bar"])
}
func testSubcommandsHandling() throws {
XCTAssertNoThrow(try Driver(args: ["swift"]))
XCTAssertNoThrow(try Driver(args: ["swift", "-I=foo"]))
XCTAssertNoThrow(try Driver(args: ["swift", ".foo"]))
XCTAssertNoThrow(try Driver(args: ["swift", "/foo"]))
XCTAssertThrowsError(try Driver(args: ["swift", "foo"]))
}
func testDriverKindParsing() throws {
func assertArgs(
_ args: String...,
parseTo driverKind: DriverKind,
leaving remainingArgs: [String],
file: StaticString = #file, line: UInt = #line
) throws {
var args = args
let result = try Driver.determineDriverKind(args: &args)
XCTAssertEqual(result, driverKind, file: file, line: line)
XCTAssertEqual(args, remainingArgs, file: file, line: line)
}
func assertArgsThrow(
_ args: String...,
file: StaticString = #file, line: UInt = #line
) throws {
var args = args
XCTAssertThrowsError(try Driver.determineDriverKind(args: &args))
}
try assertArgs("swift", parseTo: .interactive, leaving: [])
try assertArgs("/path/to/swift", parseTo: .interactive, leaving: [])
try assertArgs("swiftc", parseTo: .batch, leaving: [])
try assertArgs(".build/debug/swiftc", parseTo: .batch, leaving: [])
try assertArgs("swiftc", "--driver-mode=swift", parseTo: .interactive, leaving: [])
try assertArgs("swift", "-zelda", parseTo: .interactive, leaving: ["-zelda"])
try assertArgs("swiftc", "--driver-mode=swift", "swiftc",
parseTo: .interactive, leaving: ["swiftc"])
try assertArgsThrow("driver")
try assertArgsThrow("swiftc", "--driver-mode=blah")
try assertArgsThrow("swiftc", "--driver-mode=")
}
func testCompilerMode() throws {
do {
let driver1 = try Driver(args: ["swift", "main.swift"])
XCTAssertEqual(driver1.compilerMode, .immediate)
let driver2 = try Driver(args: ["swift"])
XCTAssertEqual(driver2.compilerMode, .intro)
}
do {
let driver1 = try Driver(args: ["swiftc", "main.swift", "-whole-module-optimization"])
XCTAssertEqual(driver1.compilerMode, .singleCompile)
let driver2 = try Driver(args: ["swiftc", "main.swift", "-whole-module-optimization", "-no-whole-module-optimization"])
XCTAssertEqual(driver2.compilerMode, .standardCompile)
let driver3 = try Driver(args: ["swiftc", "main.swift", "-g"])
XCTAssertEqual(driver3.compilerMode, .standardCompile)
}
}
func testJoinedPathOptions() throws {
var driver = try Driver(args: ["swiftc", "-c", "-I=/some/dir", "-F=other/relative/dir", "foo.swift"])
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.joinedOptionAndPath("-I=", .absolute(.init("/some/dir")))))
XCTAssertTrue(jobs[0].commandLine.contains(.joinedOptionAndPath("-F=", .relative(.init("other/relative/dir")))))
}
func testRelativeOptionOrdering() throws {
var driver = try Driver(args: ["swiftc", "foo.swift",
"-F", "/path/to/frameworks",
"-Fsystem", "/path/to/systemframeworks",
"-F", "/path/to/more/frameworks"])
let jobs = try driver.planBuild()
XCTAssertEqual(jobs[0].kind, .compile)
// The relative ordering of -F and -Fsystem options should be preserved.
XCTAssertTrue(jobs[0].commandLine.contains(subsequence: [.flag("-F"), .path(.absolute(.init("/path/to/frameworks"))),
.flag("-Fsystem"), .path(.absolute(.init("/path/to/systemframeworks"))),
.flag("-F"), .path(.absolute(.init("/path/to/more/frameworks")))]))
}
func testBatchModeDiagnostics() throws {
try assertNoDriverDiagnostics(args: "swiftc", "-enable-batch-mode") { driver in
switch driver.compilerMode {
case .batchCompile:
break
default:
XCTFail("Expected batch compile, got \(driver.compilerMode)")
}
}
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization") { driver, diagnostics in
XCTAssertEqual(driver.compilerMode, .singleCompile)
diagnostics.expect(.warning("ignoring '-enable-batch-mode' because '-whole-module-optimization' was also specified"))
}
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization", "-no-whole-module-optimization", "-index-file", "-module-name", "foo") { driver, diagnostics in
XCTAssertEqual(driver.compilerMode, .singleCompile)
diagnostics.expect(.warning("ignoring '-enable-batch-mode' because '-index-file' was also specified"))
}
try assertNoDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization", "-no-whole-module-optimization") { driver in
switch driver.compilerMode {
case .batchCompile:
break
default:
XCTFail("Expected batch compile, got \(driver.compilerMode)")
}
}
}
func testHelp() throws {
do {
var driver = try Driver(args: ["swift", "--help"], env: envWithFakeSwiftHelp)
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
let helpJob = plannedJobs.first!
XCTAssertTrue(helpJob.kind == .help)
XCTAssertTrue(helpJob.requiresInPlaceExecution)
XCTAssertTrue(helpJob.tool.name.hasSuffix("swift-help"))
let expected: [Job.ArgTemplate] = [.flag("swift")]
XCTAssertEqual(helpJob.commandLine, expected)
}
do {
var driver = try Driver(args: ["swiftc", "-help-hidden"], env: envWithFakeSwiftHelp)
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
let helpJob = plannedJobs.first!
XCTAssertTrue(helpJob.kind == .help)
XCTAssertTrue(helpJob.requiresInPlaceExecution)
XCTAssertTrue(helpJob.tool.name.hasSuffix("swift-help"))
let expected: [Job.ArgTemplate] = [.flag("swiftc"), .flag("-show-hidden")]
XCTAssertEqual(helpJob.commandLine, expected)
}
}
func testRuntimeCompatibilityVersion() throws {
try assertNoDriverDiagnostics(args: "swiftc", "a.swift", "-runtime-compatibility-version", "none")
}
func testInputFiles() throws {
let driver1 = try Driver(args: ["swiftc", "a.swift", "/tmp/b.swift"])
XCTAssertEqual(driver1.inputFiles,
[ TypedVirtualPath(file: VirtualPath.relative(RelativePath("a.swift")).intern(), type: .swift),
TypedVirtualPath(file: VirtualPath.absolute(AbsolutePath("/tmp/b.swift")).intern(), type: .swift) ])
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "wobble")
let tempDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "tmp")
let driver2 = try Driver(args: ["swiftc", "a.swift", "-working-directory", workingDirectory.pathString, rebase("b.swift", at: tempDirectory)])
XCTAssertEqual(driver2.inputFiles,
[ TypedVirtualPath(file: VirtualPath.absolute(AbsolutePath(rebase("a.swift", at: workingDirectory))).intern(), type: .swift),
TypedVirtualPath(file: VirtualPath.absolute(AbsolutePath(rebase("b.swift", at: tempDirectory))).intern(), type: .swift) ])
let driver3 = try Driver(args: ["swift", "-"])
XCTAssertEqual(driver3.inputFiles, [ TypedVirtualPath(file: .standardInput, type: .swift )])
let driver4 = try Driver(args: ["swift", "-", "-working-directory" , "-wobble"])
XCTAssertEqual(driver4.inputFiles, [ TypedVirtualPath(file: .standardInput, type: .swift )])
}
func testDashE() throws {
let fs = localFileSystem
var driver1 = try Driver(args: ["swift", "-e", "print(1)", "-e", "print(2)", "foo/bar.swift", "baz/quux.swift"], fileSystem: fs)
XCTAssertEqual(driver1.inputFiles.count, 1)
XCTAssertEqual(driver1.inputFiles[0].file.basename, "main.swift")
let tempFileContentsForDriver1 = try fs.readFileContents(XCTUnwrap(driver1.inputFiles[0].file.absolutePath))
XCTAssertTrue(tempFileContentsForDriver1.description.hasSuffix("\nprint(1)\nprint(2)\n"))
let plannedJobs = try driver1.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .interpret)
XCTAssertEqual(plannedJobs[0].commandLine.drop(while: { $0 != .flag("--") }),
[.flag("--"), .flag("foo/bar.swift"), .flag("baz/quux.swift")])
XCTAssertThrowsError(try Driver(args: ["swiftc", "baz/main.swift", "-e", "print(1)"], fileSystem: fs))
}
func testDashEJoined() throws {
let fs = localFileSystem
XCTAssertThrowsError(try Driver(args: ["swift", "-eprint(1)", "foo/bar.swift", "baz/quux.swift"], fileSystem: fs)) { error in
XCTAssertEqual(error as? OptionParseError, .unknownOption(index: 0, argument: "-eprint(1)"))
}
}
func testRecordedInputModificationDates() throws {
guard let cwd = localFileSystem.currentWorkingDirectory else {
fatalError()
}
try withTemporaryDirectory(dir: cwd, removeTreeOnDeinit: true) { path in
let main = path.appending(component: "main.swift")
let util = path.appending(component: "util.swift")
let utilRelative = util.relative(to: cwd)
try localFileSystem.writeFileContents(main, bytes: "print(hi)")
try localFileSystem.writeFileContents(util, bytes: "let hi = \"hi\"")
let mainMDate = try localFileSystem.lastModificationTime(for: .absolute(main))
let utilMDate = try localFileSystem.lastModificationTime(for: .absolute(util))
let driver = try Driver(args: [
"swiftc", main.pathString, utilRelative.pathString,
])
XCTAssertEqual(driver.recordedInputModificationDates, [
.init(file: VirtualPath.absolute(main).intern(), type: .swift) : mainMDate,
.init(file: VirtualPath.relative(utilRelative).intern(), type: .swift) : utilMDate,
])
}
}
func testPrimaryOutputKinds() throws {
let driver1 = try Driver(args: ["swiftc", "foo.swift", "-emit-module"])
XCTAssertEqual(driver1.compilerOutputType, .swiftModule)
XCTAssertEqual(driver1.linkerOutputType, nil)
let driver2 = try Driver(args: ["swiftc", "foo.swift", "-emit-library"])
XCTAssertEqual(driver2.compilerOutputType, .object)
XCTAssertEqual(driver2.linkerOutputType, .dynamicLibrary)
let driver3 = try Driver(args: ["swiftc", "-static", "foo.swift", "-emit-library"])
XCTAssertEqual(driver3.compilerOutputType, .object)
XCTAssertEqual(driver3.linkerOutputType, .staticLibrary)
let driver4 = try Driver(args: ["swiftc", "-lto=llvm-thin", "foo.swift", "-emit-library"])
XCTAssertEqual(driver4.compilerOutputType, .llvmBitcode)
let driver5 = try Driver(args: ["swiftc", "-lto=llvm-full", "foo.swift", "-emit-library"])
XCTAssertEqual(driver5.compilerOutputType, .llvmBitcode)
}
func testLtoOutputModeClash() throws {
let driver1 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-static",
"-emit-library", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver1.compilerOutputType, .llvmBitcode)
let driver2 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-emit-library", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver2.compilerOutputType, .llvmBitcode)
let driver3 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"c", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver3.compilerOutputType, .llvmBitcode)
let driver4 = try Driver(args: ["swiftc", "foo.swift", "-c","-lto=llvm-full",
"-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver4.compilerOutputType, .llvmBitcode)
let driver5 = try Driver(args: ["swiftc", "foo.swift", "-c","-lto=llvm-full",
"-emit-bc", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver5.compilerOutputType, .llvmBitcode)
let driver6 = try Driver(args: ["swiftc", "foo.swift", "-emit-bc", "-c","-lto=llvm-full",
"-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver6.compilerOutputType, .llvmBitcode)
}
func testLtoOutputPath() throws {
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-c", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver.linkerOutputType, nil)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.count, 1)
XCTAssertEqual(jobs[0].outputs.count, 1)
XCTAssertEqual(jobs[0].outputs[0].file.basename, "foo.bc")
}
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-c", "-target", "x86_64-apple-macosx10.9", "-o", "foo.o"])
XCTAssertEqual(driver.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver.linkerOutputType, nil)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.count, 1)
XCTAssertEqual(jobs[0].outputs.count, 1)
XCTAssertEqual(jobs[0].outputs[0].file.basename, "foo.o")
}
}
func testPrimaryOutputKindsDiagnostics() throws {
try assertDriverDiagnostics(args: "swift", "-i") {
$1.expect(.error("the flag '-i' is no longer required and has been removed; use 'swift input-filename'"))
}
}
func testFilePrefixMapInvalidDiagnostic() throws {
try assertDriverDiagnostics(args: "swiftc", "-c", "foo.swift", "-o", "foo.o", "-file-prefix-map", "invalid") {
$1.expect(.error("values for '-file-prefix-map' must be in the format 'original=remapped', but 'invalid' was provided"))
}
}
func testFilePrefixMapMultiplePassToFrontend() throws {
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-file-prefix-map", "foo=bar", "-file-prefix-map", "dog=doggo") { driver in
let jobs = try driver.planBuild()
let commandLine = jobs[0].commandLine
let index = commandLine.firstIndex(of: .flag("-file-prefix-map"))
let lastIndex = commandLine.lastIndex(of: .flag("-file-prefix-map"))
XCTAssertNotNil(index)
XCTAssertNotNil(lastIndex)
XCTAssertNotEqual(index, lastIndex)
XCTAssertEqual(commandLine[index!.advanced(by: 1)], .flag("foo=bar"))
XCTAssertEqual(commandLine[lastIndex!.advanced(by: 1)], .flag("dog=doggo"))
}
}
func testIndexIncludeLocals() throws {
// Make sure `-index-include-locals` is only passed to the frontend when
// requested, not by default.
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-index-store-path", "/tmp/idx") { driver in
let jobs = try driver.planBuild()
let commandLine = jobs[0].commandLine
XCTAssertTrue(commandLine.contains(.flag("-index-store-path")))
XCTAssertFalse(commandLine.contains(.flag("-index-include-locals")))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-index-store-path", "/tmp/idx", "-index-include-locals") { driver in
let jobs = try driver.planBuild()
let commandLine = jobs[0].commandLine
XCTAssertTrue(commandLine.contains(.flag("-index-store-path")))
XCTAssertTrue(commandLine.contains(.flag("-index-include-locals")))
}
}
func testMultiThreadingOutputs() throws {
try assertDriverDiagnostics(args: "swiftc", "-c", "foo.swift", "bar.swift", "-o", "bar.ll", "-o", "foo.ll", "-num-threads", "2", "-whole-module-optimization") {
$1.expect(.error("cannot specify -o when generating multiple output files"))
}
try assertDriverDiagnostics(args: "swiftc", "-c", "foo.swift", "bar.swift", "-o", "bar.ll", "-o", "foo.ll", "-num-threads", "0") {
$1.expect(.error("cannot specify -o when generating multiple output files"))
}
}
func testBaseOutputPaths() throws {
// Test the combination of -c and -o includes the base output path.
do {
var driver = try Driver(args: ["swiftc", "-c", "foo.swift", "-o", "/some/output/path/bar.o"])
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/some/output/path/bar.o"))))
}
do {
var driver = try Driver(args: ["swiftc", "-emit-sil", "foo.swift", "-o", "/some/output/path/bar.sil"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/some/output/path/bar.sil"))))
}
do {
// If no output is specified, verify we print to stdout for textual formats.
var driver = try Driver(args: ["swiftc", "-emit-assembly", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssertTrue(plannedJobs[0].commandLine.contains(.path(.standardOutput)))
}
}
func testMultithreading() throws {
XCTAssertNil(try Driver(args: ["swiftc"]).numParallelJobs)
XCTAssertEqual(try Driver(args: ["swiftc", "-j", "4"]).numParallelJobs, 4)
var env = ProcessEnv.vars
env["SWIFTC_MAXIMUM_DETERMINISM"] = "1"
XCTAssertEqual(try Driver(args: ["swiftc", "-j", "4"], env: env).numParallelJobs, 1)
}
func testMultithreadingDiagnostics() throws {
try assertDriverDiagnostics(args: "swiftc", "-j", "0") {
$1.expect(.error("invalid value '0' in '-j'"))
}
var env = ProcessEnv.vars
env["SWIFTC_MAXIMUM_DETERMINISM"] = "1"
try assertDriverDiagnostics(args: "swiftc", "-j", "8", env: env) {
$1.expect(.remark("SWIFTC_MAXIMUM_DETERMINISM overriding -j"))
}
}
func testDebugSettings() throws {
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module") { driver in
XCTAssertNil(driver.debugInfo.level)
XCTAssertEqual(driver.debugInfo.format, .dwarf)
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-g") { driver in
XCTAssertEqual(driver.debugInfo.level, .astTypes)
XCTAssertEqual(driver.debugInfo.format, .dwarf)
}
try assertNoDriverDiagnostics(args: "swiftc", "-g", "foo.swift", "-gline-tables-only") { driver in
XCTAssertEqual(driver.debugInfo.level, .lineTables)
XCTAssertEqual(driver.debugInfo.format, .dwarf)
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-debug-prefix-map", "foo=bar=baz", "-debug-prefix-map", "qux=") { driver in
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-debug-prefix-map")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("foo=bar=baz")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-debug-prefix-map")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("qux=")))
}
do {
var env = ProcessEnv.vars
env["SWIFT_DRIVER_TESTS_ENABLE_EXEC_PATH_FALLBACK"] = "1"
env["RC_DEBUG_PREFIX_MAP"] = "old=new"
var driver = try Driver(args: ["swiftc", "-c", "-target", "arm64-apple-macos12", "foo.swift"], env: env)
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-debug-prefix-map")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("old=new")))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-debug-prefix-map", "foo", "-debug-prefix-map", "bar") {
$1.expect(.error("values for '-debug-prefix-map' must be in the format 'original=remapped', but 'foo' was provided"))
$1.expect(.error("values for '-debug-prefix-map' must be in the format 'original=remapped', but 'bar' was provided"))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-g", "-debug-info-format=codeview") { driver in
XCTAssertEqual(driver.debugInfo.level, .astTypes)
XCTAssertEqual(driver.debugInfo.format, .codeView)
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-debug-info-format=dwarf") {
$1.expect(.error("option '-debug-info-format=' is missing a required argument (-g)"))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-g", "-debug-info-format=notdwarf") {
$1.expect(.error("invalid value 'notdwarf' in '-debug-info-format='"))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-gdwarf-types", "-debug-info-format=codeview") {
$1.expect(.error("argument '-debug-info-format=codeview' is not allowed with '-gdwarf-types'"))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-file-compilation-dir", ".") { driver in
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-file-compilation-dir")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag(".")))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-c", "-file-compilation-dir", ".") { driver in
let jobs = try driver.planBuild()
XCTAssertFalse(jobs[0].commandLine.contains(.flag("-file-compilation-dir")))
}
}
func testCoverageSettings() throws {
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-coverage-prefix-map", "foo=bar=baz", "-coverage-prefix-map", "qux=") { driver in
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-coverage-prefix-map")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("foo=bar=baz")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-coverage-prefix-map")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("qux=")))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-coverage-prefix-map", "foo", "-coverage-prefix-map", "bar") {
$1.expect(.error("values for '-coverage-prefix-map' must be in the format 'original=remapped', but 'foo' was provided"))
$1.expect(.error("values for '-coverage-prefix-map' must be in the format 'original=remapped', but 'bar' was provided"))
}
}
func testHermeticSealAtLink() throws {
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-experimental-hermetic-seal-at-link", "-lto=llvm-full") { driver in
let jobs = try driver.planBuild()
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-enable-llvm-vfe")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-enable-llvm-wme")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-conditional-runtime-records")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-internalize-at-link")))
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-lto=llvm-full")))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-experimental-hermetic-seal-at-link") {
$1.expect(.error("-experimental-hermetic-seal-at-link requires -lto=llvm-full or -lto=llvm-thin"))
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-experimental-hermetic-seal-at-link", "-lto=llvm-full", "-enable-library-evolution") {
$1.expect(.error("Cannot use -experimental-hermetic-seal-at-link with -enable-library-evolution"))
}
}
func testABIDescriptorOnlyWhenEnableEvolution() throws {
let flagName = "-empty-abi-descriptor"
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift") { driver in
let jobs = try driver.planBuild()
let command = jobs[0].commandLine
XCTAssertTrue(command.contains(.flag(flagName)))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-enable-library-evolution") { driver in
let jobs = try driver.planBuild()
let command = jobs[0].commandLine
XCTAssertFalse(command.contains(.flag(flagName)))
}
}
func testModuleSettings() throws {
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift") { driver in
XCTAssertNil(driver.moduleOutputInfo.output)
XCTAssertEqual(driver.moduleOutputInfo.name, "foo")
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g") { driver in
let pathHandle = driver.moduleOutputInfo.output?.outputPath
XCTAssertTrue(matchTemporary(VirtualPath.lookup(pathHandle!), "foo.swiftmodule"))
XCTAssertEqual(driver.moduleOutputInfo.name, "foo")
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-module-name", "wibble", "bar.swift", "-g") { driver in
let pathHandle = driver.moduleOutputInfo.output?.outputPath
XCTAssertTrue(matchTemporary(VirtualPath.lookup(pathHandle!), "wibble.swiftmodule"))
XCTAssertEqual(driver.moduleOutputInfo.name, "wibble")
}
try assertNoDriverDiagnostics(args: "swiftc", "-emit-module", "foo.swift", "-module-name", "wibble", "bar.swift") { driver in
XCTAssertEqual(driver.moduleOutputInfo.output, .topLevel(try VirtualPath.intern(path: "wibble.swiftmodule")))
XCTAssertEqual(driver.moduleOutputInfo.name, "wibble")
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift") { driver in
XCTAssertNil(driver.moduleOutputInfo.output)
XCTAssertEqual(driver.moduleOutputInfo.name, "main")
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-library", "-o", "libWibble.so") { driver in
XCTAssertEqual(driver.moduleOutputInfo.name, "Wibble")
}
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-library", "-o", "libWibble.so", "-module-name", "Swift") {
$1.expect(.error("module name \"Swift\" is reserved for the standard library"))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "some/dir/libFoo.so", "-module-name", "MyModule") { driver in
XCTAssertEqual(driver.moduleOutputInfo.output, .topLevel(try VirtualPath.intern(path: "some/dir/MyModule.swiftmodule")))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "/", "-module-name", "MyModule") { driver in
XCTAssertEqual(driver.moduleOutputInfo.output, .topLevel(try VirtualPath.intern(path: "/MyModule.swiftmodule")))
}
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "../../some/other/dir/libFoo.so", "-module-name", "MyModule") { driver in
XCTAssertEqual(driver.moduleOutputInfo.output, .topLevel(try VirtualPath.intern(path: "../../some/other/dir/MyModule.swiftmodule")))
}
}
func testModuleNameFallbacks() throws {
try assertNoDriverDiagnostics(args: "swiftc", "file.foo.swift")
try assertNoDriverDiagnostics(args: "swiftc", ".foo.swift")
try assertNoDriverDiagnostics(args: "swiftc", "foo-bar.swift")
}
func testPackageNameFlag() throws {
// -package-name com.perf.my-pkg (valid string)
try assertNoDriverDiagnostics(args: "swiftc", "file.swift", "bar.swift", "-module-name", "MyModule", "-package-name", "com.perf.my-pkg", "-emit-module", "-emit-module-path", "../../path/to/MyModule.swiftmodule") { driver in
XCTAssertEqual(driver.packageName, "com.perf.my-pkg")
XCTAssertEqual(driver.moduleOutputInfo.output, .topLevel(try VirtualPath.intern(path: "../../path/to/MyModule.swiftmodule")))
}
// -package-name is not passed and file doesn't contain `package` decls; should pass
try assertNoDriverDiagnostics(args: "swiftc", "file.swift") { driver in
XCTAssertNil(driver.packageName)
XCTAssertEqual(driver.moduleOutputInfo.name, "file")
}
// -package-name 123a!@#$ (valid string)
try assertNoDriverDiagnostics(args: "swiftc", "file.swift", "-module-name", "Foo", "-package-name", "123a!@#$") { driver in
XCTAssertEqual(driver.packageName, "123a!@#$")
}
// -package-name input is an empty string
try assertDriverDiagnostics(args: "swiftc", "file.swift", "-package-name", "") {
$1.expect(.error("package-name is empty"))
}
}
func testStandardCompileJobs() throws {
var driver1 = try Driver(args: ["swiftc", "foo.swift", "bar.swift", "-module-name", "Test"])
let plannedJobs = try driver1.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 3)
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
XCTAssertTrue(matchTemporary(plannedJobs[0].outputs.first!.file, "foo.o"))
XCTAssertEqual(plannedJobs[1].outputs.count, 1)
XCTAssertTrue(matchTemporary(plannedJobs[1].outputs.first!.file, "bar.o"))
XCTAssertTrue(plannedJobs[2].tool.name.contains(executableName("clang")))
XCTAssertEqual(plannedJobs[2].outputs.count, 1)
XCTAssertEqual(plannedJobs[2].outputs.first!.file, VirtualPath.relative(RelativePath(executableName("Test"))))
// Forwarding of arguments.
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "tmp")
var driver2 = try Driver(args: ["swiftc", "-color-diagnostics", "foo.swift", "bar.swift", "-working-directory", workingDirectory.pathString, "-api-diff-data-file", "diff.txt", "-Xfrontend", "-HI", "-no-color-diagnostics", "-g"])
let plannedJobs2 = try driver2.planBuild()
let compileJob = try plannedJobs2.findJob(.compile)
XCTAssert(compileJob.commandLine.contains(Job.ArgTemplate.path(.absolute(try AbsolutePath(validating: rebase("diff.txt", at: workingDirectory))))))
XCTAssert(compileJob.commandLine.contains(.flag("-HI")))
XCTAssert(!compileJob.commandLine.contains(.flag("-Xfrontend")))
XCTAssert(compileJob.commandLine.contains(.flag("-no-color-diagnostics")))
XCTAssert(!compileJob.commandLine.contains(.flag("-color-diagnostics")))
XCTAssert(compileJob.commandLine.contains(.flag("-target")))
XCTAssert(compileJob.commandLine.contains(.flag(driver2.targetTriple.triple)))
XCTAssert(compileJob.commandLine.contains(.flag("-enable-anonymous-context-mangled-names")))
var driver3 = try Driver(args: ["swiftc", "foo.swift", "bar.swift", "-emit-library", "-module-name", "Test"])
let plannedJobs3 = try driver3.planBuild()
XCTAssertTrue(plannedJobs3[0].commandLine.contains(.flag("-module-name")))
XCTAssertTrue(plannedJobs3[0].commandLine.contains(.flag("Test")))
XCTAssertTrue(plannedJobs3[0].commandLine.contains(.flag("-parse-as-library")))
}
func testModuleNaming() throws {
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift"]).moduleOutputInfo.name, "foo")
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-o", "a.out"]).moduleOutputInfo.name, "a")
// This is silly, but necessary for compatibility with the integrated driver.
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-o", "a.out.optimized"]).moduleOutputInfo.name, "main")
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-o", "a.out.optimized", "-module-name", "bar"]).moduleOutputInfo.name, "bar")
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-o", "+++.out"]).moduleOutputInfo.name, "main")
XCTAssertEqual(try Driver(args: ["swift"]).moduleOutputInfo.name, "REPL")
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-emit-library", "-o", "libBaz.dylib"]).moduleOutputInfo.name, "Baz")
try assertDriverDiagnostics(
args: ["swiftc", "foo.swift", "-module-name", "", "file.foo.swift"]
) {
$1.expect(.error("module name \"\" is not a valid identifier"))
}
try assertDriverDiagnostics(
args: ["swiftc", "foo.swift", "-module-name", "123", "file.foo.swift"]
) {
$1.expect(.error("module name \"123\" is not a valid identifier"))
}
}
func testEmitModuleSeparatelyDiagnosticPath() throws {
try withTemporaryFile { fileMapFile in
let outputMapContents: ByteString = """
{
"": {
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia",
"emit-module-diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia"
},
"foo.swift": {
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.dia"
}
}
"""
try localFileSystem.writeFileContents(fileMapFile.path, bytes: outputMapContents)
// Plain (batch/single-file) compile
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-output-file-map", fileMapFile.path.pathString,
"-emit-library", "-module-name", "Test", "-serialize-diagnostics"])
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 3)
XCTAssertTrue(plannedJobs[0].kind == .emitModule)
XCTAssertTrue(plannedJobs[1].kind == .compile)
XCTAssertTrue(plannedJobs[2].kind == .link)
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia")))]))
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.dia")))]))
}
// WMO
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-whole-module-optimization", "-emit-module",
"-output-file-map", fileMapFile.path.pathString, "-disable-cmo",
"-emit-library", "-module-name", "Test", "-serialize-diagnostics"])
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 3)
XCTAssertTrue(plannedJobs[0].kind == .compile)
XCTAssertTrue(plannedJobs[1].kind == .emitModule)
XCTAssertTrue(plannedJobs[2].kind == .link)
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia")))]))
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia")))]))
}
}
}
func testEmitModuleSeparatelyDependenciesPath() throws {
try withTemporaryFile { fileMapFile in
let outputMapContents: ByteString = """
{
"": {
"dependencies": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.d",
"emit-module-dependencies": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.d"
},
"foo.swift": {
"dependencies": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.d"
}
}
"""
try localFileSystem.writeFileContents(fileMapFile.path, bytes: outputMapContents)
// Plain (batch/single-file) compile
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-output-file-map", fileMapFile.path.pathString,
"-emit-library", "-module-name", "Test", "-emit-dependencies"])
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 3)
XCTAssertTrue(plannedJobs[0].kind == .emitModule)
XCTAssertTrue(plannedJobs[1].kind == .compile)
XCTAssertTrue(plannedJobs[2].kind == .link)
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-emit-dependencies-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.d")))]))
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-emit-dependencies-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.d")))]))
}
// WMO
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-whole-module-optimization", "-emit-module",
"-output-file-map", fileMapFile.path.pathString, "-disable-cmo",
"-emit-library", "-module-name", "Test", "-emit-dependencies"])
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 3)
XCTAssertTrue(plannedJobs[0].kind == .compile)
XCTAssertTrue(plannedJobs[1].kind == .emitModule)
XCTAssertTrue(plannedJobs[2].kind == .link)
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-emit-dependencies-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.d")))]))
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-emit-dependencies-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.d")))]))
}
}
}
func testOutputFileMapLoading() throws {
let objroot: AbsolutePath =
AbsolutePath("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build")
let contents = ByteString("""
{
"": {
"swift-dependencies": "\(objroot.appending(components: "master.swiftdeps").nativePathString(escaped: true))"
},
"/tmp/foo/Sources/foo/foo.swift": {
"dependencies": "\(objroot.appending(components: "foo.d").nativePathString(escaped: true))",
"object": "\(objroot.appending(components: "foo.swift.o").nativePathString(escaped: true))",
"swiftmodule": "\(objroot.appending(components: "foo~partial.swiftmodule").nativePathString(escaped: true))",
"swift-dependencies": "\(objroot.appending(components: "foo.swiftdeps").nativePathString(escaped: true))"
}
}
""".utf8)
try withTemporaryFile { file in
try assertNoDiagnostics { diags in
try localFileSystem.writeFileContents(file.path, bytes: contents)
let outputFileMap = try OutputFileMap.load(fileSystem: localFileSystem, file: .absolute(file.path), diagnosticEngine: diags)
let object = try outputFileMap.getOutput(inputFile: VirtualPath.intern(path: "/tmp/foo/Sources/foo/foo.swift"), outputType: .object)
XCTAssertEqual(VirtualPath.lookup(object).name, objroot.appending(components: "foo.swift.o").pathString)
let masterDeps = try outputFileMap.getOutput(inputFile: VirtualPath.intern(path: ""), outputType: .swiftDeps)
XCTAssertEqual(VirtualPath.lookup(masterDeps).name, objroot.appending(components: "master.swiftdeps").pathString)
}
}
}
func testFindingObjectPathFromllvmBCPath() throws {
let objroot: AbsolutePath =
AbsolutePath("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build")
let contents = ByteString(
"""
{
"": {
"swift-dependencies": "\(objroot.appending(components: "master.swiftdeps").nativePathString(escaped: true))"
},
"/tmp/foo/Sources/foo/foo.swift": {
"dependencies": "\(objroot.appending(components: "foo.d").nativePathString(escaped: true))",
"object": "\(objroot.appending(components: "foo.swift.o").nativePathString(escaped: true))",
"swiftmodule": "\(objroot.appending(components: "foo~partial.swiftmodule").nativePathString(escaped: true))",
"swift-dependencies": "\(objroot.appending(components: "foo.swiftdeps").nativePathString(escaped: true))",
"llvm-bc": "\(objroot.appending(components: "foo.swift.bc").nativePathString(escaped: true))"
}
}
""".utf8
)
try withTemporaryFile { file in
try assertNoDiagnostics { diags in
try localFileSystem.writeFileContents(file.path, bytes: contents)
let outputFileMap = try OutputFileMap.load(fileSystem: localFileSystem, file: .absolute(file.path), diagnosticEngine: diags)
let obj = try outputFileMap.getOutput(inputFile: VirtualPath.intern(path: "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.swift.bc"), outputType: .object)
XCTAssertEqual(VirtualPath.lookup(obj).name, objroot.appending(components: "foo.swift.o").pathString)
}
}
}
func testOutputFileMapLoadingDocAndSourceinfo() throws {
let objroot: AbsolutePath =
AbsolutePath("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build")
let contents = ByteString(
"""
{
"": {
"swift-dependencies": "\(objroot.appending(components: "master.swiftdeps").nativePathString(escaped: true))"
},
"/tmp/foo/Sources/foo/foo.swift": {
"dependencies": "\(objroot.appending(components: "foo.d").nativePathString(escaped: true))",
"object": "\(objroot.appending(components: "foo.swift.o").nativePathString(escaped: true))",
"swiftmodule": "\(objroot.appending(components: "foo~partial.swiftmodule").nativePathString(escaped: true))",
"swift-dependencies": "\(objroot.appending(components: "foo.swiftdeps").nativePathString(escaped: true))"
}
}
""".utf8
)
try withTemporaryFile { file in
try assertNoDiagnostics { diags in
try localFileSystem.writeFileContents(file.path, bytes: contents)
let outputFileMap = try OutputFileMap.load(fileSystem: localFileSystem, file: .absolute(file.path), diagnosticEngine: diags)
let doc = try outputFileMap.getOutput(inputFile: VirtualPath.intern(path: "/tmp/foo/Sources/foo/foo.swift"), outputType: .swiftDocumentation)
XCTAssertEqual(VirtualPath.lookup(doc).name, objroot.appending(components: "foo~partial.swiftdoc").pathString)
let source = try outputFileMap.getOutput(inputFile: VirtualPath.intern(path: "/tmp/foo/Sources/foo/foo.swift"), outputType: .swiftSourceInfoFile)
XCTAssertEqual(VirtualPath.lookup(source).name, objroot.appending(components: "foo~partial.swiftsourceinfo").pathString)
}
}
}
func testIndexUnitOutputPath() throws {
let contents = ByteString(
"""
{
"/tmp/main.swift": {
"object": "/tmp/build1/main.o",
"index-unit-output-path": "/tmp/build2/main.o",
},
"/tmp/second.swift": {
"object": "/tmp/build1/second.o",
"index-unit-output-path": "/tmp/build2/second.o",
}
}
""".utf8
)
func getFileListElements(for filelistOpt: String, job: Job) -> [VirtualPath] {
let optIndex = job.commandLine.firstIndex(of: .flag(filelistOpt))!
let value = job.commandLine[job.commandLine.index(after: optIndex)]
guard case let .path(.fileList(_, valueFileList)) = value else {
XCTFail("Argument wasn't a filelist")
return []
}
guard case let .list(inputs) = valueFileList else {
XCTFail("FileList wasn't List")
return []
}
return inputs
}
try withTemporaryFile { file in
try assertNoDiagnostics { diags in
try localFileSystem.writeFileContents(file.path, bytes: contents)
// 1. Incremental mode (single primary file)
// a) without filelists
var driver = try Driver(args: [
"swiftc", "-c",
"-output-file-map", file.path.pathString,
"-module-name", "test", "/tmp/second.swift", "/tmp/main.swift"
])