File: EmbedSwiftStdLibTaskAction.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (905 lines) | stat: -rw-r--r-- 40,308 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public import SWBCore
import SWBLibc
import SWBUtil
import Foundation
import struct SWBProtocol.BuildOperationMetrics
import Synchronization

fileprivate func executableFileNameMatchesSwiftRuntimeLibPattern(_ fileName: String) -> Bool {
    return fileName.hasPrefix("libswift") && fileName.hasSuffix(".dylib")
}

fileprivate extension MachO {
    func allSwiftLibNames() throws -> Set<String> {
        return try Set(self.slices()
            .flatMap { try $0.linkedLibraryPaths() }
            .compactMap { (lib: String) -> String? in
                let pcs = Path(lib)
                if (pcs.dirname.str == "@rpath" || pcs.dirname == Path("/usr/lib/swift")) && executableFileNameMatchesSwiftRuntimeLibPattern(pcs.basename) {
                    return pcs.basename
                }
                else {
                    return nil
                }
        })
    }
}

fileprivate struct Executable: Hashable {
    let path: Path
    let swiftABIVersion: SwiftABIVersion?
    let linkedSwiftLibNames: Set<String>
    let uuids: [Foundation.UUID]

    var usesSwift: Bool {
        return swiftABIVersion != nil
    }

    init(path: Path, object: MachO) throws {
        self.path = path

        let swiftABIVersions = try object.slices().compactMap{ try $0.swiftABIVersion() }
        if swiftABIVersions.isEmpty {
            self.swiftABIVersion = nil
        }
        else {
            let uniqueVersions = Set(swiftABIVersions)
            if uniqueVersions.count > 1 {
                throw StubError.error("Expected a single Swift ABI version in \(path.str) but found \(swiftABIVersions)")
            }
            else {
                self.swiftABIVersion = swiftABIVersions.first!
            }
        }

        self.linkedSwiftLibNames = try object.allSwiftLibNames()
        self.uuids = try object.slices().compactMap{ try $0.uuid() }
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(path)
    }
}

/// Embeds Swift stdlibs into a bundle. Formerly known as swift-stdlib-tool, SwiftStdLibTool, CopySwiftLibs.
public final class EmbedSwiftStdLibTaskAction: TaskAction {

