File: MachO.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 (91 lines) | stat: -rw-r--r-- 4,157 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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

package import SWBUtil

extension MachO.Slice {
    package func targetTripleStrings(infoLookup: any PlatformInfoLookup) throws -> [String] {
        #if canImport(Darwin)
        return try buildVersions().map { $0.targetTripleString(arch: self.arch, infoLookup: infoLookup) }
        #else
        throw BinaryReaderError.parseError("Mach-O parsing not supported on this platform")
        #endif
    }
}

extension BuildVersion {
    /// Creates an LLVM target triple string for the build version info, given the architecture, and using the `minos` value as the version part of the OS field.
    package func targetTripleString(arch: String, infoLookup: any PlatformInfoLookup) -> String {
        return platform.targetTripleString(arch: arch, deploymentTarget: minOSVersion, infoLookup: infoLookup)
    }
}

extension BuildVersion.Platform {
    /// The vendor of the platform, suitable for use as the OS field of an LLVM target triple.
    package func vendor(infoLookup: any PlatformInfoLookup) -> String {
        switch self {
        case .macOS, .iOS, .iOSSimulator, .macCatalyst, .tvOS, .tvOSSimulator, .watchOS, .watchOSSimulator, .xrOS, .xrOSSimulator, .driverKit:
            return "apple"
        default:
            return infoLookup.lookupPlatformInfo(platform: self)?.llvmTargetTripleVendor ?? "unknown"
        }
    }

    /// The name of the platform, suitable for use as the OS field of an LLVM target triple.
    package func name(infoLookup: any PlatformInfoLookup) -> String {
        switch self {
        case .macOS:
            return "macos"
        case .iOS, .iOSSimulator, .macCatalyst:
            return "ios"
        case .tvOS, .tvOSSimulator:
            return "tvos"
        case .watchOS, .watchOSSimulator:
            return "watchos"
        case .xrOS, .xrOSSimulator:
            return "xros"
        case .driverKit:
            return "driverkit"
        default:
            guard let llvmTargetTripleSys = infoLookup.lookupPlatformInfo(platform: self)?.llvmTargetTripleSys else {
                  fatalError("external Mach-O based platform \(self) must provide a llvmTargetTripleSys value")
            }
            return llvmTargetTripleSys
        }
    }

    /// The variant of the platform, suitable for use as the environment field of an LLVM target triple.
    ///
    /// - returns: `nil` if the platform has no associated environment value.
    package func environment(infoLookup: any PlatformInfoLookup) -> String? {
        switch self {
        case .macCatalyst:
            return "macabi"
        case .iOSSimulator, .tvOSSimulator, .watchOSSimulator, .xrOSSimulator:
            return "simulator"
        default:
            guard let environment = infoLookup.lookupPlatformInfo(platform: self)?.llvmTargetTripleEnvironment, !environment.isEmpty else {
                return nil
            }
            if environment == "macabi" {
                // Only ".macCatalyst" should return this (e.g. macOS technically specifies its environment as "macabi" but we don't report that)
                return nil
            }
            return environment
        }
    }

    /// Creates an LLVM target triple string for the platform, given the architecture and deployment target, and the platform info lookup
    package func targetTripleString(arch: String, deploymentTarget: Version?, infoLookup: any PlatformInfoLookup) -> String {
        return [arch, vendor(infoLookup: infoLookup), name(infoLookup: infoLookup) + (deploymentTarget?.canonicalDeploymentTargetForm.description ?? ""), environment(infoLookup: infoLookup)].compactMap { $0 }.joined(separator: "-")
    }
}