-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathToolchainTests.swift
More file actions
793 lines (708 loc) · 29.2 KB
/
Copy pathToolchainTests.swift
File metadata and controls
793 lines (708 loc) · 29.2 KB
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import SwiftDriverExecution
import SwiftOptions
import TSCBasic
import TestUtilities
import Testing
@testable @_spi(Testing) import SwiftDriver
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(CRT)
import CRT
#endif
@Suite struct ToolchainTests {
private var ld: AbsolutePath { get throws { try makeLdStub() } }
@Test func toolchainClangPath() throws {
// Overriding the swift executable to a specific location breaks this.
guard ProcessEnv.block["SWIFT_DRIVER_SWIFT_EXEC"] == nil,
ProcessEnv.block["SWIFT_DRIVER_SWIFT_FRONTEND_EXEC"] == nil
else {
return
}
// TODO: remove this conditional check once DarwinToolchain does not requires xcrun to look for clang.
var toolchain: Toolchain
let executor = try SwiftDriverExecutor(
diagnosticsEngine: DiagnosticsEngine(),
processSet: ProcessSet(),
fileSystem: localFileSystem,
env: ProcessEnv.block
)
#if os(macOS)
toolchain = DarwinToolchain(env: ProcessEnv.block, executor: executor)
#elseif os(Windows)
toolchain = WindowsToolchain(env: ProcessEnv.block, executor: executor)
#else
toolchain = GenericUnixToolchain(env: ProcessEnv.block, executor: executor)
#endif
expectEqual(
try? toolchain.getToolPath(.swiftCompiler).parentDirectory,
try? toolchain.getToolPath(.clang).parentDirectory
)
}
@Test func executableFallbackPath() throws {
var env = ProcessEnv.block
env["SWIFT_DRIVER_TESTS_ENABLE_EXEC_PATH_FALLBACK"] = "1"
let driver = try TestDriver(args: ["swift", "main.swift"], env: env)
#expect(throws: Never.self) { try driver.toolchain.getToolPath(.dsymutil) }
}
@Test func swiftHelpOverride() async throws {
// FIXME: On Linux, we might not have any Clang in the path. We need a
// better override.
var env = ProcessEnv.block
let swiftHelp: AbsolutePath = try AbsolutePath(validating: "/usr/bin/nonexistent-swift-help")
env["SWIFT_DRIVER_SWIFT_HELP_EXEC"] = swiftHelp.pathString
env["SWIFT_DRIVER_CLANG_EXEC"] = "/usr/bin/clang"
var driver = try TestDriver(
args: ["swiftc", "-help"],
env: env
)
let jobs = try await driver.planBuild()
#expect(jobs.count == 1)
expectEqual(jobs.first!.tool.name, swiftHelp.pathString)
}
@Test func swiftClangOverride() async throws {
var env = ProcessEnv.block
let swiftClang = try AbsolutePath(validating: "/A/Path/swift-clang")
env["SWIFT_DRIVER_CLANG_EXEC"] = swiftClang.pathString
var driver = try TestDriver(
args: ["swiftc", "-emit-library", "foo.swift", "bar.o", "-o", "foo.l"],
env: env
)
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 2)
let linkJob = jobs[1]
expectEqual(linkJob.tool.name, swiftClang.pathString)
}
@Test(.skipHostOS(.darwin, comment: "Darwin always uses `clang` to link")) func swiftClangxxOverride() async throws {
var env = ProcessEnv.block
let swiftClang = try AbsolutePath(validating: "/A/Path/swift-clang")
let swiftClangxx = try AbsolutePath(validating: "/A/Path/swift-clang++")
env["SWIFT_DRIVER_CLANG_EXEC"] = swiftClang.pathString
env["SWIFT_DRIVER_CLANGXX_EXEC"] = swiftClangxx.pathString
var driver = try TestDriver(
args: [
"swiftc", "-cxx-interoperability-mode=swift-6", "-emit-library",
"foo.swift", "bar.o", "-o", "foo.l",
],
env: env
)
let jobs = try await driver.planBuild()
let linkJob = jobs.last!
expectEqual(linkJob.tool.name, swiftClangxx.pathString)
}
@Test func toolsDirectory() async throws {
try await withTemporaryDirectory { tmpDir in
let ld = try makeClangStub(in: tmpDir)
// Drop SWIFT_DRIVER_CLANG_EXEC from the environment so it doesn't
// interfere with tool lookup.
var env = ProcessEnv.block
env.removeValue(forKey: "SWIFT_DRIVER_CLANG_EXEC")
var driver = try TestDriver(
args: [
"swiftc",
"-tools-directory", tmpDir.pathString,
"foo.swift",
],
env: env
)
let frontendJobs = try await driver.planBuild().removingAutolinkExtractJobs()
expectEqual(frontendJobs.count, 2)
expectEqual(frontendJobs[1].kind, .link)
expectEqual(frontendJobs[1].tool.absolutePath!.pathString, ld.pathString)
// WASI toolchain
do {
var env = ProcessEnv.block
env["SWIFT_DRIVER_SWIFT_AUTOLINK_EXTRACT_EXEC"] = "//bin/swift-autolink-extract"
try await withTemporaryDirectory { resourceDir in
try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk"))
{
$0.send("garbage")
}
var driver = try TestDriver(
args: [
"swiftc",
"-target", "wasm32-unknown-wasi",
"-resource-dir", resourceDir.pathString,
"-tools-directory", tmpDir.pathString,
"foo.swift",
],
env: env
)
let frontendJobs = try await driver.planBuild().removingAutolinkExtractJobs()
expectEqual(frontendJobs.count, 2)
expectJobInvocationMatches(frontendJobs[1], .flag("-B"), .path(.absolute(tmpDir)))
}
}
}
}
@Test(.requireFrontendSupportsTarget("wasm32-unknown-emscripten"))
func emscriptenToolsDirectory() async throws {
try await withTemporaryDirectory { tmpDir in
_ = try makeClangStub(in: tmpDir)
var env = ProcessEnv.block
env["SWIFT_DRIVER_SWIFT_AUTOLINK_EXTRACT_EXEC"] = "//bin/swift-autolink-extract"
env["SWIFT_DRIVER_EMCC_EXEC"] = "//bin/emcc"
try await withTemporaryDirectory { resourceDir in
try localFileSystem.writeFileContents(
resourceDir.appending(components: "emscripten", "static-executable-args.lnk")
) {
$0.send("garbage")
}
var driver = try TestDriver(
args: [
"swiftc",
"-target", "wasm32-unknown-emscripten",
"-resource-dir", resourceDir.pathString,
"-tools-directory", tmpDir.pathString,
"foo.swift",
],
env: env
)
let frontendJobs = try await driver.planBuild().removingAutolinkExtractJobs()
let linkJob = frontendJobs.last!
#expect(!linkJob.commandLine.contains(.flag("-B")))
}
}
}
@Test func toolSearching() async throws {
#if os(Windows)
let PATH = ProcessEnvironmentKey("Path")
#else
let PATH = ProcessEnvironmentKey("PATH")
#endif
let SWIFT_FRONTEND_EXEC = ProcessEnvironmentKey("SWIFT_DRIVER_SWIFT_FRONTEND_EXEC")
let SWIFT_SCANNER_LIB = ProcessEnvironmentKey("SWIFT_DRIVER_SWIFTSCAN_LIB")
var baseEnv = ProcessEnv.block
baseEnv.removeValue(forKey: SWIFT_FRONTEND_EXEC)
baseEnv.removeValue(forKey: SWIFT_SCANNER_LIB)
#if os(Windows)
let separator = ";"
#else
let separator = ":"
#endif
var driver = try TestDriver(args: ["swiftc", "-print-target-info"], env: baseEnv)
let jobs = try await driver.planBuild()
#expect(jobs.count == 1)
let defaultSwiftFrontend = jobs.first!.tool.absolutePath!
try await withTemporaryDirectory { toolsDirectory in
let customSwiftFrontend = toolsDirectory.appending(component: executableName("swift-frontend"))
let customSwiftScan = toolsDirectory.appending(component: sharedLibraryName("_InternalSwiftScan"))
try localFileSystem.createSymbolicLink(customSwiftFrontend, pointingAt: defaultSwiftFrontend, relative: false)
try await withTemporaryDirectory { tempDirectory in
let fs = TestLocalFileSystem(cwd: tempDirectory)
let anotherSwiftFrontend = tempDirectory.appending(component: executableName("swift-frontend"))
try fs.createSymbolicLink(anotherSwiftFrontend, pointingAt: defaultSwiftFrontend, relative: false)
// test if SWIFT_DRIVER_TOOLNAME_EXEC is respected
await #expect(throws: Never.self) {
var env = baseEnv
env[SWIFT_FRONTEND_EXEC] = customSwiftFrontend.pathString
env[SWIFT_SCANNER_LIB] = customSwiftScan.pathString
var driver = try TestDriver(
args: ["swiftc", "-print-target-info"],
env: env,
fileSystem: fs
)
let jobs = try await driver.planBuild()
#expect(jobs.count == 1)
expectEqual(jobs.first!.tool.name, customSwiftFrontend.pathString)
}
// test if tools directory is respected
await #expect(throws: Never.self) {
var env = baseEnv
env[SWIFT_SCANNER_LIB] = customSwiftScan.pathString
var driver = try TestDriver(
args: ["swiftc", "-print-target-info", "-tools-directory", toolsDirectory.pathString],
env: env,
fileSystem: fs
)
let jobs = try await driver.planBuild()
#expect(jobs.count == 1)
expectEqual(jobs.first!.tool.name, customSwiftFrontend.pathString)
}
// test if current working directory is searched before PATH
await #expect(throws: Never.self) {
var env = baseEnv
env[PATH] = [toolsDirectory.pathString, ProcessEnv.path!].joined(separator: separator)
env[SWIFT_SCANNER_LIB] = customSwiftScan.pathString
var driver = try TestDriver(
args: ["swiftc", "-print-target-info"],
env: env,
fileSystem: fs
)
let jobs = try await driver.planBuild()
#expect(jobs.count == 1)
expectEqual(jobs.first!.tool.name, anotherSwiftFrontend.pathString)
}
}
}
}
@Test func registrarLookup() async throws {
#if os(Windows)
let SDKROOT: AbsolutePath = localFileSystem.currentWorkingDirectory!.appending(components: "SDKROOT")
let resourceDir: AbsolutePath = localFileSystem.currentWorkingDirectory!.appending(components: "swift", "resources")
let platform: String = "windows"
#if arch(x86_64)
let arch: String = "x86_64"
#elseif arch(arm64)
let arch: String = "aarch64"
#else
#error("unsupported build architecture")
#endif
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-resource-dir",
resourceDir.nativePathString(escaped: false),
])
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
expectJobInvocationMatches(
job,
.path(.absolute(resourceDir.appending(components: platform, arch, "swiftrt.obj")))
)
}
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-sdk", SDKROOT.nativePathString(escaped: false),
])
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
expectJobInvocationMatches(
job,
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))
)
}
do {
var env = ProcessEnv.block
env["SDKROOT"] = SDKROOT.nativePathString(escaped: false)
var driver = try TestDriver(
args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj",
],
env: env
)
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
expectJobInvocationMatches(
job,
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))
)
}
do {
var env = ProcessEnv.block
env["SDKROOT"] = SDKROOT.nativePathString(escaped: false)
var driver = try TestDriver(
args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-static-stdlib",
],
env: env
)
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
expectJobInvocationMatches(
job,
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrtT.obj")))
)
#expect(
!job.commandLine.contains(
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))
)
)
}
do {
var env = ProcessEnv.block
env["SDKROOT"] = SDKROOT.nativePathString(escaped: false)
var driver = try TestDriver(
args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-nostartfiles",
],
env: env
)
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
#expect(
!job.commandLine.contains(
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))
)
)
}
// Cannot test this due to `SDKROOT` escaping from the execution environment
// into the `-print-target-info` step, which then resets the
// `runtimeResourcePath` to be the SDK relative path rahter than the
// toolchain relative path.
#if false
do {
var env = ProcessEnv.block
env["SDKROOT"] = nil
var driver = try TestDriver(
args: [
"swiftc", "-emit-library", "-o", "library.dll", "library.obj",
],
env: env
)
driver.frontendTargetInfo.runtimeResourcePath = SDKROOT
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
expectEqual(job.kind, .link)
expectJobInvocationMatches(
job,
.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))
)
}
#endif
#endif
}
@Test func findingBlockLists() throws {
let execDir = try testInputsPath.appending(components: "Dummy.xctoolchain", "usr", "bin")
let list = try Driver.findBlocklists(RelativeTo: execDir)
expectEqual(list.count, 2)
#expect(list.allSatisfy { $0.extension! == "yml" || $0.extension! == "yaml" })
}
@Test func findingBlockListVersion() throws {
let execDir = try testInputsPath.appending(components: "Dummy.xctoolchain", "usr", "bin")
let version = try Driver.findCompilerClientsConfigVersion(RelativeTo: execDir)
expectEqual(version, "compilerClientsConfig-9999.99.9")
}
@Test func useStaticResourceDir() async throws {
do {
var driver = try TestDriver(args: ["swiftc", "-emit-module", "-target", "x86_64-unknown-linux", "foo.swift"])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
#expect(!job.commandLine.contains(.flag("-use-static-resource-dir")))
expectEqual(VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path).basename, "swift")
}
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-module", "-target", "x86_64-unknown-linux", "-no-static-executable", "foo.swift",
])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
#expect(!job.commandLine.contains(.flag("-use-static-resource-dir")))
expectEqual(VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path).basename, "swift")
}
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-module", "-target", "x86_64-unknown-linux", "-no-static-stdlib", "foo.swift",
])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
#expect(!job.commandLine.contains(.flag("-use-static-resource-dir")))
expectEqual(VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path).basename, "swift")
}
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-module", "-target", "x86_64-unknown-linux", "-static-executable", "foo.swift",
])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
expectJobInvocationMatches(job, .flag("-use-static-resource-dir"))
expectEqual(VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path).basename, "swift_static")
}
do {
var driver = try TestDriver(args: [
"swiftc", "-emit-module", "-target", "x86_64-unknown-linux", "-static-stdlib", "foo.swift",
])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
expectJobInvocationMatches(job, .flag("-use-static-resource-dir"))
expectEqual(VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path).basename, "swift_static")
}
}
@Test func relativeResourceDir() async throws {
do {
var driver = try TestDriver(
args: [
"swiftc",
"-target", "x86_64-unknown-linux", "-lto=llvm-thin",
"foo.swift",
"-resource-dir", "resource/dir",
]
)
let plannedJobs = try await driver.planBuild().removingAutolinkExtractJobs()
let compileJob = plannedJobs[0]
expectEqual(compileJob.kind, .compile)
try expectJobInvocationMatches(compileJob, .flag("-resource-dir"), toPathOption("resource/dir"))
let linkJob = plannedJobs[1]
#expect(linkJob.kind == .link)
try expectJobInvocationMatches(
linkJob,
.flag("-Xlinker"),
.flag("-rpath"),
.flag("-Xlinker"),
toPathOption("resource/dir/linux")
)
try expectJobInvocationMatches(linkJob, toPathOption("resource/dir/linux/x86_64/swiftrt.o"))
try expectJobInvocationMatches(linkJob, .flag("-L"), toPathOption("resource/dir/linux"))
}
}
@Test func RelativeResourceDirLinuxPrioritizedOverSDKDirForLinkingSwiftRT() async throws {
do {
let sdkRoot = try testInputsPath.appending(component: "mock-sdk.sdk")
var env = ProcessEnv.block
env["SDKROOT"] = sdkRoot.pathString
var driver = try TestDriver(
args: [
"swiftc",
"-target", "x86_64-unknown-linux", "-lto=llvm-thin",
"foo.swift",
"-resource-dir", "resource/dir",
],
env: env
)
let plannedJobs = try await driver.planBuild().removingAutolinkExtractJobs()
let compileJob = plannedJobs[0]
expectEqual(compileJob.kind, .compile)
let linkJob = plannedJobs[1]
#expect(linkJob.kind == .link)
try expectJobInvocationMatches(
linkJob,
toPathOption("resource/dir/linux/x86_64/swiftrt.o")
)
}
}
@Test func frontendTargetInfoWithWorkingDirectory() async throws {
do {
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "absolute", "path")
var driver = try TestDriver(args: [
"swiftc", "-typecheck", "foo.swift",
"-resource-dir", "resource/dir",
"-sdk", "sdk",
"-working-directory", workingDirectory.pathString,
])
let plannedJobs = try await driver.planBuild()
let job = plannedJobs[0]
try expectJobInvocationMatches(job, .path(VirtualPath(path: rebase("resource", "dir", at: workingDirectory))))
#expect(!job.commandLine.contains(.path(.relative(try .init(validating: "resource/dir")))))
expectJobInvocationMatches(job, .path(try VirtualPath(path: rebase("sdk", at: workingDirectory))))
#expect(!job.commandLine.contains(.path(.relative(try .init(validating: "sdk")))))
}
}
@Test func pluginPaths() async throws {
try await pluginPathTest(platform: "iPhoneOS", sdk: "iPhoneOS13.0", searchPlatform: "iPhoneOS")
try await pluginPathTest(platform: "iPhoneSimulator", sdk: "iPhoneSimulator15.0", searchPlatform: "iPhoneOS")
}
func pluginPathTest(platform: String, sdk: String, searchPlatform: String) async throws {
let sdkRoot = try testInputsPath.appending(
components: ["Platform Checks", "\(platform).platform", "Developer", "SDKs", "\(sdk).sdk"])
var env = ProcessEnv.block
env["PLATFORM_DIR"] = "/tmp/PlatformDir/\(platform).platform"
let workingDirectory = try AbsolutePath(validating: "/tmp")
var driver = try TestDriver(
args: [
"swiftc", "-typecheck", "foo.swift", "-sdk", VirtualPath.absolute(sdkRoot).name, "-plugin-path", "PluginA",
"-external-plugin-path", "Plugin~B#Bexe", "-load-plugin-library", "PluginB2", "-plugin-path", "PluginC",
"-working-directory", workingDirectory.nativePathString(escaped: false),
],
env: env
)
guard driver.isFrontendArgSupported(.pluginPath) && driver.isFrontendArgSupported(.externalPluginPath) else {
return
}
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(jobs.count == 1)
let job = jobs.first!
// Check that the we have the plugin paths we expect, in the order we expect.
let pluginAIndex = try #require(
job.commandLine.firstIndex(of: .path(VirtualPath.absolute(workingDirectory.appending(component: "PluginA"))))
)
let pluginBIndex = try #require(
job.commandLine.firstIndex(
of: .path(VirtualPath.absolute(workingDirectory.appending(component: "Plugin~B#Bexe")))
)
)
#expect(pluginAIndex < pluginBIndex)
let pluginB2Index = try #require(
job.commandLine.firstIndex(of: .path(VirtualPath.absolute(workingDirectory.appending(component: "PluginB2"))))
)
#expect(pluginBIndex < pluginB2Index)
let pluginCIndex = try #require(
job.commandLine.firstIndex(of: .path(VirtualPath.absolute(workingDirectory.appending(component: "PluginC"))))
)
#expect(pluginB2Index < pluginCIndex)
#if os(macOS)
let origPlatformPath =
sdkRoot.parentDirectory.parentDirectory.parentDirectory.parentDirectory
.appending(component: "\(searchPlatform).platform")
let platformPath = origPlatformPath.appending(components: "Developer", "usr")
let platformServerPath = platformPath.appending(components: "bin", "swift-plugin-server").pathString
let platformPluginPath = platformPath.appending(components: "lib", "swift", "host", "plugins")
let platformPluginPathIndex = try #require(
job.commandLine.firstIndex(of: .flag("\(platformPluginPath)#\(platformServerPath)"))
)
let platformLocalPluginPath = platformPath.appending(components: "local", "lib", "swift", "host", "plugins")
let platformLocalPluginPathIndex = try #require(
job.commandLine.firstIndex(of: .flag("\(platformLocalPluginPath)#\(platformServerPath)"))
)
#expect(platformPluginPathIndex < platformLocalPluginPathIndex)
// Plugin paths that come from the PLATFORM_DIR environment variable.
let envOrigPlatformPath = try AbsolutePath(validating: "/tmp/PlatformDir/\(searchPlatform).platform")
let envPlatformPath = envOrigPlatformPath.appending(components: "Developer", "usr")
let envPlatformServerPath = envPlatformPath.appending(components: "bin", "swift-plugin-server").pathString
let envPlatformPluginPath = envPlatformPath.appending(components: "lib", "swift", "host", "plugins")
let envPlatformPluginPathIndex = try #require(
job.commandLine.firstIndex(of: .flag("\(envPlatformPluginPath)#\(envPlatformServerPath)"))
)
#expect(envPlatformPluginPathIndex < platformPluginPathIndex)
let toolchainPluginPathIndex = try #require(
job.commandLine.firstIndex(
of: .path(
.absolute(
try driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins")
)
)
)
)
let toolchainStdlibPath = VirtualPath.lookup(driver.frontendTargetInfo.runtimeResourcePath.path)
.appending(components: driver.frontendTargetInfo.target.triple.platformName() ?? "", "Swift.swiftmodule")
let hasToolchainStdlib = try driver.fileSystem.exists(toolchainStdlibPath)
if hasToolchainStdlib {
#expect(platformLocalPluginPathIndex > toolchainPluginPathIndex)
} else {
#expect(platformLocalPluginPathIndex < toolchainPluginPathIndex)
}
#endif
#if os(Windows)
try expectJobInvocationMatches(
job,
.flag("-plugin-path"),
.path(.absolute(driver.toolchain.executableDir.parentDirectory.appending(components: "bin")))
)
#else
try expectJobInvocationMatches(
job,
.flag("-plugin-path"),
.path(
.absolute(
driver.toolchain.executableDir.parentDirectory.appending(components: "lib", "swift", "host", "plugins")
)
)
)
try expectJobInvocationMatches(
job,
.flag("-plugin-path"),
.path(
.absolute(
driver.toolchain.executableDir.parentDirectory.appending(
components: "local",
"lib",
"swift",
"host",
"plugins"
)
)
)
)
#endif
}
static let supportedEmbeddedTriples: [String] = [
"armv6-apple-none-macho",
"armv7-apple-none-macho",
"armv7em-apple-none-macho",
"arm64-apple-none-macho",
"wasm32-unknown-none-wasm",
"armv6-none-none-eabi",
"riscv32-none-none-eabi",
"x86_64-unknown-none-elf",
].filter(probeFrontendForTarget)
// Check that we pass the toolchain plugin path before the external plugin
// path for embedded targets.
@Test(.disabled(if: ToolchainTests.supportedEmbeddedTriples.isEmpty,
"Frontend does not support any of the embedded target triples"),
arguments: ToolchainTests.supportedEmbeddedTriples)
func embeddedPluginPathOrder(
triple: String
) async throws {
let sdkRoot = try testInputsPath.appending(
components: ["Platform Checks", "iPhoneOS.platform", "Developer", "SDKs", "iPhoneOS13.0.sdk"])
try await withTemporaryDirectory { resourceDir in
try localFileSystem.createDirectory(
resourceDir.appending(components: "embedded", triple, "Swift.swiftmodule"),
recursive: true)
var driver = try TestDriver(args: [
"swiftc", "-typecheck", "foo.swift",
"-target", triple,
"-enable-experimental-feature", "Embedded",
"-sdk", VirtualPath.absolute(sdkRoot).name,
"-resource-dir", resourceDir.pathString,
])
guard driver.isFrontendArgSupported(.pluginPath) else { return }
let jobs = try await driver.planBuild().removingAutolinkExtractJobs()
let job = try #require(jobs.first)
let toolchainPluginIdx = try #require(
job.commandLine.firstIndex(of: .flag("-plugin-path")),
"Expected toolchain -plugin-path to be emitted for \(triple)")
// For toolchains that emit `-external-plugin-path` (Darwin), `-plugin-path`
// must come first so resource-dir plugins win the first-emplace race in
// PluginLoader. For toolchains that don't (GenericUnix etc.), we only
// assert that the toolchain plugin path is present — there is no
// `-external-plugin-path` to compare against.
guard driver.isFrontendArgSupported(.externalPluginPath) else { return }
guard let externalPluginIdx =
job.commandLine.firstIndex(of: .flag("-external-plugin-path")) else { return }
#expect(toolchainPluginIdx < externalPluginIdx,
"-plugin-path must precede -external-plugin-path so the toolchain libSwiftMacros wins (\(triple))")
}
}
@Test func workingDirectoryForImplicitOutputs() async throws {
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "Foo", "Bar")
var driver = try TestDriver(args: [
"swiftc", "-working-directory", workingDirectory.pathString, "-emit-executable", "-c", "/tmp/main.swift",
])
let plannedJobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(plannedJobs.count == 1)
try expectJobInvocationMatches(
plannedJobs[0],
.flag("-o"),
.path(VirtualPath(path: rebase("main.o", at: workingDirectory)))
)
}
@Test func workingDirectoryForImplicitModules() async throws {
let workingDirectory = localFileSystem.currentWorkingDirectory!.appending(components: "Foo", "Bar")
var driver = try TestDriver(args: [
"swiftc", "-working-directory", workingDirectory.pathString, "-emit-module", "/tmp/main.swift",
])
let plannedJobs = try await driver.planBuild().removingAutolinkExtractJobs()
#expect(plannedJobs.count == 2)
try expectJobInvocationMatches(
plannedJobs[0],
.flag("-o"),
.path(VirtualPath(path: rebase("main.swiftmodule", at: workingDirectory)))
)
try expectJobInvocationMatches(
plannedJobs[0],
.flag("-emit-module-doc-path"),
.path(VirtualPath(path: rebase("main.swiftdoc", at: workingDirectory)))
)
try expectJobInvocationMatches(
plannedJobs[0],
.flag("-emit-module-source-info-path"),
.path(VirtualPath(path: rebase("main.swiftsourceinfo", at: workingDirectory)))
)
}
}