peachsim 0.1.1881 → 0.1.1887

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/cli/commands/control.ts +11 -10
  2. package/cli/commands/reset.ts +1 -1
  3. package/cli/native-input.ts +1 -1
  4. package/cli/outbound-endpoints.ts +1 -1
  5. package/detox/index.ts +62 -1
  6. package/dist-lib/agent-daemon-client.cjs +1 -1
  7. package/dist-lib/agent-events.cjs +1 -1
  8. package/dist-lib/attached-projects.cjs +1 -1
  9. package/dist-lib/auth/shared-session.cjs +1 -1
  10. package/dist-lib/backend-origin.cjs +1 -1
  11. package/dist-lib/beta.cjs +1 -1
  12. package/dist-lib/beta.mjs +1 -1
  13. package/dist-lib/bridge-constants.cjs +1 -1
  14. package/dist-lib/bridge-contract-input.cjs +1 -1
  15. package/dist-lib/bridge-contract-input.mjs +1 -1
  16. package/dist-lib/bridge-contract.cjs +1 -1
  17. package/dist-lib/bridge-contract.mjs +1 -1
  18. package/dist-lib/bundle-channel.cjs +9079 -4305
  19. package/dist-lib/capture-contract.cjs +1 -1
  20. package/dist-lib/capture-contract.mjs +1 -1
  21. package/dist-lib/cloud-contract.cjs +1 -1
  22. package/dist-lib/cloud-contract.mjs +1 -1
  23. package/dist-lib/cloud-test-args.cjs +1 -1
  24. package/dist-lib/cloud-test-args.mjs +1 -1
  25. package/dist-lib/cloud.cjs +1 -1
  26. package/dist-lib/cloud.mjs +1 -1
  27. package/dist-lib/config.cjs +1 -1
  28. package/dist-lib/config.mjs +1 -1
  29. package/dist-lib/detox/index.cjs +35 -2
  30. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  31. package/dist-lib/expo-swift-bundle.cjs +8609 -3976
  32. package/dist-lib/expo-swift-metadata.cjs +5 -1
  33. package/dist-lib/expo-swift-metadata.mjs +5 -1
  34. package/dist-lib/home-paths.cjs +1 -1
  35. package/dist-lib/host/bridge-host.cjs +9894 -5149
  36. package/dist-lib/host/fetch-proxy-handler.cjs +93 -13
  37. package/dist-lib/host/fetch-proxy-overrides.cjs +10 -2
  38. package/dist-lib/host/fetch-proxy-overrides.mjs +8 -2
  39. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  40. package/dist-lib/host/websocket-proxy.cjs +1 -1
  41. package/dist-lib/index.cjs +538 -120
  42. package/dist-lib/jump-to-source-babel.cjs +1 -1
  43. package/dist-lib/jump-to-source-native.cjs +1 -1
  44. package/dist-lib/kotlin-host.cjs +1 -1
  45. package/dist-lib/menu.cjs +1 -1
  46. package/dist-lib/menu.mjs +1 -1
  47. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  48. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  49. package/dist-lib/metro-production-bundle.cjs +1 -1
  50. package/dist-lib/metro-production-bundle.mjs +1 -1
  51. package/dist-lib/metro.cjs +536 -118
  52. package/dist-lib/native-app-identity-project.cjs +408 -72
  53. package/dist-lib/native-app-identity-project.mjs +739 -375
  54. package/dist-lib/native-app-identity.cjs +37 -1
  55. package/dist-lib/native-app-identity.mjs +36 -1
  56. package/dist-lib/native-firebase-metadata.cjs +1 -1
  57. package/dist-lib/native-firebase-metadata.mjs +1 -1
  58. package/dist-lib/pdfium-raster.cjs +1 -1
  59. package/dist-lib/pdfium-raster.mjs +1 -1
  60. package/dist-lib/plist.cjs +1 -1
  61. package/dist-lib/plist.mjs +1 -1
  62. package/dist-lib/profiles.cjs +1 -1
  63. package/dist-lib/public-brand.cjs +1 -1
  64. package/dist-lib/public-origins-lib-esm.json +61 -28
  65. package/dist-lib/public-origins-lib.json +250 -92
  66. package/dist-lib/react-native-host-modules.cjs +1 -1
  67. package/dist-lib/react-native-host-modules.mjs +1 -1
  68. package/dist-lib/reap-browser.cjs +1 -1
  69. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  70. package/dist-lib/sdk.cjs +1 -1
  71. package/dist-lib/sdk.mjs +1 -1
  72. package/dist-lib/skills.cjs +16568 -11674
  73. package/dist-lib/swift-compile.cjs +6874 -2537
  74. package/dist-lib/swift-compile.mjs +6207 -1866
  75. package/dist-lib/swift-package.cjs +20 -3
  76. package/dist-lib/swift-package.mjs +20 -3
  77. package/dist-lib/swift-submit.cjs +2 -2
  78. package/dist-lib/swift-submit.mjs +2 -2
  79. package/dist-lib/swift-toolchain.cjs +19 -1
  80. package/dist-lib/swift.cjs +1 -1
  81. package/dist-lib/vite-swift.cjs +8753 -4124
  82. package/dist-lib/vite.cjs +93 -13
  83. package/dist-lib/worklet-executable.cjs +1 -1
  84. package/dist-lib/worklet-executable.mjs +1 -1
  85. package/dist-types/packages/peach-engine/src/engine/canvaskit-api.d.ts +1 -1
  86. package/dist-types/packages/peach-engine/src/engine/fontDataContract.d.ts +2 -0
  87. package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +3 -0
  88. package/dist-types/packages/peach-engine/src/render-worker/native-ui-protocol.d.ts +1 -0
  89. package/dist-types/packages/peach-engine/src/render-worker/protocol.d.ts +11 -1
  90. package/dist-types/packages/peach-engine/src/render-worker/shell-tree-wire.d.ts +2 -1
  91. package/dist-types/packages/peach-globals/src/nodeContract.d.ts +1 -0
  92. package/dist-types/packages/peach-globals/src/testBridgeContract.d.ts +10 -1
  93. package/dist-types/packages/peach-kit/src/capabilities.d.ts +1 -0
  94. package/dist-types/packages/peach-kit/src/ui/popover-placement.d.ts +1 -1
  95. package/dist-types/packages/sootsim-swift/src/securityHostContract.d.ts +10 -0
  96. package/package.json +1 -1
  97. package/runtime-companion.json +1 -1
  98. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFHost.h +555 -0
  99. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFNetwork.h +1 -0
  100. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.apinotes +6 -0
  101. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/NSURLProtocol.h +0 -1
  102. package/sdk/ios/System/Library/Frameworks/QuartzCore.framework/Modules/module.modulemap +6 -0
  103. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIBarButtonItemBadge.h +1 -1
  104. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIBarMinimization.h +1 -1
  105. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIScrollView.h +1 -1
  106. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UISearchTab.h +1 -1
  107. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UITab.h +1 -1
  108. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UITabAccessory.h +2 -2
  109. package/sdk/ios/usr/include/os/activity.h +534 -0
  110. package/sdk/ios/usr/include/os.modulemap +15 -0
  111. package/sdk/ios/wasm/include/fcntl.h +16 -0
  112. package/sdk/ios/wasm/include/string.h +5 -0
  113. package/sdk/native-hashes.json +1 -1
  114. package/src/dns-over-https.ts +61 -9
  115. package/src/expo-swift-bundle.ts +2 -7
  116. package/src/expo-swift-host.ts +11 -3
  117. package/src/host/fetch-proxy-handler.ts +51 -2
  118. package/src/host/fetch-proxy-overrides.ts +4 -0
  119. package/src/host/hostname-resolver.ts +9 -5
  120. package/src/native-app-identity-project.ts +13 -2
  121. package/src/native-app-identity.ts +47 -0
  122. package/src/nitro-adapter.lock.json +260 -260
  123. package/src/nitro-bundle.ts +7 -10
  124. package/src/nitro-superset.lock.json +4 -4
  125. package/src/podspec-sources.ts +20 -0
  126. package/src/storageSnapshotContract.ts +20 -1
  127. package/src/swift-compile.ts +409 -77
  128. package/src/swift-compiler-digest.ts +15 -0
  129. package/src/swift-coverage/xcode-packages.ts +3 -2
  130. package/src/swift-distribution.lock.json +12 -12
  131. package/src/swift-guest-archive.ts +153 -0
  132. package/src/swift-guest-compile.ts +449 -0
  133. package/src/swift-guest-image-ledger.ts +656 -0
  134. package/src/swift-guest-link-plan.ts +52 -0
  135. package/src/swift-guest-link.ts +1414 -0
  136. package/src/swift-guest-linked-spans.ts +338 -0
  137. package/src/swift-guest-manifest.ts +141 -0
  138. package/src/swift-guest-projection-storage.ts +310 -0
  139. package/src/swift-guest-projection.ts +252 -0
  140. package/src/swift-guest-runtime-plan.ts +834 -0
  141. package/src/swift-guest-source-inputs.ts +427 -0
  142. package/src/swift-guest-source-plan.ts +279 -0
  143. package/src/swift-host.ts +157 -36
  144. package/src/swift-icu.ts +60 -18
  145. package/src/swift-link-roots.ts +27 -50
  146. package/src/swift-package-manifest.ts +34 -18
  147. package/src/swift-package.ts +32 -2
  148. package/src/swift-packages.ts +138 -17
  149. package/src/swift-runtime-products.ts +73 -0
  150. package/src/swift-shared-sdk.ts +1 -1
  151. package/src/swift-submit.ts +15 -3
  152. package/src/swift-toolchain.ts +18 -0
  153. package/src/swift-wasm-prune.ts +313 -14
  154. package/src/swiftContract.ts +83 -0
  155. package/src/swiftRuntimeProductContract.ts +85 -45
  156. package/src/turbo-bundle.ts +1 -1
  157. package/src/turbo-codegen-intake.ts +10 -3
  158. package/src/turbo-runtime.lock.json +23 -23
  159. package/src/wasm-optimize.ts +16 -4
  160. package/src/xcode-project.ts +142 -14