    // This struct contains the parsed input configuration and makes it available to its member funcs (who are non-mutating and cannot modify it)
    fileprivate struct RunningTask {
        let task: any ExecutableTask
        unowned let taskAction: TaskAction
        let dynamicExecutionDelegate: any DynamicTaskExecutionDelegate
        let executionDelegate: any TaskExecutionDelegate
        let clientDelegate: any TaskExecutionClientDelegate
        let outputDelegate: any TaskOutputDelegate

        /// Combined parent + task environment
        let effectiveEnvironment: [String: String]

        var fs : any FSProxy { return executionDelegate.fs }

        var verbose: Int = 0

        private final class OutputController: Sendable {
            /// Lock to synchronize emit output operations.
            let emitOutputLock = SWBMutex(())
        }

        private let outputController = OutputController()

        // Code signing inputs
        var codeSignIdentity: String? = nil
        var keychain: String? = nil
        var otherCodeSignFlags = [String]()

        // Executables to scan for Swift usage
        var scanExecutables = Set<Executable>()

        // Directories to scan for more executables.
        // --scan-folder
        var scanDirs = Set<Path>()

        // Platform name.
        // --platform
        // or the last path component of --source-libraries
        var platform: String? = nil

        // --toolchain
        var toolchainsDirs = OrderedSet<Path>()

        // Copy source.
        // --source-libraries
        // or /path/to/swift-stdlib-tool/../../lib/swift/<--platform>
        var srcDir: Path? = nil

        // Copy destinations, signed and unsigned.
        // --destination and --unsigned-destination
        var dstDir: Path?
        var unsignedDstDir: Path? = nil

        // Resource copy destination.
        // --resource-destination
        var resourceDstDir: Path? = nil

        // Resource libraries.
        // --resource-library
        var resourceLibraries = Set<String>()

        var shouldPrint = false
        var shouldCopy = false

        // Bitcode is no longer supported, but some old libraries may contain bitcode, so we continue to strip it when directed.
        var shouldStripBitcode = false
        var bitcodeStripPath: Path? = nil

        // Effective search path for Swift libraries.
        var srcDirs = OrderedSet<Path>()

        // Path of file to which discovered dependencies should be written.
        var discoveredDepsOutputFile: Path? = nil

        /// If this is set, we should:
        /// - Ignore Swift ABI versions when we look for dependencies
        /// - Emit warnings instead of errors when we detect an ABI mismatch in embedded binaries
        let ignoreABIVersion: Bool

        // If true, then the only swift libs that should be processed are those known to not exist in the SwiftOS install.
        var filterForSwiftOS = false

        // If true, then the Swift concurrency dylibs should be copied into the app/framework's bundles.
        var backDeploySwiftConcurrency = false

        // If true, then the Swift Span dylibs should be copied into the app/framework's bundles.
        var backDeploySwiftSpan = false

        // The allowed list of libraries that should *not* be filtered when `filterForSwiftOS=true`.
        let allowedLibsForSwiftOS = ["libswiftXCTest" ]

        // The allowed list of libraries that should *not* be filtered when `backDeploySwiftConcurrency=true`.
        let allowedLibsForSwiftConcurrency = ["libswift_Concurrency"]

        // The allowed list of libraries that should *not* be filtered when `backDeploySwiftSpan=true`.
        let allowedLibsForSwiftSpan = ["libswiftCompatibilitySpan"]

        func absolutePath(_ path: Path) -> Path {
            return path.isAbsolute ? path : task.workingDirectory.join(path)
        }

        func logV(_ msg : @autoclosure () -> String) {
            if verbose > 0 {
                outputController.emitOutputLock.withLock {
                    outputDelegate.emitOutput { $0 <<< msg() <<< "\n" }
                }
            }
        }

        func logVV(_ msg : @autoclosure () -> String) {
            if verbose > 1 {
                outputController.emitOutputLock.withLock {
                    outputDelegate.emitOutput { $0 <<< msg() <<< "\n" }
                }
            }
        }

        /// - returns: `nil` if the file doesn't exist or isn't an executable.
        /// - throws if the file exists but can't be read
        func executableIfValid(path: Path) throws -> Executable? {
            guard try fs.exists(path) && !fs.isDirectory(path) && fs.isExecutable(path) else { return nil }

            do {
                let object = try MachO(data: fs.read(path))
                return try Executable(path: path, object: object)
            } catch BinaryReaderError.unrecognizedFileType {
                // Don't log any warnings if the file isn't a Mach-O, because that isn't an error case.
                return nil
            } catch let error as BinaryReaderError {
                // Log warnings if we *did* start parsing a Mach-O, and then it turned out to be corrupted.
                logV("warning: \(path.str): Failed to parse executable: \(error)")
                return nil
            }
        }

        func effectiveSourceDirectories(_ toolchainsDirs: OrderedSet<Path>, platform: String) -> [Path] {
            // FIXME: Maybe these should be defined within the toolchains or we could simply scan the toolchain directory as well.
            let swiftBackdeploymentDirs = ["usr/lib/swift-5.0", "usr/lib/swift-5.5", "usr/lib/swift-6.2"]

            var dirs = [Path]()
            for dir in toolchainsDirs {
                for path in swiftBackdeploymentDirs {
                    dirs.append(dir.join(path).join(platform))
                }
            }

            return dirs
        }

        init(task: any ExecutableTask, taskAction: TaskAction, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, executionDelegate: any TaskExecutionDelegate, clientDelegate: any TaskExecutionClientDelegate, outputDelegate: any TaskOutputDelegate) async throws {
            self.task = task
            self.taskAction = taskAction
            self.dynamicExecutionDelegate = dynamicExecutionDelegate
            self.executionDelegate = executionDelegate
            self.clientDelegate = clientDelegate
            self.outputDelegate = outputDelegate
            self.effectiveEnvironment = {
                var env = executionDelegate.environment ?? [:]
                env.addContents(of: task.environment.bindingsDictionary)
                return env
            }()

            // Read arguments
            let argsIter = task.commandLineAsStrings.makeIterator()
            precondition(argsIter.next() == "builtin-swiftStdLibTool")

            // The linker checks for the existence of this env var; its value doesn't matter
            self.ignoreABIVersion = self.effectiveEnvironment["LD_WARN_ON_SWIFT_ABI_VERSION_MISMATCHES"] != nil

            while true {
                guard let arg = argsIter.next() else { break }

                func argParam() throws -> String {
                    guard let p = argsIter.next() else { throw StubError.error("Failed to parse arguments: \(arg) requires an argument") }
                    return p
                }

                func setSingleOccurrence<T>(_ result: inout T?, _ getValue : @autoclosure () throws -> T) throws {
                    guard result == nil else { throw StubError.error("Failed to parse arguments: expected a single \(arg) argument") }
                    result = try getValue()
                }

                switch arg {
                case "--print":
                    shouldPrint = true

                case "--copy":
                    shouldCopy = true

                case "--verbose":
                    verbose += 1

                case "--scan-executable":
                    // --scan-executable <path>
                    // Scan the executable at <path> for references to Swift libraries.
                    // This option may be set multiple times.
                    let path = absolutePath(Path(try argParam()))
                    if let exe = try executableIfValid(path: path) {
                        scanExecutables.insert(exe)
                    }
                    else {
                        logV("Failed to scan executable: \(path.str)")
                    }

                case "--scan-folder":
                    // --scan-folder <path>
                    // Scan any executables inside <path> for references to Swift libraries.
                    // This option may be set multiple times.
                    scanDirs.insert(absolutePath(Path(try argParam())))

                case "--source-libraries":
                    // --source-libraries <path>
                    // Search <path> for Swift libraries.
                    // The default is /path/to/swift-stdlib-tool/../../lib/swift/<platform>/
                    var srcDir = self.srcDir
                    try setSingleOccurrence(&srcDir, absolutePath(Path(argParam())))
                    self.srcDir = srcDir

                case "--platform":
                    // --platform <macosx|iphoneos|iphonesimulator>
                    // Use the Swift libraries for <platform>.
                    try setSingleOccurrence(&platform, argParam())

                case "--toolchain":
                    // --toolchain <path> ...
                    // Find matching Swift libraries in any of the above toolchains.
                    toolchainsDirs.append(absolutePath(Path(try argParam())))

                case "--destination":
                    // --destination <path>
                    // Copy Swift libraries into <path>.
                    var dstDir = self.dstDir
                    try setSingleOccurrence(&dstDir, absolutePath(Path(argParam())))
                    self.dstDir = dstDir

                case "--unsigned-destination":
                    // --unsigned-destination <path>
                    // Copy Swift libraries into <path> without signing them.
                    var unsignedDstDir = self.unsignedDstDir
                    try setSingleOccurrence(&unsignedDstDir, absolutePath(Path(argParam())))
                    self.unsignedDstDir = unsignedDstDir

                case "--sign":
                    // --sign <identity>
                    // Sign copied Swift libraries using <identity>.
                    try setSingleOccurrence(&codeSignIdentity, argParam())

                case "--keychain":
                    // --keychain <keychain>
                    // Search <keychain> for the code signing identity.
                    try setSingleOccurrence(&keychain, argParam())

                case "--Xcodesign":
                    // --Xcodesign <option>
                    // Pass <option> to the codesign tool.
                    otherCodeSignFlags.append(try argParam())

                case "--strip-bitcode":
                    // --strip-bitcode
                    // Remove embedded bitcode from libraries copied to --destination.
                    // Libraries copied to --unsigned-destination are unmodified.
                    shouldStripBitcode = true

                case "--strip-bitcode-tool":
                    var bitcodeStripPath = self.bitcodeStripPath
                    try setSingleOccurrence(&bitcodeStripPath, absolutePath(Path(argParam())))
                    self.bitcodeStripPath = bitcodeStripPath

                case "--resource-library":
                    // --resource-library <library>
                    // Copy <library> and its dependencies as resources without signing
                    // them. These copies are in addition to any libraries copied as a result
                    // of the --scan-executable option.
                    // Any library in the Swift library search path can be specified for
                    // <library>.
                    // This option may be set multiple times.
                    resourceLibraries.insert(try argParam())

                case "--resource-destination":
                    // --resource-destination <path>
                    // The <path> to copy Swift resource libraries to.
                    var resourceDstDir = self.resourceDstDir
                    try setSingleOccurrence(&resourceDstDir, absolutePath(Path(argParam())))
                    self.resourceDstDir = resourceDstDir

                case "--emit-dependency-info":
                    // --emit-dependency-info <path>
                    // Writes a file to <path> containing paths of any discovered dependencies.
                    var discoveredDepsOutputFile = self.discoveredDepsOutputFile
                    try setSingleOccurrence(&discoveredDepsOutputFile, absolutePath(Path(argParam())))
                    self.discoveredDepsOutputFile = discoveredDepsOutputFile

                case "--filter-for-swift-os":
                    // Remove all libraries found except for those that are not present in swift in the os.
                    self.filterForSwiftOS = true

                case "--back-deploy-swift-concurrency":
                    self.backDeploySwiftConcurrency = true

                case "--back-deploy-swift-span":
                    self.backDeploySwiftSpan = true

                default:
                    throw StubError.error("unrecognized argument: \(arg)")
                }
            }

            if shouldStripBitcode && bitcodeStripPath == nil {
                throw StubError.error("Passed --strip-bitcode without --strip-bitcode-tool.")
            }

            if srcDir != nil && toolchainsDirs.count > 0 {
                logV("Ignoring --toolchain paths because --source-libraries was set.")
                toolchainsDirs.removeAll(keepingCapacity: false)
            }

            // Fix up srcDir and platform values.
            if srcDir == nil && platform == nil {
                throw StubError.error("at least one of --source-libraries and --platform must be set")
            }
            else if let srcDir = srcDir, platform == nil {
                // src_dir is set but platform is not.
                // Pick platform from src_dir's name.
                platform = srcDir.basename
            }

            srcDirs = srcDir != nil
                ? OrderedSet([srcDir!])
                : OrderedSet(effectiveSourceDirectories(toolchainsDirs, platform: platform!))
            logVV("Effective srcDirs:\n\(srcDirs.elements.map{$0.str}.joined(separator: "\n"))")

            // Add the platform to unsigned_dst_dir if it is not already present.
            if let unsignedDstDir, platform != unsignedDstDir.basename {
                self.unsignedDstDir = unsignedDstDir.join(platform!)
            }

            // If the user specifies --strip-bitcode but not --sign, this
            // will cause the dylibs to get copied, stripped, but not resigned.
            // This will cause apps to fail to launch because the code signature
            // is invalid.  In this case, ignore --strip-bitcode.
            if shouldStripBitcode && codeSignIdentity == nil {
                logV("Ignoring --strip-bitcode because --sign was not passed")
                shouldStripBitcode = false
            }

            let testLibrarySuffixes = await executionDelegate.requestContext.getKnownTestingLibraryPathSuffixes()

            // Collect executables from the --scan-folder locations.
            for embedDir in scanDirs {
                guard fs.exists(embedDir) else { continue }

                try fs.traverse(embedDir) { path throws -> Void in
                    // Skip embedded XCTest bundles when scanning folders.
                    if path.str.contains(".xctest/") {
                        logVV("Skipping \(path.str) because it is part of an XCTest bundle")
                        return
                    }

                    // Skip test support libraries (e.g. Testing, XCTest, and supporting libraries/frameworks) when scanning folders, since they are copied by test targets which depend on this target, and scanning them (beyond being unnecessary) will lead to dependency cycles.
                    guard !testLibrarySuffixes.contains(where: path.ends(with:)) else {
                        logVV("Skipping \(path.str) because it is an XCTest support library")
                        return
                    }

                    // Skip the Swift libraries which this tool itself copies.
                    guard !executableFileNameMatchesSwiftRuntimeLibPattern(path.basename) else {
                        logVV("Skipping \(path.str) because it is a Swift standard library copied by this tool")
                        return
                    }

                    // Skip temporaries created by `codesign`. These may have disappeared by the time we try to read them, and will never be executables.
                    guard path.fileExtension != "cstemp" else {
                        return
                    }

                    guard let exe = try executableIfValid(path: path) else {
                        logVV("Skipping \(path.str) because it is not an executable file")
                        return
                    }

                    // Skip this binary if it doesn't use Swift.
                    guard exe.usesSwift else {
                        logVV("Skipping \(path.str) because it is not a Swift executable file")
                        return
                    }

                    // If we get here then we can inset the executable in the list of those to scan.
                    scanExecutables.insert(exe)
                }
            }
        }

        func runProcess(_ args: [String]) async throws -> ByteString {
            logV(args.joined(separator: " "))

            final class CapturingOutputDelegate: TaskOutputDelegate {
                func incrementClangCacheHit() {
                    // TBD
                }

                func incrementClangCacheMiss() {
                    // TBD
                }
                func incrementSwiftCacheHit() {}
                func incrementSwiftCacheMiss() {}
                func incrementTaskCounter(_ counter: BuildOperationMetrics.TaskCounter) {}

                var counters: [BuildOperationMetrics.Counter : Int] = [:]
                var taskCounters: [BuildOperationMetrics.TaskCounter : Int] = [:]

                let underlyingDelegate: any TaskOutputDelegate
                var output = ByteString()

                init(outputDelegate underlyingDelegate: any TaskOutputDelegate) {
                    self.underlyingDelegate = underlyingDelegate
                }

                var startTime: Date {
                    underlyingDelegate.startTime
                }

                func emitOutput(_ data: SWBUtil.ByteString) {
                    output += data
                }

                func updateResult(_ result: SWBCore.TaskResult) {
                    underlyingDelegate.updateResult(result)
                }

                func subtaskUpToDate(_ subtask: any SWBCore.ExecutableTask) {
                    underlyingDelegate.subtaskUpToDate(subtask)
                }

                func previouslyBatchedSubtaskUpToDate(signature: SWBUtil.ByteString, target: SWBCore.ConfiguredTarget) {
                    underlyingDelegate.previouslyBatchedSubtaskUpToDate(signature: signature, target: target)
                }

                var result: SWBCore.TaskResult? {
                    underlyingDelegate.result
                }

                var diagnosticsEngine: SWBCore.DiagnosticProducingDelegateProtocolPrivate<SWBUtil.DiagnosticsEngine> {
                    underlyingDelegate.diagnosticsEngine
                }
            }

            let capturingDelegate = CapturingOutputDelegate(outputDelegate: outputDelegate)
            let processDelegate = TaskProcessDelegate(outputDelegate: capturingDelegate)
            try await taskAction.spawn(commandLine: args, environment: effectiveEnvironment, workingDirectory: task.workingDirectory, dynamicExecutionDelegate: dynamicExecutionDelegate, clientDelegate: clientDelegate, processDelegate: processDelegate)
            if let error = processDelegate.executionError {
                throw StubError.error(error)
            }
            let output = capturingDelegate.output
            let failed = (processDelegate.commandResult ?? .failed) != .succeeded
            if failed || verbose > 1 {
                logV(output.asString)
            }

            guard !failed else {
                throw RunProcessNonZeroExitError(args: args, workingDirectory: task.workingDirectory, environment: .init(effectiveEnvironment), status: {
                    if case let .exit(exitStatus, _) = processDelegate.outputDelegate.result {
                        return exitStatus
                    }
                    return .uncaughtSignal(0)
                }(), mergedOutput: output)
            }

            return output
        }

        func copyAndStripBitcode(src: Path, dst: Path) async throws {
            let _ = try await runProcess([bitcodeStripPath!.str] + [src.str, "-r", "-o", dst.str])
        }

        func copyFile(src: Path, dst: Path, stripBitcode: Bool) async throws {
            if stripBitcode {
                try await copyAndStripBitcode(src: src, dst: dst)
            }
            else {
                try fs.copy(src, to: dst)
            }
        }

        func copyLibraries(dstDir: Path, libs: Set<Executable>, stripBitcode: Bool, discoveredDependencyInfo: inout DependencyInfo) async throws {
            // If we are asked to copy a set of libs that are empty, then do nothing here. This is important due to: <rdar://problem/48292950>.
            guard !libs.isEmpty else { return }

            try fs.createDirectory(dstDir, recursive: true)

            for srcExe in libs {
                let srcPath = srcExe.path
                let dstPath = dstDir.join(srcPath.basename)

                // Compare UUIDs of src and dst and don't copy if they're the same.
                // Do not use mod times for this task: the dst copy gets code-signed
                // and bitcode-stripped so it can look newer than it really is.
                let dstExe = try executableIfValid(path: dstPath)

                logVV("Source UUIDs \(srcPath.str): \(srcExe.uuids)")
                logVV("Destination UUIDs \(dstPath.str): \(String(describing: dstExe?.uuids))")

                if let dstUUIDs = dstExe?.uuids {
                    if !dstUUIDs.isEmpty && srcExe.uuids == dstUUIDs {
                        logV("\(srcExe.path.basename) is up to date at \(dstPath.str)")
                        continue
                    }
                }

                // Perform the copy.

                logV("Copying \(srcPath.str) to \(dstPath.str)")

                if fs.exists(dstPath) {
                    try fs.remove(dstPath)
                }

                discoveredDependencyInfo.inputs.append(srcPath.str)
                discoveredDependencyInfo.outputs.append(dstPath.str)
                try await copyFile(src: srcPath, dst: dstPath, stripBitcode: stripBitcode)
            }
        }


        func queryCodeSignature(codesign: Path, _ file: Path) async throws -> ByteString {
            logV("Probing signature of \(file.str)")
            return try await runProcess([codesign.str, "-r-", "--display", file.str])
        }

        func codeSignLibrary(codesign: Path, dst: Path) async throws {
            let tmpFilePath = Path(dst.str + ".original")
            defer {
                if fs.exists(tmpFilePath) {
                    do {
                        try fs.remove(tmpFilePath)
                    }
                    catch {
                        logV("Failed to remove: '\(tmpFilePath.str)': \(error)")
                    }
                }
            }

            // Get the code signature, and copy the dylib to the side
            // to preserve it in case it does not change.  We can use
            // this to avoid unnecessary copies during delta installs
            // to devices.
            // FIXME: The build system should handle this.
            let oldSignatureData = try? await queryCodeSignature(codesign: codesign, dst)

            if oldSignatureData != nil {
                // Make a copy of the existing file, with permissions and mtime preserved.
                if fs.exists(tmpFilePath) { try fs.remove(tmpFilePath) }
                try fs.copy(dst, to: tmpFilePath)
            }

            // Proceed with (re-)codesigning.
            logV("Codesigning \(dst.str)")

            // Build the codesign invocation.
            var arguments = ["--force", "--sign", codeSignIdentity!, "--verbose"]

            if let k = keychain {
                arguments.append(contentsOf: ["--keychain", k])
            }

            // Other codesign flags come later so they can override the default flags.
            arguments.append(contentsOf: otherCodeSignFlags)

            arguments.append(dst.str)

            let _ = try await runProcess([codesign.str] + arguments)

            // If we have an existing code signature data, query the new one and compare
            // it with the code signature of the file before we re-signed it.
            // If they are the same, use the original file instead.  This preserves
            // the contents of the file and mtime for use with delta installs.
            if let oldSignatureData {
                let newSignatureData = try await queryCodeSignature(codesign: codesign, dst)

                if oldSignatureData == newSignatureData {
                    logV("Code signature of \(dst.str) is unchanged; keeping original")

                    // The two signatures match.  Unlink the new file, and re-link the old file.
                    if fs.exists(dst) {
                        try fs.remove(dst)
                    }

                    try fs.move(tmpFilePath, to: dst)
                }
            }
        }

        func findSwiftLib(srcDirs: OrderedSet<Path>, name: String, swiftVersion: SwiftABIVersion, isOptional: Bool) throws -> Executable? {
            var foundNameMatches: [Executable] = []

            for srcDir in srcDirs {
                let src = srcDir.join(name)
                if let exe = try executableIfValid(path: src) {
                    if ignoreABIVersion {
                        return exe
                    }

                    // Some of the Swift runtime libs don't use Swift, so we allow either an empty swiftVersion or a swiftVersion which satisfies the requested swiftVersion.
                    if !exe.usesSwift || exe.swiftABIVersion == swiftVersion {
                        return exe
                    }
                    else {
                        foundNameMatches.append(exe)
                    }
                }
            }

            if foundNameMatches.count > 0 {
                for match in foundNameMatches {
                    logV("Found library with mismatched Swift ABI version: \(match.path.str) \(String(describing: match.swiftABIVersion)) (requested \(swiftVersion))")
                }
            }

            if !isOptional {
                let versionStr = ignoreABIVersion ? "" : " for Swift ABI version \(swiftVersion)"
                throw StubError.error("Could not find \(name)\(versionStr)")
            }
            else {
                return nil
            }
        }

        func collectTransitiveDependencies(srcDirs: OrderedSet<Path>, executables: Set<Executable>, swiftVersion: SwiftABIVersion, requireAllDependencies: Bool, discoveredDependencyInfo: inout DependencyInfo) throws -> Set <Executable>
        {
            var worklist = Array(executables)
            var result = Set<Executable>()
            var consideredLibNames = Set<String>()

            while worklist.count > 0 {
                let exe = worklist.popLast()!
                consideredLibNames.insert(exe.path.basename)

                if !executableFileNameMatchesSwiftRuntimeLibPattern(exe.path.basename) {
                    discoveredDependencyInfo.inputs.append(exe.path.str)
                }

                for libName in exe.linkedSwiftLibNames {
                    if consideredLibNames.contains(libName) {
                        continue
                    }

                    consideredLibNames.insert(libName)

                    if let dep = try findSwiftLib(srcDirs: srcDirs, name: libName, swiftVersion: swiftVersion, isOptional: !requireAllDependencies) {
                        result.insert(dep)
                        worklist.append(dep)
                    }
                }
            }

            return result
        }

        func main() async throws -> DependencyInfo {

            // Pick a Swift version that all executables have to agree on.
            let swiftVersionOpt = try scanExecutables.reduce(nil) { (memo: (SwiftABIVersion, Executable)?, newExe: Executable) throws -> (SwiftABIVersion, Executable)? in
                switch  (memo?.0,                   newExe.swiftABIVersion) {
                case    (_,                         nil):
                    return memo

                case    (nil,                       .some(let newVersion)):
                    return (newVersion, newExe)

                case    (.some(let prevVersion),    .some(let newVersion)):
                    guard prevVersion != newVersion else { return memo }

                    let mismatch = scanExecutables.first { object -> Bool in
                        return object.swiftABIVersion != nil && object.swiftABIVersion! != newVersion
                        }!

                    let paths = [mismatch.path.str, newExe.path.str].sorted()
                    let message = "The following binaries use incompatible versions of Swift:\n\(paths.joined(separator: "\n"))"
                    if ignoreABIVersion {
                        self.outputDelegate.emitWarning(message)
                        return memo
                    }
                    else {
                        throw StubError.error(message)
                    }
                }
                }?.0

            // Discovered dependency paths, collected during processing, emitted at the end.
            var discoveredDependencyInfo = DependencyInfo(version: "swift-stdlib-tool")
            discoveredDependencyInfo.inputs.append(contentsOf: scanExecutables.map { $0.path.str })

            guard let swiftVersion = swiftVersionOpt else {
                logV("Exiting early, found no Swift version in executables.")
                return discoveredDependencyInfo
            }
            if !ignoreABIVersion {
                // Let's only show the ABI version when this is an unstable ABI being used.
                if case .unstable(_) = swiftVersion {
                    logV("Requested Swift ABI version based on scanned binaries: \(swiftVersion)")
                }
            }

            // Collect Swift library names from the input files and follow dependencies recursively.
            let dependencies = try collectTransitiveDependencies(srcDirs: srcDirs,
                                                                 executables: scanExecutables,
                                                                 swiftVersion: swiftVersion,
                                                                 requireAllDependencies: false, // If the library does not exist in srcDirs then assume the user wrote their own library named libswift* and is handling it elsewhere.
                                                                 discoveredDependencyInfo: &discoveredDependencyInfo)

            // The list of dependencies needs to be pruned based on the filtering mechanism. Under normal circumstances, no libraries are expected to be allowed.
            let swiftLibs = dependencies
                .filter {
                    let item = $0.path.basenameWithoutSuffix

                    var shouldInclude = true
                    if filterForSwiftOS && !allowedLibsForSwiftOS.contains(item) {
                        shouldInclude = false
                    }
                    if backDeploySwiftConcurrency && allowedLibsForSwiftConcurrency.contains(item) {
                        shouldInclude = true
                    }
                    if backDeploySwiftSpan && allowedLibsForSwiftSpan.contains(item) {
                        shouldInclude = true
                    }

                    return shouldInclude
                }

            // Collect all the Swift libraries that the user requested with --resource-library.

            let resourceLibrariesExecutables = try Set(resourceLibraries.map{ obj throws -> Executable in
                return try findSwiftLib(srcDirs: srcDirs, name: obj, swiftVersion: swiftVersion, isOptional: false)!
            })

            let swiftLibsForResources: Set<Executable> = try collectTransitiveDependencies(
                srcDirs: srcDirs,
                executables: resourceLibrariesExecutables,
                swiftVersion: swiftVersion,
                requireAllDependencies: true, // These are system libraries and they should be complete.
                discoveredDependencyInfo: &discoveredDependencyInfo
            ).union(resourceLibrariesExecutables)

            // Print the Swift libraries (full path to toolchain's copy)
            if shouldPrint {
                for lib in swiftLibs {
                    outputDelegate.emitNote(lib.path.str)
                }
            }

            // Copy the Swift libraries to $build_dir/$frameworks
            // and $build_dir/$unsigned_frameworks
            if shouldCopy {
                try await copyLibraries(dstDir: dstDir!, libs: swiftLibs, stripBitcode: shouldStripBitcode, discoveredDependencyInfo: &discoveredDependencyInfo)

                if unsignedDstDir != nil {
                    // Never strip bitcode from the unsigned libraries.
                    // Their existing signatures must be preserved.
                    try await copyLibraries(dstDir: unsignedDstDir!, libs: swiftLibs, stripBitcode: false, discoveredDependencyInfo: &discoveredDependencyInfo)
                }

                if let resourceDstDir = resourceDstDir {
                    // Never strip bitcode from resources libraries, for
                    // the same reason as the libraries copied to
                    // unsigned_dst_dir.
                    try await copyLibraries(dstDir: resourceDstDir, libs: swiftLibsForResources, stripBitcode: false, discoveredDependencyInfo: &discoveredDependencyInfo)
                }
            }

            // Codesign the Swift libraries in $build_dir/$frameworks
            // but not the libraries in $build_dir/$unsigned_frameworks.
            if codeSignIdentity != nil && !swiftLibs.isEmpty {
                let codesign = Path(self.effectiveEnvironment["CODESIGN"] ?? "/usr/bin/codesign")

                // Swift libraries that are up-to-date get codesigned anyway
                // (in case options changed or a previous build was incomplete).

                // Work around authentication UI problems (rdar://23019128)
                // by signing one synchronously and then signing the rest.
                let swiftLibsArray = Array(swiftLibs)
                if let lib = swiftLibsArray.first {
                    try await codeSignLibrary(codesign: codesign, dst: dstDir!.join(lib.path.basename))
                }

                let swiftLibsRest = swiftLibsArray.dropFirst()

                try await withThrowingTaskGroup(of: Void.self) { group in
                    for lib in swiftLibsRest {
                        group.addTask {
                            try await self.codeSignLibrary(codesign: codesign, dst: self.dstDir!.join(lib.path.basename))
                        }
                    }
                    try await group.waitForAll()
                }
            }

            return discoveredDependencyInfo
        }
    }

    override public class var toolIdentifier: String {
        return "embed-swift-stdlib"
    }

    override public func performTaskAction(
        _ task: any ExecutableTask,
        dynamicExecutionDelegate: any DynamicTaskExecutionDelegate,
        executionDelegate: any TaskExecutionDelegate,
        clientDelegate: any TaskExecutionClientDelegate,
        outputDelegate: any TaskOutputDelegate
    ) async -> CommandResult {
        do {
            let rt = try await RunningTask(task: task, taskAction: self, dynamicExecutionDelegate: dynamicExecutionDelegate, executionDelegate: executionDelegate, clientDelegate: clientDelegate, outputDelegate: outputDelegate)
            let discoveredDependencyInfo = try await rt.main()

            // Write out the discovered dependencies, if we've been asked to.
            if let discoveredDepsOutputFile = rt.discoveredDepsOutputFile {
                try rt.fs.write(discoveredDepsOutputFile, contents: ByteString(discoveredDependencyInfo.normalized().asBytes()))
            }

            return .succeeded
        }
        catch {
            outputDelegate.emitError("\(error)")
            return .failed
        }
    }
}