-
-
Notifications
You must be signed in to change notification settings - Fork 996
/
preprep.mjs
executable file
·226 lines (197 loc) · 6.3 KB
/
preprep.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env node
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import { env, exit, umask } from 'node:process'
import { fileURLToPath } from 'node:url'
import { extractTo } from 'archive-wasm/src/fs.mjs'
import * as _mustache from 'mustache'
import { parse as parseTOML } from 'smol-toml'
import { getConst, NATIVE_DEPS_URL, NATIVE_DEPS_ASSETS } from './utils/consts.mjs'
import { get } from './utils/fetch.mjs'
import { getMachineId } from './utils/machineId.mjs'
import { getRustTargetList } from './utils/rustup.mjs'
import { symlinkSharedLibsMacOS, symlinkSharedLibsLinux } from './utils/shared.mjs'
import { spinTask } from './utils/spinner.mjs'
import { which } from './utils/which.mjs'
if (/^(msys|mingw|cygwin)$/i.test(env.OSTYPE ?? '')) {
console.error(
'Bash for windows is not supported, please interact with this repo from Powershell or CMD'
)
exit(255)
}
// @ts-expect-error
const mustache = /** @type {import("mustache")} */ (_mustache.default)
// Limit file permissions
umask(0o026)
const __debug = env.NODE_ENV === 'debug'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// NOTE: Must point to package root path
const __root = path.resolve(path.join(__dirname, '..'))
const bugWarn =
'This is probably a bug, please open a issue with you system info at: ' +
'https://github.com/spacedriveapp/spacedrive/issues/new/choose'
// Current machine identifiers
const machineId = getMachineId()
// Basic dependeny check
if ((await Promise.all([which('cargo'), which('rustc'), which('pnpm')])).some(found => !found)) {
console.error(`Basic dependencies missing.
Make sure you have rust and pnpm installed:
https://rustup.rs
https://pnpm.io/installation
Also that you have run the setup script:
packages/scripts/${machineId[0] === 'Windows_NT' ? 'setup.ps1' : 'setup.sh'}
`)
}
// Directory where the native deps will be downloaded
const nativeDeps = path.join(__root, 'apps', '.deps')
const mobileNativeDeps = path.join(__root, 'apps', 'mobile', '.deps')
await fs.rm(nativeDeps, { force: true, recursive: true })
await fs.mkdir(nativeDeps, { mode: 0o750, recursive: true })
// Native deps for desktop app
try {
console.log('Downloading desktop native dependencies...')
const assetName = getConst(NATIVE_DEPS_ASSETS, machineId)
if (assetName == null) throw new Error('NO_ASSET')
const archiveData = await spinTask(
get((__debug && env.NATIVE_DEPS_URL) || `${NATIVE_DEPS_URL}/${assetName}`)
)
console.log(`Extracting native dependencies...`)
await spinTask(
extractTo(archiveData, nativeDeps, {
chmod: 0o600,
recursive: true,
overwrite: true,
})
)
} catch (e) {
console.error(`Failed to download native dependencies.\n${bugWarn}`)
if (__debug) console.error(e)
exit(1)
}
const rustTargets = await getRustTargetList()
const iosTargets = {
'aarch64-apple-ios': NATIVE_DEPS_ASSETS.IOS.ios.aarch64,
'aarch64-apple-ios-sim': NATIVE_DEPS_ASSETS.IOS.iossim.aarch64,
'x86_64-apple-ios': NATIVE_DEPS_ASSETS.IOS.iossim.x86_64,
}
// Native deps for mobile
try {
const mobileTargets = /** @type {Record<string, string>} */ {}
if (machineId[0] === 'Darwin')
// iOS is only supported on macOS
Object.assign(mobileTargets, iosTargets)
for (const [rustTarget, nativeTarget] of Object.entries(mobileTargets)) {
if (!rustTargets.has(rustTarget)) continue
console.log(`Downloading mobile native dependencies for ${nativeTarget}...`)
const specificMobileNativeDeps = path.join(mobileNativeDeps, rustTarget)
await fs.rm(specificMobileNativeDeps, { force: true, recursive: true })
await fs.mkdir(specificMobileNativeDeps, { mode: 0o750, recursive: true })
const archiveData = await spinTask(
get(
(__debug &&
env[`NATIVE_DEPS_${rustTarget.replaceAll('-', '_').toUpperCase()}_URL`]) ||
`${NATIVE_DEPS_URL}/${nativeTarget}`
)
)
console.log(`Extracting native dependencies...`)
await spinTask(
extractTo(archiveData, specificMobileNativeDeps, {
chmod: 0o600,
sizeLimit: 256n * 1024n * 1024n,
recursive: true,
overwrite: true,
})
)
}
} catch (e) {
console.error(`Failed to download native dependencies for mobile.\n${bugWarn}`)
if (__debug) console.error(e)
exit(1)
}
// Extra OS specific setup
try {
if (machineId[0] === 'Linux') {
console.log(`Symlink shared libs...`)
await spinTask(
symlinkSharedLibsLinux(__root, nativeDeps).catch(e => {
console.error(`Failed to symlink shared libs.\n${bugWarn}`)
throw e
})
)
} else if (machineId[0] === 'Darwin') {
// This is still required due to how ffmpeg-sys-next builds script works
console.log(`Symlink shared libs...`)
await spinTask(
symlinkSharedLibsMacOS(__root, nativeDeps).catch(e => {
console.error(`Failed to symlink shared libs.\n${bugWarn}`)
throw e
})
)
}
} catch (error) {
if (__debug) console.error(error)
exit(1)
}
// Generate .cargo/config.toml
console.log('Generating cargo config...')
try {
let isWin = false
let isMacOS = false
let isLinux = false
/** @type {boolean | { linker: string }} */
let hasLLD = false
switch (machineId[0]) {
case 'Linux':
isLinux = true
if (await which('clang')) {
if (await which('mold')) {
hasLLD = { linker: 'mold' }
} else if (await which('lld')) {
hasLLD = { linker: 'lld' }
}
}
break
case 'Darwin':
isMacOS = true
break
case 'Windows_NT':
isWin = true
hasLLD = await which('lld-link')
break
}
const configData = mustache
.render(
await fs.readFile(path.join(__root, '.cargo', 'config.toml.mustache'), {
encoding: 'utf8',
}),
{
isWin,
hasiOS: Object.keys(iosTargets).some(target => rustTargets.has(target)),
isMacOS,
isLinux,
// Escape windows path separator to be compatible with TOML parsing
protoc: path
.join(
nativeDeps,
'bin',
machineId[0] === 'Windows_NT' ? 'protoc.exe' : 'protoc'
)
.replaceAll('\\', '\\\\'),
nativeDeps: nativeDeps.replaceAll('\\', '\\\\'),
mobileNativeDeps: mobileNativeDeps.replaceAll('\\', '\\\\'),
hasLLD,
}
)
.replace(/\n\n+/g, '\n')
// Validate generated TOML
parseTOML(configData)
await fs.writeFile(path.join(__root, '.cargo', 'config.toml'), configData, {
mode: 0o751,
flag: 'w+',
})
} catch (error) {
console.error(`Failed to generate .cargo/config.toml.\n${bugWarn}`)
if (__debug) console.error(error)
exit(1)
}