Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / PackLib / DarwinSDK.swift

DarwinSDK.swift132 lines · 5.1 KiBRaw
1import Foundation
2import XUtils
3import Subprocess
4import Superutils
5
6public struct DarwinSDK {
7 package struct TemporaryBundle: ~Copyable {
8 package let url: URL
9
10 fileprivate init(url: URL) {
11 self.url = url
12 }
13
14 package consuming func install() async throws {
15 guard DarwinSDK(bundle: url) != nil else {
16 throw StringError("Invalid Darwin SDK at '\(url.path)'")
17 }
18
19 try await DarwinSDK.addHostClangResourceDir(to: url)
20
21 let destination = url.deletingLastPathComponent()
22 .appendingPathComponent("darwin.artifactbundle", isDirectory: true)
23 try FileManager.default.moveItem(at: url, to: destination)
24 }
25
26 deinit {
27 try? FileManager.default.removeItem(at: url)
28 }
29 }
30
31 public enum Flavor {
32 // can't be updated in place
33 case slim
34 // can be updated in place, includes a whole copy of Xcode.app
35 case normal
36 // from before the slim/normal split existed (version "develop")
37 case legacy
38 }
39
40 public let bundle: URL
41 public let version: String
42 public let flavor: Flavor
43
44 static func swiftPMDirectory(
45 environment: [String: String] = ProcessInfo.processInfo.environment,
46 homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser
47 ) throws -> URL {
48 if let configurationDirectory = environment["XDG_CONFIG_HOME"] {
49 guard (configurationDirectory as NSString).isAbsolutePath else {
50 throw StringError("XDG_CONFIG_HOME must be an absolute path: '\(configurationDirectory)'")
51 }
52 return URL(fileURLWithPath: configurationDirectory, isDirectory: true)
53 .appendingPathComponent("swiftpm", isDirectory: true)
54 } else {
55 return homeDirectory.appendingPathComponent(".swiftpm", isDirectory: true)
56 }
57 }
58
59 private static func swiftSDKsDirectory(
60 environment: [String: String] = ProcessInfo.processInfo.environment,
61 homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser
62 ) throws -> URL {
63 try swiftPMDirectory(environment: environment, homeDirectory: homeDirectory)
64 .appendingPathComponent("swift-sdks", isDirectory: true)
65 }
66
67 public init?(bundle: URL) {
68 self.bundle = bundle
69 if let version = try? Data(contentsOf: bundle.appendingPathComponent("darwin-sdk-version.txt")) {
70 self.version = String(decoding: version, as: UTF8.self)
71 .trimmingCharacters(in: .whitespacesAndNewlines)
72 } else if ["darwin.xtoolsdk", "darwin.artifactbundle", "darwin.artifactbundle.tmp"].contains(bundle.lastPathComponent) {
73 self.version = "develop"
74 } else {
75 return nil
76 }
77
78 if version == "develop" {
79 self.flavor = .legacy
80 } else if bundle.appendingPathComponent("Xcode.app").dirExists {
81 self.flavor = .normal
82 } else {
83 self.flavor = .slim
84 }
85 }
86
87 package static func prepareTemporaryBundle(
88 environment: [String: String] = ProcessInfo.processInfo.environment,
89 homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser
90 ) throws -> TemporaryBundle {
91 let sdksDirectory = try swiftSDKsDirectory(environment: environment, homeDirectory: homeDirectory)
92 try FileManager.default.createDirectory(at: sdksDirectory, withIntermediateDirectories: true)
93 let bundle = sdksDirectory.appendingPathComponent("darwin.artifactbundle.tmp", isDirectory: true)
94 if FileManager.default.fileExists(atPath: bundle.path) {
95 try FileManager.default.removeItem(at: bundle)
96 }
97 return TemporaryBundle(url: bundle)
98 }
99
100 private static func addHostClangResourceDir(to sdk: URL) async throws {
101 struct SwiftTargetInfo: Decodable {
102 struct Paths: Decodable {
103 let runtimeResourcePath: String
104 }
105 let paths: Paths
106 }
107
108 let process = try await Subprocess.run(
109 .path(try await BuildSettings.swiftcURL()),
110 arguments: ["-print-target-info"],
111 output: .data(limit: .max)
112 ).checkSuccess()
113 let targetInfo = try JSONDecoder().decode(
114 SwiftTargetInfo.self,
115 from: process.standardOutput
116 )
117 let hostClangResources = URL(filePath: targetInfo.paths.runtimeResourcePath).appending(path: "clang")
118 let hostInclude = hostClangResources.appending(path: "include")
119 let sdkInclude = sdk.appending(path: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include")
120 try await FileManager.default.copyItem(at: hostInclude, to: sdkInclude, preserveOwner: false)
121 }
122
123 public static func current() throws -> DarwinSDK? {
124 let bundle = try swiftSDKsDirectory().appendingPathComponent("darwin.artifactbundle", isDirectory: true)
125 guard bundle.dirExists else { return nil }
126 return DarwinSDK(bundle: bundle)
127 }
128
129 public func remove() throws {
130 try FileManager.default.removeItem(at: bundle)
131 }
132}