forked from swiftlang/swift-build
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageProductConstructionTests.swift
More file actions
1412 lines (1329 loc) · 68.9 KB
/
Copy pathPackageProductConstructionTests.swift
File metadata and controls
1412 lines (1329 loc) · 68.9 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
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import Testing
import SWBCore
import SWBProtocol
import SWBTaskConstruction
import SWBUtil
import SWBTestSupport
/// Task construction tests related to the custom package product target.
@Suite
fileprivate struct PackageProductConstructionTests: CoreBasedTests {
/// Check the basic behaviors of the package product target.
@Test(.requireSDKs(.macOS))
func basics() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("main.c"),
TestFile("d.c"),
]),
buildConfigurations: [
TestBuildConfiguration("Release", buildSettings: [
"LIBTOOL": libtoolPath.str,
"CODE_SIGNING_ALLOWED": "NO",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestStandardTarget(
"Tool", type: .commandLineTool,
buildPhases: [
TestSourcesBuildPhase(["main.c"]),
TestFrameworksBuildPhase([
"libBEGIN.a",
// Duplicate references to the same product should be ignored.
TestBuildFile(.target("SomePackageProduct")),
TestBuildFile(.target("SomePackageProduct")),
// Duplicates *within* the product should also be ignored.
TestBuildFile(.target("OtherPackageProduct")),
// Add a package which has transitive references through a static library.
TestBuildFile(.target("PackageProductWithTransitiveRefs")),
"libEND.a"]),
],
dependencies: [
"SomePackageProduct",
"SwiftyJSON",
"PackageProductWithTransitiveRefs",
]),
TestStandardTarget(
"D", type: .staticLibrary,
buildConfigurations: [],
buildPhases: [
TestSourcesBuildPhase(["d.c"])
]),
])
let testPackage = try await TestPackageProject(
"Package",
groupTree: TestGroup(
"OtherFiles",
children: [
TestFile("foo.c"),
TestFile("libBEGIN.a"),
TestFile("libEND.a"),
]),
buildConfigurations: [
TestBuildConfiguration("Release", buildSettings: [
"LIBTOOL": libtoolPath.str,
"CODE_SIGN_IDENTITY": "",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestPackageProductTarget(
"SomePackageProduct",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("A"))]),
dependencies: ["A"]),
TestPackageProductTarget(
"OtherPackageProduct",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("A"))]),
dependencies: ["A"]),
TestPackageProductTarget(
"NestedPackagedProduct",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("B"))]),
dependencies: ["B"]),
TestPackageProductTarget(
"PackageProductWithTransitiveRefs",
frameworksBuildPhase: TestFrameworksBuildPhase([
// This is redundant in the place it is used.
TestBuildFile(.target("SomePackageProduct")),
// This is a package product which should add more transitive refs.
TestBuildFile(.target("NestedPackagedProduct")),
// This is a static library which should also add more transitive refs.
TestBuildFile(.target("C")),
// This is an object file which should also add more transitive refs.
TestBuildFile(.target("E"))]),
dependencies: ["SomePackageProduct", "NestedPackageProduct", "C", "E"]),
TestStandardTarget(
"A", type: .staticLibrary,
buildPhases: [TestSourcesBuildPhase(["foo.c"])]),
TestStandardTarget(
"B", type: .staticLibrary,
buildPhases: [TestSourcesBuildPhase(["foo.c"])]),
TestStandardTarget(
"C", type: .staticLibrary,
buildPhases: [
TestSourcesBuildPhase(["foo.c"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("C_Impl"))])],
dependencies: ["C_Impl"]),
TestStandardTarget(
"C_Impl", type: .staticLibrary,
buildPhases: [TestSourcesBuildPhase(["foo.c"])]),
TestPackageProductTarget(
"SwiftyJSON",
frameworksBuildPhase: TestFrameworksBuildPhase([]),
buildConfigurations: [],
dependencies: ["D"]),
TestStandardTarget(
"E", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase([]),
TestFrameworksBuildPhase([
TestBuildFile(.target("F"))])
], dependencies: ["F"]),
TestStandardTarget(
"F", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["foo.c"])
]),
])
let testWorkspace = TestWorkspace("aWorkspace", projects: [testProject, testPackage])
let tester = try await TaskConstructionTester(getCore(), testWorkspace)
await tester.checkBuild(BuildParameters(action: .build, configuration: "Release"), runDestination: .macOS, targetName: "Tool") { results in
results.checkWarning(.contains("missing target configuration for 'D' (in target 'D' from project 'aProject')"))
results.checkNoDiagnostics()
results.checkTarget("Tool") { target in
// Check that the product reference of a package product "sees through" it to the constituent libraries.
results.checkTask(.matchTarget(target), .matchRuleType("Ld")) { task in
#expect(task.commandLine.contains(["-lBEGIN", "/tmp/aWorkspace/Package/build/Release/libA.a", "/tmp/aWorkspace/Package/build/Release/libB.a", "/tmp/aWorkspace/Package/build/Release/libC.a", "/tmp/aWorkspace/Package/build/Release/libC_Impl.a", "-lEND"]), "unexpected linker command line: \(task.commandLineAsStrings.quotedDescription)")
}
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("Tool.LinkFileList")) { task, contents in
#expect(contents == "/tmp/aWorkspace/aProject/build/aProject.build/Release/Tool.build/Objects-normal/\(results.runDestinationTargetArchitecture)/main.o\n/tmp/aWorkspace/Package/build/Release/E.o\n/tmp/aWorkspace/Package/build/Release/F.o\n")
}
}
}
}
@Test(.requireSDKs(.macOS))
func canLinkUsingObjectOnlyFrameworkBuildPhase() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("Utility.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"CODE_SIGNING_ALLOWED": "NO",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
]),
],
targets: [
TestAggregateTarget(
"ALL",
dependencies: ["DynamicUtility", "DynamicJSON"]),
TestStandardTarget(
"DynamicUtility", type: .dynamicLibrary,
buildPhases: [
TestSourcesBuildPhase([]),
TestFrameworksBuildPhase([
TestBuildFile(.target("Utility"))
]),
],
dependencies: ["Utility"]),
TestStandardTarget(
"DynamicJSON", type: .dynamicLibrary,
buildPhases: [
TestSourcesBuildPhase([]),
TestFrameworksBuildPhase([
TestBuildFile(.target("PackageProduct::SwiftyJSON")),
]),
],
dependencies: [
"PackageProduct::SwiftyJSON",
]),
TestStandardTarget(
"Utility", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["Utility.swift"])
]),
])
let testPackage = try await TestPackageProject(
"Package",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("SwiftyJSON.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"CODE_SIGN_IDENTITY": "",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
]),
],
targets: [
TestPackageProductTarget(
"PackageProduct::SwiftyJSON",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("SwiftyJSON"))]),
dependencies: ["SwiftyJSON"]),
TestStandardTarget(
"SwiftyJSON", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"])
]),
]
)
let testWorkspace = TestWorkspace("aWorkspace", projects: [testProject, testPackage])
let tester = try await TaskConstructionTester(getCore(), testWorkspace)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("DynamicJSON") { target in
results.checkTask(.matchTarget(target), .matchRuleType("Ld")) { task in
task.checkCommandLineContains(["-o", "/tmp/aWorkspace/aProject/build/Debug/DynamicJSON.dylib", "/tmp/aWorkspace/Package/build/Package.build/Debug/SwiftyJSON.build/Objects-normal/\(results.runDestinationTargetArchitecture)/SwiftyJSON.swiftmodule"])
task.checkCommandLineNoMatch([.any, "-Xlinker", "-add_ast_path", "-Xlinker", "/tmp/aWorkspace/aProject/build/aProject.build/Debug/DynamicJSON.build/Objects-normal/\(results.runDestinationTargetArchitecture)/DynamicJSON.swiftmodule", .any])
}
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("DynamicJSON.LinkFileList")) { task, contents in
#expect(contents == "/tmp/aWorkspace/Package/build/Debug/SwiftyJSON.o\n")
}
}
}
// Check deployment build.
try await withTemporaryDirectory { tmpDir in
let parameters = BuildParameters(configuration: "Debug", overrides: [
"DSTROOT": tmpDir.join("dst").str,
"DEPLOYMENT_POSTPROCESSING": "YES",
"DEPLOYMENT_LOCATION": "YES",
])
await tester.checkBuild(parameters, runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("DynamicJSON") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SymLink")) { task in
task.checkCommandLineContains(["../UninstalledProducts/macosx/DynamicJSON.dylib", "/tmp/aWorkspace/aProject/build/Debug/DynamicJSON.dylib"])
}
}
results.checkTarget("DynamicUtility") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SymLink")) { task in
task.checkCommandLineContains(["../UninstalledProducts/macosx/DynamicUtility.dylib", "/tmp/aWorkspace/aProject/build/Debug/DynamicUtility.dylib"])
}
}
}
// Check multi arch build.
let overrides = [
"ARCHS": "x86_64 x86_64h",
"VALID_ARCHS": "$(inherited) x86_64h",
]
await tester.checkBuild(BuildParameters(configuration: "Debug", overrides: overrides), runDestination: .anyMac) { results in
results.checkNoDiagnostics()
results.checkTarget("SwiftyJSON") { target in
results.checkTask(.matchTarget(target), .matchRuleType("CreateUniversalBinary")) { task in
task.checkCommandLineContains(["/tmp/aWorkspace/Package/build/Package.build/Debug/SwiftyJSON.build/Objects-normal/x86_64/Binary/SwiftyJSON.o", "/tmp/aWorkspace/Package/build/Package.build/Debug/SwiftyJSON.build/Objects-normal/x86_64h/Binary/SwiftyJSON.o", "-output", "/tmp/aWorkspace/Package/build/Debug/SwiftyJSON.o"])
}
}
}
}
}
@Test(.requireSDKs(.macOS))
func moduleMapGeneration() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("main.swift"),
TestFile("clib.c"),
TestGroup(
"clib",
children: [
TestFile("clib.h"),
]),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestStandardTarget(
"tool", type: .commandLineTool,
buildPhases: [
TestSourcesBuildPhase(["main.swift"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("clib")),
]),
],
dependencies: [
"clib",
]),
TestStandardTarget(
"clib", type: .objectFile,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"DEFINES_MODULE": "YES",
"MODULEMAP_FILE_CONTENTS": "foo",
"MODULEMAP_PATH": "$(BUILT_PRODUCTS_DIR)/somewhere/test.modulemap",
]),
],
buildPhases: [
TestSourcesBuildPhase(["clib.c"])
]),
])
let tester = try await TaskConstructionTester(getCore(), testProject)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("clib") { target in
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("test.modulemap")) { task, contents in
#expect(contents == "foo")
}
results.checkTask(.matchTarget(target), .matchRuleType("Copy"), .matchRuleItemBasename("test.modulemap")) { task in
#expect(task.outputs.map{$0.path.str} == ["/tmp/Test/aProject/build/Debug/somewhere/test.modulemap"])
}
}
}
}
@Test(.requireSDKs(.macOS))
func unsafeFlags() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("main.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"CODE_SIGNING_ALLOWED": "NO",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
]),
],
targets: [
TestStandardTarget(
"tool", type: .commandLineTool,
buildPhases: [
TestSourcesBuildPhase(["main.swift"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("SwiftyJSON")),
]),
],
dependencies: [
"SwiftyJSON",
]),
])
let testPackage = try await TestPackageProject(
"Package",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("SwiftyJSON.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"CODE_SIGN_IDENTITY": "",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
]),
],
targets: [
TestPackageProductTarget(
"SwiftyJSON",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("PACKAGE::SwiftyJSON"))]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"USES_SWIFTPM_UNSAFE_FLAGS": "YES",
]),
],
dependencies: ["PACKAGE::SwiftyJSON"]),
TestStandardTarget(
"PACKAGE::SwiftyJSON", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"])
]),
]
)
let testWorkspace = TestWorkspace("Test", projects: [testProject, testPackage])
let tester = try await TaskConstructionTester(getCore(), testWorkspace)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkError("[targetIntegrity] The package product 'SwiftyJSON' cannot be used as a dependency of this target because it uses unsafe build flags. (in target 'tool' from project 'aProject')")
}
}
@Test(.requireSDKs(.macOS, .iOS))
func bundleLoader() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("SwiftyJSON.swift"),
TestFile("SwiftyJSONTests.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"CODE_SIGN_IDENTITY": "",
"CODE_SIGNING_ALLOWED": "NO",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
"MACOSX_DEPLOYMENT_TARGET": "10.15",
"IPHONEOS_DEPLOYMENT_TARGET": "13.0",
"SUPPORTED_PLATFORMS": "$(AVAILABLE_PLATFORMS)",
"SUPPORTS_MACCATALYST": "YES",
]),
],
targets: [
TestStandardTarget(
"SwiftJSONTests", type: .unitTest,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"BUNDLE_LOADER[sdk=macosx*]": "$(BUILT_PRODUCTS_DIR)/SwiftyJSON.app/Contents/MacOS/SwiftyJSON",
"BUNDLE_LOADER": "$(BUILT_PRODUCTS_DIR)/SwiftyJSON.app/SwiftyJSON",
])
],
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSONTests.swift"]),
],
dependencies: [
"SwiftyJSON",
]),
TestStandardTarget(
"SwiftyJSON", type: .application,
buildConfigurations: [
TestBuildConfiguration("Debug", impartedBuildProperties: TestImpartedBuildProperties(buildSettings: [
"OTHER_SWIFT_FLAGS": "-Isome/path",
]))
],
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"]),
]),
])
let tester = try await TaskConstructionTester(getCore(), testProject)
for destination in [RunDestinationInfo.iOS, .macOS, .macCatalyst] {
await tester.checkBuild(runDestination: destination) { results in
results.checkNoDiagnostics()
results.checkTarget("SwiftJSONTests") { target in
results.checkTasks(.matchTarget(target), .matchRuleType("CompileSwiftSources")) { tasks in
for task in tasks {
task.checkCommandLineContains(["-Isome/path"])
// Also check that the destination is correctly applied.
switch destination {
case .iOS:
task.checkCommandLineMatches([.suffix("-apple-ios13.0")])
case .macOS:
task.checkCommandLineMatches([.suffix("-apple-macos10.15")])
case .macCatalyst:
task.checkCommandLineMatches([.suffix("-apple-ios13.1-macabi")])
default:
assertionFailure("Unhandled destination \(destination)")
}
}
}
}
}
}
}
@Test(.requireSDKs(.macOS, .iOS))
func invalidDeploymentTargets() async throws {
let testProject = try await TestPackageProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("SwiftyJSON.swift"),
TestFile("fmwk.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"CODE_SIGN_IDENTITY": "",
"ENTITLEMENTS_REQUIRED": "NO",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
"SUPPORTS_MACCATALYST": "YES",
"SUPPORTED_PLATFORMS": "iphoneos iphonesimulator macosx",
]),
],
targets: [
TestStandardTarget(
"fmwk", type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"MACOSX_DEPLOYMENT_TARGET": "10.13",
"IPHONEOS_DEPLOYMENT_TARGET": "13.0", // NOTE: *effective* deployment target is clamped to 13.1 for Mac Catalyst
])
],
buildPhases: [
TestSourcesBuildPhase(["fmwk.swift"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("SwiftyJSON")),
]),
],
dependencies: [
"SwiftyJSON",
"SwiftyJSONImpl"
]),
TestPackageProductTarget(
"SwiftyJSON",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("PACKAGE::SwiftyJSON"))]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"MACOSX_DEPLOYMENT_TARGET": "10.14",
"IPHONEOS_DEPLOYMENT_TARGET": "13.2",
"SDKROOT": "auto",
"SDK_VARIANT": "auto",
]),
],
dependencies: ["PACKAGE::SwiftyJSON"]),
TestStandardTarget(
"PACKAGE::SwiftyJSON", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"])
]),
TestPackageProductTarget(
"SwiftyJSONImpl",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("PACKAGE::SwiftyJSONImpl"))]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"MACOSX_DEPLOYMENT_TARGET": "11.0",
"IPHONEOS_DEPLOYMENT_TARGET": "13.2",
"SDKROOT": "auto",
"SDK_VARIANT": "auto",
]),
],
dependencies: ["PACKAGE::SwiftyJSONImpl"]),
TestStandardTarget(
"PACKAGE::SwiftyJSONImpl", type: .objectFile,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"])
]),
])
let tester = try await TaskConstructionTester(getCore(), testProject)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkError("[targetIntegrity] The package product 'SwiftyJSONImpl' requires minimum platform version 11.0 for the macOS platform, but this target supports 10.13 (in target 'fmwk' from project 'aProject')")
results.checkError("[targetIntegrity] The package product 'SwiftyJSON' requires minimum platform version 10.14 for the macOS platform, but this target supports 10.13 (in target 'fmwk' from project 'aProject')")
results.checkNoDiagnostics()
}
await tester.checkBuild(runDestination: .iOS) { results in
results.checkError("[targetIntegrity] The package product 'SwiftyJSONImpl' requires minimum platform version 13.2 for the iOS platform, but this target supports 13.0 (in target 'fmwk' from project 'aProject')")
results.checkError("[targetIntegrity] The package product 'SwiftyJSON' requires minimum platform version 13.2 for the iOS platform, but this target supports 13.0 (in target 'fmwk' from project 'aProject')")
results.checkNoDiagnostics()
}
await tester.checkBuild(runDestination: .iOSSimulator) { results in
results.checkError("[targetIntegrity] The package product 'SwiftyJSONImpl' requires minimum platform version 13.2 for the iOS platform, but this target supports 13.0 (in target 'fmwk' from project 'aProject')")
results.checkError("[targetIntegrity] The package product 'SwiftyJSON' requires minimum platform version 13.2 for the iOS platform, but this target supports 13.0 (in target 'fmwk' from project 'aProject')")
results.checkNoDiagnostics()
}
await tester.checkBuild(runDestination: .macCatalyst) { results in
results.checkError("[targetIntegrity] The package product 'SwiftyJSONImpl' requires minimum platform version 13.2 for the Mac Catalyst platform, but this target supports 13.1 (in target 'fmwk' from project 'aProject')")
results.checkError("[targetIntegrity] The package product 'SwiftyJSON' requires minimum platform version 13.2 for the Mac Catalyst platform, but this target supports 13.1 (in target 'fmwk' from project 'aProject')")
results.checkNoDiagnostics()
}
}
func commandLineDynamicLibraryTarget(name: String, buildSettings: [String:String]) -> TestStandardTarget {
TestStandardTarget(
name,
type: .dynamicLibrary, // we need to choose a product type that is supported on all platforms; so tool, for example, doesn't work
buildConfigurations: [ TestBuildConfiguration("Debug", buildSettings: ["PRODUCT_NAME": name,
"ONLY_ACTIVE_ARCH": "YES",
"USE_HEADERMAP": "NO",
"CODE_SIGN_IDENTITY": "",
"ENTITLEMENTS_REQUIRED": "NO",
].merging(buildSettings, uniquingKeysWith: { a, _ in
Issue.record("should not be reached")
return a
})) ],
buildPhases: [ TestSourcesBuildPhase(["best.c"]),
TestFrameworksBuildPhase([TestBuildFile("PackageLib.o")]) ],
dependencies: ["PackageLibProduct"]
)
}
func findInput(for name: String, in tasks: GenericSequence<any PlannedTask>) -> (any PlannedNode)? {
guard let task = tasks.filter({ $0.outputs.first?.path.basename == name }).first else {
Issue.record("could not find linker tasks for \(name)")
return nil
}
guard let input = task.inputs.filter({ $0.path.basename.hasSuffix("PackageLib.o") }).first else {
Issue.record("could not find linked package lib in linker task for \(name)")
return nil
}
return input
}
@Test(.requireSDKs(.macOS))
func packageProductReferences() async throws {
let core = try await getCore()
let allPlatforms = core.platformRegistry.platforms.filter { !$0.isSimulator && core.sdkRegistry.lookup($0.name) != nil && $0.name != "none" }
#expect(allPlatforms.count > 0) // ensure we don't just pass this test because we somehow ended up with no platforms
let targets = allPlatforms.map { $0.name }.map {
commandLineDynamicLibraryTarget(name: "\($0)Lib", buildSettings: ["SDKROOT": $0])
}
let macCatalystTarget = try ProcessInfo.processInfo.hostOperatingSystem() == .macOS ? commandLineDynamicLibraryTarget(name: "MacCatalystLib", buildSettings: ["SDKROOT": "macosx", "SDK_VARIANT": MacCatalystInfo.sdkVariantName]) : nil
let almostAllTargets = targets + (macCatalystTarget.map { [$0] } ?? [])
let allTargets = [TestAggregateTarget("ALL", dependencies: almostAllTargets.map { $0.name })] + almostAllTargets.map { $0 as (any TestTarget) }
let package = TestPackageProject("aPackage", groupTree: TestGroup("Package", children: [TestFile("test.c")]), targets: [
TestPackageProductTarget(
"PackageLibProduct",
frameworksBuildPhase: TestFrameworksBuildPhase([
TestBuildFile(.target("PackageLib"))]),
buildConfigurations: [
// Targets need to opt-in to specialization.
TestBuildConfiguration("Debug", buildSettings: [
"SDKROOT": "auto",
"SDK_VARIANT": "auto",
"SUPPORTED_PLATFORMS": "$(AVAILABLE_PLATFORMS)",
]),
],
dependencies: ["PackageLib"]
),
TestStandardTarget("PackageLib", type: .objectFile,
buildConfigurations: [ TestBuildConfiguration("Debug", buildSettings: [
"PRODUCT_NAME": "PackageLib",
"SDKROOT": "auto",
"SDK_VARIANT": "auto",
"SUPPORTED_PLATFORMS": "$(AVAILABLE_PLATFORMS)",
]) ],
buildPhases: [TestSourcesBuildPhase(["test.c"])])
])
let workspace = TestWorkspace("Workspace", projects: [TestProject("aProject", groupTree: TestGroup("SomeFiles", children: [TestFile("best.c")]), targets: allTargets), package])
let tester = try await TaskConstructionTester(getCore(), workspace)
await tester.checkBuild(BuildParameters(configuration: "Debug", overrides: ["EXCLUDED_ARCHS": "i386 i686 x86_64 armv7 armv7k arm64_32 arm64e riscv64"]), runDestination: .host) { results in
results.checkNoDiagnostics()
results.checkTasks(.matchRuleType("Ld")) { tasks in
let frameworkLinkerTasks = tasks.filter { $0.outputs.first?.path.basename.hasSuffix("Lib.dylib") == true || $0.outputs.first?.path.basename.hasSuffix("Lib.so") == true }
#expect(frameworkLinkerTasks.count == allPlatforms.count + (macCatalystTarget != nil ? 1 : 0))
for platform in allPlatforms {
let dylibSuffix = core.sdkRegistry.lookup(platform.name)?.defaultVariant?.llvmTargetTripleVendor == "apple" ? "dylib" : "so"
guard let input = findInput(for: "\(platform.name)Lib.\(dylibSuffix)", in: tasks) else {
return
}
if platform.name == "macosx" {
#expect(input.path.str.hasSuffix("Debug/PackageLib.o"), "incorrect linker input path for \(platform.name): \(input.path.str)")
} else {
#expect(input.path.str.hasSuffix("Debug-\(platform.name)/PackageLib.o"), "incorrect linker input path for \(platform.name): \(input.path.str)")
}
}
if macCatalystTarget != nil {
// Check package got specialized correctly for MacCatalyst.
guard let input = findInput(for: "MacCatalystLib.dylib", in: tasks) else {
return // `findInput()` will already error if there's no matching task or input
}
#expect(input.path.str.hasSuffix("Debug\(MacCatalystInfo.publicSDKBuiltProductsDirSuffix)/PackageLib.o"), "incorrect linker input path for MacCatalyst: \(input.path.str)")
}
}
}
}
@Test(.requireSDKs(.macOS))
func linkageGraphHasAllLevelTargets() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("SwiftyJSON.swift"),
TestFile("SwiftyJSONTests.swift"),
TestFile("Network.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"CODE_SIGN_IDENTITY": "",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"SKIP_INSTALL": "YES",
"TAPI_EXEC": tapiToolPath.str,
]),
],
targets: [
TestStandardTarget(
"SwiftyJSONTests", type: .unitTest,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSONTests.swift"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("Network")),
]),
],
dependencies: [
"SwiftyJSON",
"Network",
]),
TestStandardTarget(
"SwiftyJSON", type: .application,
buildPhases: [
TestSourcesBuildPhase(["SwiftyJSON.swift"]),
TestFrameworksBuildPhase([
TestBuildFile(.target("Network")),
]),
],
dependencies: [
"Network",
]),
TestStandardTarget(
"Network", type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", impartedBuildProperties: TestImpartedBuildProperties(buildSettings: [
"OTHER_SWIFT_FLAGS": "-Isome/path",
])),
],
buildPhases: [
TestSourcesBuildPhase(["Network.swift"])
]),
])
let tester = try await TaskConstructionTester(getCore(), testProject)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("SwiftyJSONTests") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContains(["-Isome/path"])
}
}
results.checkTarget("SwiftyJSON") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContains(["-Isome/path"])
}
}
}
}
@Test(.requireSDKs(.macOS))
func objCHeaderGenerationWithModuleMapContents() async throws {
let testProject = try await TestProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("foo.swift"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"PRODUCT_NAME": "$(TARGET_NAME)",
"DEFINES_MODULE": "YES",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestStandardTarget(
"foo", type: .objectFile,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"MODULEMAP_FILE_CONTENTS": "foo",
"SWIFT_OBJC_INTERFACE_HEADER_NAME": "Foo-Swift.h",
"MODULEMAP_PATH": "$(BUILT_PRODUCTS_DIR)/somewhere/test.modulemap",
"SWIFT_OBJC_INTERFACE_HEADER_DIR": "$(BUILT_PRODUCTS_DIR)/somewhere",
]),
],
buildPhases: [
TestSourcesBuildPhase(["foo.swift"])
]),
])
let tester = try await TaskConstructionTester(getCore(), testProject)
await tester.checkBuild(runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("foo") { target in
// Make sure Swift Build doesn't append anything to the explicitly provided module map contents.
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("test.modulemap")) { task, contents in
#expect(contents == "foo")
}
results.checkTask(.matchTarget(target), .matchRuleType("Copy"), .matchRuleItemBasename("test.modulemap")) { task in
#expect(task.outputs.map{$0.path.str} == ["/tmp/Test/aProject/build/Debug/somewhere/test.modulemap"])
}
results.checkTask(.matchTarget(target), .matchRuleType("SwiftMergeGeneratedHeaders"), .matchRuleItemBasename("Foo-Swift.h")) { task in
#expect(task.outputs.map{$0.path.str} == ["/tmp/Test/aProject/build/Debug/somewhere/Foo-Swift.h"])
}
// Make sure we don't get the unextended module VFS overlay and import underlying module for pure Swift target which wants to emit a modulemap using MODULEMAP_FILE_CONTENTS setting.
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineNoMatch([.anySequence, "-import-underlying-module", "-Xcc", "-ivfsoverlay", "-Xcc", "/tmp/Test/aProject/build/aProject.build/Debug/foo.build/unextended-module-overlay.yaml", .anySequence])
}
}
}
}
@Test(.requireSDKs(.macOS))
func resourceEmbedInCodeGeneration() async throws {
let testProject = try await TestPackageProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("main.swift"),
TestFile("best.txt"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"CODE_SIGNING_ALLOWED": "NO",
"GENERATE_EMBED_IN_CODE_ACCESSORS": "YES",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestStandardTarget(
"tool",
type: .application,
buildPhases: [
TestSourcesBuildPhase(["main.swift"]),
TestCopyFilesBuildPhase([TestBuildFile(.file("best.txt"), resourceRule: .embedInCode)], destinationSubfolder: .builtProductsDir),
]
),
]
)
let tester = try await TaskConstructionTester(getCore(), testProject)
if let sourceRoot = tester.workspace.projects.first?.sourceRoot {
try FileManager.default.createDirectory(at: URL(fileURLWithPath: sourceRoot.str), withIntermediateDirectories: true)
try "hello world".write(to: URL(fileURLWithPath: "\(sourceRoot.str)/best.txt"), atomically: true, encoding: .utf8)
}
await tester.checkBuild(runDestination: .macOS) { results in
results.checkNoDiagnostics()
results.checkTarget("tool") { target in
results.checkTask(.matchTarget(target), .matchRuleType("GenerateEmbedInCodeAccessor"), .matchRuleItemBasename("embedded_resources.swift")) { task in
task.checkInputs(contain: [.namePattern(.suffix("best.txt"))])
task.checkOutputs(contain: [.namePattern(.suffix("embedded_resources.swift"))])
}
}
}
}
@Test(.requireSDKs(.macOS))
func resourceBundleAccessorGeneration() async throws {
let testProject = try await TestPackageProject(
"aProject",
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("main.swift"),
TestFile("main.m"),
TestFile("main.c"),
TestFile("Assets.xcassets"),
]),
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"ASSETCATALOG_EXEC": "$(DEVELOPER_DIR)/usr/bin/actool",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "4.2",
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"GENERATE_RESOURCE_ACCESSORS": "YES",
"USE_HEADERMAP": "NO"]),
],
targets: [
TestAggregateTarget(
"ALL",
dependencies: ["tool", "objctool", "ctool",
"tool_without_resource_bundle_without_catalog",
"tool_without_resource_bundle_with_catalog"]
),
TestStandardTarget(
"tool", type: .application,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"DEFINES_MODULE": "YES",
"PACKAGE_RESOURCE_BUNDLE_NAME": "tool_resources",
"EMBED_PACKAGE_RESOURCE_BUNDLE_NAMES": "tool_resources",
"CODE_SIGNING_ALLOWED": "NO",
]),
],
buildPhases: [
TestSourcesBuildPhase(["main.swift"]),
TestFrameworksBuildPhase([TestBuildFile(.target("toolslib"))]),
],
dependencies: ["mallory", "toolslib"]
),
TestStandardTarget(
"objctool", type: .commandLineTool,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"DEFINES_MODULE": "YES",
"PACKAGE_RESOURCE_BUNDLE_NAME": "tool_resources",
]),
],
buildPhases: [
TestSourcesBuildPhase(["main.m"]),
],
dependencies: ["mallory"]
),
TestStandardTarget(
"ctool", type: .commandLineTool,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"PRODUCT_NAME": "$(TARGET_NAME)",
"USE_HEADERMAP": "NO",
"DEFINES_MODULE": "YES",
"PACKAGE_RESOURCE_BUNDLE_NAME": "tool_resources",
]),
],
buildPhases: [
TestSourcesBuildPhase(["main.c"]),