peachsim 0.1.2476 → 0.1.2478
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.
- package/cli/agent-note.ts +14 -0
- package/cli/command-agent.ts +2 -0
- package/cli/commands/control.ts +3 -2
- package/cli/commands/stream.ts +28 -8
- package/dist-lib/agent-daemon-client.cjs +1 -1
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +1 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +2 -2
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +5 -5
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +3 -3
- package/dist-lib/bundle-channel.cjs +5382 -5067
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +3 -3
- package/dist-lib/cloud-contract.cjs +2 -1
- package/dist-lib/cloud-contract.mjs +3 -3
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +2 -2
- package/dist-lib/cloud.cjs +2 -1
- package/dist-lib/cloud.mjs +7 -7
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +5 -5
- package/dist-lib/detox/index.cjs +5 -2
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/esm-chunks/{acorn-XUDP7FUU.mjs → acorn-FQLOTPEI.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-5J6SH5PL.mjs → chunk-25VCIHSN.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-MS423IWH.mjs → chunk-355SS4SH.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-YA55AHKP.mjs → chunk-3IZX74HE.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-E254ZLN5.mjs → chunk-CIMYKM6N.mjs} +10 -8
- package/dist-lib/esm-chunks/{chunk-YKTS6JGV.mjs → chunk-FFCS7MFJ.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-YZGIW7I6.mjs → chunk-GGYYAJR7.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-T5HVFQ3M.mjs → chunk-GY7RVEFV.mjs} +4 -4
- package/dist-lib/esm-chunks/{chunk-XVVVMJ7H.mjs → chunk-ICJOTWER.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-FG6NDDVL.mjs → chunk-JUPWMWSX.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-VXDXNH7I.mjs → chunk-LCAQS5UW.mjs} +2 -2
- package/dist-lib/esm-chunks/{chunk-BIODQMYB.mjs → chunk-MLVSAVNL.mjs} +2 -1
- package/dist-lib/esm-chunks/{chunk-AUTNUR7W.mjs → chunk-MQM5KR5X.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-DFHVJLBP.mjs → chunk-OVBWXPD7.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-ACRNH6CJ.mjs → chunk-Q47VNBNR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-7TOYW4KW.mjs → chunk-RSSHYN7S.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-DRUSXLT2.mjs → chunk-TUJL2AP2.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-XUNG5UYW.mjs → chunk-UETOLHI7.mjs} +2059 -3500
- package/dist-lib/esm-chunks/{chunk-G7ZQ4MZU.mjs → chunk-VAQFAFOC.mjs} +37 -7
- package/dist-lib/esm-chunks/{chunk-GR4GWWVO.mjs → chunk-VORKZFQK.mjs} +1 -1
- package/dist-lib/esm-chunks/chunk-XEZ4K3TZ.mjs +1475 -0
- package/dist-lib/esm-chunks/{chunk-VW34BZCP.mjs → chunk-XSSZZ2HY.mjs} +1 -1
- package/dist-lib/esm-chunks/{metro-layout-parser-GQPBXOFJ.mjs → metro-layout-parser-MRCK7YD5.mjs} +3 -3
- package/dist-lib/esm-chunks/{swift-package-prebuilt-IMNLLYSA.mjs → swift-package-prebuilt-LYR2P26A.mjs} +10 -9
- package/dist-lib/esm-chunks/{swift-sdk-app-module-LZE3N3OS.mjs → swift-sdk-app-module-CXY5TKBA.mjs} +15 -13
- package/dist-lib/esm-chunks/{swift-sdk-image-E2QZGC53.mjs → swift-sdk-image-6L3AMATS.mjs} +13 -12
- package/dist-lib/esm-chunks/swift-sdk-image-producer-C4TNZ4UM.mjs +294 -0
- package/dist-lib/expo-swift-bundle.cjs +4362 -4047
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +7 -7
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +5284 -4966
- package/dist-lib/host/fetch-proxy-handler.cjs +5 -5
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +4 -1
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +3 -3
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +3 -3
- package/dist-lib/metro-production-bundle.cjs +1 -1
- package/dist-lib/metro-production-bundle.mjs +4 -4
- package/dist-lib/native-app-identity-project.cjs +2 -2
- package/dist-lib/native-app-identity-project.mjs +17 -15
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +6 -6
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +3 -3
- package/dist-lib/pdfium-raster.cjs +1 -1
- package/dist-lib/pdfium-raster.mjs +2 -2
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +3 -3
- package/dist-lib/profiles.cjs +1 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +92 -81
- package/dist-lib/public-origins-lib.json +74 -69
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +2 -2
- package/dist-lib/reap-browser.cjs +1 -1
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +1 -1
- package/dist-lib/sdk.mjs +7 -7
- package/dist-lib/skills.cjs +5903 -5569
- package/dist-lib/swift-compile.cjs +1280 -965
- package/dist-lib/swift-compile.mjs +10 -9
- package/dist-lib/swift-package.cjs +1 -1
- package/dist-lib/swift-package.mjs +3 -3
- package/dist-lib/swift-submit.cjs +1 -1
- package/dist-lib/swift-submit.mjs +5 -5
- package/dist-lib/swift-svg-raster.cjs +1 -1
- package/dist-lib/swift-svg-raster.mjs +2 -2
- package/dist-lib/swift-toolchain.cjs +1 -1
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
- package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
- package/dist-lib/vendor/acorn.cjs +1 -1
- package/dist-lib/vendor/bplist-parser.cjs +1 -1
- package/dist-lib/vendor/ignore.cjs +1 -1
- package/dist-lib/vendor/magic-string.cjs +1 -1
- package/dist-lib/vendor/plist.cjs +1 -1
- package/dist-lib/vendor/smol-toml.cjs +1 -1
- package/dist-lib/vendor/stream-json.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
- package/dist-lib/vendor/sucrase.cjs +1 -1
- package/dist-lib/vendor/terser.cjs +1 -1
- package/dist-lib/vendor/valibot.cjs +1 -1
- package/dist-lib/vendor/ws.cjs +1 -1
- package/dist-lib/vendor/zod.cjs +1 -1
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +3 -3
- package/dist-types/packages/peach-engine/src/webview-automation-host.d.ts +4 -2
- package/dist-types/packages/peach-globals/src/compositorBridgeContract.d.ts +1 -0
- package/dist-types/packages/peach-globals/src/index.d.ts +1 -1
- package/dist-types/packages/peach-globals/src/replayBridgeContract.d.ts +3 -2
- package/package.json +1 -1
- package/runtime-companion.json +1 -1
- package/src/bridge-contract.ts +1 -0
- package/src/cloud-contract.ts +1 -0
- package/src/host/bridge-host.ts +7 -1
- package/src/swift-compile.ts +1 -0
- package/src/swift-sdk-image-producer.ts +336 -0
- package/src/swift-sdk-image.ts +37 -1
|
@@ -0,0 +1,1475 @@
|
|
|
1
|
+
/*! peach v0.1.2478 | (c) 2026 Tamagui LLC | Proprietary; see LICENSE */
|
|
2
|
+
|
|
3
|
+
// src/swiftTargetContract.ts
|
|
4
|
+
var SWIFT_WASM_TRIPLE = "wasm64-unknown-wasip1-threads";
|
|
5
|
+
var wasmThreadFlags = [
|
|
6
|
+
"-pthread",
|
|
7
|
+
"-matomics",
|
|
8
|
+
"-mbulk-memory",
|
|
9
|
+
"-ftls-model=local-exec"
|
|
10
|
+
];
|
|
11
|
+
var swiftWasmThreadFlags = wasmThreadFlags.flatMap((flag) => ["-Xcc", flag]);
|
|
12
|
+
var wasmThreadLinkFlags = [
|
|
13
|
+
"--import-memory",
|
|
14
|
+
"--export-memory",
|
|
15
|
+
"--shared-memory",
|
|
16
|
+
"--max-memory=4294967296",
|
|
17
|
+
"--growable-table",
|
|
18
|
+
"--export=__stack_pointer",
|
|
19
|
+
"--export=__tls_base",
|
|
20
|
+
"--export=__wasm_init_tls",
|
|
21
|
+
"--export=__tls_size",
|
|
22
|
+
"--export=__tls_align",
|
|
23
|
+
"--export=__stack_low",
|
|
24
|
+
"--export=__stack_high",
|
|
25
|
+
"--export=__data_end",
|
|
26
|
+
"--export=__heap_base",
|
|
27
|
+
"--export-table",
|
|
28
|
+
"--export=wasi_thread_start"
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
// src/swift-toolchain.ts
|
|
32
|
+
import { execFile, execFileSync, spawn } from "node:child_process";
|
|
33
|
+
import { createHash } from "node:crypto";
|
|
34
|
+
import fs from "node:fs";
|
|
35
|
+
import os from "node:os";
|
|
36
|
+
import path from "node:path";
|
|
37
|
+
import { pipeline } from "node:stream/promises";
|
|
38
|
+
import { fileURLToPath } from "node:url";
|
|
39
|
+
import { promisify } from "node:util";
|
|
40
|
+
import zlib from "node:zlib";
|
|
41
|
+
function peachsimPackageRoot() {
|
|
42
|
+
let dir = import.meta.dirname;
|
|
43
|
+
while (!fs.existsSync(path.join(dir, "package.json")) && dir !== path.dirname(dir))
|
|
44
|
+
dir = path.dirname(dir);
|
|
45
|
+
return dir;
|
|
46
|
+
}
|
|
47
|
+
function sourceSwiftCheckout() {
|
|
48
|
+
return path.resolve(peachsimPackageRoot(), "../sootsim-swift");
|
|
49
|
+
}
|
|
50
|
+
function swiftSdkOptions() {
|
|
51
|
+
return ["--swift-sdks-path", path.resolve(peachSwiftSdksDir())];
|
|
52
|
+
}
|
|
53
|
+
var compilerArchives = {
|
|
54
|
+
"swift-6.4.0-RELEASE-custom-77d2c2623ec68c0c": {
|
|
55
|
+
identity: "77d2c2623ec68c0c",
|
|
56
|
+
archive: "tar.zst",
|
|
57
|
+
hosts: {
|
|
58
|
+
"darwin-arm64": {
|
|
59
|
+
checksum: "0245c2f6593751cf29ce5920b9686f12097d0d76174c9f242e8d15dfb7ad049a",
|
|
60
|
+
manifest: "07739120bd00b8fc9270f652ee66599cc45deb80bcc5cd7d45a452567fe6ba9d"
|
|
61
|
+
},
|
|
62
|
+
"linux-x64": {
|
|
63
|
+
checksum: "75fc76f1a683bc52c402c0e969f7ba5c774717d7df571d2b59d0c0e81fa2a75e",
|
|
64
|
+
manifest: "89c3dee81d47bcbbad10a57ef110f10463e792fb9d5d450463084ccf8d1d5623"
|
|
65
|
+
},
|
|
66
|
+
"linux-arm64": {
|
|
67
|
+
checksum: "2bc4425b7092c74bb7a03a319b41824a295ee5a81f4dbb4e769598dd656a6777",
|
|
68
|
+
manifest: "4b46e0f2fd8458a9a18f9a5114db616340838108fb476fb55ed5fc9d265fdd09"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"swift-6.4.0-RELEASE-custom-34da2e3307f11598": {
|
|
73
|
+
identity: "34da2e3307f11598",
|
|
74
|
+
archive: "tar.zst",
|
|
75
|
+
hosts: {
|
|
76
|
+
"darwin-arm64": {
|
|
77
|
+
checksum: "f8f6ded1943979fbd4add8a43940485cb3b36f4dd293aa76b0c17f72d9e1c233",
|
|
78
|
+
manifest: "7e0e74025935a64f66b9ea5e6a8aee1e730a908227c12686364fa83b665fbb44"
|
|
79
|
+
},
|
|
80
|
+
"linux-x64": {
|
|
81
|
+
checksum: "8761203ffde1721bbd10820ac0dc68ec5d48e034672ba738261e509b1757c160",
|
|
82
|
+
manifest: "61fe6dcee0f6cc0c895ff511478a264c43b89a14b39bc90c6de8b873ba2f14a6"
|
|
83
|
+
},
|
|
84
|
+
"linux-arm64": {
|
|
85
|
+
checksum: "d0f5312eb032068e38bcdc8dacf12fcf4375ba789a1ee434d31d5e9a24554126",
|
|
86
|
+
manifest: "9e3218d197c9784e3c434883a21f975222a662523a1ae78a38519e53c3a0385c"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"swift-6.4.0-RELEASE-custom-0ebff005f47d6148": {
|
|
91
|
+
identity: "0ebff005f47d6148",
|
|
92
|
+
archive: "tar.zst",
|
|
93
|
+
hosts: {
|
|
94
|
+
"darwin-arm64": {
|
|
95
|
+
checksum: "2ddf2c544a17e5ba04c0c2cafd6b49394d9417e8c8d2d1c380846dc003436e10",
|
|
96
|
+
manifest: "fd7527a60ba728e275afcaf19babf5565f096cd1bcbac9c3d65e238f869df105"
|
|
97
|
+
},
|
|
98
|
+
"linux-x64": {
|
|
99
|
+
checksum: "08f0ad4175b81116842cdb752b34f92fa28133befa81e1813371ba25144b958e",
|
|
100
|
+
manifest: "f89425fdbe1137005f59875542e2aa9637c5e586f6ae9a107375c4633ff6d425"
|
|
101
|
+
},
|
|
102
|
+
"linux-arm64": {
|
|
103
|
+
checksum: "11b3368e45f8baf5d3fcffe12168744457695757f64c74de0443aac95fcb963b",
|
|
104
|
+
manifest: "f889ea5d5d6da59af0a278208349e889e5d755571abf6ef4b59f6cf49fa21e0b"
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
"swift-6.4.0-RELEASE-custom-17479c8af01737a3": {
|
|
109
|
+
identity: "17479c8af01737a3",
|
|
110
|
+
archive: "tar.zst",
|
|
111
|
+
hosts: {
|
|
112
|
+
"darwin-arm64": {
|
|
113
|
+
checksum: "b41a24b8d9b2ae13e9d6fe3935df69625e2a01cc742056049239415497e99ffb",
|
|
114
|
+
manifest: "e824fa7f4333aa4307410899987e42863de087db43b2c70b96d4e67de0b0a843"
|
|
115
|
+
},
|
|
116
|
+
"linux-x64": {
|
|
117
|
+
checksum: "fdd6e4ef39698d3a254b4d4967018cf468fdd3b6c6af461719de4c3b3ca126dc",
|
|
118
|
+
manifest: "a15dc88910eaa012849604b0a866ab028072e526b4f00e611d29762b0cf41c9a"
|
|
119
|
+
},
|
|
120
|
+
"linux-arm64": {
|
|
121
|
+
checksum: "3bfda70b772e13e4751e8ea8ca656e4382b66bc668a0ec4b1ed9740992d4bf50",
|
|
122
|
+
manifest: "4c069a442d09fe4f92aee7d2d6b40a63a8ea5928c922879e0c4d2a9ac21d65f9"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"swift-6.4.0-RELEASE-custom-38af8a6bc3d9e72c": {
|
|
127
|
+
identity: "38af8a6bc3d9e72c",
|
|
128
|
+
archive: "tar.zst",
|
|
129
|
+
hosts: {
|
|
130
|
+
"darwin-arm64": {
|
|
131
|
+
checksum: "1a7be355f9763f43e4b9e092a6917e071cbdfd2b6eaf1ba29f33893d2140a952",
|
|
132
|
+
manifest: "4ea4a563012de05be738cd9d89cd5797408813277ae07a7b7221c22fe16dacbb"
|
|
133
|
+
},
|
|
134
|
+
"linux-x64": {
|
|
135
|
+
checksum: "1daa88ea2c31d2b815414da981df9def4799b8290ba2bd4347a56e73cbce5b68",
|
|
136
|
+
manifest: "9b2b771a1b8bcf28a5bb52a4c299b94b509cee71ac825feccaff438ad29cc812"
|
|
137
|
+
},
|
|
138
|
+
"linux-arm64": {
|
|
139
|
+
checksum: "3a30617cba08bc5474c0ef24fdecc13ee7759f90c3ce847e00e3bcdb05c0315a",
|
|
140
|
+
manifest: "f3074c4a2d80641e1a620f52161473de193def016b477cda85c7584b3cfbfd7b"
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
"swift-6.4.0-RELEASE-custom-cd5337e8ab49fd61": {
|
|
145
|
+
identity: "cd5337e8ab49fd61",
|
|
146
|
+
archive: "tar.zst",
|
|
147
|
+
hosts: {
|
|
148
|
+
"darwin-arm64": {
|
|
149
|
+
checksum: "a95b73d196daebc342020ecce50ee468c1e5664398825aee79dcb700a10c86b1",
|
|
150
|
+
manifest: "9b1d49ac08f6a9e710656e9a762d144ec45b142c8ec9ad90bab819a78dd45502"
|
|
151
|
+
},
|
|
152
|
+
"linux-x64": {
|
|
153
|
+
checksum: "fa141d23c542c050bad77805517f9c277bd403b6ca0f4a271354329aaa5d09e6",
|
|
154
|
+
manifest: "5cd9d1430b61dbe38c728425abd27cd51a8b99256ba026dbf335afb36655c9e4"
|
|
155
|
+
},
|
|
156
|
+
"linux-arm64": {
|
|
157
|
+
checksum: "5b0ee093ff97d1e48b276535ceca35704608fbb10be219bd89cf89226a83d3da",
|
|
158
|
+
manifest: "177390f9963958d71f5414efa077d728ae728cb0f61e21e3287a989dc939f235"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
"swift-6.4.0-RELEASE-custom-139bc3bba3c4f73c": {
|
|
163
|
+
identity: "139bc3bba3c4f73c",
|
|
164
|
+
archive: "tar.zst",
|
|
165
|
+
hosts: {
|
|
166
|
+
"darwin-arm64": {
|
|
167
|
+
checksum: "4c35af19e099a956f38932e94aa7ea31ee6e8ccf2497a7922b9784e4adba9c4e",
|
|
168
|
+
manifest: "48753338f405766388e3556e66fbe592ff5af16089f0f577ad91bff267923dae"
|
|
169
|
+
},
|
|
170
|
+
"linux-x64": {
|
|
171
|
+
checksum: "601ba7161cdc2d8fb07701aeb7e81b2bf5368aae23643e56286ff0798f25be9c",
|
|
172
|
+
manifest: "9b3371f99d5f21b8758abc3497a8ade10e0a49d6373e9979b57eb21edc927b3f"
|
|
173
|
+
},
|
|
174
|
+
"linux-arm64": {
|
|
175
|
+
checksum: "6e8931db639655525acaeb1ab8fffd76e836d2a28b5d9ba384d00f1d2e6da3ed",
|
|
176
|
+
manifest: "5bfebf5e544c9b8a5e3405923ce57c887c6b87cae7d8891cd25e9ad251e2bc5d"
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
"swift-6.4.0-RELEASE-custom-9f57741c6333ce5e": {
|
|
181
|
+
identity: "9f57741c6333ce5e",
|
|
182
|
+
archive: "tar.zst",
|
|
183
|
+
hosts: {
|
|
184
|
+
"darwin-arm64": {
|
|
185
|
+
checksum: "97c80de3caaa961be2f2649a839eb69cec376e8b5aa68a9a1191fcf312eaa002",
|
|
186
|
+
manifest: "8177dea4013ba47bc0c0e14f5bb3d407a4a94636906e6c8273ef974d93d905b8"
|
|
187
|
+
},
|
|
188
|
+
"linux-x64": {
|
|
189
|
+
checksum: "08bf59a5899990395aa1acc764d312e91c75844a4748a4dcd51061bc0ffbc153",
|
|
190
|
+
manifest: "8e68a4780c7b29e1b3b43f473518138dc9a6ef63ed8f0fdb2ac3b5a9b487c7f2"
|
|
191
|
+
},
|
|
192
|
+
"linux-arm64": {
|
|
193
|
+
checksum: "aa8a5b2023badafbd910bddd90f702a94338f2e83f5f62f4de17e4e20e240b0e",
|
|
194
|
+
manifest: "4d0781f9a3648795e9f063c45643dc4f2c1d7bf7b766eee7cd99c726e5f73187"
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
"swift-6.4.0-RELEASE-custom-cbd9cef4b408cc96": {
|
|
199
|
+
identity: "cbd9cef4b408cc96",
|
|
200
|
+
archive: "tar.zst",
|
|
201
|
+
hosts: {
|
|
202
|
+
"darwin-arm64": {
|
|
203
|
+
checksum: "97ed2a6b10ae85e73422656fe7b44fdbd5e2343d7082ef2cb39476847988e615",
|
|
204
|
+
manifest: "49c8002809ba90b16e2619653994bc776b5bc7ef26b98796233d8b425515b734"
|
|
205
|
+
},
|
|
206
|
+
"linux-x64": {
|
|
207
|
+
checksum: "b404cffaa55b783098056cce0ef568d864bc92910dafa89dd0650d0e9d019918",
|
|
208
|
+
manifest: "19c333d87f2ffc5c716e0561b3bcb0c2a6c6015ccd4a404a739099c6aa3b898f"
|
|
209
|
+
},
|
|
210
|
+
"linux-arm64": {
|
|
211
|
+
checksum: "47d8572b0438f100494edaeb4c7b24fbf5c627d7d02a09fcd68a95c6d2d780f5",
|
|
212
|
+
manifest: "b27a2bccb5a8a0988414951b3b4a4cd9d3acc315694f59ae4c166c2f41282171"
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
"swift-6.4.0-RELEASE-custom-fb42d2535739dfd2": {
|
|
217
|
+
identity: "fb42d2535739dfd2",
|
|
218
|
+
archive: "tar.zst",
|
|
219
|
+
hosts: {
|
|
220
|
+
"darwin-arm64": {
|
|
221
|
+
checksum: "5e417d415ca74a435755e298a1d2d19ec47637fd03479de7a7d958eb382941f1",
|
|
222
|
+
manifest: "eb5a0356aebabe89c7b2780f8809e13d6e2ad6f342fed87cf99f9a5834344363"
|
|
223
|
+
},
|
|
224
|
+
"linux-arm64": {
|
|
225
|
+
checksum: "8ac7ad97f3d68bf485423001d1ce3cd92a8d4625313c47c8071867a9aeed3af9",
|
|
226
|
+
manifest: "6126266dd65b43c43d719e4dc4d33c4ea6a3267f99f34e3638637439a995af2e"
|
|
227
|
+
},
|
|
228
|
+
"linux-x64": {
|
|
229
|
+
checksum: "7d76463079619ba822123d250601bd35f6744ed5f8054a657ce472ce4dac76c8",
|
|
230
|
+
manifest: "f1a6c0c61d15eddb2ecfa4c3a72a5154621881ac2f0c413162ff1d0f4343bd0e"
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
"swift-6.4.0-RELEASE-custom-11345d60dca47496": {
|
|
235
|
+
identity: "11345d60dca47496",
|
|
236
|
+
archive: "tar.zst",
|
|
237
|
+
hosts: {
|
|
238
|
+
"darwin-arm64": {
|
|
239
|
+
checksum: "3df74a95c6f3ffa45aaa412027b390c99f9af81f6f6a41bf6d58a1a3079b5b02",
|
|
240
|
+
manifest: "8e5ed4be6adc967e4c7d955eff004c96d5dc050a589d50e1b3e694b70f9afd63"
|
|
241
|
+
},
|
|
242
|
+
"linux-x64": {
|
|
243
|
+
checksum: "a80e46add069932114bacc18640dc2e6a1a8f2a3ea7306a8ea8614d5cdf6e0e7",
|
|
244
|
+
manifest: "662d8869534f3cce827b1205973db1f9a1e1d92027005a5bdb6893f9c2a0b39d"
|
|
245
|
+
},
|
|
246
|
+
"linux-arm64": {
|
|
247
|
+
checksum: "4ca69bbc284fc7d54be57c80b744b88cce997d470ff23048795db54e7a6fbcba",
|
|
248
|
+
manifest: "85df84da2f814b6a71c264b37b83a81965ac33b5d2b465443d60c08cc7bc5dc3"
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
"swift-6.4.0-RELEASE-custom-81b2c791c27276bf": {
|
|
253
|
+
identity: "81b2c791c27276bf",
|
|
254
|
+
archive: "tar.gz",
|
|
255
|
+
hosts: {
|
|
256
|
+
"darwin-arm64": {
|
|
257
|
+
checksum: "bc51b83d9e2e01c9873875326544734486d3ff3a868d650379c304045e8838d3",
|
|
258
|
+
manifest: "a26eb9cb4fbd1dbd7faac20cced1ec5e67745bd358aa1180cc4abc1a0d21d6e6"
|
|
259
|
+
},
|
|
260
|
+
"linux-x64": {
|
|
261
|
+
checksum: "ea63bbdecae14ff8e9e3f535b521fedc65d51b31521c5d10cc332cb91882d058",
|
|
262
|
+
manifest: "b22b321ce3eb3fe044fa2122484a15c061f262c45d88a05b3fbace0a5bf038b6"
|
|
263
|
+
},
|
|
264
|
+
"linux-arm64": {
|
|
265
|
+
checksum: "3eef4ed57830908515d8def2882e312f7cabeea57ccda5b2a7585401ef98cc48",
|
|
266
|
+
manifest: "cfab342b8090fa83a697a391a7563cba14b771c24f07e1f8e19977f913ac88be"
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var SWIFT_PRIVATE_DISTRIBUTION_ENV = "PEACH_SWIFT_PRIVATE_DISTRIBUTION";
|
|
272
|
+
function privateSwiftDistributionRoot() {
|
|
273
|
+
const root = process.env[SWIFT_PRIVATE_DISTRIBUTION_ENV]?.trim();
|
|
274
|
+
return root ? path.resolve(root) : null;
|
|
275
|
+
}
|
|
276
|
+
function privateSwiftCompilerRelease() {
|
|
277
|
+
return process.env.PEACH_SWIFT_PRIVATE_COMPILER_RELEASE?.trim() || null;
|
|
278
|
+
}
|
|
279
|
+
function privateSwiftRelease() {
|
|
280
|
+
const compiler = privateSwiftCompilerRelease();
|
|
281
|
+
if (compiler) return compiler;
|
|
282
|
+
const root = privateSwiftDistributionRoot();
|
|
283
|
+
if (!root) return null;
|
|
284
|
+
const manifest = JSON.parse(
|
|
285
|
+
fs.readFileSync(path.join(root, "distribution.json"), "utf8")
|
|
286
|
+
);
|
|
287
|
+
const release = manifest && typeof manifest === "object" && Reflect.get(manifest, "release");
|
|
288
|
+
if (typeof release !== "string")
|
|
289
|
+
throw new Error(`${root} holds no Swift distribution release`);
|
|
290
|
+
return release;
|
|
291
|
+
}
|
|
292
|
+
function swiftHostCompilerPin(release) {
|
|
293
|
+
const compiler = compilerArchives[release];
|
|
294
|
+
if (!compiler) throw new Error(`unqualified host compiler ${release}`);
|
|
295
|
+
return compiler;
|
|
296
|
+
}
|
|
297
|
+
function registerSwiftHostCompiler(release, pin) {
|
|
298
|
+
compilerArchives[release] = pin;
|
|
299
|
+
}
|
|
300
|
+
function swiftHostCompilerArchive(release, host = `${process.platform}-${process.arch}`) {
|
|
301
|
+
const compiler = swiftHostCompilerPin(release);
|
|
302
|
+
const archive = compiler.hosts[host];
|
|
303
|
+
if (!archive) throw new Error(`unsupported Swift compiler host ${host}`);
|
|
304
|
+
return {
|
|
305
|
+
...archive,
|
|
306
|
+
identity: compiler.identity,
|
|
307
|
+
archive: compiler.archive,
|
|
308
|
+
url: `https://r2.sootsim.com/swift-host-compiler/${compiler.identity}/${archive.checksum}.${compiler.archive}`
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function swiftUpstreamRelease(release) {
|
|
312
|
+
return release.replace(/-custom-[a-f0-9]{16}$/, "");
|
|
313
|
+
}
|
|
314
|
+
function swiftCompilerRelease(release) {
|
|
315
|
+
return swiftUpstreamRelease(release).replace(
|
|
316
|
+
/^swift-(\d+\.\d+)\.0-RELEASE$/,
|
|
317
|
+
"swift-$1-RELEASE"
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
function swiftToolchainBin(release) {
|
|
321
|
+
if (process.platform === "darwin") {
|
|
322
|
+
const dirs = [os.homedir(), "/"].map(
|
|
323
|
+
(base) => path.join(base, `Library/Developer/Toolchains/${release}.xctoolchain/usr/bin`)
|
|
324
|
+
);
|
|
325
|
+
return dirs.find((dir) => fs.existsSync(path.join(dir, "swift"))) ?? dirs[0];
|
|
326
|
+
}
|
|
327
|
+
if (process.env.CONTRAST_SWIFT_TOOLCHAINS) {
|
|
328
|
+
return path.join(process.env.CONTRAST_SWIFT_TOOLCHAINS, release, "usr/bin");
|
|
329
|
+
}
|
|
330
|
+
const image = path.join("/opt/swift", release, "usr/bin");
|
|
331
|
+
if (fs.existsSync(path.join(image, "swift"))) return image;
|
|
332
|
+
if (release.includes("-custom-"))
|
|
333
|
+
return path.join(peachHome(), "swift", "toolchains", release, "usr/bin");
|
|
334
|
+
return (process.env.PATH ?? "").split(path.delimiter).find((dir) => dir && fs.existsSync(path.join(dir, "swift"))) ?? image;
|
|
335
|
+
}
|
|
336
|
+
var SWIFT_AUTOLINK_LINKER = ".peach-wasm-ld";
|
|
337
|
+
function writeSwiftAutolinkLinker(directory, toolchainBin, omitLibraries = []) {
|
|
338
|
+
const linker = path.join(directory, SWIFT_AUTOLINK_LINKER);
|
|
339
|
+
const script = `#!/bin/sh
|
|
340
|
+
set -eu
|
|
341
|
+
for arg in "$@"; do
|
|
342
|
+
case $arg in
|
|
343
|
+
@*)
|
|
344
|
+
rsp=\${arg#@}
|
|
345
|
+
awk 'BEGIN { RS = " "; ORS = " " } /^"-l/ { if (seen[$0]++) next }${omitLibraries.map((name2) => ` $0 == "\\"-l${name2}\\"" { next }`).join("")} $0 == "\\"-ObjC\\"" { next } { print }' "$rsp" > "$rsp.dedup"
|
|
346
|
+
mv "$rsp.dedup" "$rsp"
|
|
347
|
+
if [ "\${PEACH_DEFER_WASM_LINK:-}" = "1" ] && grep -q '"--start-lib"' "$rsp"; then
|
|
348
|
+
cp "$rsp" ${JSON.stringify(path.join(directory, ".peach-link.rsp"))}
|
|
349
|
+
out=$(awk 'BEGIN { RS = " " } prev == "\\"'-o'\\"" { gsub(/^"|"[[:space:]]*$/, ""); print; exit } { prev = $0 }' "$rsp")
|
|
350
|
+
if [ -n "$out" ]; then
|
|
351
|
+
printf '\\000asm\\001\\000\\000\\000' > "$out"
|
|
352
|
+
exit 0
|
|
353
|
+
fi
|
|
354
|
+
fi
|
|
355
|
+
;;
|
|
356
|
+
esac
|
|
357
|
+
done
|
|
358
|
+
exec ${JSON.stringify(path.join(toolchainBin, "wasm-ld"))} "$@"
|
|
359
|
+
`;
|
|
360
|
+
if (fs.existsSync(linker) && fs.readFileSync(linker, "utf8") === script) return linker;
|
|
361
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
362
|
+
const next = `${linker}.${process.pid}`;
|
|
363
|
+
fs.writeFileSync(next, script, { mode: 493 });
|
|
364
|
+
fs.renameSync(next, linker);
|
|
365
|
+
return linker;
|
|
366
|
+
}
|
|
367
|
+
function peachHome() {
|
|
368
|
+
const override = process.env.PEACH_HOME;
|
|
369
|
+
return override ? path.resolve(override) : path.join(os.homedir(), ".peach");
|
|
370
|
+
}
|
|
371
|
+
function peachSwiftSdksDir() {
|
|
372
|
+
return process.env.PEACH_SWIFT_SDKS_PATH ?? path.join(peachHome(), "swift", "sdks");
|
|
373
|
+
}
|
|
374
|
+
var BUILDERS_ROOT_ENV = "CONTRAST_BUILDERS_ROOT";
|
|
375
|
+
function buildersRootEnv() {
|
|
376
|
+
const root = process.env[BUILDERS_ROOT_ENV]?.trim();
|
|
377
|
+
return root ? root : null;
|
|
378
|
+
}
|
|
379
|
+
function swiftCacheDir() {
|
|
380
|
+
const root = buildersRootEnv();
|
|
381
|
+
return root ? path.join(root, "peach-cache") : path.join(peachHome(), "cache");
|
|
382
|
+
}
|
|
383
|
+
var SWIFT_CACHE_HOLD_MARKER = ".tm-hold";
|
|
384
|
+
var SWIFT_CACHE_HOLD_MS = 7 * 24 * 36e5;
|
|
385
|
+
function refreshSwiftCacheHold(directory) {
|
|
386
|
+
const cache = swiftCacheDir();
|
|
387
|
+
if (!fs.existsSync(cache)) return;
|
|
388
|
+
const cacheRoot = fs.realpathSync(cache);
|
|
389
|
+
const relative = path.relative(cacheRoot, fs.realpathSync(directory));
|
|
390
|
+
const [kind, key] = relative.split(path.sep);
|
|
391
|
+
if (!key || kind === ".." || path.isAbsolute(relative)) return;
|
|
392
|
+
const entry = path.join(cacheRoot, kind, key);
|
|
393
|
+
if (!buildersRootEnv()) {
|
|
394
|
+
const now = /* @__PURE__ */ new Date();
|
|
395
|
+
fs.utimesSync(entry, now, now);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const marker = path.join(entry, SWIFT_CACHE_HOLD_MARKER);
|
|
399
|
+
const { O_WRONLY, O_CREAT, O_NOFOLLOW, O_NONBLOCK } = fs.constants;
|
|
400
|
+
const fd = fs.openSync(marker, O_WRONLY | O_CREAT | O_NOFOLLOW | O_NONBLOCK, 420);
|
|
401
|
+
try {
|
|
402
|
+
if (!fs.fstatSync(fd).isFile()) throw new Error(`invalid Swift cache hold: ${marker}`);
|
|
403
|
+
const now = /* @__PURE__ */ new Date();
|
|
404
|
+
fs.futimesSync(fd, now, now);
|
|
405
|
+
} finally {
|
|
406
|
+
fs.closeSync(fd);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function swiftPackageCacheOptions() {
|
|
410
|
+
const root = path.join(swiftCacheDir(), "swiftpm");
|
|
411
|
+
return [
|
|
412
|
+
...["cache", "config", "security"].flatMap((name2) => [
|
|
413
|
+
`--${name2}-path`,
|
|
414
|
+
path.join(root, name2)
|
|
415
|
+
]),
|
|
416
|
+
"--disable-keychain"
|
|
417
|
+
];
|
|
418
|
+
}
|
|
419
|
+
var scratch;
|
|
420
|
+
var linuxLibraryPath;
|
|
421
|
+
var linuxHostSdkRoot;
|
|
422
|
+
function swiftEnvironment(base = process.env) {
|
|
423
|
+
return {
|
|
424
|
+
...base,
|
|
425
|
+
...linuxHostSdkRoot ? { SDKROOT: linuxHostSdkRoot } : {},
|
|
426
|
+
...linuxLibraryPath ? {
|
|
427
|
+
LD_LIBRARY_PATH: [linuxLibraryPath, base.LD_LIBRARY_PATH].filter(Boolean).join(":")
|
|
428
|
+
} : {},
|
|
429
|
+
...scratch ? {
|
|
430
|
+
TMPDIR: scratch.directory,
|
|
431
|
+
XDG_CONFIG_HOME: path.join(scratch.home, ".config"),
|
|
432
|
+
XDG_CACHE_HOME: path.join(scratch.home, ".cache"),
|
|
433
|
+
XDG_DATA_HOME: path.join(scratch.home, ".local", "share"),
|
|
434
|
+
...process.platform === "linux" ? { HOME: scratch.home } : {}
|
|
435
|
+
} : {}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function swiftSourceSdk(root) {
|
|
439
|
+
const pin = JSON.parse(
|
|
440
|
+
fs.readFileSync(path.join(root, "toolchain.json"), "utf8")
|
|
441
|
+
);
|
|
442
|
+
if (!pin || typeof pin !== "object" || typeof Reflect.get(pin, "wasmSdkId") !== "string" || typeof Reflect.get(pin, "wasmSdkUrl") !== "string" || typeof Reflect.get(pin, "wasmSdkChecksum") !== "string")
|
|
443
|
+
throw new Error(`${root} pins no wasm SDK id, url and checksum`);
|
|
444
|
+
return {
|
|
445
|
+
wasmSdkId: String(Reflect.get(pin, "wasmSdkId")),
|
|
446
|
+
wasmSdkUrl: String(Reflect.get(pin, "wasmSdkUrl")),
|
|
447
|
+
wasmSdkChecksum: String(Reflect.get(pin, "wasmSdkChecksum"))
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function swiftToolchain(release, sdk) {
|
|
451
|
+
validateSwiftSdkIdentity(sdk);
|
|
452
|
+
let bin;
|
|
453
|
+
try {
|
|
454
|
+
bin = swiftCompiler(release);
|
|
455
|
+
} catch (error) {
|
|
456
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
457
|
+
throw new Error(
|
|
458
|
+
`Swift compiler ${release} is unavailable or differs from its pin: ${reason}
|
|
459
|
+
${swiftInstallSteps(release, sdk)}`
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
const swift = path.join(bin, "swift");
|
|
463
|
+
const listed = [swift, sdk.wasmSdkId, ...swiftSdkOptions()].join("\0");
|
|
464
|
+
if (!listedSdks.has(listed)) {
|
|
465
|
+
const sdks = execFileSync(swift, ["sdk", "list", ...swiftSdkOptions()], {
|
|
466
|
+
encoding: "utf8",
|
|
467
|
+
env: swiftEnvironment(),
|
|
468
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
469
|
+
});
|
|
470
|
+
if (!sdks.split("\n").some((line) => line.trim() === sdk.wasmSdkId)) {
|
|
471
|
+
throw new Error(
|
|
472
|
+
`peach compiles Swift with the ${sdk.wasmSdkId} SDK, which ${swift} does not have.
|
|
473
|
+
${swiftSdkInstallStep(release, sdk)}`
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
listedSdks.add(listed);
|
|
477
|
+
}
|
|
478
|
+
try {
|
|
479
|
+
verifySwiftSdk(swift, sdk);
|
|
480
|
+
} catch (error) {
|
|
481
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
482
|
+
throw new Error(`${reason}
|
|
483
|
+
${swiftSdkInstallStep(release, sdk)}`);
|
|
484
|
+
}
|
|
485
|
+
return bin;
|
|
486
|
+
}
|
|
487
|
+
function validateSwiftSdkIdentity(sdk) {
|
|
488
|
+
if (!/^[a-zA-Z0-9._-]+$/.test(sdk.wasmSdkId) || !/^[a-f0-9]{64}$/.test(sdk.wasmSdkChecksum))
|
|
489
|
+
throw new Error("invalid Swift SDK identity or checksum");
|
|
490
|
+
}
|
|
491
|
+
var listedSdks = /* @__PURE__ */ new Set();
|
|
492
|
+
var sdkBundles = /* @__PURE__ */ new Map();
|
|
493
|
+
function swiftSdkBundle(swift, id, options) {
|
|
494
|
+
const key = [swift, id, ...options].join("\0");
|
|
495
|
+
const known = sdkBundles.get(key);
|
|
496
|
+
if (known && fs.existsSync(known)) return known;
|
|
497
|
+
const configuration = execFileSync(
|
|
498
|
+
swift,
|
|
499
|
+
["sdk", "configure", ...options, "--show-configuration", id, SWIFT_WASM_TRIPLE],
|
|
500
|
+
{ encoding: "utf8", env: swiftEnvironment(), stdio: ["ignore", "pipe", "pipe"] }
|
|
501
|
+
);
|
|
502
|
+
const sysroot = /^sdkRootPath: (.+)$/m.exec(configuration)?.[1]?.trim();
|
|
503
|
+
if (!sysroot || !path.isAbsolute(sysroot))
|
|
504
|
+
throw new Error(`Swift SDK ${id} reports no absolute sdkRootPath`);
|
|
505
|
+
let bundle = sysroot;
|
|
506
|
+
while (!bundle.endsWith(".artifactbundle")) {
|
|
507
|
+
const parent = path.dirname(bundle);
|
|
508
|
+
if (parent === bundle)
|
|
509
|
+
throw new Error(`Swift SDK ${id} is outside an artifact bundle`);
|
|
510
|
+
bundle = parent;
|
|
511
|
+
}
|
|
512
|
+
const resolved = fs.realpathSync(bundle);
|
|
513
|
+
sdkBundles.set(key, resolved);
|
|
514
|
+
return resolved;
|
|
515
|
+
}
|
|
516
|
+
var sdkFileHashes = /* @__PURE__ */ new Map();
|
|
517
|
+
var sdkFilesHashed = 0;
|
|
518
|
+
var loadedSdkHashFiles = /* @__PURE__ */ new Set();
|
|
519
|
+
function swiftSdkPayloadChecksum(bundle) {
|
|
520
|
+
const hash = createHash("sha256");
|
|
521
|
+
function visit(relative) {
|
|
522
|
+
const file = path.join(bundle, relative);
|
|
523
|
+
const stat = fs.lstatSync(file, { bigint: true });
|
|
524
|
+
if (stat.isSymbolicLink()) {
|
|
525
|
+
const resolved = path.relative(bundle, fs.realpathSync(file));
|
|
526
|
+
if (resolved === ".." || resolved.startsWith(`..${path.sep}`) || path.isAbsolute(resolved))
|
|
527
|
+
throw new Error(`Swift SDK symlink escapes its bundle: ${relative}`);
|
|
528
|
+
hash.update(JSON.stringify([relative, "link", fs.readlinkSync(file)]) + "\n");
|
|
529
|
+
} else if (stat.isDirectory()) {
|
|
530
|
+
hash.update(JSON.stringify([relative, "directory"]) + "\n");
|
|
531
|
+
for (const name2 of fs.readdirSync(file).sort()) visit(path.join(relative, name2));
|
|
532
|
+
} else if (stat.isFile()) {
|
|
533
|
+
const stamp = [stat.dev, stat.ino, stat.size, stat.mtimeNs, stat.ctimeNs].join(":");
|
|
534
|
+
let cached = sdkFileHashes.get(file);
|
|
535
|
+
if (cached?.stamp !== stamp) {
|
|
536
|
+
cached = { stamp, checksum: swiftFileChecksum(file) };
|
|
537
|
+
sdkFileHashes.set(file, cached);
|
|
538
|
+
sdkFilesHashed++;
|
|
539
|
+
}
|
|
540
|
+
hash.update(
|
|
541
|
+
JSON.stringify([relative, "file", Number(stat.mode & 0o111n), cached.checksum]) + "\n"
|
|
542
|
+
);
|
|
543
|
+
} else throw new Error(`unsupported Swift SDK payload entry: ${relative}`);
|
|
544
|
+
}
|
|
545
|
+
visit("");
|
|
546
|
+
return hash.digest("hex");
|
|
547
|
+
}
|
|
548
|
+
function swiftSdkVerificationFile(bundle, sdk) {
|
|
549
|
+
return path.join(
|
|
550
|
+
path.dirname(bundle),
|
|
551
|
+
".peach-sdk-checksums",
|
|
552
|
+
`${sdk.wasmSdkId}-${sdk.wasmSdkChecksum}.json`
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
function verifySwiftSdk(swift, sdk) {
|
|
556
|
+
const bundle = swiftSdkBundle(swift, sdk.wasmSdkId, swiftSdkOptions());
|
|
557
|
+
const file = swiftSdkVerificationFile(bundle, sdk);
|
|
558
|
+
if (!fs.existsSync(file))
|
|
559
|
+
throw new Error(
|
|
560
|
+
`Swift SDK ${sdk.wasmSdkId} has no verified archive binding for ${sdk.wasmSdkChecksum}. Hydrate it with installSwiftWasmSdk before compiling.`
|
|
561
|
+
);
|
|
562
|
+
const receipt = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
563
|
+
const stamps = file.replace(/\.json$/, ".files.json");
|
|
564
|
+
if (!loadedSdkHashFiles.has(stamps)) {
|
|
565
|
+
loadedSdkHashFiles.add(stamps);
|
|
566
|
+
const saved = fs.existsSync(stamps) ? JSON.parse(fs.readFileSync(stamps, "utf8")) : null;
|
|
567
|
+
if (saved && typeof saved === "object") {
|
|
568
|
+
for (const [entry, value] of Object.entries(saved))
|
|
569
|
+
if (Array.isArray(value) && typeof value[0] === "string" && typeof value[1] === "string")
|
|
570
|
+
sdkFileHashes.set(entry, { stamp: value[0], checksum: value[1] });
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const hashedBefore = sdkFilesHashed;
|
|
574
|
+
if (!receipt || typeof receipt !== "object" || !("wasmSdkId" in receipt) || receipt.wasmSdkId !== sdk.wasmSdkId || !("wasmSdkChecksum" in receipt) || receipt.wasmSdkChecksum !== sdk.wasmSdkChecksum || !("payloadChecksum" in receipt) || receipt.payloadChecksum !== swiftSdkPayloadChecksum(bundle))
|
|
575
|
+
throw new Error(
|
|
576
|
+
`Swift SDK ${sdk.wasmSdkId} installed payload differs from its verified archive ${sdk.wasmSdkChecksum}`
|
|
577
|
+
);
|
|
578
|
+
if (sdkFilesHashed === hashedBefore) return;
|
|
579
|
+
const temporary = `${stamps}.${process.pid}.tmp`;
|
|
580
|
+
try {
|
|
581
|
+
fs.writeFileSync(
|
|
582
|
+
temporary,
|
|
583
|
+
JSON.stringify(
|
|
584
|
+
Object.fromEntries(
|
|
585
|
+
[...sdkFileHashes].filter(([entry]) => entry.startsWith(bundle + path.sep)).map(([entry, { stamp, checksum }]) => [entry, [stamp, checksum]])
|
|
586
|
+
)
|
|
587
|
+
)
|
|
588
|
+
);
|
|
589
|
+
fs.renameSync(temporary, stamps);
|
|
590
|
+
} finally {
|
|
591
|
+
fs.rmSync(temporary, { force: true });
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function swiftFileChecksum(file) {
|
|
595
|
+
const hash = createHash("sha256");
|
|
596
|
+
const fd = fs.openSync(file, "r");
|
|
597
|
+
const buffer = Buffer.allocUnsafe(1024 * 1024);
|
|
598
|
+
try {
|
|
599
|
+
let size = fs.readSync(fd, buffer, 0, buffer.length, null);
|
|
600
|
+
while (size > 0) {
|
|
601
|
+
hash.update(buffer.subarray(0, size));
|
|
602
|
+
size = fs.readSync(fd, buffer, 0, buffer.length, null);
|
|
603
|
+
}
|
|
604
|
+
} finally {
|
|
605
|
+
fs.closeSync(fd);
|
|
606
|
+
}
|
|
607
|
+
return hash.digest("hex");
|
|
608
|
+
}
|
|
609
|
+
function swiftHostCompilerFiles(release, root) {
|
|
610
|
+
const file = path.join(root, "compiler.json");
|
|
611
|
+
if (!fs.existsSync(file))
|
|
612
|
+
throw new Error(`Swift compiler ${release} is missing ${file}`);
|
|
613
|
+
const privateCompiler = release === privateSwiftRelease();
|
|
614
|
+
const pin = privateCompiler ? {
|
|
615
|
+
identity: release.replace(/^.*-custom-/, ""),
|
|
616
|
+
manifest: swiftFileChecksum(file)
|
|
617
|
+
} : swiftHostCompilerArchive(release);
|
|
618
|
+
if (swiftFileChecksum(file) !== pin.manifest)
|
|
619
|
+
throw new Error(`Swift compiler manifest mismatch at ${root}`);
|
|
620
|
+
const manifest = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
621
|
+
if (!manifest || typeof manifest !== "object" || !("identity" in manifest) || manifest.identity !== pin.identity || !("host" in manifest) || manifest.host !== `${process.platform}-${process.arch}` || !("files" in manifest) || !manifest.files || typeof manifest.files !== "object") {
|
|
622
|
+
throw new Error(`Swift compiler identity mismatch at ${root}`);
|
|
623
|
+
}
|
|
624
|
+
const files = Object.entries(manifest.files);
|
|
625
|
+
const fileNames = Object.keys(manifest.files);
|
|
626
|
+
const build = "compilerBuild" in manifest ? manifest.compilerBuild : null;
|
|
627
|
+
const hasLinkerReceipt = build !== null && typeof build === "object" && typeof Reflect.get(build, "lldSHA256") === "string";
|
|
628
|
+
const hasSharedLLVM = build && typeof build === "object" && "hostLibraries" in build;
|
|
629
|
+
const llvmFiles = hasSharedLLVM ? process.platform === "darwin" ? 2 : 1 : 0;
|
|
630
|
+
const hasLLVMFiles = process.platform === "darwin" ? fileNames.includes("usr/lib/libLLVM.dylib") && fileNames.includes("usr/lib/libLTO.dylib") : fileNames.some((file2) => /^usr\/lib\/libLLVM\.so\.\d+\.\d+$/.test(file2));
|
|
631
|
+
const llvmTools = [
|
|
632
|
+
"llvm-ar",
|
|
633
|
+
"llvm-nm",
|
|
634
|
+
"llvm-objcopy",
|
|
635
|
+
"llvm-objdump",
|
|
636
|
+
"llvm-symbolizer"
|
|
637
|
+
];
|
|
638
|
+
const expectedFiles = [
|
|
639
|
+
"9f57741c6333ce5e",
|
|
640
|
+
"139bc3bba3c4f73c",
|
|
641
|
+
"cd5337e8ab49fd61"
|
|
642
|
+
].includes(pin.identity) ? 17 : hasLinkerReceipt || ["38af8a6bc3d9e72c", "17479c8af01737a3"].includes(pin.identity) ? 18 : 16;
|
|
643
|
+
if (files.length !== expectedFiles + llvmFiles + (hasSharedLLVM ? llvmTools.length : 0) || hasSharedLLVM && (!hasLLVMFiles || !llvmTools.every((tool) => fileNames.includes(`usr/bin/${tool}`))) || expectedFiles >= 17 && !("usr/bin/lld" in manifest.files) || expectedFiles === 18 && !fileNames.some((file2) => /\/lib_InternalSwiftScan\.(so|dylib)$/.test(file2)))
|
|
644
|
+
throw new Error("incomplete Swift compiler manifest");
|
|
645
|
+
return files.map(([relative, checksum]) => {
|
|
646
|
+
if (relative.startsWith("/") || relative.split("/").includes("..") || typeof checksum !== "string")
|
|
647
|
+
throw new Error(`Swift compiler binary mismatch: ${relative}`);
|
|
648
|
+
const binary = path.join(root, relative);
|
|
649
|
+
if (!fs.existsSync(binary))
|
|
650
|
+
throw new Error(`Swift compiler ${release} is missing ${binary}`);
|
|
651
|
+
return [relative, checksum];
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
function verifySwiftHostCompiler(release, root) {
|
|
655
|
+
for (const [relative, checksum] of swiftHostCompilerFiles(release, root)) {
|
|
656
|
+
if (swiftFileChecksum(path.join(root, relative)) !== checksum)
|
|
657
|
+
throw new Error(`Swift compiler binary mismatch: ${relative}`);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
var verifiedCompilers = /* @__PURE__ */ new Map();
|
|
661
|
+
function swiftCompiler(release, prepareHost) {
|
|
662
|
+
const cached = verifiedCompilers.get(release);
|
|
663
|
+
if (cached) {
|
|
664
|
+
prepareHost?.();
|
|
665
|
+
return cached;
|
|
666
|
+
}
|
|
667
|
+
const bin = swiftToolchainBin(release);
|
|
668
|
+
const binary = path.join(bin, "swift");
|
|
669
|
+
if (!fs.existsSync(binary))
|
|
670
|
+
throw new Error(`Swift compiler ${release} is missing ${binary}`);
|
|
671
|
+
if (release.includes("-custom-"))
|
|
672
|
+
verifySwiftHostCompiler(release, path.resolve(bin, "../.."));
|
|
673
|
+
prepareHost?.();
|
|
674
|
+
const version = execFileSync(binary, ["--version"], {
|
|
675
|
+
encoding: "utf8",
|
|
676
|
+
env: swiftEnvironment(),
|
|
677
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
678
|
+
});
|
|
679
|
+
if (!version.includes(`(${swiftCompilerRelease(release)})`))
|
|
680
|
+
throw new Error(`compiler release mismatch at ${bin}`);
|
|
681
|
+
verifiedCompilers.set(release, bin);
|
|
682
|
+
return bin;
|
|
683
|
+
}
|
|
684
|
+
function swiftSdkInstallStep(release, wasmSdk) {
|
|
685
|
+
return `install the pinned SDK:
|
|
686
|
+
${swiftSdkInstallCommand(release, wasmSdk)}`;
|
|
687
|
+
}
|
|
688
|
+
function swiftSdkInstallCommand(release, wasmSdk) {
|
|
689
|
+
const { wasmSdkId, wasmSdkUrl, wasmSdkChecksum } = wasmSdk;
|
|
690
|
+
const program = `import { installSwiftWasmSdk } from "peachsim/swift-toolchain"; await installSwiftWasmSdk(${JSON.stringify(release)}, ${JSON.stringify({ wasmSdkId, wasmSdkUrl, wasmSdkChecksum })})`;
|
|
691
|
+
const environment = `PEACH_SWIFT_SDKS_PATH='${path.resolve(peachSwiftSdksDir()).replaceAll("'", "'\\''")}' `;
|
|
692
|
+
return `${environment}node --input-type=module -e '${program.replaceAll("'", "'\\''")}'`;
|
|
693
|
+
}
|
|
694
|
+
function swiftInstallSteps(release, wasmSdk) {
|
|
695
|
+
const { note, commands } = swiftCompilerInstall(release);
|
|
696
|
+
return [
|
|
697
|
+
`install the pinned compiler and SDK${note}:`,
|
|
698
|
+
...[...commands, swiftSdkInstallCommand(release, wasmSdk)].map((line) => ` ${line}`)
|
|
699
|
+
].join("\n");
|
|
700
|
+
}
|
|
701
|
+
function swiftCompilerInstall(release) {
|
|
702
|
+
if (release === privateSwiftRelease())
|
|
703
|
+
return { note: " (private: its builder installs it)", commands: [] };
|
|
704
|
+
if (release.includes("-custom-")) {
|
|
705
|
+
const archive = swiftHostCompilerArchive(release);
|
|
706
|
+
const directory = path.resolve(swiftToolchainBin(release), "../..");
|
|
707
|
+
const file = `compiler.${archive.archive}`;
|
|
708
|
+
return {
|
|
709
|
+
note: "",
|
|
710
|
+
commands: [
|
|
711
|
+
`curl -fsSL ${archive.url} -o ${file}`,
|
|
712
|
+
`echo '${archive.checksum} ${file}' | shasum -a 256 -c -`,
|
|
713
|
+
`mkdir -p '${directory}.partial'`,
|
|
714
|
+
`tar -xf ${file} --strip-components=1 -C '${directory}.partial' && mv '${directory}.partial' '${directory}'`
|
|
715
|
+
]
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
const version = release.replace(/^swift-/, "").toLowerCase();
|
|
719
|
+
const downloads = `https://download.swift.org/swift-${version}`;
|
|
720
|
+
if (process.platform === "darwin") {
|
|
721
|
+
return {
|
|
722
|
+
note: "",
|
|
723
|
+
commands: [
|
|
724
|
+
`curl -fsSLO ${downloads}/xcode/${release}/${release}-osx.pkg`,
|
|
725
|
+
`installer -pkg ${release}-osx.pkg -target CurrentUserHomeDirectory`
|
|
726
|
+
]
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
const platform = `debian12${process.arch === "arm64" ? "-aarch64" : ""}`;
|
|
730
|
+
const name2 = `${release}-${platform}`;
|
|
731
|
+
return {
|
|
732
|
+
note: " (Debian 12 shown; other distributions: https://www.swift.org/install/linux/)",
|
|
733
|
+
commands: [
|
|
734
|
+
`curl -fsSL ${downloads}/${platform}/${release}/${name2}.tar.gz | tar -xz -C /opt`,
|
|
735
|
+
`export PATH=/opt/${name2}/usr/bin:$PATH`
|
|
736
|
+
]
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// src/process-group.ts
|
|
741
|
+
import { spawn as spawn2, spawnSync as spawnSync2 } from "node:child_process";
|
|
742
|
+
import {
|
|
743
|
+
appendFileSync,
|
|
744
|
+
mkdtempSync,
|
|
745
|
+
readdirSync as readdirSync2,
|
|
746
|
+
readFileSync as readFileSync2,
|
|
747
|
+
rmSync,
|
|
748
|
+
writeFileSync
|
|
749
|
+
} from "node:fs";
|
|
750
|
+
import { tmpdir } from "node:os";
|
|
751
|
+
import { join } from "node:path";
|
|
752
|
+
import { Duplex } from "node:stream";
|
|
753
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
754
|
+
|
|
755
|
+
// src/process-table.ts
|
|
756
|
+
import { spawnSync } from "node:child_process";
|
|
757
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
758
|
+
import { endianness } from "node:os";
|
|
759
|
+
function linuxClockTicks() {
|
|
760
|
+
const auxv = readFileSync("/proc/self/auxv");
|
|
761
|
+
const word = (offset) => endianness() === "LE" ? auxv.readBigUInt64LE(offset) : auxv.readBigUInt64BE(offset);
|
|
762
|
+
for (let offset = 0; offset + 16 <= auxv.length; offset += 16) {
|
|
763
|
+
if (word(offset) !== 17n) continue;
|
|
764
|
+
const ticks = Number(word(offset + 8));
|
|
765
|
+
if (!Number.isSafeInteger(ticks) || ticks <= 0)
|
|
766
|
+
throw new Error("unreadable process clock tick rate");
|
|
767
|
+
return ticks;
|
|
768
|
+
}
|
|
769
|
+
throw new Error("missing process clock tick rate");
|
|
770
|
+
}
|
|
771
|
+
var clockTicks;
|
|
772
|
+
function readProcessTable(timeoutMs = 1e4, pid) {
|
|
773
|
+
if (process.platform === "linux") {
|
|
774
|
+
clockTicks ?? (clockTicks = linuxClockTicks());
|
|
775
|
+
const boot = /^btime (\d+)$/m.exec(readFileSync("/proc/stat", "utf8"));
|
|
776
|
+
if (!boot) throw new Error("unreadable process boot time");
|
|
777
|
+
const bootMs = Number(boot[1]) * 1e3;
|
|
778
|
+
const deadline = Date.now() + timeoutMs;
|
|
779
|
+
const rows2 = [];
|
|
780
|
+
const names = pid === void 0 ? readdirSync("/proc") : [String(pid)];
|
|
781
|
+
for (const name2 of names) {
|
|
782
|
+
if (!/^\d+$/.test(name2)) continue;
|
|
783
|
+
if (Date.now() >= deadline)
|
|
784
|
+
throw new Error("process inspection exceeded its deadline");
|
|
785
|
+
let stat;
|
|
786
|
+
try {
|
|
787
|
+
stat = readFileSync(`/proc/${name2}/stat`, "utf8");
|
|
788
|
+
} catch (error) {
|
|
789
|
+
if (error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ESRCH"))
|
|
790
|
+
continue;
|
|
791
|
+
throw error;
|
|
792
|
+
}
|
|
793
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).trim().split(/\s+/);
|
|
794
|
+
const pgid = Number(fields[2]);
|
|
795
|
+
const ticks = Number(fields[19]);
|
|
796
|
+
if (!fields[0] || !Number.isSafeInteger(pgid) || !Number.isSafeInteger(ticks))
|
|
797
|
+
throw new Error(`unreadable process table row for ${name2}`);
|
|
798
|
+
rows2.push({
|
|
799
|
+
pid: Number(name2),
|
|
800
|
+
pgid,
|
|
801
|
+
zombie: fields[0] === "Z" || fields[0] === "X",
|
|
802
|
+
start: bootMs + ticks * 1e3 / clockTicks
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
return rows2;
|
|
806
|
+
}
|
|
807
|
+
const result = spawnSync(
|
|
808
|
+
"ps",
|
|
809
|
+
[
|
|
810
|
+
...pid === void 0 ? ["-A"] : ["-p", String(pid)],
|
|
811
|
+
"-o",
|
|
812
|
+
"pid=,pgid=,stat=,lstart="
|
|
813
|
+
],
|
|
814
|
+
{
|
|
815
|
+
encoding: "utf8",
|
|
816
|
+
env: { ...process.env, LC_ALL: "C", TZ: "UTC" },
|
|
817
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
818
|
+
timeout: timeoutMs,
|
|
819
|
+
killSignal: "SIGKILL"
|
|
820
|
+
}
|
|
821
|
+
);
|
|
822
|
+
if (!result.error && pid !== void 0 && result.status === 1 && !result.stdout.trim() && !result.stderr.trim())
|
|
823
|
+
return [];
|
|
824
|
+
if (result.error || result.status !== 0)
|
|
825
|
+
throw new Error(
|
|
826
|
+
result.error?.message || result.stderr.trim() || `ps exited ${result.status}`
|
|
827
|
+
);
|
|
828
|
+
const rows = result.stdout.split("\n").filter((line) => line.trim()).map((line) => {
|
|
829
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.+?)\s*$/.exec(line);
|
|
830
|
+
if (!match) throw new Error(`unreadable process table row: ${line}`);
|
|
831
|
+
const start = Date.parse(`${match[4]} UTC`);
|
|
832
|
+
if (Number.isNaN(start))
|
|
833
|
+
throw new Error(`unreadable process start time "${match[4]}"`);
|
|
834
|
+
return {
|
|
835
|
+
pid: Number(match[1]),
|
|
836
|
+
pgid: Number(match[2]),
|
|
837
|
+
zombie: match[3].startsWith("Z"),
|
|
838
|
+
start
|
|
839
|
+
};
|
|
840
|
+
});
|
|
841
|
+
return pid === void 0 ? rows : rows.filter((row) => row.pid === pid);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// src/process-group.ts
|
|
845
|
+
var GRACEFUL_TIMEOUT_MS = 3e3;
|
|
846
|
+
var FORCED_EXIT_TIMEOUT_MS = 1e3;
|
|
847
|
+
var EXIT_SCAN_TIMEOUT_MS = 1e4;
|
|
848
|
+
var INSPECTION_TIMEOUT_MS = 1e4;
|
|
849
|
+
var FREEZE_GUARD_NAME = "process-freeze-guard";
|
|
850
|
+
function listProcessChildren(parents, timeoutMs = 1e4) {
|
|
851
|
+
if (parents.some((pid) => !Number.isInteger(pid) || pid <= 1))
|
|
852
|
+
throw new Error("process inspection requires valid parent pids");
|
|
853
|
+
if (parents.length === 0) return [];
|
|
854
|
+
const deadline = Date.now() + timeoutMs;
|
|
855
|
+
let children;
|
|
856
|
+
if (process.platform === "linux") {
|
|
857
|
+
const lists = [];
|
|
858
|
+
for (const parent of parents) {
|
|
859
|
+
try {
|
|
860
|
+
const tasks = `/proc/${parent}/task`;
|
|
861
|
+
for (const thread of readdirSync2(tasks)) {
|
|
862
|
+
if (Date.now() >= deadline)
|
|
863
|
+
throw new Error("process descendant inspection exceeded its deadline");
|
|
864
|
+
try {
|
|
865
|
+
lists.push(readFileSync2(`${tasks}/${thread}/children`, "utf8"));
|
|
866
|
+
} catch (error) {
|
|
867
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
868
|
+
throw error;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
} catch (error) {
|
|
872
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
873
|
+
throw error;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
children = lists.join(" ");
|
|
877
|
+
} else {
|
|
878
|
+
const result = spawnSync2("pgrep", ["-P", parents.join(",")], {
|
|
879
|
+
encoding: "utf8",
|
|
880
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
881
|
+
timeout: Math.max(1, timeoutMs),
|
|
882
|
+
killSignal: "SIGKILL"
|
|
883
|
+
});
|
|
884
|
+
if (result.error || result.status !== 0 && result.status !== 1) {
|
|
885
|
+
throw new Error(
|
|
886
|
+
`could not inspect descendants of processes ${parents.join(",")}: ${result.error?.message || result.stderr.trim() || `pgrep exited ${result.status}`}`
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
children = result.stdout;
|
|
890
|
+
}
|
|
891
|
+
return [...new Set(children.trim().split(/\s+/).map(Number))].filter(
|
|
892
|
+
(pid) => Number.isInteger(pid) && pid > 1 && !parents.includes(pid)
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
function signalProcessGroup(child, signal) {
|
|
896
|
+
if (!child.pid) return;
|
|
897
|
+
try {
|
|
898
|
+
process.kill(-child.pid, signal);
|
|
899
|
+
} catch {
|
|
900
|
+
try {
|
|
901
|
+
process.kill(child.pid, signal);
|
|
902
|
+
} catch {
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
async function protectProcessFreeze(rootPid) {
|
|
907
|
+
const directory = mkdtempSync(join(tmpdir(), "peach-process-freeze-"));
|
|
908
|
+
const journal = join(directory, "targets");
|
|
909
|
+
writeFileSync(journal, `group ${rootPid}
|
|
910
|
+
pid ${rootPid}
|
|
911
|
+
`, { mode: 384 });
|
|
912
|
+
const guard = spawn2(
|
|
913
|
+
"/bin/sh",
|
|
914
|
+
[
|
|
915
|
+
"-c",
|
|
916
|
+
`
|
|
917
|
+
(
|
|
918
|
+
journal=$1
|
|
919
|
+
cleanup() {
|
|
920
|
+
trap '' HUP INT TERM
|
|
921
|
+
while read -r kind pid; do
|
|
922
|
+
case $kind in
|
|
923
|
+
group) target=-$pid ;;
|
|
924
|
+
pid) target=$pid ;;
|
|
925
|
+
*) continue ;;
|
|
926
|
+
esac
|
|
927
|
+
kill -TERM "$target" 2>/dev/null || :
|
|
928
|
+
kill -CONT "$target" 2>/dev/null || :
|
|
929
|
+
done < "$journal"
|
|
930
|
+
}
|
|
931
|
+
trap 'cleanup; exit 0' HUP INT TERM
|
|
932
|
+
trap 'rm -f "$journal"; rmdir "\${journal%/*}"' EXIT
|
|
933
|
+
printf 'ready
|
|
934
|
+
'
|
|
935
|
+
if ! IFS= read -r command <&3 || [ "$command" != disarm ]; then cleanup; fi
|
|
936
|
+
) &
|
|
937
|
+
`,
|
|
938
|
+
FREEZE_GUARD_NAME,
|
|
939
|
+
journal
|
|
940
|
+
],
|
|
941
|
+
{
|
|
942
|
+
detached: true,
|
|
943
|
+
// the launcher exits before capture, reparenting the guard outside any
|
|
944
|
+
// nested target tree. fd 3 survives that exit, unlike child.stdin.
|
|
945
|
+
stdio: ["ignore", "pipe", "ignore", "pipe"]
|
|
946
|
+
}
|
|
947
|
+
);
|
|
948
|
+
let guardClosed = false;
|
|
949
|
+
const closed = new Promise(
|
|
950
|
+
(resolveClose) => guard.once("close", () => {
|
|
951
|
+
guardClosed = true;
|
|
952
|
+
resolveClose();
|
|
953
|
+
})
|
|
954
|
+
);
|
|
955
|
+
const control = guard.stdio[3];
|
|
956
|
+
const output = guard.stdout;
|
|
957
|
+
if (!(control instanceof Duplex) || !output) {
|
|
958
|
+
guard.kill();
|
|
959
|
+
await closed;
|
|
960
|
+
rmSync(directory, { recursive: true, force: true });
|
|
961
|
+
throw new Error("process freeze guard control pipe is unavailable");
|
|
962
|
+
}
|
|
963
|
+
control.on("error", () => {
|
|
964
|
+
});
|
|
965
|
+
try {
|
|
966
|
+
await new Promise((resolveReady, reject) => {
|
|
967
|
+
let readyOutput = "";
|
|
968
|
+
let launcherExited = false;
|
|
969
|
+
const ready = () => {
|
|
970
|
+
if (launcherExited && readyOutput.includes("ready\n")) resolveReady();
|
|
971
|
+
};
|
|
972
|
+
output.on("data", (bytes) => {
|
|
973
|
+
readyOutput += bytes.toString();
|
|
974
|
+
ready();
|
|
975
|
+
});
|
|
976
|
+
guard.once("exit", (code, signal) => {
|
|
977
|
+
if (code !== 0 || signal !== null) {
|
|
978
|
+
reject(new Error("process freeze guard launcher failed"));
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
launcherExited = true;
|
|
982
|
+
ready();
|
|
983
|
+
});
|
|
984
|
+
guard.once("error", reject);
|
|
985
|
+
guard.once(
|
|
986
|
+
"close",
|
|
987
|
+
() => reject(new Error("process freeze guard exited before readiness"))
|
|
988
|
+
);
|
|
989
|
+
});
|
|
990
|
+
} catch (error) {
|
|
991
|
+
control.destroy();
|
|
992
|
+
await closed;
|
|
993
|
+
rmSync(directory, { recursive: true, force: true });
|
|
994
|
+
throw error;
|
|
995
|
+
}
|
|
996
|
+
return {
|
|
997
|
+
record(pid) {
|
|
998
|
+
if (guardClosed || control.destroyed)
|
|
999
|
+
throw new Error("process freeze guard exited during capture");
|
|
1000
|
+
appendFileSync(journal, `pid ${pid}
|
|
1001
|
+
`);
|
|
1002
|
+
},
|
|
1003
|
+
async release(disarm) {
|
|
1004
|
+
control.end(disarm ? "disarm\n" : void 0);
|
|
1005
|
+
await closed;
|
|
1006
|
+
rmSync(directory, { recursive: true, force: true });
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
function processesHaveExited(pids, pgid) {
|
|
1011
|
+
const mayExist = [-pgid, ...pids].some((target) => {
|
|
1012
|
+
try {
|
|
1013
|
+
process.kill(target, 0);
|
|
1014
|
+
return true;
|
|
1015
|
+
} catch (error) {
|
|
1016
|
+
return !(error instanceof Error && "code" in error && error.code === "ESRCH");
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
if (!mayExist) return true;
|
|
1020
|
+
const knownPids = new Set(pids);
|
|
1021
|
+
try {
|
|
1022
|
+
return !readProcessTable(EXIT_SCAN_TIMEOUT_MS).some(
|
|
1023
|
+
(row) => (row.pgid === pgid || knownPids.has(row.pid)) && !row.zombie
|
|
1024
|
+
);
|
|
1025
|
+
} catch {
|
|
1026
|
+
return false;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
function groupIsEmpty(pgid) {
|
|
1030
|
+
return processesHaveExited([], pgid);
|
|
1031
|
+
}
|
|
1032
|
+
function killOrphanedProcessGroup(pgid) {
|
|
1033
|
+
if (!Number.isInteger(pgid) || pgid <= 1)
|
|
1034
|
+
throw new Error(`invalid process group ${pgid}`);
|
|
1035
|
+
try {
|
|
1036
|
+
process.kill(-pgid, "SIGKILL");
|
|
1037
|
+
} catch (error) {
|
|
1038
|
+
const code = error instanceof Error && "code" in error ? error.code : void 0;
|
|
1039
|
+
if (code !== "ESRCH" && code !== "EPERM") throw error;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
async function waitForExit(pids, pgid, timeoutMs) {
|
|
1043
|
+
const deadline = Date.now() + timeoutMs;
|
|
1044
|
+
const gone = () => processesHaveExited(pids, pgid);
|
|
1045
|
+
while (Date.now() < deadline) {
|
|
1046
|
+
if (gone()) return true;
|
|
1047
|
+
await sleep(50);
|
|
1048
|
+
}
|
|
1049
|
+
return gone();
|
|
1050
|
+
}
|
|
1051
|
+
async function terminateProcessGroup(child, signal = "SIGTERM", gracefulTimeoutMs = GRACEFUL_TIMEOUT_MS) {
|
|
1052
|
+
if (!child.pid) {
|
|
1053
|
+
try {
|
|
1054
|
+
child.kill(signal);
|
|
1055
|
+
} catch {
|
|
1056
|
+
}
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
1060
|
+
if (groupIsEmpty(child.pid)) return;
|
|
1061
|
+
}
|
|
1062
|
+
const freeze = await protectProcessFreeze(child.pid);
|
|
1063
|
+
const descendants = /* @__PURE__ */ new Set();
|
|
1064
|
+
let inspectionError = null;
|
|
1065
|
+
let signaled = false;
|
|
1066
|
+
let gracefulDeadline = 0;
|
|
1067
|
+
try {
|
|
1068
|
+
signalProcessGroup(child, "SIGSTOP");
|
|
1069
|
+
let parents = [child.pid];
|
|
1070
|
+
const inspectionDeadline = Date.now() + INSPECTION_TIMEOUT_MS;
|
|
1071
|
+
while (parents.length > 0) {
|
|
1072
|
+
const remaining = inspectionDeadline - Date.now();
|
|
1073
|
+
if (remaining <= 0) {
|
|
1074
|
+
inspectionError = new Error("process descendant inspection exceeded its deadline");
|
|
1075
|
+
break;
|
|
1076
|
+
}
|
|
1077
|
+
let children;
|
|
1078
|
+
try {
|
|
1079
|
+
children = listProcessChildren(parents, remaining);
|
|
1080
|
+
} catch (error) {
|
|
1081
|
+
inspectionError = new Error(`could not inspect descendants: ${String(error)}`);
|
|
1082
|
+
break;
|
|
1083
|
+
}
|
|
1084
|
+
const next = [];
|
|
1085
|
+
for (const descendantPid of children) {
|
|
1086
|
+
if (!descendants.has(descendantPid)) {
|
|
1087
|
+
freeze.record(descendantPid);
|
|
1088
|
+
descendants.add(descendantPid);
|
|
1089
|
+
next.push(descendantPid);
|
|
1090
|
+
try {
|
|
1091
|
+
process.kill(descendantPid, "SIGSTOP");
|
|
1092
|
+
} catch {
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
parents = next;
|
|
1097
|
+
}
|
|
1098
|
+
gracefulDeadline = Date.now() + gracefulTimeoutMs;
|
|
1099
|
+
try {
|
|
1100
|
+
process.kill(child.pid, signal);
|
|
1101
|
+
} catch {
|
|
1102
|
+
}
|
|
1103
|
+
signaled = true;
|
|
1104
|
+
} finally {
|
|
1105
|
+
signalProcessGroup(child, "SIGCONT");
|
|
1106
|
+
for (const pid of descendants) {
|
|
1107
|
+
try {
|
|
1108
|
+
process.kill(pid, "SIGCONT");
|
|
1109
|
+
} catch {
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
await freeze.release(signaled);
|
|
1113
|
+
}
|
|
1114
|
+
const pids = [child.pid, ...descendants];
|
|
1115
|
+
let rootExited = child.exitCode !== null || child.signalCode !== null;
|
|
1116
|
+
if (!rootExited) {
|
|
1117
|
+
rootExited = await new Promise((resolveExit) => {
|
|
1118
|
+
const onExit = () => {
|
|
1119
|
+
clearTimeout(deadlineTimer);
|
|
1120
|
+
resolveExit(true);
|
|
1121
|
+
};
|
|
1122
|
+
const deadlineTimer = setTimeout(
|
|
1123
|
+
() => {
|
|
1124
|
+
child.removeListener("exit", onExit);
|
|
1125
|
+
resolveExit(false);
|
|
1126
|
+
},
|
|
1127
|
+
Math.max(0, gracefulDeadline - Date.now())
|
|
1128
|
+
);
|
|
1129
|
+
child.once("exit", onExit);
|
|
1130
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
1131
|
+
child.removeListener("exit", onExit);
|
|
1132
|
+
onExit();
|
|
1133
|
+
}
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
if (rootExited) {
|
|
1137
|
+
signalProcessGroup(child, signal);
|
|
1138
|
+
for (const pid of descendants) {
|
|
1139
|
+
try {
|
|
1140
|
+
process.kill(pid, signal);
|
|
1141
|
+
} catch {
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (await waitForExit(pids, child.pid, Math.max(0, gracefulDeadline - Date.now()))) {
|
|
1146
|
+
if (inspectionError) throw inspectionError;
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
for (const pid of pids) {
|
|
1150
|
+
try {
|
|
1151
|
+
process.kill(pid, "SIGKILL");
|
|
1152
|
+
} catch {
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
signalProcessGroup(child, "SIGKILL");
|
|
1156
|
+
const gone = await waitForExit(pids, child.pid, FORCED_EXIT_TIMEOUT_MS);
|
|
1157
|
+
if (!gone) {
|
|
1158
|
+
const survivors = new Error(
|
|
1159
|
+
`process group ${child.pid} exit could not be confirmed after SIGKILL`
|
|
1160
|
+
);
|
|
1161
|
+
if (!inspectionError) throw survivors;
|
|
1162
|
+
throw new Error(`${inspectionError.message}; ${survivors.message}`);
|
|
1163
|
+
}
|
|
1164
|
+
if (inspectionError) throw inspectionError;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// src/admitted-mac-build.ts
|
|
1168
|
+
import { spawnSync as spawnSync3 } from "node:child_process";
|
|
1169
|
+
function windowAdmission() {
|
|
1170
|
+
if (process.env.CONTRAST_HEAVY_ADMITTED === "1") {
|
|
1171
|
+
const cores = Number(process.env.CONTRAST_HEAVY_CPU_CORES);
|
|
1172
|
+
return {
|
|
1173
|
+
kind: process.env.CONTRAST_HEAVY_ADMISSION_MODE === "exclusive" ? "quiet" : "heavy",
|
|
1174
|
+
cores: Number.isFinite(cores) && cores > 0 ? Math.max(1, Math.floor(cores)) : null,
|
|
1175
|
+
scopes: []
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
const result = spawnSync3("tm", ["window", "ancestor", "--json"], {
|
|
1179
|
+
encoding: "utf8",
|
|
1180
|
+
stdio: ["ignore", "pipe", "inherit"]
|
|
1181
|
+
});
|
|
1182
|
+
if (result.error && "code" in result.error && result.error.code === "ENOENT")
|
|
1183
|
+
return null;
|
|
1184
|
+
if (result.error) throw result.error;
|
|
1185
|
+
if (result.status === 1) return null;
|
|
1186
|
+
if (result.status !== 0) throw new Error(`tm window ancestor exited ${result.status}`);
|
|
1187
|
+
const entry = JSON.parse(result.stdout);
|
|
1188
|
+
if (entry.kind === "external")
|
|
1189
|
+
return { kind: "external", name: entry.name ?? "", cores: null };
|
|
1190
|
+
if (entry.kind !== "heavy" && entry.kind !== "quiet")
|
|
1191
|
+
throw new Error(`tm window ancestor returned kind ${entry.kind}`);
|
|
1192
|
+
return {
|
|
1193
|
+
kind: entry.kind,
|
|
1194
|
+
cores: entry.cores && entry.cores > 0 ? entry.cores : null,
|
|
1195
|
+
scopes: entry.scopes ?? []
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// src/swift-root-lock.ts
|
|
1200
|
+
import {
|
|
1201
|
+
spawn as spawn3
|
|
1202
|
+
} from "node:child_process";
|
|
1203
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
1204
|
+
import {
|
|
1205
|
+
closeSync,
|
|
1206
|
+
existsSync,
|
|
1207
|
+
linkSync,
|
|
1208
|
+
mkdirSync,
|
|
1209
|
+
openSync,
|
|
1210
|
+
readdirSync as readdirSync3,
|
|
1211
|
+
readFileSync as readFileSync3,
|
|
1212
|
+
realpathSync,
|
|
1213
|
+
rmSync as rmSync2,
|
|
1214
|
+
writeFileSync as writeFileSync2
|
|
1215
|
+
} from "node:fs";
|
|
1216
|
+
import { homedir } from "node:os";
|
|
1217
|
+
import path2 from "node:path";
|
|
1218
|
+
var POLL_MS = 250;
|
|
1219
|
+
var INSPECT_ATTEMPTS = 5;
|
|
1220
|
+
var START_TOLERANCE_MS = 2e3;
|
|
1221
|
+
function errorCode(error) {
|
|
1222
|
+
return error instanceof Error && "code" in error && typeof error.code === "string" ? error.code : void 0;
|
|
1223
|
+
}
|
|
1224
|
+
function processTable(pid) {
|
|
1225
|
+
try {
|
|
1226
|
+
return readProcessTable(void 0, pid);
|
|
1227
|
+
} catch (error) {
|
|
1228
|
+
throw new Error(
|
|
1229
|
+
`swift root lock cannot read the process table: ${error instanceof Error ? error.message : String(error)}`
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
async function processTableWithRetry(pid, signal) {
|
|
1234
|
+
for (let attempt = 1; ; attempt++) {
|
|
1235
|
+
try {
|
|
1236
|
+
return processTable(pid);
|
|
1237
|
+
} catch (error) {
|
|
1238
|
+
if (attempt >= INSPECT_ATTEMPTS) throw error;
|
|
1239
|
+
await pause(POLL_MS * attempt, signal);
|
|
1240
|
+
signal?.throwIfAborted();
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
function sameProcess(row, identity) {
|
|
1245
|
+
return row.pid === identity.pid && Math.abs(row.start - identity.start) <= START_TOLERANCE_MS;
|
|
1246
|
+
}
|
|
1247
|
+
function ownIdentity(pid) {
|
|
1248
|
+
const row = processTable(pid).find(
|
|
1249
|
+
(candidate) => candidate.pid === pid && !candidate.zombie
|
|
1250
|
+
);
|
|
1251
|
+
if (!row) throw new Error(`swift root lock cannot find process ${pid}`);
|
|
1252
|
+
return { pid, start: row.start };
|
|
1253
|
+
}
|
|
1254
|
+
function parseIdentity(text) {
|
|
1255
|
+
const value = JSON.parse(text);
|
|
1256
|
+
if (value && typeof value === "object" && "pid" in value && typeof value.pid === "number" && Number.isInteger(value.pid) && value.pid > 1 && "start" in value && typeof value.start === "number")
|
|
1257
|
+
return { pid: value.pid, start: value.start };
|
|
1258
|
+
throw new Error(`swift root lock record is not a process identity: ${text}`);
|
|
1259
|
+
}
|
|
1260
|
+
function pause(ms, signal) {
|
|
1261
|
+
return new Promise((resolve) => {
|
|
1262
|
+
const done = () => {
|
|
1263
|
+
clearTimeout(timer);
|
|
1264
|
+
signal?.removeEventListener("abort", done);
|
|
1265
|
+
resolve();
|
|
1266
|
+
};
|
|
1267
|
+
const timer = setTimeout(done, ms);
|
|
1268
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
var name = (number, suffix) => `${String(number).padStart(12, "0")}.${suffix}`;
|
|
1272
|
+
function generationNumbers(directory) {
|
|
1273
|
+
const numbers = /* @__PURE__ */ new Set();
|
|
1274
|
+
for (const entry of readdirSync3(directory)) {
|
|
1275
|
+
const match = /^(\d{12})\./.exec(entry);
|
|
1276
|
+
if (match) numbers.add(Number(match[1]));
|
|
1277
|
+
}
|
|
1278
|
+
return [...numbers].sort((a, b) => a - b);
|
|
1279
|
+
}
|
|
1280
|
+
function readGeneration(directory, number) {
|
|
1281
|
+
let owner;
|
|
1282
|
+
try {
|
|
1283
|
+
owner = readFileSync3(path2.join(directory, name(number, "owner")), "utf8");
|
|
1284
|
+
} catch (error) {
|
|
1285
|
+
if (errorCode(error) === "ENOENT") return null;
|
|
1286
|
+
throw error;
|
|
1287
|
+
}
|
|
1288
|
+
return {
|
|
1289
|
+
number,
|
|
1290
|
+
holder: parseIdentity(owner),
|
|
1291
|
+
released: readdirSync3(directory).includes(name(number, "released"))
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
function readChildren(directory, number) {
|
|
1295
|
+
const prefix = name(number, "child.");
|
|
1296
|
+
return readdirSync3(directory).filter((entry) => entry.startsWith(prefix)).map((entry) => parseIdentity(readFileSync3(path2.join(directory, entry), "utf8")));
|
|
1297
|
+
}
|
|
1298
|
+
function groupAlive(table, leader) {
|
|
1299
|
+
const row = table.find((candidate) => candidate.pid === leader.pid);
|
|
1300
|
+
if (row && !sameProcess(row, leader)) return false;
|
|
1301
|
+
return table.some((candidate) => candidate.pgid === leader.pid && !candidate.zombie);
|
|
1302
|
+
}
|
|
1303
|
+
function linuxMountPath(existing) {
|
|
1304
|
+
const fd = openSync(existing, "r");
|
|
1305
|
+
try {
|
|
1306
|
+
const mountId = /^mnt_id:\s+(\d+)$/m.exec(
|
|
1307
|
+
readFileSync3("/proc/self/fdinfo/" + fd, "utf8")
|
|
1308
|
+
)?.[1];
|
|
1309
|
+
const mount = readFileSync3("/proc/self/mountinfo", "utf8").split("\n").find((line) => line.startsWith(mountId + " "))?.split(" ");
|
|
1310
|
+
if (!mount?.[4]) throw new Error("cannot identify Swift build mount for " + existing);
|
|
1311
|
+
const decode = (value) => value.replace(
|
|
1312
|
+
/\\([0-7]{3})/g,
|
|
1313
|
+
(_, octal) => String.fromCharCode(parseInt(octal, 8))
|
|
1314
|
+
);
|
|
1315
|
+
return mount[2] + ":" + path2.join(decode(mount[3]), path2.relative(decode(mount[4]), existing));
|
|
1316
|
+
} finally {
|
|
1317
|
+
closeSync(fd);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
function rootLockKey(root) {
|
|
1321
|
+
let existing = path2.resolve(root);
|
|
1322
|
+
const missing = [];
|
|
1323
|
+
while (!existsSync(existing) && path2.dirname(existing) !== existing) {
|
|
1324
|
+
missing.unshift(path2.basename(existing));
|
|
1325
|
+
existing = path2.dirname(existing);
|
|
1326
|
+
}
|
|
1327
|
+
existing = realpathSync(existing);
|
|
1328
|
+
const backingPath = process.platform === "linux" ? linuxMountPath(existing) : existing;
|
|
1329
|
+
return path2.join(backingPath, ...missing);
|
|
1330
|
+
}
|
|
1331
|
+
function swiftRootLocksDir() {
|
|
1332
|
+
return path2.join(homedir(), ".peach", "cache", "swift-root-locks");
|
|
1333
|
+
}
|
|
1334
|
+
async function withSwiftRootLock(root, work, signal, wait = true) {
|
|
1335
|
+
const directory = path2.join(
|
|
1336
|
+
swiftRootLocksDir(),
|
|
1337
|
+
createHash2("sha256").update(rootLockKey(root)).digest("hex").slice(0, 32)
|
|
1338
|
+
);
|
|
1339
|
+
mkdirSync(directory, { recursive: true });
|
|
1340
|
+
const token = randomUUID();
|
|
1341
|
+
const ownRow = (await processTableWithRetry(process.pid, signal)).find(
|
|
1342
|
+
(candidate) => candidate.pid === process.pid && !candidate.zombie
|
|
1343
|
+
);
|
|
1344
|
+
if (!ownRow) throw new Error(`swift root lock cannot find process ${process.pid}`);
|
|
1345
|
+
const owner = JSON.stringify({ pid: process.pid, start: ownRow.start });
|
|
1346
|
+
const pending = path2.join(directory, `pending.${token}`);
|
|
1347
|
+
let held = 0;
|
|
1348
|
+
let announced = 0;
|
|
1349
|
+
let current = null;
|
|
1350
|
+
const waiting = async (reason) => {
|
|
1351
|
+
if (announced !== current?.number) {
|
|
1352
|
+
announced = current?.number ?? 0;
|
|
1353
|
+
process.stderr.write(`[swift-root-lock] waiting for ${root}: ${reason}
|
|
1354
|
+
`);
|
|
1355
|
+
}
|
|
1356
|
+
await pause(POLL_MS, signal);
|
|
1357
|
+
};
|
|
1358
|
+
for (; ; ) {
|
|
1359
|
+
signal?.throwIfAborted();
|
|
1360
|
+
const newest = generationNumbers(directory).at(-1) ?? 0;
|
|
1361
|
+
current = newest > 0 ? readGeneration(directory, newest) : null;
|
|
1362
|
+
if (newest > 0 && !current) continue;
|
|
1363
|
+
if (current && !current.released) {
|
|
1364
|
+
const { holder: identity } = current;
|
|
1365
|
+
const holder = await processTableWithRetry(identity.pid, signal);
|
|
1366
|
+
if (holder.some((row) => !row.zombie && sameProcess(row, identity))) {
|
|
1367
|
+
if (!wait) return void 0;
|
|
1368
|
+
await waiting(`held by pid ${identity.pid}`);
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
const children2 = readChildren(directory, newest);
|
|
1372
|
+
if (children2.length) {
|
|
1373
|
+
const table = await processTableWithRetry(void 0, signal);
|
|
1374
|
+
const orphaned = children2.filter((child) => groupAlive(table, child));
|
|
1375
|
+
if (orphaned.length) {
|
|
1376
|
+
if (!wait) return void 0;
|
|
1377
|
+
for (const child of orphaned) killOrphanedProcessGroup(child.pid);
|
|
1378
|
+
await waiting(
|
|
1379
|
+
`stopping compiler groups ${orphaned.map((child) => child.pid).join(", ")} left by dead pid ${identity.pid}`
|
|
1380
|
+
);
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
writeFileSync2(pending, owner);
|
|
1386
|
+
try {
|
|
1387
|
+
linkSync(pending, path2.join(directory, name(newest + 1, "owner")));
|
|
1388
|
+
} catch (error) {
|
|
1389
|
+
if (errorCode(error) === "EEXIST") continue;
|
|
1390
|
+
throw error;
|
|
1391
|
+
} finally {
|
|
1392
|
+
rmSync2(pending, { force: true });
|
|
1393
|
+
}
|
|
1394
|
+
const numbers = generationNumbers(directory);
|
|
1395
|
+
if (numbers.at(-1) !== newest + 1) {
|
|
1396
|
+
rmSync2(path2.join(directory, name(newest + 1, "owner")), { force: true });
|
|
1397
|
+
continue;
|
|
1398
|
+
}
|
|
1399
|
+
held = newest + 1;
|
|
1400
|
+
for (const number of numbers)
|
|
1401
|
+
if (number < held) {
|
|
1402
|
+
for (const entry of readdirSync3(directory))
|
|
1403
|
+
if (entry.startsWith(name(number, "")))
|
|
1404
|
+
rmSync2(path2.join(directory, entry), { force: true });
|
|
1405
|
+
}
|
|
1406
|
+
break;
|
|
1407
|
+
}
|
|
1408
|
+
const children = [];
|
|
1409
|
+
const lease = {
|
|
1410
|
+
register(child) {
|
|
1411
|
+
const index = children.push(child);
|
|
1412
|
+
if (child.pid === void 0) throw new Error("could not register compiler process");
|
|
1413
|
+
const childPending = `${pending}.child.${index}`;
|
|
1414
|
+
try {
|
|
1415
|
+
writeFileSync2(childPending, JSON.stringify(ownIdentity(child.pid)));
|
|
1416
|
+
linkSync(childPending, path2.join(directory, name(held, `child.${index}`)));
|
|
1417
|
+
} finally {
|
|
1418
|
+
rmSync2(childPending, { force: true });
|
|
1419
|
+
}
|
|
1420
|
+
},
|
|
1421
|
+
spawn(command, args, options, companion) {
|
|
1422
|
+
const child = spawn3(
|
|
1423
|
+
"/bin/sh",
|
|
1424
|
+
["-c", 'IFS= read -r go || exit 1; exec "$@" </dev/null', "sh", command, ...args],
|
|
1425
|
+
{ ...options, detached: true, stdio: ["pipe", "pipe", "pipe"] }
|
|
1426
|
+
);
|
|
1427
|
+
child.stdin.on("error", () => {
|
|
1428
|
+
});
|
|
1429
|
+
if (child.pid === void 0) {
|
|
1430
|
+
child.stdin.destroy();
|
|
1431
|
+
return child;
|
|
1432
|
+
}
|
|
1433
|
+
try {
|
|
1434
|
+
lease.register(child);
|
|
1435
|
+
companion?.register(child);
|
|
1436
|
+
} catch (error) {
|
|
1437
|
+
child.stdin.destroy();
|
|
1438
|
+
throw error;
|
|
1439
|
+
}
|
|
1440
|
+
child.stdin.end("go\n");
|
|
1441
|
+
return child;
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
try {
|
|
1445
|
+
return await work(lease);
|
|
1446
|
+
} finally {
|
|
1447
|
+
await Promise.all(children.map((child) => terminateProcessGroup(child, "SIGKILL", 0)));
|
|
1448
|
+
writeFileSync2(path2.join(directory, name(held, "released")), "");
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
export {
|
|
1453
|
+
SWIFT_WASM_TRIPLE,
|
|
1454
|
+
wasmThreadFlags,
|
|
1455
|
+
wasmThreadLinkFlags,
|
|
1456
|
+
sourceSwiftCheckout,
|
|
1457
|
+
swiftSdkOptions,
|
|
1458
|
+
privateSwiftRelease,
|
|
1459
|
+
registerSwiftHostCompiler,
|
|
1460
|
+
swiftToolchainBin,
|
|
1461
|
+
SWIFT_AUTOLINK_LINKER,
|
|
1462
|
+
writeSwiftAutolinkLinker,
|
|
1463
|
+
swiftCacheDir,
|
|
1464
|
+
SWIFT_CACHE_HOLD_MARKER,
|
|
1465
|
+
refreshSwiftCacheHold,
|
|
1466
|
+
swiftPackageCacheOptions,
|
|
1467
|
+
swiftEnvironment,
|
|
1468
|
+
swiftSourceSdk,
|
|
1469
|
+
swiftToolchain,
|
|
1470
|
+
swiftFileChecksum,
|
|
1471
|
+
swiftCompiler,
|
|
1472
|
+
terminateProcessGroup,
|
|
1473
|
+
windowAdmission,
|
|
1474
|
+
withSwiftRootLock
|
|
1475
|
+
};
|