@@ -0,0 +1,279 @@
1
+ import path from 'node:path'
2
+ import { swiftTargetModuleName } from './swift-package'
3
+ import type { GuestCallerScope } from './swift-guest-image-ledger'
4
+ import type { GuestLinkArtifact, GuestLinkPlan } from './swift-guest-link-plan'
5
+ import type { SwiftSourceLinkGraph } from './swiftContract'
6
+
7
+ // memberships come from resolved product kinds. static targets belong to the
8
+ // image that links them; dynamic products and the executable own their image.
9
+ export interface GuestSourceImage {
10
+ readonly id: number
11
+ readonly owner: string
12
+ readonly members: readonly string[]
13
+ }
14
+
15
+ export interface GuestSourceArtifact extends Omit<GuestLinkArtifact, 'image'> {
16
+ readonly sourceTarget: string
17
+ }
18
+
19
+ function swiftPackageLinkage(
20
+ target: SwiftSourceLinkGraph['targets'][number],
21
+ ): 'static' | 'dynamic' {
22
+ const linkage =
23
+ target.machOType === 'staticlib'
24
+ ? 'static'
25
+ : target.machOType === 'mh_dylib'
26
+ ? 'dynamic'
27
+ : null
28
+ if (!linkage) throw new Error(`unresolved SwiftPM library ${target.id}`)
29
+ if (
30
+ target.productType !== 'swiftpm.library.automatic' &&
31
+ target.productType !== `swiftpm.library.${linkage}`
32
+ )
33
+ throw new Error(`conflicting SwiftPM library linkage for ${target.id}`)
34
+ return linkage
35
+ }
36
+
37
+ /** Build image membership from resolved source products and their direct links. */
38
+ export function guestSourceImages(graph: SwiftSourceLinkGraph): GuestSourceImage[] {
39
+ const targets = new Map(graph.targets.map((target) => [target.id, target]))
40
+ const entry = targets.get(graph.entry)
41
+ if (!entry || entry.productType !== 'com.apple.product-type.application')
42
+ throw new Error('source graph has no application entry')
43
+ const images: { id: number; owner: string; members: string[] }[] = [
44
+ { id: 0, owner: entry.id, members: [] },
45
+ ]
46
+ const ownership = new Map<string, number>()
47
+ const active = new Set<string>()
48
+ const visit = (id: string, image: (typeof images)[number]) => {
49
+ if (active.has(id)) throw new Error(`cyclic source links at ${id}`)
50
+ const prior = ownership.get(id)
51
+ if (prior !== undefined) {
52
+ if (prior !== image.id)
53
+ throw new Error(`source target ${id} belongs to multiple images`)
54
+ return
55
+ }
56
+ const target = targets.get(id)
57
+ if (!target) throw new Error(`unknown source target ${id}`)
58
+ ownership.set(id, image.id)
59
+ image.members.push(id)
60
+ active.add(id)
61
+ for (const edge of target.dependencies) {
62
+ if (edge.attributes.length)
63
+ throw new Error(`unsupported link attributes for ${id} -> ${edge.target}`)
64
+ const dependency = targets.get(edge.target)
65
+ if (!dependency) throw new Error(`unknown source target ${edge.target}`)
66
+ const type = dependency.productType
67
+ let dynamic: boolean
68
+ if (type.startsWith('swiftpm.target.')) {
69
+ if (type !== 'swiftpm.target.regular' && type !== 'swiftpm.target.binary')
70
+ throw new Error(`unsupported SwiftPM target ${edge.target}`)
71
+ dynamic = false
72
+ } else if (type.startsWith('swiftpm.library.')) {
73
+ dynamic = swiftPackageLinkage(dependency) === 'dynamic'
74
+ } else if (type === 'com.apple.product-type.library.static') {
75
+ if (dependency.machOType && dependency.machOType !== 'staticlib')
76
+ throw new Error(`conflicting source library linkage for ${edge.target}`)
77
+ dynamic = false
78
+ } else if (type === 'com.apple.product-type.library.dynamic') {
79
+ if (dependency.machOType && dependency.machOType !== 'mh_dylib')
80
+ throw new Error(`conflicting source library linkage for ${edge.target}`)
81
+ dynamic = true
82
+ } else if (dependency.machOType === 'staticlib') dynamic = false
83
+ else if (dependency.machOType === 'mh_dylib') dynamic = true
84
+ else throw new Error(`unresolved source product ${edge.target}`)
85
+ if (!dynamic) visit(edge.target, image)
86
+ else {
87
+ const existing = ownership.get(edge.target)
88
+ const next =
89
+ existing === undefined
90
+ ? { id: images.length, owner: edge.target, members: [] }
91
+ : images[existing]!
92
+ if (existing === undefined) images.push(next)
93
+ visit(edge.target, next)
94
+ }
95
+ }
96
+ active.delete(id)
97
+ }
98
+ visit(entry.id, images[0]!)
99
+ return images
100
+ }
101
+
102
+ /** Resolve actual SwiftPM object paths to the source targets that built them. */
103
+ /** the binary target a `.peach-libraries/lib<target>-<digest>.a` archive belongs to. */
104
+ export function libraryArchiveTarget(file: string): string | null {
105
+ const match = /^lib(.+)-[0-9a-f]{64}\.a$/.exec(path.basename(file))
106
+ return match && path.basename(path.dirname(file)) === '.peach-libraries'
107
+ ? match[1]!
108
+ : null
109
+ }
110
+
111
+ export function guestSourceObjects(
112
+ graph: SwiftSourceLinkGraph,
113
+ buildDirectory: string,
114
+ objects: readonly string[],
115
+ generatedOwners: ReadonlyMap<string, string>,
116
+ ): { owned: GuestSourceArtifact[]; unowned: string[] } {
117
+ const names = new Set(
118
+ graph.targets.flatMap((target) =>
119
+ target.sourceTarget === null ? [] : [target.sourceTarget],
120
+ ),
121
+ )
122
+ // swiftpm names each build directory by its target's c99 module name
123
+ const owners = new Map(
124
+ [...generatedOwners].map(([name, owner]) => [swiftTargetModuleName(name), owner]),
125
+ )
126
+ // a binary target's prebuilt WebAssembly library is named for the target
127
+ const libraries = new Map(
128
+ graph.targets.flatMap((target) =>
129
+ target.productType === 'swiftpm.target.binary' && target.sourceTarget !== null
130
+ ? [[target.productName, target.sourceTarget] as const]
131
+ : [],
132
+ ),
133
+ )
134
+ const owned: GuestSourceArtifact[] = []
135
+ const unowned: string[] = []
136
+ for (const object of objects) {
137
+ const library = libraryArchiveTarget(object)
138
+ const libraryOwner = library === null ? undefined : libraries.get(library)
139
+ if (libraryOwner !== undefined) {
140
+ owned.push({
141
+ path: object,
142
+ kind: 'archive',
143
+ sourceTarget: libraryOwner,
144
+ instrumentDynamicLookups: false,
145
+ })
146
+ continue
147
+ }
148
+ const relative = path.relative(buildDirectory, object)
149
+ const [directory] = relative.split(path.sep)
150
+ const generated = directory?.endsWith('.build') ? directory.slice(0, -6) : ''
151
+ const target = owners.get(generated)
152
+ if (target && names.has(target) && object.endsWith('.o')) {
153
+ owned.push({
154
+ path: object,
155
+ kind: 'object',
156
+ sourceTarget: target,
157
+ instrumentDynamicLookups: true,
158
+ })
159
+ } else unowned.push(object)
160
+ }
161
+ return { owned, unowned }
162
+ }
163
+
164
+ // normal load dependencies are searched depth first, once per image. the
165
+ // caller must leave upward, delayed and other nonordinary links out of these
166
+ // edges; reexports that rename symbols need separate resolution metadata.
167
+ export function ordinaryGuestCallerScopes(
168
+ images: GuestLinkPlan['images'],
169
+ ): GuestCallerScope[] {
170
+ const byId = new Map(images.map((image) => [image.id, image]))
171
+ return images.map((caller) => {
172
+ const visited = new Set([caller.id])
173
+ const next: number[] = []
174
+ const visit = (id: number) => {
175
+ if (visited.has(id)) return
176
+ const image = byId.get(id)
177
+ if (!image) throw new Error(`unknown linked image ${id}`)
178
+ visited.add(id)
179
+ next.push(id)
180
+ for (const dependency of image.dependencies) visit(dependency)
181
+ }
182
+ for (const dependency of caller.dependencies) visit(dependency)
183
+ return { image: caller.id, next }
184
+ })
185
+ }
186
+
187
+ export function planGuestSourceLink(
188
+ graph: SwiftSourceLinkGraph,
189
+ images: readonly GuestSourceImage[],
190
+ callers: readonly GuestCallerScope[],
191
+ artifacts: readonly GuestSourceArtifact[],
192
+ symbols: readonly string[],
193
+ ): GuestLinkPlan {
194
+ const targets = new Map(graph.targets.map((target) => [target.id, target]))
195
+ const ownerByTarget = new Map<string, number>()
196
+ const imageIds = new Set<number>()
197
+ for (const image of images) {
198
+ if (imageIds.has(image.id)) throw new Error(`duplicate guest image ${image.id}`)
199
+ imageIds.add(image.id)
200
+ if (!targets.has(image.owner) || !image.members.includes(image.owner))
201
+ throw new Error(`image ${image.id} has no source owner`)
202
+ for (const id of image.members) {
203
+ const target = targets.get(id)
204
+ if (!target) throw new Error(`unknown source target ${id}`)
205
+ if (target.productType.startsWith('swiftpm.library.')) {
206
+ const linkage = swiftPackageLinkage(target)
207
+ if (linkage === 'dynamic' && id !== image.owner)
208
+ throw new Error(`dynamic SwiftPM library ${id} cannot be part of another image`)
209
+ if (linkage === 'static' && id === image.owner)
210
+ throw new Error(`static SwiftPM library ${id} cannot own an image`)
211
+ }
212
+ if (ownerByTarget.has(id))
213
+ throw new Error(`source target ${id} belongs to multiple images`)
214
+ if (
215
+ id !== image.owner &&
216
+ (target.machOType === 'mh_dylib' ||
217
+ target.machOType === 'mh_execute' ||
218
+ target.productType === 'com.apple.product-type.library.dynamic')
219
+ )
220
+ throw new Error(`dynamic source target ${id} cannot be part of another image`)
221
+ ownerByTarget.set(id, image.id)
222
+ }
223
+ }
224
+ if (!ownerByTarget.has(graph.entry)) throw new Error('source entry has no guest image')
225
+ const plannedImages = images.map((image) => {
226
+ const dependencies: number[] = []
227
+ const visited = new Set<string>()
228
+ const active = new Set<string>()
229
+ const visit = (id: string) => {
230
+ if (active.has(id)) throw new Error(`cyclic static source links at ${id}`)
231
+ if (visited.has(id)) return
232
+ visited.add(id)
233
+ active.add(id)
234
+ const target = targets.get(id)!
235
+ if (
236
+ target.linkerFlags.some((flag) =>
237
+ /(?:^|,)-(?:weak|reexport|upward|lazy)(?:[_-]|$)/.test(flag),
238
+ )
239
+ )
240
+ throw new Error(`unsupported special link flag for ${id}`)
241
+ for (const edge of target.dependencies) {
242
+ if (edge.attributes.length)
243
+ throw new Error(`unsupported link attributes for ${id} -> ${edge.target}`)
244
+ const dependencyImage = ownerByTarget.get(edge.target)
245
+ if (dependencyImage === undefined)
246
+ throw new Error(`linked target ${edge.target} has no guest image`)
247
+ if (dependencyImage === image.id) visit(edge.target)
248
+ else if (!dependencies.includes(dependencyImage))
249
+ dependencies.push(dependencyImage)
250
+ }
251
+ active.delete(id)
252
+ }
253
+ visit(image.owner)
254
+ for (const member of image.members)
255
+ if (!visited.has(member))
256
+ throw new Error(`source target ${member} is not linked into image ${image.id}`)
257
+ return { id: image.id, name: targets.get(image.owner)!.productName, dependencies }
258
+ })
259
+ const sourceTargets = new Map<string, number>()
260
+ for (const target of graph.targets) {
261
+ if (target.sourceTarget === null) continue
262
+ const image = ownerByTarget.get(target.id)
263
+ if (image === undefined) continue
264
+ if (sourceTargets.has(target.sourceTarget))
265
+ throw new Error(`ambiguous source target ${target.sourceTarget}`)
266
+ sourceTargets.set(target.sourceTarget, image)
267
+ }
268
+ return {
269
+ images: plannedImages,
270
+ callers,
271
+ artifacts: artifacts.map(({ sourceTarget, ...artifact }) => {
272
+ const image = sourceTargets.get(sourceTarget)
273
+ if (image === undefined)
274
+ throw new Error(`unowned link artifact ${artifact.path} (${sourceTarget})`)
275
+ return { ...artifact, image }
276
+ }),
277
+ symbols,
278
+ }
279
+ }
package/src/swift-host.ts CHANGED
@@ -27,7 +27,9 @@ import {
27
27
  missingSwiftModules,
28
28
  swiftAvailableModules,
29
29
  swiftSystemLibrarySurface,
30
- swiftIcuBrotliQ4,
30
+ swiftIcuAddress,
31
+ swiftIcuBundleAsset,
32
+ swiftIcuPacked,
31
33
  swiftImportNames,
32
34
  swiftPeachModules,
33
35
  withNativeSourceGlue,
@@ -115,6 +117,7 @@ import {
115
117
  import { privateSwiftDistributionRoot, swiftCacheDir } from './swift-toolchain'
116
118
  import { swiftProjectFileProblem } from './swiftContract'
117
119
  import {
120
+ type SwiftSourceLinkGraph,
118
121
  SWIFT_NATIVE_INPUT_ROOT,
119
122
  SWIFT_NATIVE_SDK_INCLUDE,
120
123
  SWIFT_XCODE_BUILD_CONDITION,
@@ -319,7 +322,7 @@ interface XcodeProjectEntry {
319
322
  target?: string
320
323
  /** the build configuration a selected scheme runs; xcode's default otherwise. */
321
324
  configuration?: string
322
- /** peach's own modules: a remote product named one links it, never its source. */
325
+ /** the selected distribution's modules, including metadata-only native intake. */
323
326
  provided: ReadonlySet<string> | null
324
327
  /** the module root a local compile links, which builds binary stand-ins. */
325
328
  moduleRoot: string | null
@@ -476,12 +479,13 @@ export function openXcodeProject(
476
479
  nativeModules,
477
480
  })
478
481
  const root = app.rootDir
482
+ const distribution = moduleRoot ?? interfaceRoot
479
483
  xcodeProjects().set(root, {
480
484
  xcodeproj,
481
485
  nativeModules,
482
486
  target: app.name,
483
487
  configuration: app.configuration,
484
- provided: moduleRoot ? new Set(swiftPeachModules(moduleRoot)) : null,
488
+ provided: distribution ? new Set(swiftPeachModules(distribution)) : null,
485
489
  moduleRoot: moduleRoot ?? null,
486
490
  interfaceRoot: interfaceRoot ?? null,
487
491
  })
@@ -701,6 +705,7 @@ function xcodeInputKey(
701
705
 
702
706
  interface XcodeLayout {
703
707
  app: XcodeAppTarget
708
+ appSwiftFlags: string[]
704
709
  checkouts: string
705
710
  /** framework and package targets, with root-relative paths and the nested target directories they exclude. */
706
711
  targets: (SwiftPackageTarget & { exclude: string[] })[]
@@ -718,6 +723,7 @@ interface XcodeLayout {
718
723
  /** the symbol sources xcode generates from package target catalogs, by input key. */
719
724
  generatedSources: Record<string, string>
720
725
  nativeTargets: SwiftNativeTarget[]
726
+ sourceLinkGraph: SwiftSourceLinkGraph
721
727
  headerAliases: SwiftHeaderAliases
722
728
  nativeFiles: Map<string, Buffer>
723
729
  files: string[]
@@ -921,12 +927,24 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
921
927
  files.add(file)
922
928
  }
923
929
  }
930
+ const standinModules = new Set(standins.map((standin) => standin.module))
924
931
  for (const standin of standins)
925
932
  targets.push({
926
933
  name: standin.module,
927
934
  path: path.posix.join(SWIFT_STANDINS_DIR, standin.module),
928
935
  publicHeadersPath: standin.publicHeadersPath,
929
- dependencies: standin.dependencies?.map((name) => ({ kind: 'target', name })),
936
+ dependencies: [
937
+ ...new Set([
938
+ ...(standin.dependencies ?? []),
939
+ ...Object.entries(standin.files).flatMap(([file, contents]) =>
940
+ file.endsWith('.swift')
941
+ ? swiftImportNames(contents).filter(
942
+ (name) => name !== standin.module && standinModules.has(name),
943
+ )
944
+ : [],
945
+ ),
946
+ ]),
947
+ ].map((name) => ({ kind: 'target', name })),
930
948
  executable: false,
931
949
  resources: false,
932
950
  exclude: [],
@@ -976,7 +994,7 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
976
994
  cDefines: packageMacros,
977
995
  cxxDefines: packageMacros,
978
996
  path: target.remote ? path.posix.join(XCODE_PACKAGES_DIR, posix) : posix,
979
- exclude: [],
997
+ exclude: target.exclude ?? [],
980
998
  defines: [
981
999
  ...(packageConfiguration === 'debug' ? ['DEBUG'] : []),
982
1000
  ...target.packageDefines
@@ -1079,9 +1097,12 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
1079
1097
  // swiftpm assigns a file to one target: a target directory nested in
1080
1098
  // another's is that one's exclusion
1081
1099
  for (const target of targets) {
1082
- target.exclude = targets
1083
- .filter((other) => other.path.startsWith(`${target.path}/`))
1084
- .map((other) => other.path.slice(target.path.length + 1))
1100
+ target.exclude = [
1101
+ ...target.exclude,
1102
+ ...targets
1103
+ .filter((other) => other.path.startsWith(`${target.path}/`))
1104
+ .map((other) => other.path.slice(target.path.length + 1)),
1105
+ ]
1085
1106
  }
1086
1107
  // a search path some pod xcconfigs stringify from an array embeds the
1087
1108
  // workspace's absolute path, which names the host: key it by the input root
@@ -1266,9 +1287,7 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
1266
1287
  if (!settings.publicHeaders) continue
1267
1288
  const target = packages.targets.find((value) => value.name === settings.name)
1268
1289
  if (!target) continue
1269
- const prefixHeader = path.posix.join(target.path, 'PeachPodPrefix.pch')
1270
- generatedSources[prefixHeader] =
1271
- '#ifdef __OBJC__\n#import <UIKit/UIKit.h>\n#import <Foundation/Foundation.h>\n#endif\n'
1290
+ // a SwiftPM clang target has no prefix header: SwiftPM never injects one.
1272
1291
  nativeTargets.push({
1273
1292
  name: target.name,
1274
1293
  deploymentTarget: settings.deploymentTarget,
@@ -1282,7 +1301,7 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
1282
1301
  .map(relative),
1283
1302
  flattensPublicHeaders: false,
1284
1303
  bridgingHeader: null,
1285
- prefixHeader,
1304
+ prefixHeader: null,
1286
1305
  headerSearchPaths: settings.includes.map(relative),
1287
1306
  moduleMaps: settings.moduleMaps.map((map) => ({
1288
1307
  module: map.module,
@@ -1301,8 +1320,21 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
1301
1320
  ),
1302
1321
  })
1303
1322
  }
1323
+ const graphTargets = [...app.sourceLinkGraph.targets, ...packages.sourceLinks.targets]
1324
+ const binaryTargets = new Set(
1325
+ graphTargets.flatMap((target) =>
1326
+ target.productType === 'swiftpm.target.binary' ? [target.productName] : [],
1327
+ ),
1328
+ )
1329
+ const vendoredNames = new Set(app.binaryFrameworks.map((binary) => binary.name))
1330
+ const vendoredModules = standins.flatMap((standin) =>
1331
+ vendoredNames.has(standin.target) && !binaryTargets.has(standin.target)
1332
+ ? [standin.module]
1333
+ : [],
1334
+ )
1304
1335
  return {
1305
1336
  app,
1337
+ appSwiftFlags: nativeFlags(app.swift?.flags ?? []),
1306
1338
  checkouts,
1307
1339
  targets,
1308
1340
  packages,
@@ -1315,6 +1347,73 @@ async function buildXcodeLayout(root: string): Promise<XcodeLayout | null> {
1315
1347
  resourceBundles,
1316
1348
  generatedSources,
1317
1349
  nativeTargets,
1350
+ sourceLinkGraph: {
1351
+ entry: relative(app.sourceLinkGraph.entry),
1352
+ targets: graphTargets.flatMap((target) => {
1353
+ // a binary target builds from the source modules that stand in for
1354
+ // its xcframework; the first names the target, the rest link behind it.
1355
+ const standinModules =
1356
+ target.productType === 'swiftpm.target.binary'
1357
+ ? standins
1358
+ .filter((standin) => standin.target === target.productName)
1359
+ .map((standin) => standin.module)
1360
+ : []
1361
+ const original =
1362
+ target.id === app.id
1363
+ ? app
1364
+ : app.frameworks.find((framework) => framework.id === target.id)
1365
+ const products = original
1366
+ ? (packages.sourceLinks.owners.find(
1367
+ (owner) => owner.module === original.moduleName,
1368
+ )?.products ?? [])
1369
+ : []
1370
+ const buildVersion = {
1371
+ ...target.buildVersion,
1372
+ sdk: target.buildVersion.sdk ?? iosSdk.sdk,
1373
+ }
1374
+ const [first, ...rest] = standinModules
1375
+ const standinId = (module: string) => `${target.id}/#standin/${module}`
1376
+ // a framework the project vendors links into the app target itself
1377
+ const vendoredLinks = target.id === app.id ? vendoredModules : []
1378
+ return [
1379
+ {
1380
+ ...target,
1381
+ id: relative(target.id),
1382
+ sourceTarget:
1383
+ target.sourceTarget === null
1384
+ ? (first ?? null)
1385
+ : original
1386
+ ? (frameworkNames.get(original) ?? original.moduleName)
1387
+ : target.sourceTarget,
1388
+ buildVersion,
1389
+ dependencies: [
1390
+ ...target.dependencies,
1391
+ ...products,
1392
+ ...rest.map((module) => ({ target: standinId(module), attributes: [] })),
1393
+ ...vendoredLinks.map((module) => ({
1394
+ target: standinId(module),
1395
+ attributes: [],
1396
+ })),
1397
+ ].map((link) => ({
1398
+ ...link,
1399
+ target: relative(link.target),
1400
+ })),
1401
+ },
1402
+ ...[...rest, ...vendoredLinks].map((module) => ({
1403
+ ...target,
1404
+ id: relative(standinId(module)),
1405
+ sourceTarget: module,
1406
+ productName: module,
1407
+ productType: 'swiftpm.target.binary',
1408
+ machOType: null,
1409
+ installName: null,
1410
+ buildVersion,
1411
+ linkerFlags: [],
1412
+ dependencies: [],
1413
+ })),
1414
+ ]
1415
+ }),
1416
+ },
1318
1417
  headerAliases,
1319
1418
  nativeFiles,
1320
1419
  files: [...files].sort(),
@@ -1379,20 +1478,22 @@ function xcodeManifest(layout: XcodeLayout): string {
1379
1478
  ...layout.app,
1380
1479
  defines: [...layout.app.defines, SWIFT_XCODE_BUILD_CONDITION],
1381
1480
  cxxInterop: appSwift?.cxxInterop,
1382
- swiftFlags:
1383
- appSwift?.cxxInterop && appSwift.cxxStandard
1481
+ swiftFlags: [
1482
+ ...layout.appSwiftFlags,
1483
+ ...(appSwift?.cxxInterop && appSwift.cxxStandard
1384
1484
  ? ['-Xcc', `-std=${appSwift.cxxStandard}`]
1385
- : undefined,
1485
+ : []),
1486
+ ],
1386
1487
  })
1387
1488
  return [
1388
1489
  '// swift-tools-version: 6.2',
1389
1490
  'import PackageDescription',
1390
1491
  `let package = Package(name: "app", ${layout.app.iosDeploymentTarget ? `platforms: [.iOS(${JSON.stringify(layout.app.iosDeploymentTarget)})], ` : ''}dependencies: [${[...nativePaths].map(([name, directory]) => `.package(name: ${JSON.stringify(name)}, path: ${JSON.stringify(directory)})`).join(', ')}], targets: [`,
1391
- ` .executableTarget(name: "${layout.app.moduleName}", dependencies: [${[...appProducts, ...sdkProducts].join(', ')}], path: "${XCODE_APP_DIR}", ${appSettings}),`,
1492
+ ` .executableTarget(name: "${layout.app.moduleName}", dependencies: [${[...appProducts, ...layout.targets.map((target) => JSON.stringify(target.name)), ...sdkProducts].join(', ')}], path: "${XCODE_APP_DIR}", ${appSettings}),`,
1392
1493
  ...layout.targets.map((target) =>
1393
1494
  target.systemLibrary
1394
1495
  ? ` .systemLibrary(name: "${target.name}", path: "${target.path}"),`
1395
- : ` .target(name: "${target.name}", dependencies: [${dependencies(target)}], path: "${target.path}", ${target.exclude.length > 0 ? `exclude: ${JSON.stringify(target.exclude)}, ` : ''}${target.resources ? 'resources: [], ' : ''}${target.publicHeadersPath === undefined ? '' : `publicHeadersPath: ${JSON.stringify(target.publicHeadersPath)}, `}${clangSettings(target)}${swiftManifestSettings(target)}${libraries(target)}),`,
1496
+ : ` .target(name: "${target.name}", ${target.dependencies === undefined ? '' : `dependencies: [${dependencies(target)}], `}path: "${target.path}", ${target.exclude.length > 0 ? `exclude: ${JSON.stringify(target.exclude)}, ` : ''}${target.resources ? 'resources: [], ' : ''}${target.publicHeadersPath === undefined ? '' : `publicHeadersPath: ${JSON.stringify(target.publicHeadersPath)}, `}${clangSettings(target)}${swiftManifestSettings(target)}${libraries(target)}),`,
1396
1497
  ),
1397
1498
  '])',
1398
1499
  '',
@@ -1474,6 +1575,7 @@ function sourceTreeFiles(dir: string, canonicalRoot: string): string[] {
1474
1575
  walk(full)
1475
1576
  } else if (stat.isFile()) {
1476
1577
  found.push(full)
1578
+ if (found.length > 4096) throw new Error('swift package exceeds 4096 input files')
1477
1579
  }
1478
1580
  }
1479
1581
  ancestors.delete(realDirectory)
@@ -2164,7 +2266,7 @@ export function entryRootId(packagePath: string, file: string): string {
2164
2266
  export function icuUrl(origin: string, artifact: SwiftArtifact): string {
2165
2267
  return 'url' in artifact.icu
2166
2268
  ? artifact.icu.url
2167
- : `${origin}${ICU_PATH}${artifact.icu.sha256}`
2269
+ : `${origin}${ICU_PATH}${swiftIcuAddress(artifact.icu)}`
2168
2270
  }
2169
2271
 
2170
2272
  // a c stand-in whose framework has a WebAssembly build links that library,
@@ -2244,6 +2346,7 @@ interface SwiftPackageSnapshot {
2244
2346
  packageGuestImports?: string[]
2245
2347
  files: Record<string, string>
2246
2348
  nativeTargets?: SwiftNativeTarget[]
2349
+ sourceLinkGraph?: SwiftSourceLinkGraph
2247
2350
  headerAliases?: SwiftHeaderAliases
2248
2351
  defaultLocalization?: string
2249
2352
  entry: string
@@ -2361,7 +2464,12 @@ function packageSnapshot(
2361
2464
  ...((project.nativeTargets ?? layout?.nativeTargets)?.length
2362
2465
  ? { nativeTargets: project.nativeTargets ?? layout?.nativeTargets }
2363
2466
  : {}),
2364
- ...(layout ? { defaultLocalization: layout.app.developmentRegion } : {}),
2467
+ ...(layout
2468
+ ? {
2469
+ sourceLinkGraph: layout.sourceLinkGraph,
2470
+ defaultLocalization: layout.app.developmentRegion,
2471
+ }
2472
+ : {}),
2365
2473
  entry: layout
2366
2474
  ? xcodeInputKey(layout, path.resolve(project.packagePath, entry), entry)
2367
2475
  : entry,
@@ -2424,6 +2532,7 @@ function snapshotCompileInput(snapshot: SwiftPackageSnapshot): SwiftCompileInput
2424
2532
  : {}),
2425
2533
  ...(snapshot.projectFiles ? { projectFiles: snapshot.projectFiles } : {}),
2426
2534
  ...(snapshot.nativeTargets ? { nativeTargets: snapshot.nativeTargets } : {}),
2535
+ ...(snapshot.sourceLinkGraph ? { sourceLinkGraph: snapshot.sourceLinkGraph } : {}),
2427
2536
  ...(snapshot.headerAliases ? { headerAliases: snapshot.headerAliases } : {}),
2428
2537
  ...(snapshot.defaultLocalization
2429
2538
  ? { defaultLocalization: snapshot.defaultLocalization }
@@ -2807,6 +2916,7 @@ async function compileRemote(
2807
2916
  }
2808
2917
  : {}),
2809
2918
  ...(snapshot.nativeTargets ? { nativeTargets: snapshot.nativeTargets } : {}),
2919
+ ...(snapshot.sourceLinkGraph ? { sourceLinkGraph: snapshot.sourceLinkGraph } : {}),
2810
2920
  ...(snapshot.headerAliases ? { headerAliases: snapshot.headerAliases } : {}),
2811
2921
  ...(snapshot.defaultLocalization
2812
2922
  ? { defaultLocalization: snapshot.defaultLocalization }
@@ -3142,22 +3252,34 @@ export async function serveSwiftPackageAsset(
3142
3252
  for (const project of projects().values()) {
3143
3253
  const icu = project.artifacts.find(
3144
3254
  (artifact) =>
3145
- 'sha256' in artifact.icu && pathname === `${ICU_PATH}${artifact.icu.sha256}`,
3255
+ 'sha256' in artifact.icu &&
3256
+ pathname === `${ICU_PATH}${swiftIcuAddress(artifact.icu)}`,
3146
3257
  )?.icu
3147
- if (!icu || 'url' in icu || pathname !== `${ICU_PATH}${icu.sha256}`) continue
3258
+ if (!icu || 'url' in icu) continue
3148
3259
  // the address is the content, so the browser keeps it across reloads,
3149
- // restarts and every app built with this toolchain
3260
+ // restarts and every app built with this toolchain. the tenant reads
3261
+ // the index and then one pack at a time by range.
3262
+ const packed = await swiftIcuPacked(icu)
3150
3263
  res.setHeader('access-control-allow-origin', '*')
3151
3264
  res.setHeader('cache-control', 'public, max-age=31536000, immutable')
3152
- res.setHeader('vary', 'accept-encoding')
3153
- if (acceptsBr) {
3154
- res.setHeader('content-type', 'application/octet-stream')
3155
- res.setHeader('content-encoding', 'br')
3156
- res.end(await swiftIcuBrotliQ4(icu))
3157
- } else {
3158
- res.setHeader('content-type', 'application/gzip')
3159
- res.end(icu.gzip)
3265
+ res.setHeader('content-type', 'application/octet-stream')
3266
+ res.setHeader('accept-ranges', 'bytes')
3267
+ const range = /^bytes=(\d+)-(\d+)$/.exec(res.req?.headers.range ?? '')
3268
+ if (!range) {
3269
+ res.end(packed)
3270
+ return true
3271
+ }
3272
+ const start = Number(range[1])
3273
+ const end = Math.min(Number(range[2]), packed.length - 1)
3274
+ if (start > end) {
3275
+ res.statusCode = 416
3276
+ res.setHeader('content-range', `bytes */${packed.length}`)
3277
+ res.end()
3278
+ return true
3160
3279
  }
3280
+ res.statusCode = 206
3281
+ res.setHeader('content-range', `bytes ${start}-${end}/${packed.length}`)
3282
+ res.end(packed.subarray(start, end + 1))
3161
3283
  return true
3162
3284
  }
3163
3285
  res.statusCode = 404
@@ -3187,7 +3309,7 @@ export async function serveSwiftPackageAsset(
3187
3309
  }
3188
3310
 
3189
3311
  // packages retain the exact bytes the foreground host serves, including ICU.
3190
- export function swiftPackageAssets(packagePath: string, hash: string) {
3312
+ export async function swiftPackageAssets(packagePath: string, hash: string) {
3191
3313
  const project = projects().get(fs.realpathSync(packagePath))
3192
3314
  const artifact = project?.artifacts.find((candidate) => candidate.hash === hash)
3193
3315
  if (!project || !artifact) throw new Error(`Swift artifact ${hash} was not built`)
@@ -3199,11 +3321,10 @@ export function swiftPackageAssets(packagePath: string, hash: string) {
3199
3321
  ])
3200
3322
  for (const [key, resource] of project.resourceFiles)
3201
3323
  assets.set(`${RESOURCE_PATH}${key}`, resource)
3202
- if ('sha256' in artifact.icu)
3203
- assets.set(`${ICU_PATH}${artifact.icu.sha256}`, {
3204
- bytes: artifact.icu.gzip,
3205
- contentType: 'application/gzip',
3206
- })
3324
+ if ('sha256' in artifact.icu) {
3325
+ const asset = await swiftIcuBundleAsset(artifact.icu)
3326
+ assets.set(`/${asset.path}`, asset)
3327
+ }
3207
3328
  return assets
3208
3329
  }
3209
3330